Files
napalm-proxmox/tests/test_pbs_connection.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

97 lines
3.6 KiB
Python

"""Proxmox Backup Server: the second driver of this package (#52).
Its API and web UI listen on 8007, not on PVE's 8006, and its node is always
``localhost``: a PBS has no cluster to look itself up in.
"""
from __future__ import annotations
import tomllib
from pathlib import Path
from unittest.mock import MagicMock, patch
import pytest
from napalm.base.exceptions import ConnectionException
from napalm_device_types.roles import role_keys_of
from napalm_proxmox import ProxmoxBackupDriver
def _open(username: str = "root", password: str = "secret", **optional_args):
drv = ProxmoxBackupDriver("pbs1", username, password, optional_args=optional_args)
api = MagicMock()
api.version.get.return_value = {"version": "4.0.14", "release": "4.0", "repoid": "abc"}
with patch("napalm_proxmox.backup_driver.ProxmoxAPI", return_value=api) as cls:
drv.open()
return drv, api, cls.call_args.kwargs
class TestOpen:
def test_password_login_on_the_pbs_port(self):
_, _, kwargs = _open()
assert kwargs == {
"host": "pbs1",
"user": "root@pam",
"password": "secret",
"port": 8007,
"verify_ssl": True,
"backend": "https",
"service": "PBS",
}
def test_token_is_split_for_proxmoxer(self):
_, _, kwargs = _open(token_name="root@pam!netork", token_value="s3cret")
assert (kwargs["user"], kwargs["token_name"], kwargs["token_value"]) == (
"root@pam",
"netork",
"s3cret",
)
assert "password" not in kwargs
def test_port_from_the_arguments(self):
assert _open(port=443)[2]["port"] == 443
def test_node_is_localhost_without_asking_a_cluster(self):
drv, api, _ = _open()
assert drv._node_name == "localhost"
api.cluster.status.get.assert_not_called()
def test_open_proves_the_login(self):
"""proxmoxer logs in lazily for a token; open() must fail on a bad one,
not the first getter."""
drv = ProxmoxBackupDriver("pbs1", "root", "", optional_args={"token_name": "a!b"})
api = MagicMock()
api.version.get.side_effect = Exception("401 Unauthorized")
with patch("napalm_proxmox.backup_driver.ProxmoxAPI", return_value=api):
with pytest.raises(ConnectionException, match="401"):
drv.open()
def test_alive_and_closed(self):
drv, api, _ = _open()
assert drv.is_alive() == {"is_alive": True}
drv.close()
assert drv.is_alive() == {"is_alive": False}
class TestDriverFacts:
def test_backup_server_first_storage_second(self):
assert role_keys_of(ProxmoxBackupDriver)[:2] == ["backup_server", "storage"]
def test_found_on_8007_by_its_own_banner(self):
assert [(p.scheme, p.port) for p in ProxmoxBackupDriver.PORT_SPECS] == [("https", 8007)]
mandatory = [r.pattern for r in ProxmoxBackupDriver.HTTP_FINGERPRINT if r.mandatory]
assert mandatory == ["proxmox backup server"]
def test_names(self):
assert ProxmoxBackupDriver.DRIVER_NAME == "proxmox_backup"
assert ProxmoxBackupDriver.platform == "proxmox_backup"
assert ProxmoxBackupDriver.VENDOR == "Proxmox"
assert ProxmoxBackupDriver.FULL_UPGRADE is True
def test_entry_point(self):
project = tomllib.loads((Path(__file__).parents[1] / "pyproject.toml").read_text())
assert project["project"]["entry-points"]["napalm.drivers"] == {
"proxmox": "napalm_proxmox:ProxmoxDriver",
"proxmox_backup": "napalm_proxmox:ProxmoxBackupDriver",
}