mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-22 10:21:25 -07:00
[stable-9] Unit tests: make set_module_args() a context manager, and remove copies of it in some tests (#9840)
Unit tests: make set_module_args() a context manager, and remove copies of it in some tests (#9838)
Make set_module_args() a context manager, and remove copies of set_module_args().
Prepares for Data Tagging.
(cherry picked from commit a1781d09dd
)
This commit is contained in:
parent
9a6bd80613
commit
013fb9c006
80 changed files with 3745 additions and 3977 deletions
|
@ -21,9 +21,9 @@ if tuple(map(int, __version__.split('.'))) < (3, 4, 0):
|
|||
|
||||
|
||||
def test_redis_data_without_arguments(capfd):
|
||||
set_module_args({})
|
||||
with pytest.raises(SystemExit) as results:
|
||||
redis_data.main()
|
||||
with set_module_args({}):
|
||||
with pytest.raises(SystemExit) as results:
|
||||
redis_data.main()
|
||||
out, err = capfd.readouterr()
|
||||
assert not err
|
||||
assert json.loads(out)['failed']
|
||||
|
@ -31,16 +31,16 @@ def test_redis_data_without_arguments(capfd):
|
|||
|
||||
@pytest.mark.skipif(not HAS_REDIS_USERNAME_OPTION, reason="Redis version < 3.4.0")
|
||||
def test_redis_data_key(capfd, mocker):
|
||||
set_module_args({'login_host': 'localhost',
|
||||
'login_user': 'root',
|
||||
'login_password': 'secret',
|
||||
'key': 'foo',
|
||||
'value': 'baz',
|
||||
'_ansible_check_mode': False})
|
||||
mocker.patch('redis.Redis.get', return_value='bar')
|
||||
mocker.patch('redis.Redis.set', return_value=True)
|
||||
with pytest.raises(SystemExit):
|
||||
redis_data.main()
|
||||
with set_module_args({'login_host': 'localhost',
|
||||
'login_user': 'root',
|
||||
'login_password': 'secret',
|
||||
'key': 'foo',
|
||||
'value': 'baz',
|
||||
'_ansible_check_mode': False}):
|
||||
mocker.patch('redis.Redis.get', return_value='bar')
|
||||
mocker.patch('redis.Redis.set', return_value=True)
|
||||
with pytest.raises(SystemExit):
|
||||
redis_data.main()
|
||||
out, err = capfd.readouterr()
|
||||
print(out)
|
||||
assert not err
|
||||
|
@ -52,17 +52,17 @@ def test_redis_data_key(capfd, mocker):
|
|||
|
||||
@pytest.mark.skipif(not HAS_REDIS_USERNAME_OPTION, reason="Redis version < 3.4.0")
|
||||
def test_redis_data_existing_key_nx(capfd, mocker):
|
||||
set_module_args({'login_host': 'localhost',
|
||||
'login_user': 'root',
|
||||
'login_password': 'secret',
|
||||
'key': 'foo',
|
||||
'value': 'baz',
|
||||
'non_existing': True,
|
||||
'_ansible_check_mode': False})
|
||||
mocker.patch('redis.Redis.get', return_value='bar')
|
||||
mocker.patch('redis.Redis.set', return_value=None)
|
||||
with pytest.raises(SystemExit):
|
||||
redis_data.main()
|
||||
with set_module_args({'login_host': 'localhost',
|
||||
'login_user': 'root',
|
||||
'login_password': 'secret',
|
||||
'key': 'foo',
|
||||
'value': 'baz',
|
||||
'non_existing': True,
|
||||
'_ansible_check_mode': False}):
|
||||
mocker.patch('redis.Redis.get', return_value='bar')
|
||||
mocker.patch('redis.Redis.set', return_value=None)
|
||||
with pytest.raises(SystemExit):
|
||||
redis_data.main()
|
||||
out, err = capfd.readouterr()
|
||||
print(out)
|
||||
assert not err
|
||||
|
@ -76,17 +76,17 @@ def test_redis_data_existing_key_nx(capfd, mocker):
|
|||
|
||||
@pytest.mark.skipif(not HAS_REDIS_USERNAME_OPTION, reason="Redis version < 3.4.0")
|
||||
def test_redis_data_non_existing_key_xx(capfd, mocker):
|
||||
set_module_args({'login_host': 'localhost',
|
||||
'login_user': 'root',
|
||||
'login_password': 'secret',
|
||||
'key': 'foo',
|
||||
'value': 'baz',
|
||||
'existing': True,
|
||||
'_ansible_check_mode': False})
|
||||
mocker.patch('redis.Redis.get', return_value=None)
|
||||
mocker.patch('redis.Redis.set', return_value=None)
|
||||
with pytest.raises(SystemExit):
|
||||
redis_data.main()
|
||||
with set_module_args({'login_host': 'localhost',
|
||||
'login_user': 'root',
|
||||
'login_password': 'secret',
|
||||
'key': 'foo',
|
||||
'value': 'baz',
|
||||
'existing': True,
|
||||
'_ansible_check_mode': False}):
|
||||
mocker.patch('redis.Redis.get', return_value=None)
|
||||
mocker.patch('redis.Redis.set', return_value=None)
|
||||
with pytest.raises(SystemExit):
|
||||
redis_data.main()
|
||||
out, err = capfd.readouterr()
|
||||
print(out)
|
||||
assert not err
|
||||
|
@ -100,15 +100,15 @@ def test_redis_data_non_existing_key_xx(capfd, mocker):
|
|||
|
||||
@pytest.mark.skipif(not HAS_REDIS_USERNAME_OPTION, reason="Redis version < 3.4.0")
|
||||
def test_redis_data_delete_present_key(capfd, mocker):
|
||||
set_module_args({'login_host': 'localhost',
|
||||
'login_user': 'root',
|
||||
'login_password': 'secret',
|
||||
'key': 'foo',
|
||||
'state': 'absent'})
|
||||
mocker.patch('redis.Redis.get', return_value='bar')
|
||||
mocker.patch('redis.Redis.delete', return_value=1)
|
||||
with pytest.raises(SystemExit):
|
||||
redis_data.main()
|
||||
with set_module_args({'login_host': 'localhost',
|
||||
'login_user': 'root',
|
||||
'login_password': 'secret',
|
||||
'key': 'foo',
|
||||
'state': 'absent'}):
|
||||
mocker.patch('redis.Redis.get', return_value='bar')
|
||||
mocker.patch('redis.Redis.delete', return_value=1)
|
||||
with pytest.raises(SystemExit):
|
||||
redis_data.main()
|
||||
out, err = capfd.readouterr()
|
||||
print(out)
|
||||
assert not err
|
||||
|
@ -118,15 +118,15 @@ def test_redis_data_delete_present_key(capfd, mocker):
|
|||
|
||||
@pytest.mark.skipif(not HAS_REDIS_USERNAME_OPTION, reason="Redis version < 3.4.0")
|
||||
def test_redis_data_delete_absent_key(capfd, mocker):
|
||||
set_module_args({'login_host': 'localhost',
|
||||
'login_user': 'root',
|
||||
'login_password': 'secret',
|
||||
'key': 'foo',
|
||||
'state': 'absent'})
|
||||
mocker.patch('redis.Redis.delete', return_value=0)
|
||||
mocker.patch('redis.Redis.get', return_value=None)
|
||||
with pytest.raises(SystemExit):
|
||||
redis_data.main()
|
||||
with set_module_args({'login_host': 'localhost',
|
||||
'login_user': 'root',
|
||||
'login_password': 'secret',
|
||||
'key': 'foo',
|
||||
'state': 'absent'}):
|
||||
mocker.patch('redis.Redis.delete', return_value=0)
|
||||
mocker.patch('redis.Redis.get', return_value=None)
|
||||
with pytest.raises(SystemExit):
|
||||
redis_data.main()
|
||||
out, err = capfd.readouterr()
|
||||
print(out)
|
||||
assert not err
|
||||
|
@ -136,14 +136,14 @@ def test_redis_data_delete_absent_key(capfd, mocker):
|
|||
|
||||
@pytest.mark.skipif(HAS_REDIS_USERNAME_OPTION, reason="Redis version > 3.4.0")
|
||||
def test_redis_data_fail_username(capfd, mocker):
|
||||
set_module_args({'login_host': 'localhost',
|
||||
'login_user': 'root',
|
||||
'login_password': 'secret',
|
||||
'key': 'foo',
|
||||
'value': 'baz',
|
||||
'_ansible_check_mode': False})
|
||||
with pytest.raises(SystemExit):
|
||||
redis_data.main()
|
||||
with set_module_args({'login_host': 'localhost',
|
||||
'login_user': 'root',
|
||||
'login_password': 'secret',
|
||||
'key': 'foo',
|
||||
'value': 'baz',
|
||||
'_ansible_check_mode': False}):
|
||||
with pytest.raises(SystemExit):
|
||||
redis_data.main()
|
||||
out, err = capfd.readouterr()
|
||||
print(out)
|
||||
assert not err
|
||||
|
@ -153,15 +153,15 @@ def test_redis_data_fail_username(capfd, mocker):
|
|||
|
||||
|
||||
def test_redis_data_key_no_username(capfd, mocker):
|
||||
set_module_args({'login_host': 'localhost',
|
||||
'login_password': 'secret',
|
||||
'key': 'foo',
|
||||
'value': 'baz',
|
||||
'_ansible_check_mode': False})
|
||||
mocker.patch('redis.Redis.get', return_value='bar')
|
||||
mocker.patch('redis.Redis.set', return_value=True)
|
||||
with pytest.raises(SystemExit):
|
||||
redis_data.main()
|
||||
with set_module_args({'login_host': 'localhost',
|
||||
'login_password': 'secret',
|
||||
'key': 'foo',
|
||||
'value': 'baz',
|
||||
'_ansible_check_mode': False}):
|
||||
mocker.patch('redis.Redis.get', return_value='bar')
|
||||
mocker.patch('redis.Redis.set', return_value=True)
|
||||
with pytest.raises(SystemExit):
|
||||
redis_data.main()
|
||||
out, err = capfd.readouterr()
|
||||
print(out)
|
||||
assert not err
|
||||
|
@ -172,15 +172,15 @@ def test_redis_data_key_no_username(capfd, mocker):
|
|||
|
||||
|
||||
def test_redis_delete_key_no_username(capfd, mocker):
|
||||
set_module_args({'login_host': 'localhost',
|
||||
'login_password': 'secret',
|
||||
'key': 'foo',
|
||||
'state': 'absent',
|
||||
'_ansible_check_mode': False})
|
||||
mocker.patch('redis.Redis.get', return_value='bar')
|
||||
mocker.patch('redis.Redis.delete', return_value=1)
|
||||
with pytest.raises(SystemExit):
|
||||
redis_data.main()
|
||||
with set_module_args({'login_host': 'localhost',
|
||||
'login_password': 'secret',
|
||||
'key': 'foo',
|
||||
'state': 'absent',
|
||||
'_ansible_check_mode': False}):
|
||||
mocker.patch('redis.Redis.get', return_value='bar')
|
||||
mocker.patch('redis.Redis.delete', return_value=1)
|
||||
with pytest.raises(SystemExit):
|
||||
redis_data.main()
|
||||
out, err = capfd.readouterr()
|
||||
print(out)
|
||||
assert not err
|
||||
|
@ -189,15 +189,15 @@ def test_redis_delete_key_no_username(capfd, mocker):
|
|||
|
||||
|
||||
def test_redis_delete_key_non_existent_key(capfd, mocker):
|
||||
set_module_args({'login_host': 'localhost',
|
||||
'login_password': 'secret',
|
||||
'key': 'foo',
|
||||
'state': 'absent',
|
||||
'_ansible_check_mode': False})
|
||||
mocker.patch('redis.Redis.get', return_value=None)
|
||||
mocker.patch('redis.Redis.delete', return_value=0)
|
||||
with pytest.raises(SystemExit):
|
||||
redis_data.main()
|
||||
with set_module_args({'login_host': 'localhost',
|
||||
'login_password': 'secret',
|
||||
'key': 'foo',
|
||||
'state': 'absent',
|
||||
'_ansible_check_mode': False}):
|
||||
mocker.patch('redis.Redis.get', return_value=None)
|
||||
mocker.patch('redis.Redis.delete', return_value=0)
|
||||
with pytest.raises(SystemExit):
|
||||
redis_data.main()
|
||||
out, err = capfd.readouterr()
|
||||
print(out)
|
||||
assert not err
|
||||
|
@ -206,15 +206,15 @@ def test_redis_delete_key_non_existent_key(capfd, mocker):
|
|||
|
||||
|
||||
def test_redis_set_key_check_mode_nochange(capfd, mocker):
|
||||
set_module_args({'login_host': 'localhost',
|
||||
'login_password': 'secret',
|
||||
'key': 'foo',
|
||||
'state': 'present',
|
||||
'value': 'bar',
|
||||
'_ansible_check_mode': True})
|
||||
mocker.patch('redis.Redis.get', return_value='bar')
|
||||
with pytest.raises(SystemExit):
|
||||
redis_data.main()
|
||||
with set_module_args({'login_host': 'localhost',
|
||||
'login_password': 'secret',
|
||||
'key': 'foo',
|
||||
'state': 'present',
|
||||
'value': 'bar',
|
||||
'_ansible_check_mode': True}):
|
||||
mocker.patch('redis.Redis.get', return_value='bar')
|
||||
with pytest.raises(SystemExit):
|
||||
redis_data.main()
|
||||
out, err = capfd.readouterr()
|
||||
print(out)
|
||||
assert not err
|
||||
|
@ -225,15 +225,15 @@ def test_redis_set_key_check_mode_nochange(capfd, mocker):
|
|||
|
||||
|
||||
def test_redis_set_key_check_mode_delete_nx(capfd, mocker):
|
||||
set_module_args({'login_host': 'localhost',
|
||||
'login_password': 'secret',
|
||||
'key': 'foo',
|
||||
'state': 'present',
|
||||
'value': 'baz',
|
||||
'_ansible_check_mode': True})
|
||||
mocker.patch('redis.Redis.get', return_value=None)
|
||||
with pytest.raises(SystemExit):
|
||||
redis_data.main()
|
||||
with set_module_args({'login_host': 'localhost',
|
||||
'login_password': 'secret',
|
||||
'key': 'foo',
|
||||
'state': 'present',
|
||||
'value': 'baz',
|
||||
'_ansible_check_mode': True}):
|
||||
mocker.patch('redis.Redis.get', return_value=None)
|
||||
with pytest.raises(SystemExit):
|
||||
redis_data.main()
|
||||
out, err = capfd.readouterr()
|
||||
print(out)
|
||||
assert not err
|
||||
|
@ -243,15 +243,15 @@ def test_redis_set_key_check_mode_delete_nx(capfd, mocker):
|
|||
|
||||
|
||||
def test_redis_set_key_check_mode_delete(capfd, mocker):
|
||||
set_module_args({'login_host': 'localhost',
|
||||
'login_password': 'secret',
|
||||
'key': 'foo',
|
||||
'state': 'present',
|
||||
'value': 'baz',
|
||||
'_ansible_check_mode': True})
|
||||
mocker.patch('redis.Redis.get', return_value='bar')
|
||||
with pytest.raises(SystemExit):
|
||||
redis_data.main()
|
||||
with set_module_args({'login_host': 'localhost',
|
||||
'login_password': 'secret',
|
||||
'key': 'foo',
|
||||
'state': 'present',
|
||||
'value': 'baz',
|
||||
'_ansible_check_mode': True}):
|
||||
mocker.patch('redis.Redis.get', return_value='bar')
|
||||
with pytest.raises(SystemExit):
|
||||
redis_data.main()
|
||||
out, err = capfd.readouterr()
|
||||
print(out)
|
||||
assert not err
|
||||
|
@ -261,15 +261,15 @@ def test_redis_set_key_check_mode_delete(capfd, mocker):
|
|||
|
||||
|
||||
def test_redis_set_key_check_mode(capfd, mocker):
|
||||
set_module_args({'login_host': 'localhost',
|
||||
'login_password': 'secret',
|
||||
'key': 'foo',
|
||||
'state': 'present',
|
||||
'value': 'baz',
|
||||
'_ansible_check_mode': True})
|
||||
mocker.patch('redis.Redis.get', return_value='bar')
|
||||
with pytest.raises(SystemExit):
|
||||
redis_data.main()
|
||||
with set_module_args({'login_host': 'localhost',
|
||||
'login_password': 'secret',
|
||||
'key': 'foo',
|
||||
'state': 'present',
|
||||
'value': 'baz',
|
||||
'_ansible_check_mode': True}):
|
||||
mocker.patch('redis.Redis.get', return_value='bar')
|
||||
with pytest.raises(SystemExit):
|
||||
redis_data.main()
|
||||
out, err = capfd.readouterr()
|
||||
print(out)
|
||||
assert not err
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue