"""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", }