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:
Christian Manivong committed 2026-11-01 21:44:23 +01:00
1 parent d142db73e5
commit 52d63eb689
3 files changed
+45 -8

No files matched your search

+34
View File
@@ -10,6 +10,7 @@ contract's ``install_package``. Every step reads its exit status.
from __future__ import annotations
import re
import subprocess
import pytest
@@ -36,6 +37,18 @@ class _Node:
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
def node():
return _Node()
@@ -87,6 +100,27 @@ class TestSnmpAgent:
assert result["success"] is False
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):
assert driver.get_management_peer() == "10.7.224.20"