driver / standard (pull_request) Successful in 16s
driver / lint (pull_request) Successful in 14s
driver / types (pull_request) Successful in 59s
driver / test (3.12) (pull_request) Successful in 1m1s
driver / test (3.13) (pull_request) Successful in 53s
driver / lowest (pull_request) Successful in 28s
driver / build (pull_request) Successful in 26s
driver / security (pull_request) Successful in 54s
CI / driver (pull_request) Successful in 2m25s
driver / publish (pull_request) Skipped
textfsm 2.1.1 (2026-11-10, through napalm) installs a top-level package "tests" into site-packages. It shadows this repo's tests directory, which is no package, so "from tests.conftest import ..." found textfsm's and test_bmc_driver.py and test_x10_redfish.py failed to import: every test run on CI fails since that release, main included. The helpers are imported as conftest and test_ipmi, the way test_driver.py already does; pytest puts tests/ on the path for them.
229 lines
8.7 KiB
Python
229 lines
8.7 KiB
Python
"""Time sync of a BMC over Redfish (NetOrk/netork#2367): ``ManagerNetworkProtocol.NTP``.
|
|
|
|
The BMC's NTP servers are ``NTP.NTPServers``, in use while ``NTP.ProtocolEnabled`` is
|
|
not false. Its clock runs on UTC or an offset (``Manager.DateTimeLocalOffset``), which
|
|
is no zone name: only a manager that names an IANA zone (``TimeZoneName``, Manager
|
|
v1.10) reads one, and the zone is never written.
|
|
|
|
The answers are the DMTF mockups (public-telemetry's NTP block), and the shapes BMCs
|
|
give the array: a fixed number of slots with empty strings for the unused ones
|
|
(three on an iDRAC 9, two on an iLO 5), with an ``@odata.etag`` to PATCH against.
|
|
"""
|
|
|
|
import copy
|
|
import json
|
|
|
|
import pytest
|
|
from conftest import load
|
|
from napalm_device_types.time_sync import TimeSyncMixin
|
|
|
|
import napalm_redfish.ipmi as ipmi
|
|
from napalm_redfish import RedfishDriver
|
|
from napalm_redfish.bmc import BmcDriver
|
|
from napalm_redfish.ipmi import IpmiDriver
|
|
from napalm_redfish.transport import RedfishError
|
|
|
|
MANAGER = "/redfish/v1/Managers/BMC"
|
|
PROTOCOL = MANAGER + "/NetworkProtocol"
|
|
TELEMETRY_NTP = load("dmtf-public-telemetry", "/redfish/v1/Managers/1/NetworkProtocol")["NTP"]
|
|
|
|
|
|
class Bmc:
|
|
"""The rackmount mockup's NetworkProtocol with an NTP block, PATCHed in place."""
|
|
|
|
def __init__(self, server, ntp: dict, etag: str | None = None) -> None:
|
|
self.body = load("dmtf-public-rackmount1", PROTOCOL)
|
|
self.body["NTP"] = copy.deepcopy(ntp)
|
|
self.etag = etag
|
|
self.patches: list[tuple[dict, str | None]] = []
|
|
self.refuse: tuple[int, dict] | None = None
|
|
server.answer("GET", PROTOCOL, self._get)
|
|
server.answer("PATCH", PROTOCOL, self._patch)
|
|
|
|
def _get(self, request):
|
|
body = dict(self.body)
|
|
if self.etag:
|
|
body["@odata.etag"] = self.etag
|
|
return 200, body
|
|
|
|
def _patch(self, request):
|
|
patch = json.loads(request.body)
|
|
self.patches.append((patch, request.headers.get("If-Match")))
|
|
if self.refuse:
|
|
return self.refuse
|
|
self.body["NTP"].update(patch["NTP"])
|
|
self.etag = self.etag and f'W/"{len(self.patches)}"'
|
|
return 204, {}
|
|
|
|
|
|
@pytest.fixture
|
|
def driver(server):
|
|
d = RedfishDriver("bmc.example.net", "netork", "s3cret", optional_args={"verify_ssl": False})
|
|
d.open()
|
|
yield d
|
|
d.close()
|
|
|
|
|
|
# --- opt-in --------------------------------------------------------------------------------
|
|
|
|
|
|
def test_redfish_and_bmc_offer_it_and_write_only_servers():
|
|
for cls in (RedfishDriver, BmcDriver):
|
|
assert issubclass(cls, TimeSyncMixin)
|
|
assert callable(cls.get_time_sync)
|
|
assert callable(cls.configure_time_sync)
|
|
assert cls.time_sync_writes == frozenset({"ntp_servers"})
|
|
|
|
|
|
def test_ipmi_has_no_time_sync():
|
|
"""IPMI has no NTP settings: the driver does not offer it."""
|
|
assert getattr(IpmiDriver, "get_time_sync", None) is None
|
|
assert getattr(IpmiDriver, "configure_time_sync", None) is None
|
|
|
|
|
|
# --- reading -------------------------------------------------------------------------------
|
|
|
|
|
|
def test_reads_the_servers_of_the_telemetry_mockup(driver, server):
|
|
Bmc(server, TELEMETRY_NTP)
|
|
assert driver.get_time_sync() == {
|
|
"ntp_servers": ["0.pool.ntp.org", "1.pool.ntp.org", "time-a.nist.gov"],
|
|
"timezone": None, # the manager has only DateTimeLocalOffset "+06:00"
|
|
"synchronized": None,
|
|
"daemon": None,
|
|
}
|
|
|
|
|
|
def test_empty_slots_are_no_servers(driver, server):
|
|
Bmc(server, {"NTPServers": ["10.0.0.1", "", ""], "ProtocolEnabled": True})
|
|
assert driver.get_time_sync()["ntp_servers"] == ["10.0.0.1"]
|
|
|
|
|
|
def test_servers_of_a_disabled_ntp_are_not_in_use(driver, server):
|
|
Bmc(server, {"NTPServers": ["10.0.0.1", ""], "ProtocolEnabled": False})
|
|
assert driver.get_time_sync()["ntp_servers"] == []
|
|
assert driver.get_ntp_servers() == {}
|
|
|
|
|
|
def test_get_ntp_servers_names_the_same_servers(driver, server):
|
|
Bmc(server, TELEMETRY_NTP)
|
|
assert list(driver.get_ntp_servers()) == driver.get_time_sync()["ntp_servers"]
|
|
|
|
|
|
def test_a_named_iana_zone_is_read(driver, server):
|
|
manager = load("dmtf-public-rackmount1", MANAGER)
|
|
server.override("GET", MANAGER, 200, {**manager, "TimeZoneName": "Europe/Berlin"})
|
|
Bmc(server, TELEMETRY_NTP)
|
|
assert driver.get_time_sync()["timezone"] == "Europe/Berlin"
|
|
|
|
|
|
def test_a_zone_name_that_is_no_iana_zone_is_none(driver, server):
|
|
manager = load("dmtf-public-rackmount1", MANAGER)
|
|
server.override("GET", MANAGER, 200, {**manager, "TimeZoneName": "GMT+01:00"})
|
|
Bmc(server, TELEMETRY_NTP)
|
|
assert driver.get_time_sync()["timezone"] is None
|
|
|
|
|
|
def test_a_bmc_without_ntp_settings_cannot_tell(driver):
|
|
"""The rackmount mockup's NetworkProtocol (and a Supermicro X10's, v1_0_0) has no NTP."""
|
|
with pytest.raises(RedfishError, match="NTP"):
|
|
driver.get_time_sync()
|
|
assert driver.get_ntp_servers() == {}
|
|
|
|
|
|
# --- writing -------------------------------------------------------------------------------
|
|
|
|
|
|
def test_fills_fixed_slots_with_empty_strings_and_the_etag(driver, server):
|
|
bmc = Bmc(server, {"NTPServers": ["", "", ""], "ProtocolEnabled": True}, etag='W/"a1"')
|
|
result = driver.configure_time_sync(ntp_servers=["ntp1.example.org", "10.0.0.1"])
|
|
assert result["success"], result["output"]
|
|
assert bmc.patches == [
|
|
({"NTP": {"NTPServers": ["ntp1.example.org", "10.0.0.1", ""]}}, 'W/"a1"'),
|
|
]
|
|
assert [s["step"] for s in result["steps"]] == ["ntp_servers", "verify"]
|
|
|
|
|
|
def test_more_servers_than_slots_are_refused_before_writing(driver, server):
|
|
bmc = Bmc(server, {"NTPServers": ["", ""], "ProtocolEnabled": True})
|
|
result = driver.configure_time_sync(ntp_servers=["a.example.org", "b.example.org", "c.org"])
|
|
assert not result["success"]
|
|
assert "2 NTP servers" in result["steps"][0]["output"]
|
|
assert bmc.patches == []
|
|
|
|
|
|
def test_an_array_without_placeholders_gets_exactly_the_servers(driver, server):
|
|
bmc = Bmc(server, TELEMETRY_NTP)
|
|
result = driver.configure_time_sync(ntp_servers=["10.0.0.1"])
|
|
assert result["success"], result["output"]
|
|
assert bmc.patches == [({"NTP": {"NTPServers": ["10.0.0.1"]}}, None)]
|
|
assert driver.get_time_sync()["ntp_servers"] == ["10.0.0.1"]
|
|
|
|
|
|
def test_turns_a_disabled_ntp_on(driver, server):
|
|
bmc = Bmc(server, {"NTPServers": ["10.0.0.1", ""], "ProtocolEnabled": False})
|
|
result = driver.configure_time_sync(ntp_servers=["10.0.0.1"])
|
|
assert result["success"], result["output"]
|
|
assert bmc.patches == [
|
|
({"NTP": {"NTPServers": ["10.0.0.1", ""], "ProtocolEnabled": True}}, None),
|
|
]
|
|
|
|
|
|
def test_the_same_servers_again_patch_nothing(driver, server):
|
|
bmc = Bmc(server, {"NTPServers": ["NTP1.example.org", "", ""], "ProtocolEnabled": True})
|
|
result = driver.configure_time_sync(ntp_servers=["ntp1.example.org"])
|
|
assert result["success"], result["output"]
|
|
assert bmc.patches == []
|
|
|
|
|
|
def test_a_refusal_fails_the_step_with_the_bmc_message(driver, server):
|
|
bmc = Bmc(server, {"NTPServers": ["", ""], "ProtocolEnabled": True}, etag='W/"a1"')
|
|
bmc.refuse = (400, {"error": {"message": "NTPServers is read-only while DHCP sets them"}})
|
|
result = driver.configure_time_sync(ntp_servers=["10.0.0.1"])
|
|
assert not result["success"]
|
|
assert "read-only while DHCP" in result["steps"][0]["output"]
|
|
|
|
|
|
def test_no_ntp_settings_no_write(driver, server):
|
|
result = driver.configure_time_sync(ntp_servers=["10.0.0.1"])
|
|
assert not result["success"]
|
|
assert "NTP" in result["steps"][0]["output"]
|
|
assert server.calls("PATCH") == []
|
|
|
|
|
|
def test_the_zone_is_refused(driver):
|
|
with pytest.raises(ValueError, match="timezone"):
|
|
driver.configure_time_sync(timezone="Europe/Berlin")
|
|
|
|
|
|
# --- the bmc driver: over Redfish only -----------------------------------------------------
|
|
|
|
|
|
@pytest.fixture
|
|
def bmc_driver(server, monkeypatch):
|
|
from test_ipmi import FakeCommand
|
|
|
|
FakeCommand.instances = []
|
|
monkeypatch.setattr(ipmi, "Command", FakeCommand)
|
|
d = BmcDriver("bmc.example.net", "netork", "s3cret", optional_args={"verify_ssl": False})
|
|
d.open()
|
|
yield d
|
|
d.close()
|
|
|
|
|
|
def test_bmc_reads_and_writes_over_redfish(bmc_driver, server):
|
|
bmc = Bmc(server, {"NTPServers": ["", ""], "ProtocolEnabled": True})
|
|
result = bmc_driver.configure_time_sync(ntp_servers=["10.0.0.1"])
|
|
assert result["success"], result["output"]
|
|
assert bmc.patches == [({"NTP": {"NTPServers": ["10.0.0.1", ""]}}, None)]
|
|
assert bmc_driver.get_time_sync()["ntp_servers"] == ["10.0.0.1"]
|
|
|
|
|
|
def test_bmc_without_redfish_cannot_tell_nor_write(bmc_driver):
|
|
bmc_driver._redfish_ok = False
|
|
with pytest.raises(NotImplementedError, match="Redfish"):
|
|
bmc_driver.get_time_sync()
|
|
result = bmc_driver.configure_time_sync(ntp_servers=["10.0.0.1"])
|
|
assert not result["success"]
|
|
assert "Redfish" in result["steps"][0]["output"]
|