|
| 1 | +# Copyright (c) Ansible project |
| 2 | +# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 | +# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | + |
| 5 | +from __future__ import (absolute_import, division, print_function) |
| 6 | +__metaclass__ = type |
| 7 | + |
| 8 | +from ansible_collections.community.general.tests.unit.compat.mock import patch |
| 9 | +from ansible_collections.community.general.plugins.modules import bootc_manage |
| 10 | +from ansible_collections.community.general.tests.unit.plugins.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase, set_module_args |
| 11 | + |
| 12 | + |
| 13 | +class TestBootcManageModule(ModuleTestCase): |
| 14 | + |
| 15 | + def setUp(self): |
| 16 | + super(TestBootcManageModule, self).setUp() |
| 17 | + self.module = bootc_manage |
| 18 | + |
| 19 | + def tearDown(self): |
| 20 | + super(TestBootcManageModule, self).tearDown() |
| 21 | + |
| 22 | + def test_switch_without_image(self): |
| 23 | + """Failure if state is 'switch' but no image provided""" |
| 24 | + set_module_args({'state': 'switch'}) |
| 25 | + with self.assertRaises(AnsibleFailJson) as result: |
| 26 | + self.module.main() |
| 27 | + self.assertEqual(result.exception.args[0]['msg'], "state is switch but all of the following are missing: image") |
| 28 | + |
| 29 | + def test_switch_with_image(self): |
| 30 | + """Test successful switch with image provided""" |
| 31 | + set_module_args({'state': 'switch', 'image': 'example.com/image:latest'}) |
| 32 | + with patch('ansible.module_utils.basic.AnsibleModule.run_command') as run_command_mock: |
| 33 | + run_command_mock.return_value = (0, 'Queued for next boot: ', '') |
| 34 | + with self.assertRaises(AnsibleExitJson) as result: |
| 35 | + self.module.main() |
| 36 | + self.assertTrue(result.exception.args[0]['changed']) |
| 37 | + |
| 38 | + def test_latest_state(self): |
| 39 | + """Test successful upgrade to the latest state""" |
| 40 | + set_module_args({'state': 'latest'}) |
| 41 | + with patch('ansible.module_utils.basic.AnsibleModule.run_command') as run_command_mock: |
| 42 | + run_command_mock.return_value = (0, 'Queued for next boot: ', '') |
| 43 | + with self.assertRaises(AnsibleExitJson) as result: |
| 44 | + self.module.main() |
| 45 | + self.assertTrue(result.exception.args[0]['changed']) |
| 46 | + |
| 47 | + def test_latest_state_no_change(self): |
| 48 | + """Test no change for latest state""" |
| 49 | + set_module_args({'state': 'latest'}) |
| 50 | + with patch('ansible.module_utils.basic.AnsibleModule.run_command') as run_command_mock: |
| 51 | + run_command_mock.return_value = (0, 'No changes in ', '') |
| 52 | + with self.assertRaises(AnsibleExitJson) as result: |
| 53 | + self.module.main() |
| 54 | + self.assertFalse(result.exception.args[0]['changed']) |
| 55 | + |
| 56 | + def test_switch_image_failure(self): |
| 57 | + """Test failure during image switch""" |
| 58 | + set_module_args({'state': 'switch', 'image': 'example.com/image:latest'}) |
| 59 | + with patch('ansible.module_utils.basic.AnsibleModule.run_command') as run_command_mock: |
| 60 | + run_command_mock.return_value = (1, '', 'ERROR') |
| 61 | + with self.assertRaises(AnsibleFailJson) as result: |
| 62 | + self.module.main() |
| 63 | + self.assertEqual(result.exception.args[0]['msg'], 'ERROR: Command execution failed.') |
| 64 | + |
| 65 | + def test_latest_state_failure(self): |
| 66 | + """Test failure during upgrade""" |
| 67 | + set_module_args({'state': 'latest'}) |
| 68 | + with patch('ansible.module_utils.basic.AnsibleModule.run_command') as run_command_mock: |
| 69 | + run_command_mock.return_value = (1, '', 'ERROR') |
| 70 | + with self.assertRaises(AnsibleFailJson) as result: |
| 71 | + self.module.main() |
| 72 | + self.assertEqual(result.exception.args[0]['msg'], 'ERROR: Command execution failed.') |
0 commit comments