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