| 1 |
# |
| 2 |
# Copyright (c) 2025, Jana Hoch <[email protected]> |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
|
| 6 |
|
| 7 |
from unittest.mock import Mock, patch |
| 8 |
|
| 9 |
import pytest |
| 10 |
|
| 11 |
proxmoxer = pytest.importorskip("proxmoxer") |
| 12 |
|
| 13 |
from ansible.module_utils.compat.version import LooseVersion |
| 14 |
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import ( |
| 15 |
ModuleTestCase, |
| 16 |
set_module_args, |
| 17 |
) |
| 18 |
|
| 19 |
import ansible_collections.community.proxmox.plugins.module_utils.proxmox as proxmox_utils |
| 20 |
from ansible_collections.community.proxmox.plugins.modules import proxmox_vnet |
| 21 |
|
| 22 |
|
| 23 |
def exit_json(*args, **kwargs): |
| 24 |
"""function to patch over exit_json; package return data into an exception""" |
| 25 |
if "changed" not in kwargs: |
| 26 |
kwargs["changed"] = False |
| 27 |
raise SystemExit(kwargs) |
| 28 |
|
| 29 |
|
| 30 |
def fail_json(*args, **kwargs): |
| 31 |
"""function to patch over fail_json; package return data into an exception""" |
| 32 |
kwargs["failed"] = True |
| 33 |
raise SystemExit(kwargs) |
| 34 |
|
| 35 |
|
| 36 |
def get_module_args_zone(zone, vnet, state="present", update=True, alias=None): |
| 37 |
return { |
| 38 |
"api_host": "host", |
| 39 |
"api_user": "user", |
| 40 |
"api_password": "password", |
| 41 |
"zone": zone, |
| 42 |
"state": state, |
| 43 |
"update": update, |
| 44 |
"vnet": vnet, |
| 45 |
"alias": alias, |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
RAW_VNETS = [ |
| 50 |
{ |
| 51 |
"type": "vnet", |
| 52 |
"vnet": "test", |
| 53 |
"zone": "ans1", |
| 54 |
"alias": "test1", |
| 55 |
"digest": "79ee852ce6fd2cc12c047363e7059a761fe68a8c", |
| 56 |
"isolate-ports": 1, |
| 57 |
}, |
| 58 |
{"type": "vnet", "zone": "test1", "digest": "79ee852ce6fd2cc12c047363e7059a761fe68a8c", "vnet": "test2"}, |
| 59 |
] |
| 60 |
|
| 61 |
|
| 62 |
class TestProxmoxVnetModule(ModuleTestCase): |
| 63 |
def setUp(self): |
| 64 |
super().setUp() |
| 65 |
proxmox_utils.HAS_PROXMOXER = True |
| 66 |
self.module = proxmox_vnet |
| 67 |
self.fail_json_patcher = patch( |
| 68 |
"ansible.module_utils.basic.AnsibleModule.fail_json", new=Mock(side_effect=fail_json) |
| 69 |
) |
| 70 |
self.exit_json_patcher = patch("ansible.module_utils.basic.AnsibleModule.exit_json", new=exit_json) |
| 71 |
|
| 72 |
self.fail_json_mock = self.fail_json_patcher.start() |
| 73 |
self.exit_json_patcher.start() |
| 74 |
self.connect_mock = patch( |
| 75 |
"ansible_collections.community.proxmox.plugins.module_utils.proxmox.ProxmoxAnsible._connect", |
| 76 |
).start() |
| 77 |
self.version_mock = patch.object( |
| 78 |
proxmox_vnet.ProxmoxVnetAnsible, "version", return_value=LooseVersion("9.0") |
| 79 |
).start() |
| 80 |
self.connect_mock.return_value.cluster.return_value.sdn.return_value.vnets.return_value.get.return_value = ( |
| 81 |
RAW_VNETS.copy() |
| 82 |
) |
| 83 |
|
| 84 |
def tearDown(self): |
| 85 |
self.connect_mock.stop() |
| 86 |
self.exit_json_patcher.stop() |
| 87 |
self.fail_json_patcher.stop() |
| 88 |
self.version_mock.stop() |
| 89 |
super().tearDown() |
| 90 |
|
| 91 |
def test_vnet_present(self): |
| 92 |
# Create new Vnet |
| 93 |
with pytest.raises(SystemExit) as exc_info, set_module_args(get_module_args_zone(zone="ztest", vnet="vtest")): |
| 94 |
self.module.main() |
| 95 |
result = exc_info.value.args[0] |
| 96 |
assert result["changed"] is True |
| 97 |
assert result["msg"] == "Create new vnet vtest" |
| 98 |
assert result["vnet"] == "vtest" |
| 99 |
|
| 100 |
# Update the vnet |
| 101 |
with pytest.raises(SystemExit) as exc_info, set_module_args( |
| 102 |
get_module_args_zone(zone="test1", vnet="test2", alias="test", update=True) |
| 103 |
): |
| 104 |
self.module.main() |
| 105 |
result = exc_info.value.args[0] |
| 106 |
assert result["changed"] is True |
| 107 |
assert result["msg"] == "updated vnet test2" |
| 108 |
assert result["vnet"] == "test2" |
| 109 |
|
| 110 |
# Vnet needs to be updated but update=False |
| 111 |
with pytest.raises(SystemExit) as exc_info, set_module_args( |
| 112 |
get_module_args_zone(zone="test1", vnet="test2", alias="test", update=False) |
| 113 |
): |
| 114 |
self.module.main() |
| 115 |
result = exc_info.value.args[0] |
| 116 |
assert self.fail_json_mock.called |
| 117 |
assert result["failed"] is True |
| 118 |
assert result["msg"] == "vnet test2 needs to be updated but update is false." |
| 119 |
|
| 120 |
def test_zone_absent(self): |
| 121 |
with pytest.raises(SystemExit) as exc_info, set_module_args( |
| 122 |
get_module_args_zone(zone="test1", vnet="test2", state="absent") |
| 123 |
): |
| 124 |
self.module.main() |
| 125 |
result = exc_info.value.args[0] |
| 126 |
assert result["changed"] is True |
| 127 |
assert result["msg"] == "Deleted vnet test2" |
| 128 |
assert result["vnet"] == "test2" |