diff --git a/CHANGELOG.md b/CHANGELOG.md index 1bb4fd9..d48777c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -73,6 +73,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 delete was queued, and raises when the delete fails (#15). - `ping()` fills `results` with each reply's address and round-trip time; it was always empty (#12). +- A failed cloud-image download in `create_vm_from_cloud_init()` says why + (wget's own message, such as "No space left on device") and removes its + partial `.tmp` file instead of leaving it to fill the node's disk (#3). ## [0.8.0] - 2026-10-23 diff --git a/napalm_proxmox/vm_provision_mixin.py b/napalm_proxmox/vm_provision_mixin.py index bce78f5..02d6194 100644 --- a/napalm_proxmox/vm_provision_mixin.py +++ b/napalm_proxmox/vm_provision_mixin.py @@ -186,12 +186,7 @@ class ProxmoxVMProvisionMixin: timeout=30, ) if "EXISTS" not in exists: - _logger.info(f"Downloading cloud image {image_url} -> {local_path}") - self._run_node_command( - f"wget -q -O {local_path}.tmp '{image_url}' " - f"&& mv {local_path}.tmp {local_path}", - timeout=timeout, - ) + self._fetch_image(image_url, local_path, timeout) if not image_checksum: return local_path @@ -216,6 +211,20 @@ class ProxmoxVMProvisionMixin: raise AssertionError("unreachable") # loop always returns or raises above + def _fetch_image(self, image_url: str, local_path: str, timeout: int) -> None: + """Download *image_url* to *local_path* on the node, through a ``.tmp``. + + wget runs with ``-nv`` so its reason ("No space left on device") + reaches the error, and a failed download leaves no partial ``.tmp``. + """ + _logger.info(f"Downloading cloud image {image_url} -> {local_path}") + tmp = f"{local_path}.tmp" + self._run_node_command( + f"wget -nv -O {tmp} '{image_url}' && mv {tmp} {local_path} " + f"|| {{ s=$?; rm -f {tmp}; exit $s; }}", + timeout=timeout, + ) + def _cloud_image_on_node(self, image_url: str, image_checksum: str | None, timeout: int) -> str: """The node path of the image, downloaded, verified and unpacked. diff --git a/tests/quality_baseline.json b/tests/quality_baseline.json index fdceda3..6c9c8d9 100644 --- a/tests/quality_baseline.json +++ b/tests/quality_baseline.json @@ -37,7 +37,7 @@ "napalm_proxmox/system_mixin.py::ProxmoxSystemMixin.set_hostname": 114, "napalm_proxmox/vm_mixin.py::ProxmoxVMMixin.get_vm_interfaces": 179, "napalm_proxmox/vm_mixin.py::ProxmoxVMMixin.get_vms": 114, - "napalm_proxmox/vm_provision_mixin.py::ProxmoxVMProvisionMixin._download_cloud_image": 61, + "napalm_proxmox/vm_provision_mixin.py::ProxmoxVMProvisionMixin._download_cloud_image": 56, "napalm_proxmox/vm_provision_mixin.py::ProxmoxVMProvisionMixin.create_vm_from_cloud_init": 232, "napalm_proxmox/vm_provision_mixin.py::ProxmoxVMProvisionMixin.destroy_vm": 56, "napalm_proxmox/vm_provision_mixin.py::ProxmoxVMProvisionMixin.get_vm_status": 92 diff --git a/tests/test_vm_provision_mixin.py b/tests/test_vm_provision_mixin.py index e828903..d6a0235 100644 --- a/tests/test_vm_provision_mixin.py +++ b/tests/test_vm_provision_mixin.py @@ -1364,3 +1364,52 @@ def test_create_vm_passes_every_ssh_key_one_per_line(): 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 ")