refactor(config): take the revision contract from napalm-device-types
driver / standard (pull_request) Successful in 17s
driver / lint (pull_request) Successful in 15s
driver / types (pull_request) Successful in 1m0s
driver / test (3.12) (pull_request) Successful in 1m11s
driver / test (3.13) (pull_request) Successful in 1m3s
driver / lowest (pull_request) Successful in 35s
driver / build (pull_request) Successful in 29s
driver / security (pull_request) Successful in 1m1s
CI / driver (pull_request) Successful in 2m44s
driver / publish (pull_request) Skipped

napalm-device-types 4.38.0 declares ConfigRevisionsMixin: the listing,
the reading, ConfigRevisionDict and the exceptions, and the revert as a
template. The driver keeps the device communication: the three backup
API calls, the revert step, and config_revision_content(), which leaves
out the <revision> block every save writes into config.xml and the
indentation between elements.

The template reads the newest revision after the revert and checks that
it holds the revision's configuration. OPNsense answers "reverted" even
when restoreBackup() could not load the file and kept the previous one;
that now raises ConfigRevertFailed instead of passing. A refusal of the
firewall raises ConfigRevertFailed too. An id the history does not list
still never reaches a request path.

Refs NetOrk/netork#2511
This commit is contained in:
Christian Manivong committed 2026-11-13 22:33:58 +01:00
1 parent 5a20023c54
commit a65a3ed6e3
5 files changed
+174 -62

No files matched your search

