test(opened-ports): ufw's commands on a shell; a missing rule deletes as done

This commit is contained in:
christianmanivong committed 2026-11-26 08:16:25 +01:00
1 parent ebd3255e5a
commit cbd1cd3d11
2 files changed
+71 -5

No files matched your search

+9 -4
View File
@@ -223,15 +223,20 @@ def ufw_allow_recorded_command(source: str, port: int, proto: str, purpose: str)
}
)
return (
f'{{ o=$(LC_ALL=C ufw allow {_ufw_rule(source, port, proto)} 2>&1) && echo "$o"'
f' && case "$o" in *Skipping*) ;;'
f"{{ o=$(LC_ALL=C ufw allow {_ufw_rule(source, port, proto)} 2>&1);"
f' s=$?; echo "$o"; [ "$s" -eq 0 ] && case "$o" in *Skipping*) ;;'
f" *'Rule added'*|*'Rules updated'*) {record_opened_rule_command(line)};; esac; }}"
)
def ufw_delete_command(rule: OpenedRuleDict) -> str:
"""The shell, as root, that deletes *rule* from ufw."""
return f"LC_ALL=C ufw delete allow {_ufw_rule(rule['source'], rule['port'], rule['proto'])}"
"""The shell, as root, that deletes *rule* from ufw; done too where ufw no longer
has it ("Could not delete non-existent rule")."""
return (
"{ o=$(LC_ALL=C ufw delete allow"
f" {_ufw_rule(rule['source'], rule['port'], rule['proto'])} 2>&1)"
' || case "$o" in *non-existent*) ;; *) echo "$o"; false ;; esac; }'
)
def revoked_output(
+62 -1
View File
@@ -2,6 +2,9 @@
from __future__ import annotations
import os
import subprocess
import pytest
from napalm_device_types.opened_ports import (
@@ -154,9 +157,67 @@ class TestCommands:
assert "exit" not in command
def test_ufw_delete(self):
assert ufw_delete_command(UFW) == (
assert (
"LC_ALL=C ufw delete allow from 10.40.100.254/32 to any port 10050 proto tcp"
in ufw_delete_command(UFW)
)
assert "exit" not in ufw_delete_command(UFW)
def _shell(tmp_path, command: str, ufw_says: str, ufw_status: int = 0):
"""Run *command* with a fake ufw that answers *ufw_says*, the record under *tmp_path*."""
fake = tmp_path / "bin" / "ufw"
fake.parent.mkdir(exist_ok=True)
fake.write_text(f"#!/bin/sh\necho '{ufw_says}'\nexit {ufw_status}\n")
fake.chmod(0o755)
record = tmp_path / "netork" / "opened-ports"
command = command.replace(
"install -d -m 755 -o root -g root /var/lib/netork", f"mkdir -p {record.parent}"
).replace(OPENED_PORTS_RECORD, str(record))
done = subprocess.run(
["sh", "-c", command],
env={**os.environ, "PATH": f"{fake.parent}:{os.environ['PATH']}"},
capture_output=True,
text=True,
)
return done, (record.read_text() if record.exists() else "")
class TestUfwOnAShell:
COMMAND = ufw_allow_recorded_command("10.40.100.254/32", 10050, "tcp", "zabbix-agent")
@pytest.mark.parametrize("says", ["Rule added", "Rules updated"])
def test_a_rule_ufw_added_is_recorded(self, tmp_path, says):
done, record = _shell(tmp_path, self.COMMAND, says)
assert done.returncode == 0, done.stderr
assert record == opened_rule_line(UFW) + "\n"
def test_a_rule_ufw_had_is_not_recorded(self, tmp_path):
done, record = _shell(tmp_path, self.COMMAND, "Skipping adding existing rule")
assert done.returncode == 0, done.stderr
assert record == ""
def test_a_refused_rule_fails_and_is_not_recorded(self, tmp_path):
done, record = _shell(tmp_path, self.COMMAND, "ERROR: Bad port", ufw_status=1)
assert done.returncode != 0
assert "Bad port" in done.stdout
assert record == ""
def test_deleting_a_rule_ufw_no_longer_has_is_done(self, tmp_path):
done, _ = _shell(
tmp_path, ufw_delete_command(UFW), "Could not delete non-existent rule", ufw_status=1
)
assert done.returncode == 0
def test_a_delete_ufw_refuses_fails(self, tmp_path):
done, _ = _shell(tmp_path, ufw_delete_command(UFW), "ERROR: problem", ufw_status=1)
assert done.returncode != 0
assert "problem" in done.stdout
def test_rules_text_keeps_what_it_is_given_one_per_line(self):
assert opened_rules_text([]) == ""