"""OpenMediaVault renames itself through its own database, not through /etc. The rename napalm-linux offers (``hostnamectl``, ``/etc/hosts``) would be taken back by the next apply of OMV's settings, which regenerates both from ``conf.system.network.dns``. So the name goes where OMV's web UI puts it: ``Network.setGeneralSettings``, then ``Config.applyChanges`` -- for the modules the rename made dirty only, so changes someone left pending in the web UI (here ``apt`` and ``rsyslog``) are not deployed along with it. """ from __future__ import annotations import json import shlex import pytest from napalm_openmediavault.openmediavault import OpenMediaVaultDriver _RENAME_DIRTIES = ["hostname", "hosts", "postfix", "avahi", "samba", "systemdnetworkd"] class Omv: """The NAS as ``_sudo_status`` sees it: omv-rpc calls and the dirty-modules file.""" def __init__(self, *, dirty=("apt", "rsyslog"), fail=""): self.settings = {"hostname": "nas", "domainname": "home.example"} self.dirty = list(dirty) self.fail = fail self.calls: list[tuple[str, str, object]] = [] self.commands: list[str] = [] def __call__(self, command: str, read_timeout: float = 100) -> tuple[str, int | None]: self.commands.append(command) 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) == ("Network", "getGeneralSettings"): return json.dumps(self.settings), 0 if (service, method) == ("Network", "setGeneralSettings"): self.settings = params self.dirty += [m for m in _RENAME_DIRTIES if m not in self.dirty] return json.dumps(params), 0 if (service, method) == ("Config", "applyChanges"): self.dirty = [m for m in self.dirty if m not in params["modules"]] return "null", 0 raise AssertionError(command) def call(self, method: str): 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 # type: ignore[method-assign] return driver def test_bare_name_keeps_the_domain(): omv = Omv() _driver(omv).set_hostname("nas2") assert omv.call("Network.setGeneralSettings") == [ {"hostname": "nas2", "domainname": "home.example"} ] def test_fqdn_sets_the_domain_too(): omv = Omv() _driver(omv).set_hostname("nas2.lab.example") assert omv.call("Network.setGeneralSettings") == [ {"hostname": "nas2", "domainname": "lab.example"} ] def test_only_what_the_rename_made_dirty_is_applied(): omv = Omv() _driver(omv).set_hostname("nas2") assert omv.call("Config.applyChanges") == [{"modules": sorted(_RENAME_DIRTIES), "force": False}] assert omv.dirty == ["apt", "rsyslog"] def test_a_hostname_module_already_dirty_is_applied_as_well(): omv = Omv(dirty=("apt", "samba")) _driver(omv).set_hostname("nas2") (apply,) = omv.call("Config.applyChanges") assert "samba" in apply["modules"] assert "apt" not in apply["modules"] def test_nothing_dirty_nothing_applied(): omv = Omv() omv.settings = {"hostname": "nas2", "domainname": "home.example"} driver = _driver(omv) driver._sudo_status = _no_new_dirt(omv) # type: ignore[method-assign] driver.set_hostname("nas2") assert omv.call("Config.applyChanges") == [] def _no_new_dirt(omv: Omv): """OMV marks nothing dirty when the settings did not change.""" def status(command: str, read_timeout: float = 100): if "setGeneralSettings" in command: omv.calls.append(("Network", "setGeneralSettings", json.loads(shlex.split(command)[5]))) return json.dumps(omv.settings), 0 return omv(command, read_timeout) return status @pytest.mark.parametrize("name", ["", "nas_2", "-nas", "nas.", "a" * 64, "nas;reboot", "$(id)"]) def test_invalid_name_runs_nothing(name): omv = Omv() with pytest.raises(ValueError): _driver(omv).set_hostname(name) assert omv.commands == [] def test_unreadable_settings_change_nothing(): omv = Omv(fail="Network.getGeneralSettings") with pytest.raises(RuntimeError, match="getGeneralSettings"): _driver(omv).set_hostname("nas2") assert omv.call("Network.setGeneralSettings") == [] def test_failed_apply_raises(): omv = Omv(fail="Config.applyChanges") with pytest.raises(RuntimeError, match="applyChanges"): _driver(omv).set_hostname("nas2") def test_offered_as_a_capability(): assert callable(getattr(OpenMediaVaultDriver, "set_hostname", None))