Files
napalm-proxmox/tests/test_pbs_host.py
T
Christian Manivong 2b2cd9b867
driver / lint (pull_request) Successful in 14s
driver / standard (pull_request) Successful in 16s
driver / types (pull_request) Successful in 1m5s
driver / test (3.13) (pull_request) Successful in 1m4s
driver / test (3.12) (pull_request) Successful in 1m10s
driver / lowest (pull_request) Successful in 31s
driver / build (pull_request) Successful in 30s
driver / security (pull_request) Successful in 1m0s
CI / driver (pull_request) Successful in 2m20s
driver / publish (pull_request) Skipped
feat(pbs): Proxmox Backup Server as entry point proxmox_backup (#52)
A second driver in this package, ProxmoxBackupDriver. Its API and web UI
are on 8007, not on PVE's 8006, and its node is always localhost; open()
proves the login with GET /version, so a bad token fails there.

Roles: BackupServerDriver (napalm-device-types 4.25.0) first, then
StorageDriver for the disks and ZFS pools the server manages. It reads
datastores, the backup groups of every namespace with their newest
snapshot, a group's snapshots, the GC, verify, prune, sync and tape jobs
with how their last run ended, and the remotes without their secrets;
it starts garbage collection and jobs, verifies, protects and deletes
snapshots, and follows the tasks. As a host it reports facts,
interfaces, CPU and memory, disks, pools, its pbs-subscription and the
same packages, updates, host readers and sshd hardening as a PVE node.

A privilege-separated token without an ACL entry gets an empty list from
PBS, not a refusal, so every read checks /access/permissions and raises
PermissionError. A removable datastore that is not mounted is reported
as unmounted and left out of the group listing.

The fixture is what a PBS 4.2 answered, sanitised; groups, snapshots and
jobs it did not have yet follow the API schema.
2026-11-02 22:29:21 +01:00

194 lines
6.4 KiB
Python

"""The PBS driver as a host: facts, interfaces, environment, disks and pools for
the storage role, subscription and packages (#52)."""
from __future__ import annotations
import pytest
from napalm.base.exceptions import ConnectionException
from napalm_proxmox import ProxmoxBackupDriver
from tests.pbs_fake import FakePbsApi
DMI = "QEMU\nStandard PC (Q35 + ICH9, 2009)\npc-q35-10.0\n\n"
LINKS = "lo 00:00:00:00:00:00\nnic0 02:00:00:00:00:80\n"
def _driver(ssh: dict[str, str] | None = None, **responses) -> ProxmoxBackupDriver:
"""A PBS driver on recorded answers; *ssh* maps a command's start to its output,
and without it the server has no SSH login."""
drv = ProxmoxBackupDriver("192.0.2.80", "root", "", optional_args={"token_name": "a!b"})
api = FakePbsApi(**responses)
drv._api = api
drv._node_name = "localhost"
def exec_ssh(command: str) -> str:
if ssh is None:
raise ConnectionException("no SSH credential for 192.0.2.80")
for start, output in ssh.items():
if start in command:
return output
return ""
drv._exec_ssh_command = exec_ssh # type: ignore[method-assign]
return drv
SSH = {"hostname -f": "pbs.example.com", "dmi": DMI, "/sys/class/net": LINKS}
class TestFacts:
def test_from_the_api_and_ssh(self):
facts = _driver(SSH).get_facts()
assert facts == {
"uptime": 5234.0,
"vendor": "QEMU",
"model": "Standard PC (Q35 + ICH9, 2009)",
"hostname": "pbs",
"fqdn": "pbs.example.com",
"os_version": "Proxmox Backup Server 4.2",
"serial_number": "",
"interface_list": ["nic0"],
"running_kernel": "7.0.0-3-pve",
}
def test_without_ssh_the_api_alone(self):
"""A token-only server: no shell, so no DMI and no host name of its own."""
facts = _driver().get_facts()
assert (facts["hostname"], facts["fqdn"]) == ("192.0.2.80", "192.0.2.80")
assert (facts["vendor"], facts["model"]) == (
"Proxmox Server Solutions GmbH",
"Proxmox Backup Server",
)
assert facts["running_kernel"] == "7.0.0-3-pve"
class TestInterfaces:
def test_recorded_interface_with_its_mac_over_ssh(self):
assert _driver(SSH).get_interfaces() == {
"nic0": {
"is_up": True,
"is_enabled": True,
"description": "",
"last_flapped": -1.0,
"speed": -1.0,
"mtu": 1500,
"mac_address": "02:00:00:00:00:80",
}
}
def test_without_ssh_no_mac(self):
assert _driver().get_interfaces()["nic0"]["mac_address"] == ""
def test_addresses(self):
assert _driver().get_interfaces_ip() == {
"nic0": {"ipv4": {"192.0.2.80": {"prefix_length": 24}}}
}
def test_environment_from_the_node_status():
env = _driver().get_environment()
assert env["cpu"] == {"0": {"%usage": 1.49}}
assert env["memory"] == {"available_ram": 4103909376, "used_ram": 541790208}
assert (env["fans"], env["temperature"], env["power"]) == ({}, {}, {})
class TestStorageRole:
def test_recorded_disks(self):
disks = _driver().get_disks()
assert [d["slot"] for d in disks] == ["sda", "sdc"]
assert disks[1] == {
"slot": "sdc",
"model": "Samsung_SSD_850_EVO_1TB",
"serial": "S0000000000000X",
"vendor": "ASMT",
# as the server reports it: behind a USB bridge it took the SSD for a disk
"type": "hdd",
"size": 1000204886016,
"rpm": 0,
"temperature": -1,
"health": "healthy",
"pool": "",
}
assert disks[0]["type"] == "ssd"
@pytest.mark.parametrize(
("status", "health"), [("passed", "healthy"), ("failed", "failed"), ("unknown", "unknown")]
)
def test_disk_health(self, status, health):
drv = _driver(
**{
"nodes/localhost/disks/list": [
{"name": "sdc", "devpath": "/dev/sdc", "status": status}
]
}
)
assert drv.get_disks()[0]["health"] == health
def test_recorded_server_has_no_zfs_pool(self):
assert _driver().get_disk_pools() == {}
def test_zfs_pool(self):
drv = _driver(
**{
"nodes/localhost/disks/zfs": [
{
"name": "tank",
"size": 1000,
"alloc": 400,
"free": 600,
"health": "DEGRADED",
"dedup": 1.0,
"frag": 3,
}
]
}
)
assert drv.get_disk_pools() == {
"tank": {
"name": "tank",
"type": "zfs",
"level": "",
"status": "degraded",
"total": 1000,
"used": 400,
"available": 600,
"disks": [],
"auto_expand": False,
"dedup": False,
"compression": "",
}
}
class TestHost:
def test_subscription_is_the_pbs_one(self):
assert _driver().get_licenses() == [
{"name": "pbs-subscription", "status": "none", "detail": "There is no subscription key"}
]
def test_reboot_and_shutdown_through_the_api(self):
drv = _driver(**{"POST nodes/localhost/status": None})
drv.reboot_host()
drv.shutdown_host()
calls = [c for c in drv._api.calls if c[0] == "POST"]
assert calls == [
("POST", "nodes/localhost/status", {"command": "reboot"}),
("POST", "nodes/localhost/status", {"command": "shutdown"}),
]
def test_packages_name_their_source(self):
drv = _driver({"dpkg-query": "proxmox-backup-server\t4.2.0-1\tinstalled\t100\t\t\n"})
(pkg,) = drv.get_packages()
assert (pkg["name"], pkg["source"]) == ("proxmox-backup-server", "pbs")
def test_os_capabilities_present(self):
for name in (
"get_kernel_facts",
"get_listening_sockets",
"get_available_updates",
"apply_updates",
"get_ssh_server_algorithms",
"get_services",
):
assert hasattr(ProxmoxBackupDriver, name), name