| 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 annotations |
| 6 |
|
| 7 |
from unittest.mock import patch |
| 8 |
|
| 9 |
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import ( |
| 10 |
AnsibleExitJson, |
| 11 |
AnsibleFailJson, |
| 12 |
ModuleTestCase, |
| 13 |
set_module_args, |
| 14 |
) |
| 15 |
|
| 16 |
from ansible_collections.community.general.plugins.modules import bootc_manage |
| 17 |
|
| 18 |
|
| 19 |
class TestBootcManageModule(ModuleTestCase): |
| 20 |
def setUp(self): |
| 21 |
super().setUp() |
| 22 |
self.module = bootc_manage |
| 23 |
|
| 24 |
def tearDown(self): |
| 25 |
super().tearDown() |
| 26 |
|
| 27 |
def test_switch_without_image(self): |
| 28 |
"""Failure if state is 'switch' but no image provided""" |
| 29 |
with set_module_args({"state": "switch"}): |
| 30 |
with self.assertRaises(AnsibleFailJson) as result: |
| 31 |
self.module.main() |
| 32 |
self.assertEqual(result.exception.args[0]["msg"], "state is switch but all of the following are missing: image") |
| 33 |
|
| 34 |
def test_switch_with_image(self): |
| 35 |
"""Test successful switch with image provided""" |
| 36 |
with set_module_args({"state": "switch", "image": "example.com/image:latest"}): |
| 37 |
with patch("ansible.module_utils.basic.AnsibleModule.run_command") as run_command_mock: |
| 38 |
run_command_mock.return_value = (0, "Queued for next boot: ", "") |
| 39 |
with self.assertRaises(AnsibleExitJson) as result: |
| 40 |
self.module.main() |
| 41 |
self.assertTrue(result.exception.args[0]["changed"]) |
| 42 |
|
| 43 |
def test_latest_state(self): |
| 44 |
"""Test successful upgrade to the latest state""" |
| 45 |
with set_module_args({"state": "latest"}): |
| 46 |
with patch("ansible.module_utils.basic.AnsibleModule.run_command") as run_command_mock: |
| 47 |
run_command_mock.return_value = (0, "Queued for next boot: ", "") |
| 48 |
with self.assertRaises(AnsibleExitJson) as result: |
| 49 |
self.module.main() |
| 50 |
self.assertTrue(result.exception.args[0]["changed"]) |
| 51 |
|
| 52 |
def test_latest_state_no_change(self): |
| 53 |
"""Test no change for latest state""" |
| 54 |
with set_module_args({"state": "latest"}): |
| 55 |
with patch("ansible.module_utils.basic.AnsibleModule.run_command") as run_command_mock: |
| 56 |
run_command_mock.return_value = (0, "No changes in ", "") |
| 57 |
with self.assertRaises(AnsibleExitJson) as result: |
| 58 |
self.module.main() |
| 59 |
self.assertFalse(result.exception.args[0]["changed"]) |
| 60 |
|
| 61 |
def test_switch_image_failure(self): |
| 62 |
"""Test failure during image switch""" |
| 63 |
with set_module_args({"state": "switch", "image": "example.com/image:latest"}): |
| 64 |
with patch("ansible.module_utils.basic.AnsibleModule.run_command") as run_command_mock: |
| 65 |
run_command_mock.return_value = (1, "", "ERROR") |
| 66 |
with self.assertRaises(AnsibleFailJson) as result: |
| 67 |
self.module.main() |
| 68 |
self.assertEqual(result.exception.args[0]["msg"], "ERROR: Command execution failed.") |
| 69 |
|
| 70 |
def test_latest_state_failure(self): |
| 71 |
"""Test failure during upgrade""" |
| 72 |
with set_module_args({"state": "latest"}): |
| 73 |
with patch("ansible.module_utils.basic.AnsibleModule.run_command") as run_command_mock: |
| 74 |
run_command_mock.return_value = (1, "", "ERROR") |
| 75 |
with self.assertRaises(AnsibleFailJson) as result: |
| 76 |
self.module.main() |
| 77 |
self.assertEqual(result.exception.args[0]["msg"], "ERROR: Command execution failed.") |