driver / standard (pull_request) Successful in 19s
driver / lint (pull_request) Successful in 15s
driver / types (pull_request) Successful in 1m5s
driver / test (3.12) (pull_request) Failing after 1m6s
driver / lowest (pull_request) Failing after 5s
driver / test (3.13) (pull_request) Failing after 58s
driver / build (pull_request) Successful in 31s
driver / security (pull_request) Successful in 1m9s
CI / driver (pull_request) Failing after 2m49s
driver / publish (pull_request) Skipped
A failure after the VM shell existed (disk import, NIC or snippet config, start) raised without the VMID, so the caller could not remove the half-built VM and it stayed on the node. It now raises napalm-device-types' VMProvisionIncomplete with vmid and node, the failure as its cause. A failure before the shell exists raises as before. The NIC config strings are built by a helper of their own, which keeps create_vm_from_cloud_init within its quality baseline. Closes #28
126 lines
4.1 KiB
Python
126 lines
4.1 KiB
Python
"""A creation that fails once the VM exists names that VM (#28).
|
|
|
|
Without its id the caller cannot remove the half-built VM, and it stays on the
|
|
node. A failure before the VM shell exists leaves nothing behind and raises
|
|
as it did.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
from napalm_device_types.provisioning import VMProvisionIncomplete
|
|
|
|
from napalm_proxmox.vm_provision_mixin import ProxmoxVMProvisionMixin
|
|
|
|
_DEBIAN_URL = (
|
|
"https://cloud.debian.org/images/cloud/trixie/latest/debian-13-genericcloud-amd64.qcow2"
|
|
)
|
|
|
|
|
|
def _mixin() -> tuple:
|
|
"""A mixin whose node answers like Proxmox and hands out VMID 101."""
|
|
mixin = ProxmoxVMProvisionMixin()
|
|
mixin._node_name = "pve1"
|
|
api = MagicMock()
|
|
api.cluster.nextid.get.return_value = 101
|
|
api.storage.return_value.get.return_value = {"path": "/var/lib/vz"}
|
|
node = MagicMock()
|
|
node.storage.get.return_value = [
|
|
{"storage": "local-lvm", "type": "lvmthin", "content": "images,rootdir", "enabled": 1},
|
|
{"storage": "local", "type": "dir", "content": "snippets,iso", "enabled": 1},
|
|
]
|
|
vm = MagicMock()
|
|
node.qemu.return_value = vm
|
|
vm.config.get.return_value = {"unused0": "local-lvm:vm-101-disk-0"}
|
|
vm.status.start.post.return_value = "UPID:pve1:start"
|
|
node.tasks.return_value.status.get.return_value = {"status": "stopped", "exitstatus": "OK"}
|
|
mixin._api = api
|
|
mixin._node_api = MagicMock(return_value=node)
|
|
mixin._download_cloud_image = MagicMock(
|
|
return_value="/var/lib/vz/template/netork-images/x.qcow2"
|
|
)
|
|
mixin._run_node_command = MagicMock(return_value="")
|
|
return mixin, node, vm
|
|
|
|
|
|
def _create(mixin):
|
|
with patch("time.sleep"):
|
|
return mixin.create_vm_from_cloud_init(
|
|
name="half-built",
|
|
image_url=_DEBIAN_URL,
|
|
cpu=2,
|
|
memory=2048,
|
|
nics=[{"bridge": "vmbr0"}],
|
|
cloud_init_config={"hostname": "half-built"},
|
|
)
|
|
|
|
|
|
def test_a_failed_disk_import_names_the_vm_it_left_behind():
|
|
mixin, _node, _vm = _mixin()
|
|
failure = RuntimeError("qm importdisk 101: storage 'local-lvm' is full")
|
|
mixin._download_cloud_image.side_effect = failure
|
|
|
|
with pytest.raises(VMProvisionIncomplete) as caught:
|
|
_create(mixin)
|
|
|
|
assert (caught.value.vmid, caught.value.node) == ("101", "pve1")
|
|
assert caught.value.__cause__ is failure
|
|
assert str(caught.value) == str(failure)
|
|
|
|
|
|
def test_a_failed_start_names_the_vm_it_left_behind():
|
|
mixin, node, _vm = _mixin()
|
|
node.tasks.return_value.status.get.return_value = {
|
|
"status": "stopped",
|
|
"exitstatus": "start failed: QEMU exited with code 1",
|
|
}
|
|
|
|
with pytest.raises(VMProvisionIncomplete, match="QEMU exited") as caught:
|
|
_create(mixin)
|
|
|
|
assert (caught.value.vmid, caught.value.node) == ("101", "pve1")
|
|
|
|
|
|
def test_an_invalid_nic_after_the_shell_exists_names_the_vm_too():
|
|
mixin, _node, _vm = _mixin()
|
|
|
|
with pytest.raises(VMProvisionIncomplete, match="bridge is required") as caught:
|
|
with patch("time.sleep"):
|
|
mixin.create_vm_from_cloud_init(
|
|
name="half-built",
|
|
image_url=_DEBIAN_URL,
|
|
cpu=2,
|
|
memory=2048,
|
|
nics=[{"vlan_tag": 10}],
|
|
cloud_init_config={},
|
|
)
|
|
|
|
assert isinstance(caught.value.__cause__, ValueError)
|
|
assert caught.value.vmid == "101"
|
|
|
|
|
|
def test_a_failure_creating_the_shell_leaves_no_vm_to_name():
|
|
mixin, node, _vm = _mixin()
|
|
failure = RuntimeError("VM 101 already exists")
|
|
node.qemu.post.side_effect = failure
|
|
|
|
with pytest.raises(RuntimeError) as caught:
|
|
_create(mixin)
|
|
|
|
assert caught.value is failure
|
|
assert not isinstance(caught.value, VMProvisionIncomplete)
|
|
|
|
|
|
def test_a_failure_taking_the_vmid_leaves_no_vm_to_name():
|
|
mixin, node, _vm = _mixin()
|
|
failure = RuntimeError("cluster not ready - no quorum?")
|
|
mixin._api.cluster.nextid.get.side_effect = failure
|
|
|
|
with pytest.raises(RuntimeError) as caught:
|
|
_create(mixin)
|
|
|
|
assert caught.value is failure
|
|
node.qemu.post.assert_not_called()
|