"""Time sync on OpenWrt (napalm-device-types ``TimeSyncMixin``, NetOrk/netork#2364). OpenWrt keeps both values in UCI's ``system`` config: the servers in the ``system.ntp.server`` list, which sysntpd hands to BusyBox ntpd, and the zone twice -- ``zonename`` (the IANA name, as LuCI writes it: with spaces for underscores) and ``timezone``, the POSIX rule libc reads as ``TZ``. The rule is the one that counts (netOrk#1849: an IANA name in ``timezone`` left the AP on UTC). The device is a fake ``uci`` behind ``run_command``: it renders ``uci -q show system`` the way uci prints it, and applies the ``uci set``/``delete``/``add_list`` lines a write sends, so the template's ``verify`` step reads back what was written. """ from __future__ import annotations import shlex from typing import Any from unittest.mock import MagicMock, patch import pytest from napalm_device_types import CommandResult from napalm_device_types.posix_tz import UnknownTimezone, posix_tz from napalm_device_types.time_sync import TIME_SYNC_VALUES from napalm_openwrt.openwrt import OpenWrtDriver from napalm_openwrt.time_sync_mixin import parse_system_uci, zone_for_rule # ``uci show system`` of OpenWrt 23.05 out of the box: zonename 'UTC', the rule GMT0, # the four pool servers as a list. STOCK = """\ system.@system[0]=system system.@system[0].hostname='OpenWrt' system.@system[0].timezone='GMT0' system.@system[0].zonename='UTC' system.@system[0].ttylogin='0' system.@system[0].log_size='128' system.@system[0].urandom_seed='0' system.ntp=timeserver system.ntp.enabled='1' system.ntp.enable_server='0' system.ntp.server='0.openwrt.pool.ntp.org' '1.openwrt.pool.ntp.org' '2.openwrt.pool.ntp.org' '3.openwrt.pool.ntp.org' """ # An AP configured through LuCI (the get_system_config fixture of netOrk#164). LUCI_BERLIN = """\ system.@system[0]=system system.@system[0].hostname='ap-eze-Garten' system.@system[0].timezone='CET-1CEST,M3.5.0,M10.5.0/3' system.@system[0].zonename='Europe/Berlin' system.@system[0].log_remote='1' system.@system[0].log_ip='10.10.40.2' system.ntp=timeserver system.ntp.server='0.openwrt.pool.ntp.org' '1.openwrt.pool.ntp.org' """ # LuCI's zone names have spaces where the IANA names have underscores. LUCI_NEW_YORK = """\ system.@system[0]=system system.@system[0].timezone='EST5EDT,M3.2.0,M11.1.0' system.@system[0].zonename='America/New York' system.ntp=timeserver system.ntp.server='10.0.0.1' """ # netOrk#1849: the IANA name written as the rule. libc cannot read it; the AP runs on UTC. NAME_AS_RULE = """\ system.@system[0]=system system.@system[0].timezone='Europe/Berlin' system.@system[0].zonename='Europe/Berlin' system.ntp=timeserver system.ntp.server='10.0.0.1' """ # A server list kept as one option, the servers apart by spaces: sysntpd splits it. ONE_OPTION = """\ system.@system[0]=system system.@system[0].timezone='UTC' system.ntp=timeserver system.ntp.server='0.openwrt.pool.ntp.org 1.openwrt.pool.ntp.org' system.ntp.enabled='1' """ def _render(sections: list[tuple[str, str, dict[str, list[str] | str]]]) -> str: """*sections* as ``uci show system`` prints them.""" lines = [] for name, kind, options in sections: lines.append(f"system.{name}={kind}") for option, value in options.items(): values = value if isinstance(value, list) else [value] lines.append(f"system.{name}.{option}=" + " ".join(_quoted(v) for v in values)) return "\n".join(lines) + "\n" def _quoted(value: str) -> str: """*value* in single quotes, a quote in it as ``'\\''``: how uci prints every value.""" return "'" + value.replace("'", "'\\''") + "'" def _parse(output: str) -> list[tuple[str, str, dict[str, list[str] | str]]]: """``uci show system`` as the fake's state (a list is a list, an option a string).""" sections: list[tuple[str, str, dict[str, list[str] | str]]] = [] for line in output.splitlines(): key, _, raw = line.partition("=") parts = key.split(".") if len(parts) == 2: sections.append((parts[1], raw, {})) continue values = shlex.split(raw) option: list[str] | str = values if raw.count("'") > 2 else values[0] next(s for s in sections if s[0] == parts[1])[2][parts[2]] = option return sections class _Uci: """A router's ``system`` config behind ``run_command``; keeps every command it ran.""" def __init__(self, show: str = STOCK, fail: str = "") -> None: self.sections = _parse(show) self.fail = fail self.ran: list[tuple[str, bool]] = [] def writes(self) -> list[str]: return [c for c, _ in self.ran if c != "uci -q show system"] def __call__( self, command: str, *, privileged: bool = False, timeout: float = 60, stdin: bytes | None = None, ) -> CommandResult: self.ran.append((command, privileged)) if command == "uci -q show system": return CommandResult(_render(self.sections), "", 0) for part in command.split(" && "): if self.fail and self.fail in part: return CommandResult("", f"{self.fail}: failed", 1) words = shlex.split(part) if words[0] == "uci": self._uci(words[1:]) return CommandResult("", "", 0) def _section(self, name: str) -> dict[str, list[str] | str]: return next(s for s in self.sections if s[0] == name)[2] def _uci(self, args: list[str]) -> None: verb, rest = args[0], args[1:] if verb == "commit": return key, _, value = rest[0].partition("=") _, section, *option = key.split(".") if verb == "set" and not option: self.sections.append((section, value, {})) elif verb == "set": self._section(section)[option[0]] = value elif verb == "add_list": options = self._section(section) kept = options.get(option[0], []) options[option[0]] = [*([kept] if isinstance(kept, str) else kept), value] elif verb == "delete": del self._section(section)[option[0]] else: # pragma: no cover - a command the driver does not send raise AssertionError(args) def _driver(uci: _Uci, username: str = "root") -> OpenWrtDriver: with patch("napalm_openwrt.openwrt.ConnectHandler"): drv = OpenWrtDriver(hostname="192.0.2.15", username=username, password="") drv.device = MagicMock() drv.run_command = uci # type: ignore[method-assign] return drv def _read(show: str) -> dict[str, Any]: return dict(_driver(_Uci(show)).get_time_sync()) # -- the driver takes part ---------------------------------------------------------------------- def test_the_driver_reads_and_writes_both_values(): drv = _driver(_Uci()) assert callable(getattr(drv, "get_time_sync", None)) assert callable(getattr(drv, "configure_time_sync", None)) assert drv.time_sync_writes == TIME_SYNC_VALUES # -- reading ------------------------------------------------------------------------------------ def test_a_stock_router_reads_its_pool_servers_and_utc(): # zonename 'UTC' with the rule GMT0: the same time, so the name stands. assert _read(STOCK) == { "ntp_servers": [ "0.openwrt.pool.ntp.org", "1.openwrt.pool.ntp.org", "2.openwrt.pool.ntp.org", "3.openwrt.pool.ntp.org", ], "timezone": "UTC", "synchronized": None, "daemon": None, } def test_the_read_is_one_uci_show_as_the_login_user(): uci = _Uci() _driver(uci).get_time_sync() assert uci.ran == [("uci -q show system", False)] def test_a_zone_set_through_luci_reads_by_its_name(): reading = _read(LUCI_BERLIN) assert reading["timezone"] == "Europe/Berlin" assert reading["ntp_servers"] == ["0.openwrt.pool.ntp.org", "1.openwrt.pool.ntp.org"] def test_luci_spaces_read_as_the_iana_underscores(): assert _read(LUCI_NEW_YORK)["timezone"] == "America/New_York" def test_a_name_whose_rule_is_not_in_force_is_not_read_as_the_zone(): # netOrk#1849: the AP runs on UTC; reading Europe/Berlin would hide that. assert _read(NAME_AS_RULE)["timezone"] is None def test_a_rule_without_a_name_reads_as_the_one_zone_that_has_it(): show = "system.@system[0]=system\nsystem.@system[0].timezone='IST-5:30'\n" assert _read(show)["timezone"] == "Asia/Kolkata" def test_a_rule_many_zones_share_reads_as_no_zone(): # Berlin, Paris, Madrid, ... all have this rule: which one is not the driver's to guess. show = "system.@system[0]=system\nsystem.@system[0].timezone='CET-1CEST,M3.5.0,M10.5.0/3'\n" assert _read(show)["timezone"] is None def test_a_section_without_a_rule_runs_on_utc(): # /etc/init.d/system defaults the rule to UTC. assert _read("system.@system[0]=system\n")["timezone"] == "Etc/UTC" def test_no_system_section_reads_no_zone(): assert _read("")["timezone"] is None @pytest.mark.parametrize( ("rule", "zone"), [ ("UTC", "Etc/UTC"), ("UTC0", "Etc/UTC"), ("<+0545>-5:45", "Asia/Kathmandu"), ("NZST-12NZDT,M9.5.0,M4.1.0/3", "Pacific/Auckland"), ("GMT0", None), # Abidjan, Monrovia, Etc/GMT, ... ("<-00>0", None), # Factory: the zone is not known ("Europe/Berlin", None), ("", None), ], ) def test_zone_for_rule(rule, zone): assert zone_for_rule(rule) == zone def test_a_server_list_kept_as_one_option_is_split_as_sysntpd_does(): assert _read(ONE_OPTION)["ntp_servers"] == [ "0.openwrt.pool.ntp.org", "1.openwrt.pool.ntp.org", ] def test_a_disabled_client_has_no_servers(): # sysntpd starts no ntpd: the list names sources the clock does not take time from. show = STOCK.replace("system.ntp.enabled='1'", "system.ntp.enabled='0'") assert _read(show)["ntp_servers"] == [] def test_no_ntp_section_has_no_servers(): show = "system.@system[0]=system\nsystem.@system[0].timezone='UTC'\n" assert _read(show)["ntp_servers"] == [] def test_a_line_uci_would_not_print_is_passed_over(): reading = parse_system_uci(LUCI_BERLIN + "system.@system[0].zonename='Europe/Paris\n") assert reading.zone == "Europe/Berlin" def test_a_failed_read_raises(): drv = _driver(_Uci()) drv.run_command = lambda *a, **k: CommandResult("", "uci: Entry not found", 1) # type: ignore[method-assign] with pytest.raises(RuntimeError, match="Entry not found"): drv.get_time_sync() # -- get_ntp_servers agrees --------------------------------------------------------------------- @pytest.mark.parametrize("show", [STOCK, LUCI_BERLIN, ONE_OPTION, NAME_AS_RULE]) def test_get_ntp_servers_names_the_same_servers(show): drv = _driver(_Uci(show)) assert list(drv.get_ntp_servers()) == drv.get_time_sync()["ntp_servers"] def test_get_ntp_servers_reads_every_server_of_a_list(): # It used to keep only the first of 'a' 'b' 'c'. assert len(_driver(_Uci(STOCK)).get_ntp_servers()) == 4 def test_the_peers_are_the_servers(): drv = _driver(_Uci(STOCK)) assert drv.get_ntp_peers() == drv.get_ntp_servers() def test_get_ntp_servers_of_a_disabled_client_is_empty(): show = STOCK.replace("system.ntp.enabled='1'", "system.ntp.enabled='0'") assert _driver(_Uci(show)).get_ntp_servers() == {} # -- writing the servers ------------------------------------------------------------------------ def test_the_servers_replace_the_list_and_ntpd_restarts(): uci = _Uci(STOCK) result = _driver(uci).configure_time_sync(ntp_servers=["10.0.0.1", "ntp.example.org"]) assert result["success"] is True, result["output"] assert [s["step"] for s in result["steps"]] == ["ntp_servers", "verify"] assert uci._section("ntp")["server"] == ["10.0.0.1", "ntp.example.org"] (write,) = uci.writes() assert write.endswith( "uci commit system && /etc/init.d/sysntpd enable && /etc/init.d/sysntpd restart" ) assert all(privileged for c, privileged in uci.ran if c in uci.writes()) def test_writing_servers_turns_off_the_ones_dhcp_brings(): # sysntpd adds the servers of a DHCP lease to the list unless use_dhcp is 0. uci = _Uci(STOCK) _driver(uci).configure_time_sync(ntp_servers=["10.0.0.1"]) assert uci._section("ntp")["use_dhcp"] == "0" def test_writing_servers_enables_a_disabled_client(): uci = _Uci(STOCK.replace("system.ntp.enabled='1'", "system.ntp.enabled='0'")) result = _driver(uci).configure_time_sync(ntp_servers=["10.0.0.1"]) assert result["success"] is True, result["output"] assert uci._section("ntp")["enabled"] == "1" assert "/etc/init.d/sysntpd enable" in uci.writes()[0] def test_a_missing_ntp_section_is_made(): uci = _Uci("system.@system[0]=system\nsystem.@system[0].timezone='UTC'\n") result = _driver(uci).configure_time_sync(ntp_servers=["10.0.0.1"]) assert result["success"] is True, result["output"] assert ("ntp", "timeserver") in [(s[0], s[1]) for s in uci.sections] assert "uci delete system.ntp.server" not in uci.writes()[0] def test_the_same_servers_again_write_nothing(): # netOrk aligns on every poll: an aligned router must not restart ntpd each time. uci = _Uci(STOCK) drv = _driver(uci) drv.configure_time_sync(ntp_servers=["10.0.0.1", "10.0.0.2"]) uci.ran.clear() result = drv.configure_time_sync(ntp_servers=["10.0.0.1", "10.0.0.2"]) assert result["success"] is True assert uci.writes() == [] assert "already" in result["steps"][0]["output"] def test_the_same_servers_in_another_order_are_written(): uci = _Uci(LUCI_BERLIN) _driver(uci).configure_time_sync( ntp_servers=["1.openwrt.pool.ntp.org", "0.openwrt.pool.ntp.org"] ) assert uci._section("ntp")["server"] == ["1.openwrt.pool.ntp.org", "0.openwrt.pool.ntp.org"] def test_a_refused_uci_write_restarts_nothing(): uci = _Uci(STOCK, fail="uci commit system") result = _driver(uci).configure_time_sync(ntp_servers=["10.0.0.1"]) assert result["success"] is False assert result["steps"][-1]["step"] == "ntp_servers" assert "failed" in result["output"] def test_a_login_other_than_root_writes_nothing(): uci = _Uci(STOCK) drv = _driver(uci, username="admin") drv.run_command = lambda c, **k: ( # type: ignore[method-assign] drv._check_privilege(k.get("privileged", False)) or uci(c, **k) ) result = drv.configure_time_sync(ntp_servers=["10.0.0.1"]) assert result["success"] is False assert "no sudo" in result["output"] assert uci.writes() == [] # -- writing the zone --------------------------------------------------------------------------- def test_the_zone_is_written_as_name_and_rule(): uci = _Uci(STOCK) result = _driver(uci).configure_time_sync(timezone="Europe/Berlin") assert result["success"] is True, result["output"] assert [s["step"] for s in result["steps"]] == ["timezone", "verify"] system = uci._section("@system[0]") assert system["zonename"] == "Europe/Berlin" assert system["timezone"] == "CET-1CEST,M3.5.0,M10.5.0/3" def test_the_zone_applies_to_the_system_and_to_logd(): # /etc/init.d/system reload writes /tmp/TZ; logd reads the zone when it starts. uci = _Uci(STOCK) _driver(uci).configure_time_sync(timezone="Europe/Berlin") (write,) = uci.writes() assert write.endswith( "uci commit system && /etc/init.d/system reload && /etc/init.d/log restart" ) assert "sysntpd" not in write def test_a_zone_name_is_written_as_luci_does(): uci = _Uci(STOCK) result = _driver(uci).configure_time_sync(timezone="America/New_York") assert result["success"] is True, result["output"] assert uci._section("@system[0]")["zonename"] == "America/New York" assert uci._section("@system[0]")["timezone"] == posix_tz("America/New_York") def test_a_rule_with_quoted_names_is_one_shell_word(): uci = _Uci(STOCK) _driver(uci).configure_time_sync(timezone="Asia/Kathmandu") assert uci._section("@system[0]")["timezone"] == "<+0545>-5:45" def test_the_name_as_rule_of_1849_is_repaired(): uci = _Uci(NAME_AS_RULE) result = _driver(uci).configure_time_sync(timezone="Europe/Berlin") assert result["success"] is True, result["output"] assert uci._section("@system[0]")["timezone"] == "CET-1CEST,M3.5.0,M10.5.0/3" @pytest.mark.parametrize( ("show", "zone"), [(LUCI_BERLIN, "Europe/Berlin"), (LUCI_NEW_YORK, "America/New_York"), (STOCK, "UTC")], ) def test_the_zone_it_has_writes_nothing(show, zone): # The stock GMT0 is UTC's time under another name: no rewrite on every poll. uci = _Uci(show) result = _driver(uci).configure_time_sync(timezone=zone) assert result["success"] is True, result["output"] assert uci.writes() == [] def test_a_zone_twice_writes_once(): uci = _Uci(STOCK) drv = _driver(uci) drv.configure_time_sync(timezone="Pacific/Auckland") drv.configure_time_sync(timezone="Pacific/Auckland") assert len(uci.writes()) == 1 def test_both_values_in_one_run(): uci = _Uci(STOCK) result = _driver(uci).configure_time_sync(ntp_servers=["10.0.0.1"], timezone="Europe/Berlin") assert result["success"] is True, result["output"] assert [s["step"] for s in result["steps"]] == ["ntp_servers", "timezone", "verify"] def test_a_zone_without_a_rule_is_not_written(): # The template lets only zones of the database through; the step does not rely on it. uci = _Uci(STOCK) outcome = _driver(uci)._write_time_zone("Mars/Olympus_Mons") assert outcome["ok"] is False assert uci.ran == [] def test_a_posix_rule_as_zone_is_refused_before_anything_runs(): uci = _Uci(STOCK) with pytest.raises(UnknownTimezone): _driver(uci).configure_time_sync(timezone="CET-1CEST,M3.5.0,M10.5.0/3") assert uci.ran == []