[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:
Felix Fontein 2025-03-07 07:31:42 +01:00 committed by GitHub
parent 9a6bd80613
commit 013fb9c006
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
80 changed files with 3745 additions and 3977 deletions

View file

@ -23,9 +23,9 @@ if tuple(map(int, __version__.split('.'))) < (3, 4, 0):
def test_redis_data_info_without_arguments(capfd):
set_module_args({})
with pytest.raises(SystemExit):
redis_data_info.main()
with set_module_args({}):
with pytest.raises(SystemExit):
redis_data_info.main()
out, err = capfd.readouterr()
assert not err
assert json.loads(out)['failed']
@ -33,14 +33,16 @@ def test_redis_data_info_without_arguments(capfd):
@pytest.mark.skipif(not HAS_REDIS_USERNAME_OPTION, reason="Redis version < 3.4.0")
def test_redis_data_info_existing_key(capfd, mocker):
set_module_args({'login_host': 'localhost',
'login_user': 'root',
'login_password': 'secret',
'key': 'foo',
'_ansible_check_mode': False})
mocker.patch('redis.Redis.get', return_value='bar')
with pytest.raises(SystemExit):
redis_data_info.main()
with set_module_args({
'login_host': 'localhost',
'login_user': 'root',
'login_password': 'secret',
'key': 'foo',
'_ansible_check_mode': False
}):
mocker.patch('redis.Redis.get', return_value='bar')
with pytest.raises(SystemExit):
redis_data_info.main()
out, err = capfd.readouterr()
print(out)
assert not err
@ -50,14 +52,16 @@ def test_redis_data_info_existing_key(capfd, mocker):
@pytest.mark.skipif(not HAS_REDIS_USERNAME_OPTION, reason="Redis version < 3.4.0")
def test_redis_data_info_absent_key(capfd, mocker):
set_module_args({'login_host': 'localhost',
'login_user': 'root',
'login_password': 'secret',
'key': 'foo',
'_ansible_check_mode': False})
mocker.patch('redis.Redis.get', return_value=None)
with pytest.raises(SystemExit):
redis_data_info.main()
with set_module_args({
'login_host': 'localhost',
'login_user': 'root',
'login_password': 'secret',
'key': 'foo',
'_ansible_check_mode': False
}):
mocker.patch('redis.Redis.get', return_value=None)
with pytest.raises(SystemExit):
redis_data_info.main()
out, err = capfd.readouterr()
print(out)
assert not err
@ -67,13 +71,15 @@ def test_redis_data_info_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',
'_ansible_check_mode': False})
with pytest.raises(SystemExit):
redis_data_info.main()
with set_module_args({
'login_host': 'localhost',
'login_user': 'root',
'login_password': 'secret',
'key': 'foo',
'_ansible_check_mode': False
}):
with pytest.raises(SystemExit):
redis_data_info.main()
out, err = capfd.readouterr()
print(out)
assert not err
@ -84,13 +90,15 @@ def test_redis_data_fail_username(capfd, mocker):
@pytest.mark.skipif(HAS_REDIS_USERNAME_OPTION, reason="Redis version > 3.4.0")
def test_redis_data_info_absent_key_no_username(capfd, mocker):
set_module_args({'login_host': 'localhost',
'login_password': 'secret',
'key': 'foo',
'_ansible_check_mode': False})
mocker.patch('redis.Redis.get', return_value=None)
with pytest.raises(SystemExit):
redis_data_info.main()
with set_module_args({
'login_host': 'localhost',
'login_password': 'secret',
'key': 'foo',
'_ansible_check_mode': False
}):
mocker.patch('redis.Redis.get', return_value=None)
with pytest.raises(SystemExit):
redis_data_info.main()
out, err = capfd.readouterr()
print(out)
assert not err
@ -100,13 +108,15 @@ def test_redis_data_info_absent_key_no_username(capfd, mocker):
@pytest.mark.skipif(HAS_REDIS_USERNAME_OPTION, reason="Redis version > 3.4.0")
def test_redis_data_info_existing_key_no_username(capfd, mocker):
set_module_args({'login_host': 'localhost',
'login_password': 'secret',
'key': 'foo',
'_ansible_check_mode': False})
mocker.patch('redis.Redis.get', return_value='bar')
with pytest.raises(SystemExit):
redis_data_info.main()
with set_module_args({
'login_host': 'localhost',
'login_password': 'secret',
'key': 'foo',
'_ansible_check_mode': False
}):
mocker.patch('redis.Redis.get', return_value='bar')
with pytest.raises(SystemExit):
redis_data_info.main()
out, err = capfd.readouterr()
print(out)
assert not err