fix(hlth-csp): accept the status declarations the unit already passes

The systemd unit has passed --accept-status since f247b29fd0, but the
wrapper script never learned the flag. argparse rejected it, so the
service failed with status=2/INVALIDARGUMENT on every host that deploys
the role, in compose and in swarm alike.

The checker consumes every token after a list flag that does not start
with "--", and a URL does not, so both lists now share a single
separator placed before the URLs. The previous placement sat directly
behind --ignore-network-blocks-from and would have fed a second list
straight into the URL parser.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
kevinveenbirkenbachandClaude Opus 5 committed 2026-09-16 20:35:35 +02:00
1 parent e177a7c4a9
commit 60d187410a
3 files changed
+151

No files matched your search

@@ -0,0 +1,55 @@
import importlib.util
import unittest
from . import PROJECT_ROOT
SCRIPT = PROJECT_ROOT / "roles" / "sys-ctl-hlth-csp" / "files" / "python" / "script.py"
URLS = ["https://a.test/", "https://b.test/"]
def _load():
spec = importlib.util.spec_from_file_location("csp_wrapper", SCRIPT)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
def _cmd(**kwargs):
return _load().build_docker_cmd(
image="img",
urls=URLS,
short_mode=False,
ignore_network_blocks_from=kwargs.get("ignore", []),
accept_status=kwargs.get("accept", []),
)
class TestListFlagsAreSeparatedFromUrls(unittest.TestCase):
def test_accept_status_is_separated_from_the_urls(self):
cmd = _cmd(accept=["a.test=403"])
self.assertEqual(
cmd[cmd.index("--accept-status") + 2],
"--",
"the checker consumes every token after a list flag that does not "
"start with '--', and a URL does not, so the URLs would be read as "
"more status declarations",
)
def test_two_list_flags_share_a_single_separator(self):
cmd = _cmd(ignore=["x.test"], accept=["y.test=403"])
self.assertEqual(cmd.count("--"), 1)
self.assertLess(cmd.index("--"), cmd.index(URLS[0]))
def test_without_a_list_flag_no_separator_is_emitted(self):
self.assertNotIn("--", _cmd())
def test_every_url_survives_the_separator(self):
cmd = _cmd(accept=["y.test=403"])
self.assertEqual(cmd[cmd.index("--") + 1 :], URLS)
if __name__ == "__main__":
unittest.main()
@@ -0,0 +1,75 @@
import re
import subprocess
import sys
import unittest
from utils.cache.files import read_text
from . import PROJECT_ROOT
ROLE = PROJECT_ROOT / "roles" / "sys-ctl-hlth-csp"
TASKS = ROLE / "tasks" / "00_core.yml"
SCRIPT = ROLE / "files" / "python" / "script.py"
_FLAG = re.compile(r"'\s*(--[a-z][a-z0-9-]*)")
_VALUE = {
"--nginx-config-dir": "/nonexistent",
"--image": "img",
"--tor-proxy": "socks5://127.0.0.1:9050",
"--onion-timeout": "1000",
}
_LIST_VALUE = {
"--ignore-network-blocks-from": "a.test",
"--skip-domain": "a.test",
"--accept-status": "a.test=403",
}
def _flags_the_unit_passes() -> set[str]:
return set(_FLAG.findall(read_text(str(TASKS))))
def _invoke(flag: str) -> subprocess.CompletedProcess:
argv = [
sys.executable,
str(SCRIPT),
"--nginx-config-dir",
"/nonexistent",
"--image",
"img",
]
if flag in _LIST_VALUE:
argv.extend([flag, _LIST_VALUE[flag]])
elif flag in _VALUE and flag not in ("--nginx-config-dir", "--image"):
argv.extend([flag, _VALUE[flag]])
elif flag not in _VALUE:
argv.append(flag)
return subprocess.run(argv, capture_output=True, text=True, timeout=60, check=False)
class TestUnitFlagsAreParsable(unittest.TestCase):
def test_the_unit_passes_at_least_the_known_flags(self):
found = _flags_the_unit_passes()
self.assertIn("--accept-status", found)
self.assertIn("--onion-timeout", found)
def test_every_flag_the_unit_passes_is_accepted_by_the_script(self):
for flag in sorted(_flags_the_unit_passes()):
with self.subTest(flag=flag):
completed = _invoke(flag)
self.assertNotIn(
"unrecognized arguments",
completed.stderr,
f"the systemd unit passes {flag} but script.py rejects it, "
f"which fails the service with status=2/INVALIDARGUMENT",
)
def test_an_undeclared_flag_is_still_rejected(self):
completed = _invoke("--definitely-not-a-flag")
self.assertIn("unrecognized arguments", completed.stderr)
if __name__ == "__main__":
unittest.main()