[PR #9838/a1781d09 backport][stable-10] Unit tests: make set_module_args() a context manager, and remove copies of it in some tests (#9839)

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)

Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
patchback[bot] 2025-03-07 07:27:55 +01:00 committed by GitHub
commit 7d45b678e4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
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'])