+111 -12
View File
@@ -14,14 +14,16 @@ first configuration of a new install is.
from __future__ import annotations
import copy
import json
from pathlib import Path
from unittest.mock import MagicMock, patch
from xml.etree.ElementTree import ParseError
import pytest
import requests
from napalm_device_types import ConfigRevertFailed, ConfigRevisionsMixin, UnknownConfigRevision
from napalm_opnsense.config_revisions import UnknownConfigRevision
from napalm_opnsense.opnsense import OPNsenseDriver
FIXTURES = Path(__file__).parent / "fixtures"
@@ -32,15 +34,50 @@ HISTORY_PATH = "/api/core/backup/backups/this"
NEWEST = "config-1794603885.1239.xml"
OLDEST = "config-1793516400.0012.xml"
WITH_SUFFIX = "config-1794475810.0548_123456789.xml"
#: The id of the revision a revert saves, as the fake firewall names it.
REVERTED = "config-1794700000.0001.xml"
def _stamped(config: str, user: str, time: str) -> str:
"""*config* with the <revision> block every save writes into config.xml."""
block = (
f" <revision>\n <username>{user}</username>\n <time>{time}</time>\n"
" <description>/api/core/backup/revert_backup made changes</description>\n"
" </revision>\n"
)
return config.replace("</opnsense>", block + "</opnsense>")
#: The oldest revision is the sample; every later one names the host otherwise.
FILES = {
OLDEST: _stamped(CONFIG_XML, "root@192.0.2.20", "1793516400.00"),
WITH_SUFFIX: _stamped(CONFIG_XML, "apiuser@192.0.2.10", "1794475810.05"),
"config-1794603482.8721.xml": _stamped(
CONFIG_XML.replace("<hostname>OPNsense</hostname>", "<hostname>gw</hostname>"),
"root@192.0.2.20",
"1794603482.87",
),
NEWEST: _stamped(
CONFIG_XML.replace("<hostname>OPNsense</hostname>", "<hostname>gw1</hostname>"),
"apiuser@192.0.2.10",
"1794603885.12",
),
}
class Firewall:
"""Answers as the firewall's backup API would; every request is recorded."""
"""Answers as the firewall's backup API would; every request is recorded.
def __init__(self, history=HISTORY, files=None, revert_answer=None):
self.history = history
self.files = files if files is not None else {OLDEST: CONFIG_XML}
A revert it takes saves the revision as a new newest one, restamped; one it does
not take (``takes=False``) still answers "reverted", as OPNsense does when it falls
back to the previous file.
"""
def __init__(self, history=HISTORY, files=None, revert_answer=None, takes=True):
self.history = copy.deepcopy(history)
self.files = dict(FILES if files is None else files)
self.revert_answer = revert_answer or {"status": "reverted"}
self.takes = takes
self.requests: list[tuple[str, str]] = []
def get(self, url, timeout=None):
@@ -68,9 +105,17 @@ class Firewall:
response.raise_for_status.return_value = None
if isinstance(self.revert_answer, Exception):
response.raise_for_status.side_effect = self.revert_answer
elif self.revert_answer.get("status") == "reverted" and self.takes:
self._save(path.rsplit("/", 1)[1])
response.json.return_value = self.revert_answer
return response
def _save(self, revision_id):
body = self.files[revision_id].split(" <revision>")[0] + "</opnsense>\n"
self.files[REVERTED] = _stamped(body, "apiuser@192.0.2.10", "1794700000.00")
item = {"time": "1794700000.00", "description": "", "username": "", "id": REVERTED}
self.history["items"].insert(0, item)
@property
def posts(self) -> list[str]:
return [path for method, path in self.requests if method == "POST"]
@@ -205,7 +250,7 @@ class TestGetConfigRevision:
def test_the_revisions_config_xml(self, driver):
firewall = _on(driver, Firewall())
assert driver.get_config_revision(OLDEST) == CONFIG_XML
assert driver.get_config_revision(OLDEST) == FILES[OLDEST]
assert firewall.requests == [("GET", f"/api/core/backup/download/this/{OLDEST}")]
def test_a_revision_the_firewall_has_no_file_for_raises(self, driver):
@@ -237,14 +282,18 @@ class TestGetConfigRevision:
class TestRevertConfigRevision:
def test_reverts_through_the_backup_api(self, driver):
def test_reverts_through_the_backup_api_and_checks_the_result(self, driver):
firewall = _on(driver, Firewall())
driver.revert_config_revision(OLDEST)
now = driver.revert_config_revision(OLDEST)
assert now["id"] == REVERTED
assert firewall.requests == [
("GET", HISTORY_PATH),
("GET", f"/api/core/backup/download/this/{OLDEST}"),
("POST", f"/api/core/backup/revert_backup/{OLDEST}"),
("GET", HISTORY_PATH),
("GET", f"/api/core/backup/download/this/{REVERTED}"),
]
def test_an_id_with_the_suffix_of_a_fast_save(self, driver):
@@ -254,6 +303,13 @@ class TestRevertConfigRevision:
assert firewall.posts == [f"/api/core/backup/revert_backup/{WITH_SUFFIX}"]
def test_a_revert_the_firewall_did_not_take_fails(self, driver):
# restoreBackup() falls back to the previous file and the answer is "reverted".
_on(driver, Firewall(takes=False))
with pytest.raises(ConfigRevertFailed, match=OLDEST):
driver.revert_config_revision(OLDEST)
def test_a_revision_not_in_the_history_is_refused(self, driver):
firewall = _on(driver, Firewall())
@@ -263,13 +319,13 @@ class TestRevertConfigRevision:
assert firewall.posts == []
@pytest.mark.parametrize("revision_id", ["", "../config.xml", "config-1.xml/x", None])
def test_an_id_that_names_no_revision_is_refused_before_any_request(self, driver, revision_id):
def test_an_id_that_names_no_revision_never_reaches_a_request_path(self, driver, revision_id):
firewall = _on(driver, Firewall())
with pytest.raises(UnknownConfigRevision):
driver.revert_config_revision(revision_id)
assert firewall.requests == []
assert firewall.requests == [("GET", HISTORY_PATH)]
def test_a_history_that_cannot_be_read_reverts_nothing(self, driver):
firewall = _on(driver, Firewall(history=requests.HTTPError("403 Forbidden")))
@@ -290,13 +346,13 @@ class TestRevertConfigRevision:
reason = "User apiuser denied for write access (user-config-readonly set)"
_on(driver, Firewall(revert_answer={"errorMessage": reason, "errorTitle": "Error"}))
with pytest.raises(RuntimeError, match="user-config-readonly"):
with pytest.raises(ConfigRevertFailed, match="user-config-readonly"):
driver.revert_config_revision(OLDEST)
def test_any_other_answer_is_a_failed_revert(self, driver):
_on(driver, Firewall(revert_answer={"status": "failed"}))
with pytest.raises(RuntimeError, match="failed"):
with pytest.raises(ConfigRevertFailed, match="failed"):
driver.revert_config_revision(OLDEST)
def test_an_http_error_raises(self, driver):
@@ -306,7 +362,50 @@ class TestRevertConfigRevision:
driver.revert_config_revision(OLDEST)
class TestConfigRevisionContent:
def test_the_revision_block_does_not_count(self, driver):
# Same configuration, saved by someone else at another time.
assert driver.config_revision_content(FILES[OLDEST]) == driver.config_revision_content(
FILES[WITH_SUFFIX]
)
def test_a_changed_setting_counts(self, driver):
assert driver.config_revision_content(FILES[OLDEST]) != driver.config_revision_content(
FILES[NEWEST]
)
def test_a_configuration_without_a_revision_block(self, driver):
stamped = driver.config_revision_content(FILES[OLDEST])
assert driver.config_revision_content(CONFIG_XML) == stamped
def test_a_revision_element_deeper_down_counts(self, driver):
# Only the root's <revision> is the save's stamp.
deeper = CONFIG_XML.replace("<theme>", "<theme><revision>7</revision>", 1)
assert driver.config_revision_content(deeper) != driver.config_revision_content(CONFIG_XML)
def test_how_the_xml_is_written_does_not_count(self, driver):
# Attribute order, quotes and empty elements are no difference of the configuration.
a = '<?xml version="1.0"?>\n<opnsense><x a="1" b="2"></x></opnsense>'
b = "<opnsense><x b='2' a='1'/></opnsense>"
assert driver.config_revision_content(a) == driver.config_revision_content(b)
def test_the_blanks_of_a_value_count(self, driver):
padded = CONFIG_XML.replace("<theme>opnsense</theme>", "<theme>opnsense </theme>")
assert driver.config_revision_content(padded) != driver.config_revision_content(CONFIG_XML)
def test_text_that_is_no_xml_raises(self, driver):
with pytest.raises(ParseError):
driver.config_revision_content("<html><body>Login")
class TestWhatTheDriverOffers:
def test_it_keeps_the_contract_of_device_types(self):
assert issubclass(OPNsenseDriver, ConfigRevisionsMixin)
def test_it_reads_and_reverts_revisions(self):
for name in ("get_config_revisions", "get_config_revision", "revert_config_revision"):
assert callable(getattr(OPNsenseDriver, name, None)), name