Files
napalm-proxmox/tests/test_running_backups.py
T
Christian Manivong 3877a5425a
driver / lint (pull_request) Successful in 13s
driver / standard (pull_request) Successful in 14s
driver / types (pull_request) Successful in 54s
driver / test (3.13) (pull_request) Successful in 1m2s
driver / test (3.12) (pull_request) Successful in 1m5s
driver / lowest (pull_request) Successful in 31s
driver / build (pull_request) Successful in 26s
driver / security (pull_request) Successful in 54s
CI / driver (pull_request) Successful in 2m10s
driver / publish (pull_request) Skipped
feat(running-backups): what a restart of a PVE node, a guest or a PBS would abort
netOrk holds a host's restart after patching while a backup runs on it, and
a guest's restart while its hypervisor is backing it up (NetOrk/netork#2318).
It asks right before the restart, so both drivers answer
get_running_backups() from napalm-device-types 4.34.0:

- PVE: the node's active vzdump tasks, each with the guests it is taking now
  (its own guest, or for a job the guests locked for their backup), and a
  guest locked without a task on its own, since the task list can lag and a
  lock can outlive its task.
- PBS: the running backup, sync, verify, gc, prune and tape tasks; a restore
  or housekeeping is no backup.

A failed read raises, so netOrk reads it as "not checked", never as "none".
2026-11-10 23:45:48 +01:00

152 lines
5.4 KiB
Python

"""get_running_backups: what a restart of the node, or of one of its guests, would abort.
PVE: the node's active vzdump tasks, and the guests locked for their backup
(``lock: backup``); a task names its guest when it backs up one, else the
guests it is taking now are the locked ones. PBS: the running backup, sync,
verify, garbage collection, prune and tape tasks. A failed read raises: an
empty list would say nothing runs (NetOrk/netork#2318).
"""
from __future__ import annotations
import pytest
from proxmoxer.core import ResourceException
from napalm_proxmox import ProxmoxBackupDriver
from tests.pbs_fake import FakePbsApi
T0 = 1793660400
UPID_JOB = "UPID:pve1:0001F2A3:00C4B1D2:6731A0C0:vzdump::root@pam:"
UPID_ONE = "UPID:pve1:0001F2A4:00C4B1D3:6731A0D0:vzdump:105:root@pam:"
def _pve(driver, tasks, resources):
node = driver._api.nodes.return_value
node.tasks.get.return_value = tasks
driver._api.cluster.resources.get.return_value = resources
return node
def test_nothing_runs(driver):
_pve(driver, [], [{"vmid": 101, "node": "pve1", "type": "qemu"}])
assert driver.get_running_backups() == []
def test_asks_for_the_nodes_active_vzdump_tasks_and_the_guests(driver):
node = _pve(driver, [], [])
driver.get_running_backups()
node.tasks.get.assert_called_once_with(source="active", typefilter="vzdump")
driver._api.cluster.resources.get.assert_called_once_with(type="vm")
def test_a_job_takes_the_guests_locked_on_this_node(driver):
_pve(
driver,
[{"upid": UPID_JOB, "type": "vzdump", "id": "", "starttime": T0}],
[
{"vmid": 101, "node": "pve1", "type": "qemu", "lock": "backup"},
{"vmid": 102, "node": "pve1", "type": "lxc"},
{"vmid": 103, "node": "pve2", "type": "qemu", "lock": "backup"},
{"vmid": 104, "node": "pve1", "type": "qemu", "lock": "migrate"},
],
)
assert driver.get_running_backups() == [
{"kind": "backup", "job": None, "task": UPID_JOB, "guests": ["101"], "since": T0}
]
def test_a_task_for_one_guest_names_it(driver):
_pve(
driver,
[{"upid": UPID_ONE, "type": "vzdump", "id": "105", "starttime": T0}],
[{"vmid": 105, "node": "pve1", "type": "qemu", "lock": "backup"}],
)
assert driver.get_running_backups() == [
{"kind": "backup", "job": None, "task": UPID_ONE, "guests": ["105"], "since": T0}
]
def test_a_guest_locked_without_a_task_still_counts(driver):
"""The task list can lag, or a lock outlive its task: the guest is not restarted."""
_pve(driver, [], [{"vmid": 101, "node": "pve1", "type": "qemu", "lock": "backup"}])
assert driver.get_running_backups() == [
{"kind": "backup", "job": None, "task": None, "guests": ["101"], "since": None}
]
def test_another_tasks_type_is_left_out(driver):
_pve(driver, [{"upid": "UPID:pve1:1:2:3:qmstart:101:root@pam:", "type": "qmstart"}], [])
assert driver.get_running_backups() == []
def test_a_failed_read_raises(driver):
driver._api.nodes.return_value.tasks.get.side_effect = ResourceException(
403, "Forbidden", "Permission check failed (/nodes/pve1, Sys.Audit)"
)
with pytest.raises(ResourceException):
driver.get_running_backups()
# --------------------------------------------------------------------------- #
# Proxmox Backup Server
# --------------------------------------------------------------------------- #
def _pbs(tasks):
drv = ProxmoxBackupDriver("pbs1", "root", "secret")
drv._api = FakePbsApi(**{"nodes/localhost/tasks?running=1": tasks})
drv._node_name = "localhost"
return drv
def _task(worker_type: str, worker_id: str | None, n: int = 1) -> dict:
upid = f"UPID:localhost:{n:08X}:00000000:00000000:6731A0C0:{worker_type}:{worker_id or ''}:root@pam:"
return {"upid": upid, "worker_type": worker_type, "worker_id": worker_id, "starttime": T0 + n}
def test_pbs_kinds():
tasks = [
_task("backup", "store1:vm/101", 1),
_task("syncjob", "store1:offsite:store1::s-1a2b", 2),
_task("verificationjob", "store1:v-3c4d", 3),
_task("verify_snapshot", "store1:vm/101/2026-11-01T00:00:00Z", 4),
_task("garbage_collection", "store1", 5),
_task("prunejob", "store1:p-5e6f", 6),
_task("tape-backup-job", "weekly", 7),
]
found = _pbs(tasks).get_running_backups()
assert [(b["kind"], b["job"]) for b in found] == [
("backup", "store1:vm/101"),
("sync", "store1:offsite:store1::s-1a2b"),
("verify", "store1:v-3c4d"),
("verify", "store1:vm/101/2026-11-01T00:00:00Z"),
("gc", "store1"),
("prune", "store1:p-5e6f"),
("tape", "weekly"),
]
assert all(b["guests"] == [] for b in found)
assert found[0]["task"] == tasks[0]["upid"]
assert found[0]["since"] == T0 + 1
def test_pbs_leaves_out_what_is_no_backup():
"""A restore reads, a log rotation is housekeeping: neither is broken by a restart
the way a backup is."""
assert (
_pbs([_task("reader", "store1:vm/101"), _task("logrotate", None)]).get_running_backups()
== []
)
def test_pbs_nothing_runs():
assert _pbs([]).get_running_backups() == []
def test_pbs_a_failed_read_raises():
drv = _pbs([])
drv._api.responses["nodes/localhost/tasks?running=1"] = ResourceException(
403, "Forbidden", "permission check failed"
)
with pytest.raises(ResourceException):
drv.get_running_backups()