"""OpenMediaVault keeps its time servers and zone in its own database (NetOrk/netork#2370). OMV generates ``/etc/chrony/chrony.conf`` and the zone from ``conf.system.time`` (``ntp.enable``, ``ntp.timeservers``, ``timezone``) through Salt. napalm-linux's POSIX write -- a drop-in under ``sources.d`` with every other source commented out, ``timedatectl set-timezone`` -- would be taken back the next time OMV deploys its time settings, and the web UI would keep showing its own servers. So the servers and the zone are read from and written to ``System.getTimeSettings`` / ``setTimeSettings``, then ``Config.applyChanges`` deploys the modules that change made dirty (``chrony``, ``timezone``) -- and nothing someone left pending in the web UI for other modules. Whether the clock is synchronised, and by which client, is still the host's reading. The answers are shaped like OMV 7's ``System.getTimeSettings`` (``date`` aside, the fields of ``conf.system.time``; the servers one comma-separated string). """ from __future__ import annotations import json import shlex import pytest from napalm_device_types import CommandResult, UnknownTimezone from napalm_openmediavault.openmediavault import OpenMediaVaultDriver #: The host's own reading, as PosixTimeSyncMixin's command prints it on OMV 7: chronyd #: runs, with the chrony.conf OMV generated, and a selected source. READING = """\ @@ts timedatectl Timezone=Europe/Berlin NTP=yes NTPSynchronized=yes @@ts zone Europe/Berlin @@ts active chrony chrony active chronyd chrony active systemd-timesyncd systemd-timesyncd inactive @@ts timesync @@ts chronyc MS Name/IP address Stratum Poll Reach LastRx Last sample =============================================================================== ^* 192.0.2.1 2 6 377 40 -45us[ -63us] +/- 10ms @@ts chrony 0 main /etc/chrony/chrony.conf # This file is auto-generated by openmediavault (https://www.openmediavault.org) # WARNING: Do not edit this file, your changes will get lost. pool pool.ntp.org iburst driftfile /var/lib/chrony/chrony.drift makestep 1.0 3 rtcsync @@ts end """ #: ``System.getTimeSettings`` on OMV 7, as ``omv-rpc`` prints it. SETTINGS = { "date": {"local": "Tue Nov 11 10:00:00 2026", "ISO8601": "2026-11-11T10:00:00+01:00"}, "timezone": "Europe/Berlin", "ntpenable": True, "ntptimeservers": "pool.ntp.org", "ntpclients": "192.0.2.0/24", } #: What OMV marks dirty when ``conf.system.time`` changes. _TIME_MODULES = {"ntptimeservers": "chrony", "ntpenable": "chrony", "timezone": "timezone"} class Omv: """The NAS: omv-rpc and the dirty-modules file as ``_sudo_status`` sees them, and the host's reading as ``run_command`` does.""" def __init__(self, *, settings=None, dirty=("apt", "rsyslog"), fail=""): self.settings = dict(SETTINGS if settings is None else settings) self.dirty = list(dirty) self.fail = fail self.calls: list[tuple[str, str, object]] = [] self.commands: list[tuple[str, bool]] = [] def sudo_status(self, command: str, read_timeout: float = 100) -> tuple[str, int | None]: if command.startswith("cat /var/lib/openmediavault/dirtymodules.json"): return json.dumps(self.dirty), 0 argv = shlex.split(command) assert argv[:3] == ["omv-rpc", "-u", "admin"], command service, method = argv[3], argv[4] params = json.loads(argv[5]) if len(argv) > 5 else None self.calls.append((service, method, params)) if f"{service}.{method}" == self.fail: return '{"response":null,"error":{"message":"boom"}}', 1 if (service, method) == ("System", "getTimeSettings"): return json.dumps(self.settings), 0 if (service, method) == ("System", "setTimeSettings"): assert isinstance(params, dict) for field, value in params.items(): if self.settings.get(field) != value and _TIME_MODULES.get(field): if _TIME_MODULES[field] not in self.dirty: self.dirty.append(_TIME_MODULES[field]) self.settings.update(params) return json.dumps(params), 0 if (service, method) == ("Config", "applyChanges"): assert isinstance(params, dict) self.dirty = [m for m in self.dirty if m not in params["modules"]] return "null", 0 raise AssertionError(command) def run_command(self, command, *, privileged=False, timeout=60, stdin=None): # type: ignore[no-untyped-def] self.commands.append((command, privileged)) assert "@@ts" in command, command return CommandResult(READING, "", 0) def call(self, method: str) -> list[object]: return [params for s, m, params in self.calls if f"{s}.{m}" == method] def _driver(omv: Omv) -> OpenMediaVaultDriver: driver = OpenMediaVaultDriver("nas", "admin", "p") driver._sudo_status = omv.sudo_status # type: ignore[method-assign] driver.run_command = omv.run_command # type: ignore[method-assign] return driver # -- reading --------------------------------------------------------------------------------- def test_servers_and_zone_are_omvs_the_clock_is_the_hosts(): omv = Omv(settings={**SETTINGS, "ntptimeservers": "ntp1.example.org,192.0.2.1"}) assert _driver(omv).get_time_sync() == { "ntp_servers": ["ntp1.example.org", "192.0.2.1"], "timezone": "Europe/Berlin", "synchronized": True, "daemon": "chrony", } def test_the_reading_changes_nothing(): omv = Omv() _driver(omv).get_time_sync() assert [m for _s, m, _p in omv.calls] == ["getTimeSettings"] assert not any(privileged for _c, privileged in omv.commands) def test_servers_separated_by_spaces_as_well(): omv = Omv(settings={**SETTINGS, "ntptimeservers": " ntp1.example.org, ntp2.example.org "}) assert _driver(omv).get_time_sync()["ntp_servers"] == ["ntp1.example.org", "ntp2.example.org"] @pytest.mark.parametrize("off", [False, "0", "false", None]) def test_ntp_switched_off_is_no_servers(off): """OMV stops chronyd with NTP off: the servers it still lists keep nothing.""" omv = Omv(settings={**SETTINGS, "ntpenable": off}) assert _driver(omv).get_time_sync()["ntp_servers"] == [] def test_ntp_on_as_the_database_spells_it(): omv = Omv(settings={**SETTINGS, "ntpenable": "1"}) assert _driver(omv).get_time_sync()["ntp_servers"] == ["pool.ntp.org"] def test_an_answer_that_is_no_object_raises(): omv = Omv() driver = _driver(omv) driver._sudo_status = lambda command, read_timeout=100: ("null", 0) # type: ignore[method-assign] with pytest.raises(RuntimeError, match="getTimeSettings"): driver.get_time_sync() def test_no_zone_stored_is_none(): omv = Omv(settings={**SETTINGS, "timezone": ""}) assert _driver(omv).get_time_sync()["timezone"] is None def test_unreadable_settings_raise(): omv = Omv(fail="System.getTimeSettings") with pytest.raises(RuntimeError, match="getTimeSettings"): _driver(omv).get_time_sync() # -- writing --------------------------------------------------------------------------------- def test_servers_go_through_omvs_settings(): omv = Omv() result = _driver(omv).configure_time_sync(ntp_servers=["ntp1.example.org", "192.0.2.1"]) assert result["success"], result["output"] assert omv.call("System.setTimeSettings") == [ { "timezone": "Europe/Berlin", "ntpenable": True, "ntptimeservers": "ntp1.example.org,192.0.2.1", "ntpclients": "192.0.2.0/24", } ] assert [s["step"] for s in result["steps"]] == ["ntp_servers", "verify"] def test_servers_switch_ntp_on(): omv = Omv(settings={**SETTINGS, "ntpenable": False}) _driver(omv).configure_time_sync(ntp_servers=["ntp1.example.org"]) (params,) = omv.call("System.setTimeSettings") assert isinstance(params, dict) assert params["ntpenable"] is True def test_only_what_the_change_made_dirty_is_applied(): omv = Omv() _driver(omv).configure_time_sync(ntp_servers=["ntp1.example.org"]) assert omv.call("Config.applyChanges") == [{"modules": ["chrony"], "force": False}] assert omv.dirty == ["apt", "rsyslog"] def test_zone_goes_through_omvs_settings(): omv = Omv() result = _driver(omv).configure_time_sync(timezone="America/New_York") assert result["success"], result["output"] (params,) = omv.call("System.setTimeSettings") assert isinstance(params, dict) assert params["timezone"] == "America/New_York" assert params["ntptimeservers"] == "pool.ntp.org" assert omv.call("Config.applyChanges") == [{"modules": ["timezone"], "force": False}] def test_a_time_module_already_dirty_is_applied_as_well(): omv = Omv(dirty=("apt", "timezone")) _driver(omv).configure_time_sync(ntp_servers=["ntp1.example.org"]) (apply,) = omv.call("Config.applyChanges") assert isinstance(apply, dict) assert apply["modules"] == ["chrony", "timezone"] def test_nothing_on_the_host_itself_is_written(): """No drop-in, no commented-out line, no ``timedatectl``: OMV would take them back.""" omv = Omv() _driver(omv).configure_time_sync(ntp_servers=["ntp1.example.org"], timezone="Europe/Paris") assert not any(privileged for _c, privileged in omv.commands) def test_the_same_values_again_change_nothing(): omv = Omv() result = _driver(omv).configure_time_sync( ntp_servers=["POOL.ntp.org"], timezone="Europe/Berlin" ) assert result["success"], result["output"] assert omv.call("System.setTimeSettings") == [] assert omv.call("Config.applyChanges") == [] def test_written_twice_is_written_once(): omv = Omv() driver = _driver(omv) driver.configure_time_sync(ntp_servers=["ntp1.example.org"], timezone="Europe/Paris") driver.configure_time_sync(ntp_servers=["ntp1.example.org"], timezone="Europe/Paris") assert len(omv.call("System.setTimeSettings")) == 2 # servers, zone: the first time only assert len(omv.call("Config.applyChanges")) == 2 def test_both_in_one_run(): omv = Omv() result = _driver(omv).configure_time_sync( ntp_servers=["ntp1.example.org"], timezone="Europe/Paris" ) assert result["success"], result["output"] assert [s["step"] for s in result["steps"]] == ["ntp_servers", "timezone", "verify"] assert omv.settings["ntptimeservers"] == "ntp1.example.org" assert omv.settings["timezone"] == "Europe/Paris" def test_a_refused_setting_fails_the_step_and_applies_nothing(): omv = Omv(fail="System.setTimeSettings") result = _driver(omv).configure_time_sync(ntp_servers=["ntp1.example.org"]) assert not result["success"] assert result["steps"][-1]["step"] == "ntp_servers" assert "setTimeSettings" in result["steps"][-1]["output"] assert omv.call("Config.applyChanges") == [] def test_a_failed_apply_fails_the_step(): omv = Omv(fail="Config.applyChanges") result = _driver(omv).configure_time_sync(timezone="Europe/Paris") assert not result["success"] assert result["steps"][-1]["step"] == "timezone" @pytest.mark.parametrize( ("kwargs", "error"), [ ({"ntp_servers": ["bad server"]}, ValueError), ({"ntp_servers": []}, ValueError), ({"timezone": "CET-1CEST,M3.5.0,M10.5.0/3"}, UnknownTimezone), ({}, ValueError), ], ) def test_the_contracts_refusals_run_nothing(kwargs, error): omv = Omv() with pytest.raises(error): _driver(omv).configure_time_sync(**kwargs) assert omv.calls == [] assert omv.commands == [] def test_offered_as_a_capability_for_both_values(): assert callable(getattr(OpenMediaVaultDriver, "get_time_sync", None)) assert callable(getattr(OpenMediaVaultDriver, "configure_time_sync", None)) assert OpenMediaVaultDriver.time_sync_writes == frozenset({"ntp_servers", "timezone"})