[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

@ -21,52 +21,52 @@ class TestBootcManageModule(ModuleTestCase):
def test_switch_without_image(self):
"""Failure if state is 'switch' but no image provided"""
set_module_args({'state': 'switch'})
with self.assertRaises(AnsibleFailJson) as result:
self.module.main()
with set_module_args({'state': 'switch'}):
with self.assertRaises(AnsibleFailJson) as result:
self.module.main()
self.assertEqual(result.exception.args[0]['msg'], "state is switch but all of the following are missing: image")
def test_switch_with_image(self):
"""Test successful switch with image provided"""
set_module_args({'state': 'switch', 'image': 'example.com/image:latest'})
with patch('ansible.module_utils.basic.AnsibleModule.run_command') as run_command_mock:
run_command_mock.return_value = (0, 'Queued for next boot: ', '')
with self.assertRaises(AnsibleExitJson) as result:
self.module.main()
with set_module_args({'state': 'switch', 'image': 'example.com/image:latest'}):
with patch('ansible.module_utils.basic.AnsibleModule.run_command') as run_command_mock:
run_command_mock.return_value = (0, 'Queued for next boot: ', '')
with self.assertRaises(AnsibleExitJson) as result:
self.module.main()
self.assertTrue(result.exception.args[0]['changed'])
def test_latest_state(self):
"""Test successful upgrade to the latest state"""
set_module_args({'state': 'latest'})
with patch('ansible.module_utils.basic.AnsibleModule.run_command') as run_command_mock:
run_command_mock.return_value = (0, 'Queued for next boot: ', '')
with self.assertRaises(AnsibleExitJson) as result:
self.module.main()
with set_module_args({'state': 'latest'}):
with patch('ansible.module_utils.basic.AnsibleModule.run_command') as run_command_mock:
run_command_mock.return_value = (0, 'Queued for next boot: ', '')
with self.assertRaises(AnsibleExitJson) as result:
self.module.main()
self.assertTrue(result.exception.args[0]['changed'])
def test_latest_state_no_change(self):
"""Test no change for latest state"""
set_module_args({'state': 'latest'})
with patch('ansible.module_utils.basic.AnsibleModule.run_command') as run_command_mock:
run_command_mock.return_value = (0, 'No changes in ', '')
with self.assertRaises(AnsibleExitJson) as result:
self.module.main()
with set_module_args({'state': 'latest'}):
with patch('ansible.module_utils.basic.AnsibleModule.run_command') as run_command_mock:
run_command_mock.return_value = (0, 'No changes in ', '')
with self.assertRaises(AnsibleExitJson) as result:
self.module.main()
self.assertFalse(result.exception.args[0]['changed'])
def test_switch_image_failure(self):
"""Test failure during image switch"""
set_module_args({'state': 'switch', 'image': 'example.com/image:latest'})
with patch('ansible.module_utils.basic.AnsibleModule.run_command') as run_command_mock:
run_command_mock.return_value = (1, '', 'ERROR')
with self.assertRaises(AnsibleFailJson) as result:
self.module.main()
with set_module_args({'state': 'switch', 'image': 'example.com/image:latest'}):
with patch('ansible.module_utils.basic.AnsibleModule.run_command') as run_command_mock:
run_command_mock.return_value = (1, '', 'ERROR')
with self.assertRaises(AnsibleFailJson) as result:
self.module.main()
self.assertEqual(result.exception.args[0]['msg'], 'ERROR: Command execution failed.')
def test_latest_state_failure(self):
"""Test failure during upgrade"""
set_module_args({'state': 'latest'})
with patch('ansible.module_utils.basic.AnsibleModule.run_command') as run_command_mock:
run_command_mock.return_value = (1, '', 'ERROR')
with self.assertRaises(AnsibleFailJson) as result:
self.module.main()
with set_module_args({'state': 'latest'}):
with patch('ansible.module_utils.basic.AnsibleModule.run_command') as run_command_mock:
run_command_mock.return_value = (1, '', 'ERROR')
with self.assertRaises(AnsibleFailJson) as result:
self.module.main()
self.assertEqual(result.exception.args[0]['msg'], 'ERROR: Command execution failed.')