fix(snmp): let the snmpd.conf write report its exit status
The write ended in `exit $s`, which ends the shell inside the braces _rc wraps around every command, so __PVE_RC was never printed and the step always failed. The write now shares the apt file writer, which ends in a test instead of exit. Closes #30
This commit is contained in:
1 parent
d142db73e5
commit
52d63eb689
3 files changed
+45
-8
No files matched your search
@@ -42,6 +42,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
means "confirmed just now" (NetOrk/netork#1327).
|
means "confirmed just now" (NetOrk/netork#1327).
|
||||||
- `set_vlan()` finds the node's switch uplink again. Since 0.8.0 every call failed
|
- `set_vlan()` finds the node's switch uplink again. Since 0.8.0 every call failed
|
||||||
with "no LLDP-detected switch uplink found", whatever LLDP reported (#37).
|
with "no LLDP-detected switch uplink found", whatever LLDP reported (#37).
|
||||||
|
- `configure_snmp_agent()` completes: its "configure" step reported a failure
|
||||||
|
with empty output although `/etc/snmp/snmpd.conf` was written, so the firewall
|
||||||
|
and start steps never ran (#30).
|
||||||
|
|
||||||
## [0.8.0] - 2026-10-23
|
## [0.8.0] - 2026-10-23
|
||||||
|
|
||||||
|
|||||||
@@ -45,6 +45,8 @@ _SUBSCRIPTION_STATUS = {
|
|||||||
_PACKAGE_NAME = re.compile(r"^[a-zA-Z0-9][a-zA-Z0-9_.+:~-]*$")
|
_PACKAGE_NAME = re.compile(r"^[a-zA-Z0-9][a-zA-Z0-9_.+:~-]*$")
|
||||||
#: What may stand in an snmpd.conf value: no whitespace, no quote, no comment.
|
#: What may stand in an snmpd.conf value: no whitespace, no quote, no comment.
|
||||||
_SNMP_WORD = re.compile(r'^[^\s"#]+$')
|
_SNMP_WORD = re.compile(r'^[^\s"#]+$')
|
||||||
|
#: The net-snmp agent's configuration file.
|
||||||
|
_SNMPD_CONF = "/etc/snmp/snmpd.conf"
|
||||||
|
|
||||||
|
|
||||||
def _net(value: str) -> str:
|
def _net(value: str) -> str:
|
||||||
@@ -527,8 +529,12 @@ class ProxmoxSystemMixin:
|
|||||||
return self._step(command, done)
|
return self._step(command, done)
|
||||||
|
|
||||||
def _write_apt_file(self, path: str, content: str) -> dict:
|
def _write_apt_file(self, path: str, content: str) -> dict:
|
||||||
|
return self._write_root_file(path, content)
|
||||||
|
|
||||||
|
def _write_root_file(self, path: str, content: str) -> dict:
|
||||||
|
"""Write *content* to *path* as root, mode 644, through a temporary file."""
|
||||||
encoded = base64.b64encode(content.encode()).decode()
|
encoded = base64.b64encode(content.encode()).decode()
|
||||||
# No ``exit`` here: ``_rc`` reads the status after the command's braces.
|
# No ``exit`` here: it would end the shell before ``_rc`` reads the status.
|
||||||
return self._step(
|
return self._step(
|
||||||
f'umask 077 && t=$(mktemp) && echo {encoded} | base64 -d > "$t" && '
|
f'umask 077 && t=$(mktemp) && echo {encoded} | base64 -d > "$t" && '
|
||||||
f'install -m 644 -o root -g root "$t" {shlex.quote(path)}; '
|
f'install -m 644 -o root -g root "$t" {shlex.quote(path)}; '
|
||||||
@@ -559,13 +565,7 @@ class ProxmoxSystemMixin:
|
|||||||
)
|
)
|
||||||
|
|
||||||
def _write_snmp_agent_config(self, settings: dict) -> dict:
|
def _write_snmp_agent_config(self, settings: dict) -> dict:
|
||||||
encoded = base64.b64encode(render_snmpd_conf(settings).encode()).decode()
|
return self._write_root_file(_SNMPD_CONF, render_snmpd_conf(settings))
|
||||||
return self._step(
|
|
||||||
f'umask 077 && t=$(mktemp) && echo {encoded} | base64 -d > "$t" && '
|
|
||||||
f'install -m 644 -o root -g root "$t" /etc/snmp/snmpd.conf; '
|
|
||||||
's=$?; rm -f "$t"; exit $s',
|
|
||||||
"wrote /etc/snmp/snmpd.conf",
|
|
||||||
)
|
|
||||||
|
|
||||||
def _allow_snmp_sources(self, settings: dict) -> dict:
|
def _allow_snmp_sources(self, settings: dict) -> dict:
|
||||||
sources = [_net(s) for s in settings.get("allowed_sources", [])]
|
sources = [_net(s) for s in settings.get("allowed_sources", [])]
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ contract's ``install_package``. Every step reads its exit status.
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import re
|
import re
|
||||||
|
import subprocess
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
@@ -36,6 +37,18 @@ class _Node:
|
|||||||
return f"{out}\n__PVE_RC={rc}" if "__PVE_RC" in command else out
|
return f"{out}\n__PVE_RC={rc}" if "__PVE_RC" in command else out
|
||||||
|
|
||||||
|
|
||||||
|
def _shell_driver(prelude: str) -> ProxmoxDriver:
|
||||||
|
"""A driver whose node shell is a real local ``sh``, *prelude* run first."""
|
||||||
|
|
||||||
|
def shell(command: str) -> str:
|
||||||
|
run = subprocess.run(["sh", "-c", prelude + command], capture_output=True, text=True)
|
||||||
|
return run.stdout
|
||||||
|
|
||||||
|
d = ProxmoxDriver("pve", "root@pam", "pw")
|
||||||
|
d._exec_ssh_command = shell
|
||||||
|
return d
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def node():
|
def node():
|
||||||
return _Node()
|
return _Node()
|
||||||
@@ -87,6 +100,27 @@ class TestSnmpAgent:
|
|||||||
assert result["success"] is False
|
assert result["success"] is False
|
||||||
assert not any("/etc/snmp/snmpd.conf" in c for c in node.ran)
|
assert not any("/etc/snmp/snmpd.conf" in c for c in node.ran)
|
||||||
|
|
||||||
|
def test_the_written_config_reports_success_through_a_real_shell(self, tmp_path):
|
||||||
|
"""The configure step's own exit status must reach ``_rc`` (#30).
|
||||||
|
|
||||||
|
``_Node`` answers ``__PVE_RC=0`` on its own; a real ``sh`` shows whether the
|
||||||
|
wrapped command still prints it. ``install`` is a function writing to tmp_path.
|
||||||
|
"""
|
||||||
|
target = tmp_path / "snmpd.conf"
|
||||||
|
d = _shell_driver(f'install() {{ cat "$7" > {target}; }}; ')
|
||||||
|
|
||||||
|
step = d._write_snmp_agent_config(SETTINGS)
|
||||||
|
|
||||||
|
assert step == {"ok": True, "output": "wrote /etc/snmp/snmpd.conf"}
|
||||||
|
assert "rocommunity c0mm 10.7.224.20/32" in target.read_text()
|
||||||
|
|
||||||
|
def test_a_refused_write_reports_failure_through_a_real_shell(self):
|
||||||
|
d = _shell_driver('install() { echo "install: read-only file system"; return 1; }; ')
|
||||||
|
|
||||||
|
step = d._write_snmp_agent_config(SETTINGS)
|
||||||
|
|
||||||
|
assert step == {"ok": False, "output": "install: read-only file system"}
|
||||||
|
|
||||||
def test_the_management_peer(self, driver):
|
def test_the_management_peer(self, driver):
|
||||||
assert driver.get_management_peer() == "10.7.224.20"
|
assert driver.get_management_peer() == "10.7.224.20"
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user