1
0
mirror of https://github.com/ansible-collections/community.general.git synced 2025-07-30 18:43:09 +03:00

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.
This commit is contained in:
Felix Fontein
2025-03-07 07:21:03 +01:00
committed by GitHub
parent 402f725424
commit a1781d09dd
84 changed files with 4043 additions and 4302 deletions

View File

@ -52,15 +52,15 @@ class RhsmRepositoryReleaseModuleTestCase(ModuleTestCase):
def test_release_set(self):
# test that the module attempts to change the release when the current
# release is not the same as the user-specific target release
set_module_args({'release': '7.5'})
self.module_main_command.side_effect = [
# first call, get_release: returns different version so set_release is called
(0, '7.4', ''),
# second call, set_release: just needs to exit with 0 rc
(0, '', ''),
]
with set_module_args({'release': '7.5'}):
self.module_main_command.side_effect = [
# first call, get_release: returns different version so set_release is called
(0, '7.4', ''),
# second call, set_release: just needs to exit with 0 rc
(0, '', ''),
]
result = self.module_main(AnsibleExitJson)
result = self.module_main(AnsibleExitJson)
self.assertTrue(result['changed'])
self.assertEqual('7.5', result['current_release'])
@ -72,13 +72,13 @@ class RhsmRepositoryReleaseModuleTestCase(ModuleTestCase):
def test_release_set_idempotent(self):
# test that the module does not attempt to change the release when
# the current release matches the user-specified target release
set_module_args({'release': '7.5'})
self.module_main_command.side_effect = [
# first call, get_release: returns same version, set_release is not called
(0, '7.5', ''),
]
with set_module_args({'release': '7.5'}):
self.module_main_command.side_effect = [
# first call, get_release: returns same version, set_release is not called
(0, '7.5', ''),
]
result = self.module_main(AnsibleExitJson)
result = self.module_main(AnsibleExitJson)
self.assertFalse(result['changed'])
self.assertEqual('7.5', result['current_release'])
@ -89,15 +89,15 @@ class RhsmRepositoryReleaseModuleTestCase(ModuleTestCase):
def test_release_unset(self):
# test that the module attempts to change the release when the current
# release is not the same as the user-specific target release
set_module_args({'release': None})
self.module_main_command.side_effect = [
# first call, get_release: returns version so set_release is called
(0, '7.5', ''),
# second call, set_release: just needs to exit with 0 rc
(0, '', ''),
]
with set_module_args({'release': None}):
self.module_main_command.side_effect = [
# first call, get_release: returns version so set_release is called
(0, '7.5', ''),
# second call, set_release: just needs to exit with 0 rc
(0, '', ''),
]
result = self.module_main(AnsibleExitJson)
result = self.module_main(AnsibleExitJson)
self.assertTrue(result['changed'])
self.assertIsNone(result['current_release'])
@ -109,13 +109,13 @@ class RhsmRepositoryReleaseModuleTestCase(ModuleTestCase):
def test_release_unset_idempotent(self):
# test that the module attempts to change the release when the current
# release is not the same as the user-specific target release
set_module_args({'release': None})
self.module_main_command.side_effect = [
# first call, get_release: returns no version, set_release is not called
(0, 'Release not set', ''),
]
with set_module_args({'release': None}):
self.module_main_command.side_effect = [
# first call, get_release: returns no version, set_release is not called
(0, 'Release not set', ''),
]
result = self.module_main(AnsibleExitJson)
result = self.module_main(AnsibleExitJson)
self.assertFalse(result['changed'])
self.assertIsNone(result['current_release'])
@ -126,9 +126,8 @@ class RhsmRepositoryReleaseModuleTestCase(ModuleTestCase):
def test_release_insane(self):
# test that insane values for release trigger fail_json
insane_value = 'this is an insane release value'
set_module_args({'release': insane_value})
result = self.module_main(AnsibleFailJson)
with set_module_args({'release': insane_value}):
result = self.module_main(AnsibleFailJson)
# also ensure that the fail msg includes the insane value
self.assertIn(insane_value, result['msg'])