"""Time sync on a Proxmox VE node and a Proxmox Backup Server (NetOrk/netork#2365). Neither API keeps time servers: both products run chrony on Debian. The servers are napalm-device-types' ``PosixTimeSyncMixin``'s, over the driver's SSH session (``run_command``, as root); the zone is read from the host the same way and written through the product's own ``PUT /nodes/{node}/time``, which also keeps ``/etc/timezone`` for the web UI. One netOrk device is one node: a PVE node in a cluster has its own clock, and only that node is written. The readings are what ``reading_command`` prints on a PVE 8 / PBS 3 host (Debian 12's chrony 4.3, its configuration comments shortened). """ from __future__ import annotations from typing import Any from unittest.mock import patch import pytest from napalm_device_types import CommandResult, PosixTimeSyncMixin, TimeSyncMixin from proxmoxer.core import ResourceException from napalm_proxmox import ProxmoxBackupDriver, ProxmoxDriver from tests.conftest import _build_mock_api from tests.pbs_fake import FakePbsApi, forbidden _ACTIVE_CHRONY = """\ chrony chrony active chronyd chrony active systemd-timesyncd systemd-timesyncd inactive ntpsec ntpd inactive ntp ntpd inactive ntpd ntpd inactive openntpd openntpd inactive""" _CHRONYC_SYNCED = """\ MS Name/IP address Stratum Poll Reach LastRx Last sample =============================================================================== ^- 162.159.200.123 3 6 377 41 +814us[ +814us] +/- 14ms ^* 185.248.188.98 2 6 377 40 -45us[ -63us] +/- 10ms""" #: Debian 12's /etc/chrony/chrony.conf as Proxmox VE 8 and PBS 3 ship it. _CHRONY_CONF = """\ # Welcome to the chrony configuration file. See chrony.conf(5) for more # information about usable directives. # Include configuration files found in /etc/chrony/conf.d. confdir /etc/chrony/conf.d # Use Debian vendor zone. {pool}pool 2.debian.pool.ntp.org iburst # Use time sources from DHCP. {dhcp}sourcedir /run/chrony-dhcp # Use NTP sources found in /etc/chrony/sources.d. sourcedir /etc/chrony/sources.d keyfile /etc/chrony/chrony.keys driftfile /var/lib/chrony/chrony.drift ntsdumpdir /var/lib/chrony logdir /var/log/chrony maxupdateskew 100.0 rtcsync makestep 1 3 leapsectz right/UTC""" _MARK = "#napalm-time-sync: " def _reading( zone: str = "Europe/Berlin", *, servers: tuple[str, ...] = (), timedatectl: bool = True ) -> str: """What the reading prints; *servers* in netOrk's sources file, the vendor's pool commented out with it (as a write leaves the host).""" written = bool(servers) conf = _CHRONY_CONF.format(pool=_MARK if written else "", dhcp=_MARK if written else "") parts = ["@@ts timedatectl"] if timedatectl: parts += [ f"Timezone={zone}", "LocalRTC=no", "CanNTP=yes", "NTP=yes", "NTPSynchronized=yes", "TimeUSec=Wed 2026-11-11 14:23:16 CET", "RTCTimeUSec=Wed 2026-11-11 13:23:16 CET", ] parts += ["@@ts zone", zone if timedatectl else ""] parts += ["@@ts active", _ACTIVE_CHRONY, "@@ts timesync", "@@ts chronyc", _CHRONYC_SYNCED] parts += ["@@ts chrony 0 main /etc/chrony/chrony.conf", conf, ""] if written: parts += ["@@ts chrony 14 sourcedir /etc/chrony/sources.d/netork.sources"] parts += [f"{'pool' if '.pool.' in s else 'server'} {s} iburst" for s in servers] parts += [""] parts += ["@@ts end", ""] return "\n".join(parts) class Node: """The node's command channel: the readings in turn (the last one stays), and every other command succeeds. Records ``(command, privileged, stdin)``.""" def __init__(self, *readings: str) -> None: self.readings = list(readings) self.calls: list[tuple[str, bool, bytes | None]] = [] def __call__( self, command: str, *, privileged: bool = False, timeout: float = 60, stdin: bytes | None = None, ) -> CommandResult: self.calls.append((command, privileged, stdin)) if "@@ts timedatectl" in command: reading = self.readings.pop(0) if len(self.readings) > 1 else self.readings[0] return CommandResult(reading, "", 0) if command.startswith("t=$(mktemp)"): return CommandResult("/tmp/tmp.Xk3fQ9", "", 0) return CommandResult("", "", 0) def root(self) -> list[str]: return [c for c, privileged, _ in self.calls if privileged] def staged(self) -> list[bytes]: return [stdin for _, _, stdin in self.calls if stdin is not None] def _pve(*readings: str, node: str | None = None) -> tuple[ProxmoxDriver, Any, Node]: args = {"verify_ssl": False} if node is None else {"verify_ssl": False, "node": node} driver = ProxmoxDriver("pve1.example.com", "root", "secret", optional_args=args) api = _build_mock_api() with patch("napalm_proxmox.driver.ProxmoxAPI", return_value=api): driver.open() shell = Node(*readings) driver.run_command = shell # type: ignore[method-assign] return driver, api, shell def _pbs(*readings: str, **responses: Any) -> tuple[ProxmoxBackupDriver, FakePbsApi, Node]: api = FakePbsApi(**responses) driver = ProxmoxBackupDriver("pbs1.example.com", "root@pam", "secret") with patch("napalm_proxmox.backup_driver.ProxmoxAPI", return_value=api): driver.open() shell = Node(*readings) driver.run_command = shell # type: ignore[method-assign] return driver, api, shell # -- the contract ----------------------------------------------------------------------------- @pytest.mark.parametrize("cls", [ProxmoxDriver, ProxmoxBackupDriver], ids=["pve", "pbs"]) def test_both_drivers_read_and_write_servers_and_zone(cls: type) -> None: assert issubclass(cls, TimeSyncMixin) assert issubclass(cls, PosixTimeSyncMixin) assert callable(getattr(cls, "get_time_sync", None)) assert callable(getattr(cls, "configure_time_sync", None)) assert cls.time_sync_writes == frozenset({"ntp_servers", "timezone"}) # -- reading ---------------------------------------------------------------------------------- def test_pve_reads_chrony_and_the_zone_in_one_round_trip() -> None: driver, api, shell = _pve(_reading()) assert driver.get_time_sync() == { "ntp_servers": ["2.debian.pool.ntp.org"], "timezone": "Europe/Berlin", "synchronized": True, "daemon": "chrony", } assert len(shell.calls) == 1 assert shell.root() == [] # the reading writes nothing api.nodes.return_value.time.put.assert_not_called() def test_pbs_reads_chrony_and_the_zone() -> None: driver, api, shell = _pbs(_reading("Europe/Vienna", servers=("10.0.0.1",))) assert driver.get_time_sync() == { "ntp_servers": ["10.0.0.1"], "timezone": "Europe/Vienna", "synchronized": True, "daemon": "chrony", } assert len(shell.calls) == 1 assert not [c for c in api.calls if c[0] != "GET"] def test_a_host_without_a_zone_of_its_own_reads_the_apis() -> None: """No timedatectl and no /etc/timezone or /etc/localtime link: the API's word.""" driver, api, _ = _pve(_reading(timedatectl=False)) api.nodes.return_value.time.get.return_value = {"timezone": "Europe/Paris"} assert driver.get_time_sync()["timezone"] == "Europe/Paris" def test_no_zone_anywhere_reads_none() -> None: driver, _, _ = _pbs(_reading(timedatectl=False), **{"nodes/localhost/time": forbidden()}) assert driver.get_time_sync()["timezone"] is None def test_a_cut_reading_raises() -> None: driver, _, _ = _pve("@@ts timedatectl\nTimezone=Europe/Berlin\n") with pytest.raises(ValueError, match="cut short"): driver.get_time_sync() @pytest.mark.parametrize("make", [_pve, _pbs], ids=["pve", "pbs"]) def test_get_ntp_servers_is_what_get_time_sync_reads(make: Any) -> None: servers = ("10.0.0.1", "0.de.pool.ntp.org") driver, _, _ = make(_reading(servers=servers)) assert list(driver.get_ntp_servers()) == list(servers) assert driver.get_ntp_servers() == {s: {} for s in driver.get_time_sync()["ntp_servers"]} def test_get_ntp_servers_no_longer_asks_an_api_path_pve_does_not_have() -> None: """``GET /nodes/{node}/ntp`` does not exist; asking it read as "no servers".""" driver, api, _ = _pve(_reading()) assert driver.get_ntp_servers() == {"2.debian.pool.ntp.org": {}} api.nodes.return_value.ntp.get.assert_not_called() # -- writing the zone ------------------------------------------------------------------------- def test_pve_writes_the_zone_through_its_node_api_and_reads_it_back() -> None: driver, api, shell = _pve(_reading("Europe/Vienna")) result = driver.configure_time_sync(timezone="Europe/Vienna") assert result["success"], result["output"] assert [s["step"] for s in result["steps"]] == ["timezone", "verify"] api.nodes.return_value.time.put.assert_called_once_with(timezone="Europe/Vienna") assert shell.root() == [] # not timedatectl: the API keeps /etc/timezone as well def test_in_a_cluster_only_this_node_is_written() -> None: driver, api, _ = _pve(_reading("Europe/Vienna"), node="pve2") api.nodes.reset_mock() driver.configure_time_sync(timezone="Europe/Vienna") assert {c.args for c in api.nodes.call_args_list} == {("pve2",)} api.nodes.return_value.time.put.assert_called_once_with(timezone="Europe/Vienna") def test_pbs_writes_the_zone_through_its_api() -> None: driver, api, _ = _pbs(_reading("Europe/Vienna"), **{"PUT nodes/localhost/time": None}) result = driver.configure_time_sync(timezone="Europe/Vienna") assert result["success"], result["output"] assert ("PUT", "nodes/localhost/time", {"timezone": "Europe/Vienna"}) in api.calls def test_a_refused_zone_fails_the_step_and_says_why() -> None: """An API token without ``Sys.Modify``: the step fails, nothing else is tried.""" driver, api, shell = _pbs(_reading(), **{"PUT nodes/localhost/time": forbidden()}) result = driver.configure_time_sync(timezone="Europe/Vienna") assert not result["success"] assert result["steps"][-1]["step"] == "timezone" assert "403" in result["steps"][-1]["output"] assert shell.calls == [] def test_a_zone_the_device_did_not_take_fails_verify() -> None: driver, api, _ = _pve(_reading("Europe/Berlin")) result = driver.configure_time_sync(timezone="Europe/Vienna") assert not result["success"] assert result["steps"][-1] == { "step": "verify", "ok": False, "output": "the device is in the time zone Europe/Berlin", } def test_a_proxmoxer_error_on_the_zone_is_the_steps_output() -> None: driver, api, _ = _pve(_reading()) api.nodes.return_value.time.put.side_effect = ResourceException( 400, "Bad Request", "Parameter verification failed: timezone: No such timezone" ) result = driver.configure_time_sync(timezone="Europe/Vienna") assert not result["success"] assert "No such timezone" in result["steps"][0]["output"] # -- writing the servers ---------------------------------------------------------------------- @pytest.mark.parametrize("make", [_pve, _pbs], ids=["pve", "pbs"]) def test_servers_go_to_chronys_sources_d_and_the_vendor_pool_stops_counting(make: Any) -> None: servers = ["10.0.0.1", "0.de.pool.ntp.org"] driver, _, shell = make(_reading(), _reading(servers=tuple(servers))) result = driver.configure_time_sync(ntp_servers=servers) assert result["success"], result["output"] assert shell.staged() == [b"server 10.0.0.1 iburst\npool 0.de.pool.ntp.org iburst\n"] root = shell.root() assert any( "/etc/chrony/sources.d/netork.sources" in c and c.startswith("install") for c in root ) # line 8 is the vendor's pool, line 11 the DHCP sourcedir: both in the main file, # which chronyd reads only when it starts. sed = next(c for c in root if c.startswith("sed -i")) assert "8{" in sed and "11{" in sed and sed.endswith("/etc/chrony/chrony.conf") assert root[-1] == "systemctl restart chrony.service" def test_servers_already_written_are_written_the_same_way_again() -> None: """No churn: the second write stages the same file and comments out nothing.""" servers = ("10.0.0.1",) driver, _, shell = _pve(_reading(servers=servers)) result = driver.configure_time_sync(ntp_servers=list(servers)) assert result["success"], result["output"] assert shell.staged() == [b"server 10.0.0.1 iburst\n"] assert not [c for c in shell.root() if c.startswith("sed")] assert "chronyc reload sources" in shell.root() def test_both_values_in_one_change() -> None: driver, api, _ = _pve(_reading(), _reading("Europe/Vienna", servers=("10.0.0.1",))) result = driver.configure_time_sync(ntp_servers=["10.0.0.1"], timezone="Europe/Vienna") assert result["success"], result["output"] assert [s["step"] for s in result["steps"]] == ["ntp_servers", "timezone", "verify"] api.nodes.return_value.time.put.assert_called_once_with(timezone="Europe/Vienna") def test_the_templates_refusals_hold() -> None: driver, api, shell = _pve(_reading()) with pytest.raises(ValueError): driver.configure_time_sync() with pytest.raises(ValueError): driver.configure_time_sync(ntp_servers=["not a server"]) with pytest.raises(ValueError): driver.configure_time_sync(timezone="CET-1CEST,M3.5.0,M10.5.0/3") assert shell.calls == [] api.nodes.return_value.time.put.assert_not_called()