"""Time sync on OPNsense: the time servers and the zone of config.xml (NetOrk/netork#2363). OPNsense keeps both in its configuration: ````, space-separated in the order of *Services -> Network Time -> General*, and ````, an IANA name set on *System -> Settings -> General*. No API controller reads or writes either, up to 26.7 and on ``master``: both pages are legacy PHP. So the driver reads them from config.xml (``/api/core/backup/download/this``, as ``get_config`` does), asks ntpd over ``/api/ntpd/service/status`` whether the clock follows a peer, and writes nothing: ``time_sync_writes`` is empty and ``configure_time_sync`` is None. ``config_xml_sample.xml`` is OPNsense's own ``src/etc/config.xml.sample`` (core ``master``, 2026-11) without the default root user. ``ntpd_service_status.json`` is the answer of ``/api/ntpd/service/status`` in its 26.7 shape: ``ntpq -pnw`` as rows, the pool associations marked ``__pool``, the system peer ``*``. """ from __future__ import annotations import json from pathlib import Path from unittest.mock import MagicMock, patch from xml.etree.ElementTree import ParseError import pytest import requests from napalm_device_types import TimeSyncMixin from napalm_opnsense.opnsense import OPNsenseDriver FIXTURES = Path(__file__).parent / "fixtures" CONFIG_XML = (FIXTURES / "config_xml_sample.xml").read_text() NTPD_STATUS = json.loads((FIXTURES / "ntpd_service_status.json").read_text()) CONFIG_PATH = "/api/core/backup/download/this" STATUS_PATH = "/api/ntpd/service/status" POOLS = [f"{n}.opnsense.pool.ntp.org" for n in range(4)] def _configured(servers: str | None = None, zone: str | None = None) -> str: """The sample config.xml with other time servers or another zone ("" = element empty, None = as the sample has it).""" xml = CONFIG_XML if servers is not None: xml = xml.replace(" ".join(POOLS), servers) if zone is not None: xml = xml.replace("Etc/UTC", f"{zone}") return xml def _without(element: str, xml: str = CONFIG_XML) -> str: start = xml.index(f"<{element}>") end = xml.index(f"") + len(f"") return xml[:start] + xml[end:] class Firewall: """Answers GETs as the firewall's API would; every request is recorded.""" def __init__(self, config=CONFIG_XML, status=NTPD_STATUS, status_error=None): self.config = config self.status = status self.status_error = status_error self.paths: list[str] = [] def get(self, url, timeout=None): path = url.removeprefix("https://fw") self.paths.append(path) response = MagicMock() response.raise_for_status.return_value = None if path == CONFIG_PATH: if isinstance(self.config, Exception): response.raise_for_status.side_effect = self.config response.text = self.config response.json.side_effect = json.JSONDecodeError("Expecting value", "<", 0) return response if path == STATUS_PATH: if self.status_error is not None: response.raise_for_status.side_effect = self.status_error response.json.return_value = self.status return response raise AssertionError(f"unexpected GET {path}") @pytest.fixture def driver(): with patch("napalm_opnsense.opnsense.requests.Session"): drv = OPNsenseDriver( hostname="fw", username="k", password="s", optional_args={"verify": False} ) drv.session = MagicMock() yield drv def _on(driver, firewall: Firewall) -> Firewall: driver.session.get.side_effect = firewall.get return firewall class TestGetTimeSync: def test_the_servers_and_zone_of_config_xml(self, driver): _on(driver, Firewall()) assert driver.get_time_sync() == { "ntp_servers": POOLS, "timezone": "Etc/UTC", "synchronized": True, "daemon": "ntpd", } def test_the_servers_in_the_order_the_firewall_keeps_them(self, driver): _on(driver, Firewall(config=_configured("10.0.0.1 ptbtime1.ptb.de time.cloudflare.com"))) assert driver.get_time_sync()["ntp_servers"] == [ "10.0.0.1", "ptbtime1.ptb.de", "time.cloudflare.com", ] def test_the_zone_by_its_iana_name(self, driver): _on(driver, Firewall(config=_configured(zone="Europe/Berlin"))) assert driver.get_time_sync()["timezone"] == "Europe/Berlin" @pytest.mark.parametrize( "config", [_configured(zone=""), _without("timezone")], ids=["empty", "missing"] ) def test_no_zone_is_the_utc_opnsense_runs_then(self, driver, config): # system_timezone_configure() puts Etc/UTC in place for an empty or missing zone. _on(driver, Firewall(config=config)) assert driver.get_time_sync()["timezone"] == "Etc/UTC" @pytest.mark.parametrize( "config", [_configured(servers=""), _without("timeservers")], ids=["empty", "missing"] ) def test_no_servers_is_an_unsynchronised_clock_and_ntpd_is_not_asked(self, driver, config): # Without OPNsense does not run ntpd (ntpd_enabled()). firewall = _on(driver, Firewall(config=config)) sync = driver.get_time_sync() assert sync["ntp_servers"] == [] assert sync["synchronized"] is False assert STATUS_PATH not in firewall.paths def test_a_configuration_without_a_system_section(self, driver): _on(driver, Firewall(config=_without("system"))) sync = driver.get_time_sync() assert (sync["ntp_servers"], sync["timezone"]) == ([], "Etc/UTC") def test_no_system_peer_is_an_unsynchronised_clock(self, driver): rows = [ dict(row, status="+" if row["status"] == "*" else row["status"]) for row in NTPD_STATUS["rows"] ] _on(driver, Firewall(status={**NTPD_STATUS, "rows": rows})) assert driver.get_time_sync()["synchronized"] is False def test_ntpd_without_peers_yet_is_an_unsynchronised_clock(self, driver): _on(driver, Firewall(status={"total": 0, "rowCount": 0, "current": 1, "rows": []})) assert driver.get_time_sync()["synchronized"] is False @pytest.mark.parametrize("status", [None, [], {"rows": "x"}], ids=["null", "list", "rows"]) def test_a_status_of_another_shape_is_unknown(self, driver, status): _on(driver, Firewall(status=status)) assert driver.get_time_sync()["synchronized"] is None def test_a_failed_status_read_is_unknown_and_keeps_the_rest(self, driver): _on(driver, Firewall(status_error=requests.HTTPError("403 Forbidden"))) sync = driver.get_time_sync() assert sync["synchronized"] is None assert sync["ntp_servers"] == POOLS assert sync["timezone"] == "Etc/UTC" def test_a_failed_configuration_read_raises(self, driver): # "No servers" would be a reading; a key without the backup privilege is none. _on(driver, Firewall(config=requests.HTTPError("403 Forbidden"))) with pytest.raises(requests.HTTPError, match="403"): driver.get_time_sync() def test_a_configuration_that_is_no_xml_raises(self, driver): _on(driver, Firewall(config="Login")) with pytest.raises(ParseError): driver.get_time_sync() def test_a_reading_only_reads(self, driver): firewall = _on(driver, Firewall()) driver.get_time_sync() assert firewall.paths == [CONFIG_PATH, STATUS_PATH] driver.session.post.assert_not_called() class TestGetNtpServers: def test_the_configured_servers_as_napalm_keys_them(self, driver): _on(driver, Firewall()) servers = driver.get_ntp_servers() assert servers == {name: {} for name in POOLS} assert list(servers) == POOLS def test_the_same_servers_as_get_time_sync(self, driver): _on(driver, Firewall(config=_configured("10.0.0.1 ptbtime1.ptb.de"))) assert list(driver.get_ntp_servers()) == driver.get_time_sync()["ntp_servers"] def test_only_the_configuration_is_read(self, driver): firewall = _on(driver, Firewall()) driver.get_ntp_servers() assert firewall.paths == [CONFIG_PATH] def test_no_servers_configured(self, driver): _on(driver, Firewall(config=_without("timeservers"))) assert driver.get_ntp_servers() == {} def test_a_failed_read_raises_instead_of_reading_as_no_servers(self, driver): _on(driver, Firewall(config=requests.HTTPError("403 Forbidden"))) with pytest.raises(requests.HTTPError): driver.get_ntp_servers() class TestWhatTheDriverOffers: def test_it_reads_time_sync(self): assert issubclass(OPNsenseDriver, TimeSyncMixin) assert callable(getattr(OPNsenseDriver, "get_time_sync", None)) def test_it_writes_nothing(self): # Both values are set on legacy pages; no API controller writes them. assert OPNsenseDriver.time_sync_writes == frozenset() assert getattr(OPNsenseDriver, "configure_time_sync", None) is None def test_where_a_person_sets_the_servers(self): assert OPNsenseDriver.TIME_SERVERS_MENU == ("Services", "Network Time", "General") def test_where_a_person_sets_the_zone(self): assert OPNsenseDriver.TIME_ZONE_MENU == ("System", "Settings", "General")