diff --git a/CHANGELOG.md b/CHANGELOG.md index 690e7b0..908c05c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -69,6 +69,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 `ssh_private_key_str`, not only RSA, and uses the key even when a password is set too (the password stays the fallback). A key that cannot be read is logged as a warning and the password used (#17). +- `destroy_vm()` returns once Proxmox has deleted the VM, not as soon as the + delete was queued, and raises when the delete fails (#15). ## [0.8.0] - 2026-10-23 diff --git a/napalm_proxmox/vm_provision_mixin.py b/napalm_proxmox/vm_provision_mixin.py index 0d7577d..bce78f5 100644 --- a/napalm_proxmox/vm_provision_mixin.py +++ b/napalm_proxmox/vm_provision_mixin.py @@ -819,16 +819,7 @@ class ProxmoxVMProvisionMixin: snippets = self._cicustom_snippets(vmid_int) # Step 2: Delete VM - # Proxmox's API parameter is hyphenated (destroy-unreferenced-disks), - # not a valid Python identifier — proxmoxer forwards kwargs to the - # request verbatim with no underscore-to-hyphen translation, so this - # must be built as a dict and unpacked rather than passed as a kwarg. - _logger.debug(f"Deleting VM {vmid} configuration and disks") - delete_params = { - "purge": 1, - "destroy-unreferenced-disks": 1 if remove_disk else 0, - } - self._node_api().qemu(vmid_int).delete(**delete_params) + self._delete_vm(vmid_int, remove_disk, timeout) # Step 3: Clean up Cloud-Init snippets # (This is best-effort; snippet files may be unreachable if storage is unavailable) @@ -845,6 +836,21 @@ class ProxmoxVMProvisionMixin: _logger.exception(f"Failed to destroy VM {vmid}: {e}") raise + def _delete_vm(self, vmid: int, remove_disk: bool, timeout: int) -> None: + """Delete the VM and wait for it to be gone: the delete is a task.""" + _logger.debug(f"Deleting VM {vmid} configuration and disks") + # Proxmox's API parameter is hyphenated (destroy-unreferenced-disks), + # not a valid Python identifier — proxmoxer forwards kwargs to the + # request verbatim with no underscore-to-hyphen translation, so this + # must be built as a dict and unpacked rather than passed as a kwarg. + delete_params = { + "purge": 1, + "destroy-unreferenced-disks": 1 if remove_disk else 0, + } + upid = self._node_api().qemu(vmid).delete(**delete_params) + if upid: + self._wait_for_task(upid, timeout=timeout) + def _cicustom_snippets(self, vmid: int) -> list[tuple[str, str]]: """``(storage, path)`` of every snippet the VM's cicustom names. diff --git a/tests/quality_baseline.json b/tests/quality_baseline.json index ef77295..fdceda3 100644 --- a/tests/quality_baseline.json +++ b/tests/quality_baseline.json @@ -39,7 +39,7 @@ "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.create_vm_from_cloud_init": 232, - "napalm_proxmox/vm_provision_mixin.py::ProxmoxVMProvisionMixin.destroy_vm": 65, + "napalm_proxmox/vm_provision_mixin.py::ProxmoxVMProvisionMixin.destroy_vm": 56, "napalm_proxmox/vm_provision_mixin.py::ProxmoxVMProvisionMixin.get_vm_status": 92 }, "mypy_relaxed": [ diff --git a/tests/test_vm_provision_mixin.py b/tests/test_vm_provision_mixin.py index 06fbe36..e828903 100644 --- a/tests/test_vm_provision_mixin.py +++ b/tests/test_vm_provision_mixin.py @@ -484,6 +484,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 # ---------------------------------------------------------------------------