[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

@ -91,110 +91,112 @@ class TestSimpleinitMSB(ModuleTestCase):
def tearDown(self):
super(TestSimpleinitMSB, self).tearDown()
def init_module(self, args):
set_module_args(args)
return SimpleinitMSB(build_module())
@patch('os.path.exists', return_value=True)
@patch('ansible.module_utils.basic.AnsibleModule.get_bin_path', return_value="/sbin/telinit")
def test_get_service_tools(self, *args, **kwargs):
simpleinit_msb = self.init_module({
with set_module_args({
'name': 'smgl-suspend-single',
'state': 'running',
})
}):
simpleinit_msb = SimpleinitMSB(build_module())
simpleinit_msb.get_service_tools()
simpleinit_msb.get_service_tools()
self.assertEqual(simpleinit_msb.telinit_cmd, "/sbin/telinit")
self.assertEqual(simpleinit_msb.telinit_cmd, "/sbin/telinit")
@patch('ansible_collections.community.general.plugins.modules.simpleinit_msb.SimpleinitMSB.execute_command')
def test_service_exists(self, execute_command):
simpleinit_msb = self.init_module({
with set_module_args({
'name': 'smgl-suspend-single',
'state': 'running',
})
}):
simpleinit_msb = SimpleinitMSB(build_module())
execute_command.return_value = (0, _TELINIT_LIST, "")
execute_command.return_value = (0, _TELINIT_LIST, "")
simpleinit_msb.service_exists()
simpleinit_msb.service_exists()
@patch('ansible_collections.community.general.plugins.modules.simpleinit_msb.SimpleinitMSB.execute_command')
def test_service_exists_not(self, execute_command):
simpleinit_msb = self.init_module({
with set_module_args({
'name': 'ntp',
'state': 'running',
})
}):
simpleinit_msb = SimpleinitMSB(build_module())
execute_command.return_value = (0, _TELINIT_LIST, "")
execute_command.return_value = (0, _TELINIT_LIST, "")
with self.assertRaises(AnsibleFailJson) as context:
simpleinit_msb.service_exists()
with self.assertRaises(AnsibleFailJson) as context:
simpleinit_msb.service_exists()
self.assertEqual("telinit could not find the requested service: ntp", context.exception.args[0]["msg"])
self.assertEqual("telinit could not find the requested service: ntp", context.exception.args[0]["msg"])
@patch('ansible_collections.community.general.plugins.modules.simpleinit_msb.SimpleinitMSB.service_exists')
@patch('ansible_collections.community.general.plugins.modules.simpleinit_msb.SimpleinitMSB.execute_command')
def test_check_service_enabled(self, execute_command, service_exists):
simpleinit_msb = self.init_module({
with set_module_args({
'name': 'nscd',
'state': 'running',
'enabled': 'true',
})
}):
simpleinit_msb = SimpleinitMSB(build_module())
service_exists.return_value = True
execute_command.return_value = (0, _TELINIT_LIST_ENABLED, "")
service_exists.return_value = True
execute_command.return_value = (0, _TELINIT_LIST_ENABLED, "")
self.assertTrue(simpleinit_msb.service_enabled())
self.assertTrue(simpleinit_msb.service_enabled())
# Race condition check
with patch('ansible_collections.community.general.plugins.modules.simpleinit_msb.SimpleinitMSB.service_enabled', return_value=False):
execute_command.return_value = (0, "", _TELINIT_ALREADY_ENABLED)
# Race condition check
with patch('ansible_collections.community.general.plugins.modules.simpleinit_msb.SimpleinitMSB.service_enabled', return_value=False):
execute_command.return_value = (0, "", _TELINIT_ALREADY_ENABLED)
simpleinit_msb.service_enable()
simpleinit_msb.service_enable()
self.assertFalse(simpleinit_msb.changed)
self.assertFalse(simpleinit_msb.changed)
@patch('ansible_collections.community.general.plugins.modules.simpleinit_msb.SimpleinitMSB.service_exists')
@patch('ansible_collections.community.general.plugins.modules.simpleinit_msb.SimpleinitMSB.execute_command')
def test_check_service_disabled(self, execute_command, service_exists):
simpleinit_msb = self.init_module({
with set_module_args({
'name': 'sysstat',
'state': 'stopped',
'enabled': 'false',
})
}):
simpleinit_msb = SimpleinitMSB(build_module())
service_exists.return_value = True
execute_command.return_value = (0, _TELINIT_LIST_DISABLED, "")
service_exists.return_value = True
execute_command.return_value = (0, _TELINIT_LIST_DISABLED, "")
self.assertFalse(simpleinit_msb.service_enabled())
self.assertFalse(simpleinit_msb.service_enabled())
# Race condition check
with patch('ansible_collections.community.general.plugins.modules.simpleinit_msb.SimpleinitMSB.service_enabled', return_value=True):
execute_command.return_value = (0, "", _TELINIT_ALREADY_DISABLED)
# Race condition check
with patch('ansible_collections.community.general.plugins.modules.simpleinit_msb.SimpleinitMSB.service_enabled', return_value=True):
execute_command.return_value = (0, "", _TELINIT_ALREADY_DISABLED)
simpleinit_msb.service_enable()
simpleinit_msb.service_enable()
self.assertFalse(simpleinit_msb.changed)
self.assertFalse(simpleinit_msb.changed)
@patch('ansible_collections.community.general.plugins.modules.simpleinit_msb.SimpleinitMSB.service_control')
def test_check_service_running(self, service_control):
simpleinit_msb = self.init_module({
with set_module_args({
'name': 'sshd',
'state': 'running',
})
}):
simpleinit_msb = SimpleinitMSB(build_module())
service_control.return_value = (0, _TELINIT_STATUS_RUNNING, "")
service_control.return_value = (0, _TELINIT_STATUS_RUNNING, "")
self.assertFalse(simpleinit_msb.get_service_status())
self.assertFalse(simpleinit_msb.get_service_status())
@patch('ansible_collections.community.general.plugins.modules.simpleinit_msb.SimpleinitMSB.service_control')
def test_check_service_running_not(self, service_control):
simpleinit_msb = self.init_module({
with set_module_args({
'name': 'smgl-metalog',
'state': 'running',
})
}):
simpleinit_msb = SimpleinitMSB(build_module())
service_control.return_value = (0, _TELINIT_STATUS_RUNNING_NOT, "")
service_control.return_value = (0, _TELINIT_STATUS_RUNNING_NOT, "")
self.assertFalse(simpleinit_msb.get_service_status())
self.assertFalse(simpleinit_msb.get_service_status())