| 1 |
# |
| 2 |
# Copyright (c) 2021, Ansible Project |
| 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 DEFAULT, patch |
| 8 |
|
| 9 |
import pytest |
| 10 |
|
| 11 |
proxmoxer = pytest.importorskip("proxmoxer") |
| 12 |
|
| 13 |
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import ( |
| 14 |
AnsibleExitJson, |
| 15 |
AnsibleFailJson, |
| 16 |
ModuleTestCase, |
| 17 |
set_module_args, |
| 18 |
) |
| 19 |
|
| 20 |
import ansible_collections.community.proxmox.plugins.module_utils.proxmox as proxmox_utils |
| 21 |
from ansible_collections.community.proxmox.plugins.modules import proxmox_kvm |
| 22 |
|
| 23 |
|
| 24 |
class TestProxmoxKvmModule(ModuleTestCase): |
| 25 |
def setUp(self): |
| 26 |
super().setUp() |
| 27 |
proxmox_utils.HAS_PROXMOXER = True |
| 28 |
self.module = proxmox_kvm |
| 29 |
self.connect_mock = patch( |
| 30 |
"ansible_collections.community.proxmox.plugins.module_utils.proxmox.ProxmoxAnsible._connect" |
| 31 |
).start() |
| 32 |
self.get_node_mock = patch.object(proxmox_utils.ProxmoxAnsible, "get_node").start() |
| 33 |
self.get_vm_mock = patch.object(proxmox_utils.ProxmoxAnsible, "get_vm").start() |
| 34 |
self.create_vm_mock = patch.object(proxmox_kvm.ProxmoxKvmAnsible, "create_vm").start() |
| 35 |
|
| 36 |
def tearDown(self): |
| 37 |
self.create_vm_mock.stop() |
| 38 |
self.get_vm_mock.stop() |
| 39 |
self.get_node_mock.stop() |
| 40 |
self.connect_mock.stop() |
| 41 |
super().tearDown() |
| 42 |
|
| 43 |
def test_module_fail_when_required_args_missing(self): |
| 44 |
with self.assertRaises(AnsibleFailJson), set_module_args({}): |
| 45 |
self.module.main() |
| 46 |
|
| 47 |
def test_module_exits_unchaged_when_provided_vmid_exists(self): |
| 48 |
with set_module_args( |
| 49 |
{ |
| 50 |
"api_host": "host", |
| 51 |
"api_user": "user", |
| 52 |
"api_password": "password", |
| 53 |
"vmid": "100", |
| 54 |
"node": "pve", |
| 55 |
} |
| 56 |
): |
| 57 |
self.get_vm_mock.return_value = [{"vmid": "100"}] |
| 58 |
with pytest.raises(AnsibleExitJson) as exc_info: |
| 59 |
self.module.main() |
| 60 |
|
| 61 |
assert self.get_vm_mock.call_count == 1 |
| 62 |
result = exc_info.value.args[0] |
| 63 |
assert result["changed"] is False |
| 64 |
assert result["msg"] == "VM with vmid <100> already exists" |
| 65 |
|
| 66 |
def test_vm_created_when_vmid_not_exist_but_name_already_exist(self): |
| 67 |
with set_module_args( |
| 68 |
{ |
| 69 |
"api_host": "host", |
| 70 |
"api_user": "user", |
| 71 |
"api_password": "password", |
| 72 |
"vmid": "100", |
| 73 |
"name": "existing.vm.local", |
| 74 |
"node": "pve", |
| 75 |
} |
| 76 |
): |
| 77 |
self.get_vm_mock.return_value = None |
| 78 |
with pytest.raises(AnsibleExitJson) as exc_info: |
| 79 |
self.module.main() |
| 80 |
|
| 81 |
assert self.get_vm_mock.call_count == 1 |
| 82 |
assert self.get_node_mock.call_count == 1 |
| 83 |
result = exc_info.value.args[0] |
| 84 |
assert result["changed"] is True |
| 85 |
assert result["msg"] == "VM existing.vm.local with vmid 100 deployed" |
| 86 |
|
| 87 |
def test_vm_not_created_when_name_already_exist_and_vmid_not_set(self): |
| 88 |
with set_module_args( |
| 89 |
{ |
| 90 |
"api_host": "host", |
| 91 |
"api_user": "user", |
| 92 |
"api_password": "password", |
| 93 |
"name": "existing.vm.local", |
| 94 |
"node": "pve", |
| 95 |
} |
| 96 |
), patch.object(proxmox_utils.ProxmoxAnsible, "get_vmid") as get_vmid_mock: |
| 97 |
get_vmid_mock.return_value = { |
| 98 |
"vmid": 100, |
| 99 |
"name": "existing.vm.local", |
| 100 |
} |
| 101 |
with pytest.raises(AnsibleExitJson) as exc_info: |
| 102 |
self.module.main() |
| 103 |
|
| 104 |
assert get_vmid_mock.call_count == 1 |
| 105 |
result = exc_info.value.args[0] |
| 106 |
assert result["changed"] is False |
| 107 |
|
| 108 |
def test_vm_created_when_name_doesnt_exist_and_vmid_not_set(self): |
| 109 |
with set_module_args( |
| 110 |
{ |
| 111 |
"api_host": "host", |
| 112 |
"api_user": "user", |
| 113 |
"api_password": "password", |
| 114 |
"name": "existing.vm.local", |
| 115 |
"node": "pve", |
| 116 |
} |
| 117 |
): |
| 118 |
self.get_vm_mock.return_value = None |
| 119 |
with patch.multiple(proxmox_utils.ProxmoxAnsible, get_vmid=DEFAULT, get_nextvmid=DEFAULT) as utils_mock: |
| 120 |
utils_mock["get_vmid"].return_value = None |
| 121 |
utils_mock["get_nextvmid"].return_value = 101 |
| 122 |
with pytest.raises(AnsibleExitJson) as exc_info: |
| 123 |
self.module.main() |
| 124 |
|
| 125 |
assert utils_mock["get_vmid"].call_count == 1 |
| 126 |
assert utils_mock["get_nextvmid"].call_count == 1 |
| 127 |
result = exc_info.value.args[0] |
| 128 |
assert result["changed"] is True |
| 129 |
assert result["msg"] == "VM existing.vm.local with vmid 101 deployed" |
| 130 |
|
| 131 |
def test_parse_mac(self): |
| 132 |
assert proxmox_kvm.parse_mac("virtio=00:11:22:AA:BB:CC,bridge=vmbr0,firewall=1") == "00:11:22:AA:BB:CC" |
| 133 |
|
| 134 |
def test_parse_dev(self): |
| 135 |
assert proxmox_kvm.parse_dev("local-lvm:vm-1000-disk-0,format=qcow2") == "local-lvm:vm-1000-disk-0" |
| 136 |
assert proxmox_kvm.parse_dev("local-lvm:vm-101-disk-1,size=8G") == "local-lvm:vm-101-disk-1" |
| 137 |
assert proxmox_kvm.parse_dev("local-zfs:vm-1001-disk-0") == "local-zfs:vm-1001-disk-0" |