driver / standard (pull_request) Successful in 14s
driver / lint (pull_request) Successful in 12s
driver / test (3.12) (pull_request) Successful in 53s
driver / types (pull_request) Successful in 1m0s
driver / test (3.13) (pull_request) Successful in 52s
driver / lowest (pull_request) Successful in 26s
driver / build (pull_request) Successful in 26s
driver / security (pull_request) Successful in 1m0s
CI / driver (pull_request) Successful in 2m26s
driver / publish (pull_request) Skipped
netOrk keeps one list of NTP servers per site and aligns a device that differs (NetOrk/netork#2360). The Fritz!Box takes part through TR-064's Time:1 service: GetInfo reads NewNTPServer1/2, SetNTPServers writes them. AVM documents Time:1's zone fields as not supported, and the box keeps its zone as a POSIX rule or nothing, so the zone is read as None unless the box names an IANA zone, and time_sync_writes is {"ntp_servers"}. The box has two server slots: a longer list is refused before anything is written, and a list the box already has is not written again, so a poll that aligns the box every time does not churn. TR-064 says neither whether the clock is synchronised nor whether the box serves time to its LAN (NetOrk/netork#2371); both stay unreported. Needs napalm-device-types 4.35.0. Refs NetOrk/netork#2367
172 lines
6.5 KiB
Python
172 lines
6.5 KiB
Python
"""get_time_sync() / configure_time_sync() over TR-064's Time:1 service (NetOrk/netork#2367).
|
|
|
|
The answers are what AVM documents for ``urn:dslforum-org:service:Time:1``
|
|
(TR-064 Support -- Time, version 5, 2025-08-07): ``GetInfo`` returns the two NTP
|
|
servers and the box's clock; the zone fields are "not supported" -- a box fills
|
|
``NewLocalTimeZoneName`` with a POSIX rule or leaves it empty. ``SetNTPServers``
|
|
takes exactly two names; an empty one is an unused slot.
|
|
"""
|
|
|
|
from unittest.mock import MagicMock
|
|
|
|
import pytest
|
|
from fritzconnection.core.exceptions import FritzServiceError
|
|
|
|
from napalm_fritzbox.fritzbox import FritzBoxDriver
|
|
|
|
GET_INFO = {
|
|
"NewNTPServer1": "ntp.example.com",
|
|
"NewNTPServer2": "",
|
|
"NewCurrentLocalTime": "2026-11-11T10:00:00+01:00",
|
|
"NewLocalTimeZone": "",
|
|
"NewLocalTimeZoneName": "CET-1CEST,M3.5.0,M10.5.0/3",
|
|
"NewDaylightSavingsUsed": False,
|
|
"NewDaylightSavingsStart": "0001-01-01T00:00:00",
|
|
"NewDaylightSavingsEnd": "0001-01-01T00:00:00",
|
|
}
|
|
|
|
|
|
class FakeTimeService:
|
|
"""The box's Time:1 service: GetInfo reads what SetNTPServers stored."""
|
|
|
|
def __init__(self, info: dict) -> None:
|
|
self.info = dict(info)
|
|
self.sets: list[dict] = []
|
|
|
|
def __call__(self, service: str, action: str, **kwargs: str) -> dict:
|
|
if service != "Time1":
|
|
raise FritzServiceError(f"no service {service}")
|
|
if action == "GetInfo":
|
|
return dict(self.info)
|
|
if action == "SetNTPServers":
|
|
self.sets.append(kwargs)
|
|
self.info["NewNTPServer1"] = kwargs["NewNTPServer1"]
|
|
self.info["NewNTPServer2"] = kwargs["NewNTPServer2"]
|
|
return {}
|
|
raise AssertionError(f"unexpected action {action}")
|
|
|
|
|
|
@pytest.fixture
|
|
def box():
|
|
fake = FakeTimeService(GET_INFO)
|
|
mock_fc = MagicMock()
|
|
mock_fc.services = {"Time1": MagicMock()}
|
|
mock_fc.call_action.side_effect = fake
|
|
drv = FritzBoxDriver(hostname="fritz.box", username="user", password="pass")
|
|
drv.fc = mock_fc
|
|
yield drv, fake
|
|
drv.close()
|
|
|
|
|
|
class TestRead:
|
|
def test_is_offered(self, box):
|
|
drv, _ = box
|
|
assert callable(getattr(drv, "get_time_sync", None))
|
|
assert callable(getattr(drv, "configure_time_sync", None))
|
|
|
|
def test_servers_without_the_empty_slot(self, box):
|
|
drv, _ = box
|
|
assert drv.get_time_sync() == {
|
|
"ntp_servers": ["ntp.example.com"],
|
|
"timezone": None,
|
|
"synchronized": None,
|
|
"daemon": None,
|
|
}
|
|
|
|
def test_both_servers_in_the_box_order(self, box):
|
|
drv, fake = box
|
|
fake.info.update(NewNTPServer1=" 2.europe.pool.ntp.org ", NewNTPServer2="192.0.2.1")
|
|
assert drv.get_time_sync()["ntp_servers"] == ["2.europe.pool.ntp.org", "192.0.2.1"]
|
|
|
|
def test_no_servers(self, box):
|
|
drv, fake = box
|
|
fake.info.update(NewNTPServer1="", NewNTPServer2="")
|
|
assert drv.get_time_sync()["ntp_servers"] == []
|
|
|
|
def test_a_posix_rule_is_no_zone_name(self, box):
|
|
"""The box keeps an offset and a summer-time rule, not a name."""
|
|
drv, _ = box
|
|
assert drv.get_time_sync()["timezone"] is None
|
|
|
|
def test_a_zone_name_the_box_gives_is_kept(self, box):
|
|
drv, fake = box
|
|
fake.info["NewLocalTimeZoneName"] = "Europe/Berlin"
|
|
assert drv.get_time_sync()["timezone"] == "Europe/Berlin"
|
|
|
|
def test_a_missing_zone_field_is_none(self, box):
|
|
drv, fake = box
|
|
del fake.info["NewLocalTimeZoneName"]
|
|
assert drv.get_time_sync()["timezone"] is None
|
|
|
|
def test_a_box_without_the_service_cannot_tell(self, box):
|
|
drv, _ = box
|
|
drv.fc.services = {}
|
|
with pytest.raises(FritzServiceError):
|
|
drv.get_time_sync()
|
|
|
|
def test_system_config_names_the_same_servers(self, box):
|
|
drv, fake = box
|
|
fake.info.update(NewNTPServer1="a.example.org", NewNTPServer2="b.example.org")
|
|
config = drv.get_system_config()
|
|
assert [config["ntp_server"], config["ntp_server2"]] == drv.get_time_sync()["ntp_servers"]
|
|
|
|
|
|
class TestWrite:
|
|
def test_writes_only_servers(self, box):
|
|
drv, _ = box
|
|
assert drv.time_sync_writes == frozenset({"ntp_servers"})
|
|
with pytest.raises(ValueError, match="timezone"):
|
|
drv.configure_time_sync(timezone="Europe/Berlin")
|
|
|
|
def test_two_servers(self, box):
|
|
drv, fake = box
|
|
result = drv.configure_time_sync(ntp_servers=["ntp1.example.org", "10.0.0.1"])
|
|
assert result["success"], result["output"]
|
|
assert fake.sets == [{"NewNTPServer1": "ntp1.example.org", "NewNTPServer2": "10.0.0.1"}]
|
|
assert [s["step"] for s in result["steps"]] == ["ntp_servers", "verify"]
|
|
|
|
def test_one_server_empties_the_second_slot(self, box):
|
|
drv, fake = box
|
|
fake.info["NewNTPServer2"] = "old.example.org"
|
|
result = drv.configure_time_sync(ntp_servers=["10.0.0.1"])
|
|
assert result["success"], result["output"]
|
|
assert fake.sets == [{"NewNTPServer1": "10.0.0.1", "NewNTPServer2": ""}]
|
|
assert drv.get_time_sync()["ntp_servers"] == ["10.0.0.1"]
|
|
|
|
def test_more_than_two_is_refused_before_writing(self, box):
|
|
drv, fake = box
|
|
result = drv.configure_time_sync(ntp_servers=["a.example.org", "b.example.org", "c.org"])
|
|
assert not result["success"]
|
|
assert result["steps"][0]["step"] == "ntp_servers"
|
|
assert "two" in result["steps"][0]["output"]
|
|
assert fake.sets == []
|
|
|
|
def test_the_same_servers_again_write_nothing(self, box):
|
|
drv, fake = box
|
|
result = drv.configure_time_sync(ntp_servers=["NTP.example.com"])
|
|
assert result["success"], result["output"]
|
|
assert fake.sets == []
|
|
|
|
def test_a_box_that_keeps_other_servers_fails_verify(self, box):
|
|
drv, fake = box
|
|
drv.fc.call_action.side_effect = lambda s, a, **kw: (
|
|
{} if a == "SetNTPServers" else fake(s, a)
|
|
)
|
|
result = drv.configure_time_sync(ntp_servers=["10.0.0.1"])
|
|
assert not result["success"]
|
|
assert result["steps"][-1]["step"] == "verify"
|
|
assert "ntp.example.com" in result["steps"][-1]["output"]
|
|
|
|
def test_a_refused_write_fails_the_step(self, box):
|
|
drv, fake = box
|
|
|
|
def refuse(service, action, **kwargs):
|
|
if action == "SetNTPServers":
|
|
raise FritzServiceError("UPnPError 606: Action not authorized")
|
|
return fake(service, action)
|
|
|
|
drv.fc.call_action.side_effect = refuse
|
|
result = drv.configure_time_sync(ntp_servers=["10.0.0.1"])
|
|
assert not result["success"]
|
|
assert "606" in result["steps"][0]["output"]
|