diff --git a/roles/sys-ctl-hlth-csp/files/python/script.py b/roles/sys-ctl-hlth-csp/files/python/script.py index c79e8ddb2b..761ff3159a 100755 --- a/roles/sys-ctl-hlth-csp/files/python/script.py +++ b/roles/sys-ctl-hlth-csp/files/python/script.py @@ -131,6 +131,7 @@ def build_docker_cmd( use_host_network: bool = True, proxy: str = "", timeout_ms: int = 0, + accept_status: list[str] | None = None, ) -> list[str]: cmd = ["container", "run", "--rm"] @@ -155,6 +156,12 @@ def build_docker_cmd( if ignore_network_blocks_from: cmd.append("--ignore-network-blocks-from") cmd.extend(ignore_network_blocks_from) + + if accept_status: + cmd.append("--accept-status") + cmd.extend(accept_status) + + if ignore_network_blocks_from or accept_status: cmd.append("--") cmd.extend(urls) @@ -170,6 +177,7 @@ def run_checker( use_host_network: bool = True, proxy: str = "", timeout_ms: int = 0, + accept_status: list[str] | None = None, ) -> int: """ Runs the CSP checker container and returns its exit code. @@ -186,6 +194,7 @@ def run_checker( use_host_network=use_host_network, proxy=proxy, timeout_ms=timeout_ms, + accept_status=accept_status, ) try: @@ -245,6 +254,17 @@ def main() -> None: "otherwise treat as unreachable." ), ) + parser.add_argument( + "--accept-status", + nargs="*", + default=[], + help=( + "Per-vhost status codes the probe must treat as healthy, as " + "=[,]. Declared in the role's " + "server.status_codes; everything below 300 passes without being " + "listed." + ), + ) parser.add_argument( "--onion-timeout", type=int, @@ -322,6 +342,7 @@ def main() -> None: use_host_network=not bool(args.no_host_network), proxy=batch_proxy, timeout_ms=batch_timeout, + accept_status=list(args.accept_status or []), ) rc = rc or batch_rc sys.exit(rc) diff --git a/tests/unit/python/roles/sys-ctl-hlth-csp/test_list_flags_are_separated.py b/tests/unit/python/roles/sys-ctl-hlth-csp/test_list_flags_are_separated.py new file mode 100644 index 0000000000..59fb813bef --- /dev/null +++ b/tests/unit/python/roles/sys-ctl-hlth-csp/test_list_flags_are_separated.py @@ -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() diff --git a/tests/unit/python/roles/sys-ctl-hlth-csp/test_unit_flags_are_parsable.py b/tests/unit/python/roles/sys-ctl-hlth-csp/test_unit_flags_are_parsable.py new file mode 100644 index 0000000000..759832c95a --- /dev/null +++ b/tests/unit/python/roles/sys-ctl-hlth-csp/test_unit_flags_are_parsable.py @@ -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()