Merge remote-tracking branch 'origin/main' into fix/half-built-vm-28
# Conflicts: # CHANGELOG.md # tests/quality_baseline.json
This commit is contained in:
commit
0f1918f230
28 files changed
+2720
-162
No files matched your search
@@ -488,6 +488,42 @@ def test_destroy_vm_already_stopped():
|
||||
assert mock_vm.delete.called
|
||||
|
||||
|
||||
def _destroy_mixin(delete_task: dict) -> tuple[ProxmoxVMProvisionMixin, MagicMock]:
|
||||
mixin = ProxmoxVMProvisionMixin()
|
||||
node = MagicMock()
|
||||
mixin._node_api = MagicMock(return_value=node)
|
||||
vm = node.qemu.return_value
|
||||
vm.status.stop.post.return_value = "UPID:pve1:125:stop"
|
||||
vm.config.get.return_value = {}
|
||||
vm.delete.return_value = "UPID:pve1:126:qmdestroy"
|
||||
tasks = {
|
||||
"UPID:pve1:125:stop": {"status": "stopped", "exitstatus": "OK"},
|
||||
"UPID:pve1:126:qmdestroy": delete_task,
|
||||
}
|
||||
node.tasks.side_effect = lambda upid: MagicMock(**{"status.get.return_value": tasks[upid]})
|
||||
return mixin, node
|
||||
|
||||
|
||||
def test_destroy_vm_waits_for_the_delete_task():
|
||||
"""destroy_vm returns once the VM is gone, not when its delete was queued (#15)."""
|
||||
mixin, node = _destroy_mixin({"status": "stopped", "exitstatus": "OK"})
|
||||
|
||||
with patch("time.sleep"):
|
||||
mixin.destroy_vm("101")
|
||||
|
||||
assert [c.args[0] for c in node.tasks.call_args_list] == [
|
||||
"UPID:pve1:125:stop",
|
||||
"UPID:pve1:126:qmdestroy",
|
||||
]
|
||||
|
||||
|
||||
def test_destroy_vm_raises_when_the_delete_task_fails():
|
||||
mixin, _ = _destroy_mixin({"status": "stopped", "exitstatus": "VM is locked (backup)"})
|
||||
|
||||
with patch("time.sleep"), pytest.raises(RuntimeError, match="VM is locked"):
|
||||
mixin.destroy_vm("101")
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# get_network_targets
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -1284,3 +1320,100 @@ def test_create_vm_cloudinit_drive_uses_image_storage_not_snippet_storage():
|
||||
cloud_init_call = next(c for c in mock_vm.config.post.call_args_list if "ide2" in c[1])
|
||||
assert cloud_init_call[1]["ide2"] == "local-zfs:cloudinit"
|
||||
assert cloud_init_call[1]["cicustom"].startswith("user=local:snippets/")
|
||||
|
||||
|
||||
def test_create_vm_passes_every_ssh_key_one_per_line():
|
||||
"""Proxmox's sshkeys is one key per line, URL-encoded (napalm-proxmox#23).
|
||||
|
||||
Joined with ";", only the first key worked. Base64 keys carry "/" and "+",
|
||||
which Proxmox's urlencoded format refuses unescaped.
|
||||
"""
|
||||
import re
|
||||
from urllib.parse import unquote
|
||||
|
||||
mixin = ProxmoxVMProvisionMixin()
|
||||
mixin._node_name = "pve1"
|
||||
mock_api = MagicMock()
|
||||
mock_api.cluster.nextid.get.return_value = 110
|
||||
mock_api.storage.return_value.get.return_value = {"path": "/var/lib/vz"}
|
||||
mock_node = MagicMock()
|
||||
mock_node.storage.get.return_value = [
|
||||
{"storage": "local", "type": "dir", "content": "images,snippets", "enabled": 1},
|
||||
]
|
||||
mixin._api = mock_api
|
||||
mixin._node_api = MagicMock(return_value=mock_node)
|
||||
mixin._download_cloud_image = MagicMock(return_value="/var/lib/vz/x.qcow2")
|
||||
mixin._run_node_command = MagicMock(return_value="")
|
||||
mock_vm = mock_node.qemu.return_value
|
||||
mock_vm.config.get.return_value = {"unused0": "local:vm-110-disk-0"}
|
||||
mock_vm.status.start.post.return_value = "UPID:pve1:136:start"
|
||||
mock_node.tasks.return_value.status.get.return_value = {"status": "stopped", "exitstatus": "OK"}
|
||||
keys = [
|
||||
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI/+k1 alice@example",
|
||||
"ssh-rsa AAAAB3NzaC1yc2EAAAADAQAB/+k2 bob@example",
|
||||
]
|
||||
|
||||
with patch("time.sleep"):
|
||||
mixin.create_vm_from_cloud_init(
|
||||
name="keys-vm",
|
||||
image_url="https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-genericcloud-amd64.qcow2",
|
||||
cpu=1,
|
||||
memory=512,
|
||||
nics=[{"bridge": "vmbr0"}],
|
||||
cloud_init_config={},
|
||||
ssh_public_keys=keys,
|
||||
)
|
||||
|
||||
call = next(c for c in mock_vm.config.post.call_args_list if "sshkeys" in c[1])
|
||||
sshkeys = call[1]["sshkeys"]
|
||||
assert unquote(sshkeys) == "\n".join(keys)
|
||||
assert re.fullmatch(r"[-%a-zA-Z0-9_.!~*'()]*", sshkeys)
|
||||
|
||||
|
||||
class _LocalShell:
|
||||
"""The node's SSH session as a local ``sh``, *prelude* run before each command."""
|
||||
|
||||
def __init__(self, prelude: str) -> None:
|
||||
self.prelude = prelude
|
||||
|
||||
def exec_command(self, command: str, timeout: int | None = None):
|
||||
import subprocess
|
||||
|
||||
run = subprocess.run(
|
||||
["sh", "-c", self.prelude + command], capture_output=True, timeout=timeout
|
||||
)
|
||||
return _mock_ssh_exec(run.returncode, run.stdout.decode(), run.stderr.decode())
|
||||
|
||||
|
||||
# wget as it fails on a full disk: part of the file written, the reason on stderr.
|
||||
_WGET_DISK_FULL = (
|
||||
"wget() { "
|
||||
'while [ $# -gt 0 ]; do [ "$1" = -O ] && o=$2; shift; done; '
|
||||
'echo partial > "$o"; '
|
||||
'echo "$o: No space left on device" >&2; return 3; }; '
|
||||
)
|
||||
|
||||
|
||||
def test_download_cloud_image_failure_says_why_and_leaves_no_tmp(tmp_path, monkeypatch):
|
||||
"""A failed download names wget's reason and removes its .tmp (napalm-proxmox#3)."""
|
||||
import napalm_proxmox.vm_provision_mixin as module
|
||||
|
||||
monkeypatch.setattr(module, "_IMAGE_CACHE_DIR", str(tmp_path))
|
||||
mixin = ProxmoxVMProvisionMixin()
|
||||
mixin._node_ssh = MagicMock(return_value=_LocalShell(_WGET_DISK_FULL))
|
||||
|
||||
with pytest.raises(RuntimeError, match="No space left on device") as err:
|
||||
mixin._download_cloud_image("https://example.org/img/debian.qcow2", None, timeout=10)
|
||||
|
||||
assert "exit 3" in str(err.value)
|
||||
assert list(tmp_path.iterdir()) == []
|
||||
|
||||
|
||||
def test_download_cloud_image_does_not_silence_wget():
|
||||
mixin = ProxmoxVMProvisionMixin()
|
||||
mixin._run_node_command = MagicMock(side_effect=["MISSING", ""])
|
||||
|
||||
mixin._download_cloud_image("https://example.org/img/debian.qcow2", None, timeout=10)
|
||||
|
||||
download = mixin._run_node_command.call_args_list[1][0][0]
|
||||
assert download.startswith("wget -nv ")
|
||||
Reference in new issue
Block a user