| 1 |
# Copyright (c) Contributors to the 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 lvg_rename |
| 17 |
|
| 18 |
VGS_OUTPUT = """\ |
| 19 |
vg_data_testhost1;XKZ5gn-YhWY-NlrT-QCFN-qmMG-VGT9-7uOmex |
| 20 |
vg_sys_testhost2;xgy2SJ-YlYd-fde2-e3oG-zdXL-0xGf-ihqG2H |
| 21 |
""" |
| 22 |
|
| 23 |
|
| 24 |
class TestLvgRename(ModuleTestCase): |
| 25 |
"""Tests for lvg_rename internals""" |
| 26 |
|
| 27 |
module = lvg_rename |
| 28 |
module_path = "ansible_collections.community.general.plugins.modules.lvg_rename" |
| 29 |
|
| 30 |
def setUp(self): |
| 31 |
"""Prepare mocks for module testing""" |
| 32 |
super().setUp() |
| 33 |
|
| 34 |
self.mock_run_responses = {} |
| 35 |
|
| 36 |
patched_module_get_bin_path = patch(f"{self.module_path}.AnsibleModule.get_bin_path") |
| 37 |
self.mock_module_get_bin_path = patched_module_get_bin_path.start() |
| 38 |
self.mock_module_get_bin_path.return_value = "/mocpath" |
| 39 |
self.addCleanup(patched_module_get_bin_path.stop) |
| 40 |
|
| 41 |
patched_module_run_command = patch(f"{self.module_path}.AnsibleModule.run_command") |
| 42 |
self.mock_module_run_command = patched_module_run_command.start() |
| 43 |
self.addCleanup(patched_module_run_command.stop) |
| 44 |
|
| 45 |
def test_vg_not_found_by_name(self): |
| 46 |
"""When the VG by the specified by vg name not found, the module should exit with error""" |
| 47 |
failed = True |
| 48 |
self.mock_module_run_command.side_effect = [(0, VGS_OUTPUT, "")] |
| 49 |
expected_msg = "Both current (vg_missing) and new (vg_data_testhost2) VG are missing." |
| 50 |
|
| 51 |
module_args = { |
| 52 |
"vg": "vg_missing", |
| 53 |
"vg_new": "vg_data_testhost2", |
| 54 |
} |
| 55 |
with set_module_args(args=module_args): |
| 56 |
with self.assertRaises(AnsibleFailJson) as result: |
| 57 |
self.module.main() |
| 58 |
|
| 59 |
self.assertEqual(len(self.mock_module_run_command.mock_calls), 1) |
| 60 |
self.assertIs(result.exception.args[0]["failed"], failed) |
| 61 |
self.assertEqual(result.exception.args[0]["msg"], expected_msg) |
| 62 |
|
| 63 |
def test_vg_not_found_by_uuid(self): |
| 64 |
"""When the VG by the specified vg UUID not found, the module should exit with error""" |
| 65 |
failed = True |
| 66 |
self.mock_module_run_command.side_effect = [(0, VGS_OUTPUT, "")] |
| 67 |
expected_msg = ( |
| 68 |
"Both current (Yfj4YG-c8nI-z7w5-B7Fw-i2eM-HqlF-ApFVp0) and new (vg_data_testhost2) VG are missing." |
| 69 |
) |
| 70 |
|
| 71 |
module_args = { |
| 72 |
"vg": "Yfj4YG-c8nI-z7w5-B7Fw-i2eM-HqlF-ApFVp0", |
| 73 |
"vg_new": "vg_data_testhost2", |
| 74 |
} |
| 75 |
with set_module_args(args=module_args): |
| 76 |
with self.assertRaises(AnsibleFailJson) as result: |
| 77 |
self.module.main() |
| 78 |
|
| 79 |
self.assertEqual(len(self.mock_module_run_command.mock_calls), 1) |
| 80 |
self.assertIs(result.exception.args[0]["failed"], failed) |
| 81 |
self.assertEqual(result.exception.args[0]["msg"], expected_msg) |
| 82 |
|
| 83 |
def test_vg_and_vg_new_both_exists(self): |
| 84 |
"""When a VG found for both vg and vg_new options, the module should exit with error""" |
| 85 |
failed = True |
| 86 |
self.mock_module_run_command.side_effect = [(0, VGS_OUTPUT, "")] |
| 87 |
expected_msg = "The new VG name (vg_sys_testhost2) is already in use." |
| 88 |
|
| 89 |
module_args = { |
| 90 |
"vg": "vg_data_testhost1", |
| 91 |
"vg_new": "vg_sys_testhost2", |
| 92 |
} |
| 93 |
with set_module_args(args=module_args): |
| 94 |
with self.assertRaises(AnsibleFailJson) as result: |
| 95 |
self.module.main() |
| 96 |
|
| 97 |
self.assertEqual(len(self.mock_module_run_command.mock_calls), 1) |
| 98 |
self.assertIs(result.exception.args[0]["failed"], failed) |
| 99 |
self.assertEqual(result.exception.args[0]["msg"], expected_msg) |
| 100 |
|
| 101 |
def test_vg_needs_renaming(self): |
| 102 |
"""When the VG found for vg option and there is no VG for vg_new option, |
| 103 |
the module should call vgrename""" |
| 104 |
changed = True |
| 105 |
self.mock_module_run_command.side_effect = [ |
| 106 |
(0, VGS_OUTPUT, ""), |
| 107 |
(0, ' Volume group "vg_data_testhost1" successfully renamed to "vg_data_testhost2"', ""), |
| 108 |
] |
| 109 |
expected_msg = ' Volume group "vg_data_testhost1" successfully renamed to "vg_data_testhost2"' |
| 110 |
|
| 111 |
module_args = { |
| 112 |
"vg": "/dev/vg_data_testhost1", |
| 113 |
"vg_new": "vg_data_testhost2", |
| 114 |
} |
| 115 |
with set_module_args(args=module_args): |
| 116 |
with self.assertRaises(AnsibleExitJson) as result: |
| 117 |
self.module.main() |
| 118 |
|
| 119 |
self.assertEqual(len(self.mock_module_run_command.mock_calls), 2) |
| 120 |
self.assertIs(result.exception.args[0]["changed"], changed) |
| 121 |
self.assertEqual(result.exception.args[0]["msg"], expected_msg) |
| 122 |
|
| 123 |
def test_vg_needs_renaming_in_check_mode(self): |
| 124 |
"""When running in check mode and the VG found for vg option and there is no VG for vg_new option, |
| 125 |
the module should not call vgrename""" |
| 126 |
changed = True |
| 127 |
self.mock_module_run_command.side_effect = [(0, VGS_OUTPUT, "")] |
| 128 |
expected_msg = "Running in check mode. The module would rename VG /dev/vg_data_testhost1 to vg_data_testhost2." |
| 129 |
|
| 130 |
module_args = { |
| 131 |
"vg": "/dev/vg_data_testhost1", |
| 132 |
"vg_new": "vg_data_testhost2", |
| 133 |
"_ansible_check_mode": True, |
| 134 |
} |
| 135 |
with set_module_args(args=module_args): |
| 136 |
with self.assertRaises(AnsibleExitJson) as result: |
| 137 |
self.module.main() |
| 138 |
|
| 139 |
self.assertEqual(len(self.mock_module_run_command.mock_calls), 1) |
| 140 |
self.assertIs(result.exception.args[0]["changed"], changed) |
| 141 |
self.assertEqual(result.exception.args[0]["msg"], expected_msg) |
| 142 |
|
| 143 |
def test_vg_needs_no_renaming(self): |
| 144 |
"""When the VG not found for vg option and the VG found for vg_new option, |
| 145 |
the module should not call vgrename""" |
| 146 |
changed = False |
| 147 |
self.mock_module_run_command.side_effect = [(0, VGS_OUTPUT, "")] |
| 148 |
expected_msg = "The new VG (vg_data_testhost1) already exists, nothing to do." |
| 149 |
|
| 150 |
module_args = { |
| 151 |
"vg": "vg_data_testhostX", |
| 152 |
"vg_new": "vg_data_testhost1", |
| 153 |
} |
| 154 |
with set_module_args(args=module_args): |
| 155 |
with self.assertRaises(AnsibleExitJson) as result: |
| 156 |
self.module.main() |
| 157 |
|
| 158 |
self.assertEqual(len(self.mock_module_run_command.mock_calls), 1) |
| 159 |
self.assertIs(result.exception.args[0]["changed"], changed) |
| 160 |
self.assertEqual(result.exception.args[0]["msg"], expected_msg) |