diff --git a/requirements/requirements.galaxy.yml b/requirements/requirements.galaxy.yml index 0a1db299fb..ef98c31538 100644 --- a/requirements/requirements.galaxy.yml +++ b/requirements/requirements.galaxy.yml @@ -7,10 +7,10 @@ collections: - name: kewlfft.aur version: 0.13.0 - name: community.general - version: 13.4.0 + version: 13.5.0 - name: community.docker version: 5.4.0 - name: community.library_inventory_filtering_v1 version: 1.1.5 - name: hetzner.hcloud - version: 7.1.0 + version: 7.2.0 diff --git a/requirements/requirements.git.yml b/requirements/requirements.git.yml index eef6cbd713..58808ee14c 100644 --- a/requirements/requirements.git.yml +++ b/requirements/requirements.git.yml @@ -18,7 +18,7 @@ collections: - name: community.general source: https://github.com/ansible-collections/community.general.git type: git - version: 13.4.0 + version: 13.5.0 - name: community.docker source: https://github.com/ansible-collections/community.docker.git @@ -33,4 +33,4 @@ collections: - name: hetzner.hcloud source: https://github.com/ansible-collections/hetzner.hcloud.git type: git - version: 7.1.0 + version: 7.2.0 diff --git a/roles/svc-net-tor/files/shell/entrypoint.sh b/roles/svc-net-tor/files/shell/entrypoint.sh index ccf615aad1..27fe61bc7d 100644 --- a/roles/svc-net-tor/files/shell/entrypoint.sh +++ b/roles/svc-net-tor/files/shell/entrypoint.sh @@ -21,6 +21,9 @@ if [ "${TOR_FLAVOR}" = "chutney" ]; then CHUTNEY_LISTEN_ADDRESS=127.0.0.1 \ ./chutney init --net "${net}" ./chutney configure + for torrc in net/nodes/*/torrc; do + echo "NumCPUs 1" >>"${torrc}" + done ./chutney start ./chutney wait_for_bootstrap diff --git a/roles/svc-net-tor/files/test/test.sh b/roles/svc-net-tor/files/test/test.sh index 68677e034b..f3a8989df7 100755 --- a/roles/svc-net-tor/files/test/test.sh +++ b/roles/svc-net-tor/files/test/test.sh @@ -18,6 +18,8 @@ # SLEEP_SECONDS wait between attempts # TOR_DNSMASQ_CONF the dnsmasq drop-in path; its directory is scanned for # upstreams when a probe fails +# TOR_CONTAINER address of the tor container +# TOR_FLAVOR public or chutney set -uo pipefail @@ -26,6 +28,8 @@ NGINX_SERVERS_DIR="${NGINX_SERVERS_DIR:?pass NGINX_SERVERS_DIR as env (the deplo RETRIES="${RETRIES:?pass RETRIES as env (attempts per domain, from svc-net-tor test.env)}" SLEEP_SECONDS="${SLEEP_SECONDS:?pass SLEEP_SECONDS as env (wait between attempts, from svc-net-tor test.env)}" TOR_DNSMASQ_CONF="${TOR_DNSMASQ_CONF:?pass TOR_DNSMASQ_CONF as env (the dnsmasq drop-in path, from svc-net-tor vars)}" +TOR_CONTAINER="${TOR_CONTAINER:?pass TOR_CONTAINER as env (address of the tor container, from svc-net-tor test.env)}" +TOR_FLAVOR="${TOR_FLAVOR:?pass TOR_FLAVOR as env (public or chutney, from svc-net-tor test.env)}" discover_domains() { find "${NGINX_SERVERS_DIR}/http" "${NGINX_SERVERS_DIR}/https" \ @@ -102,4 +106,19 @@ echo "[INFO] ${#domains[@]} probed, ${failed} failed" "$(dirname "${BASH_SOURCE[0]}")/onion_ports.py" || failed=$((failed + 1)) +if [[ "${TOR_FLAVOR}" == "chutney" ]]; then + status=0 + workers="$(container exec "${TOR_CONTAINER}" sh -c 'grep -cH "^NumCPUs 1$" /opt/chutney/net/nodes/*/torrc')" || status=$? + unbounded="$(sed -n 's/:0$//p' <<<"${workers}")" + if [[ "${status}" -gt 1 || -z "${workers}" ]]; then + echo "[FAIL] cannot read the chutney node torrcs in ${TOR_CONTAINER}" + failed=$((failed + 1)) + elif [[ -n "${unbounded}" ]]; then + echo "[FAIL] chutney nodes without NumCPUs 1 start one worker per host core: ${unbounded//$'\n'/ }" + failed=$((failed + 1)) + else + echo "[OK] every chutney node runs one worker" + fi +fi + [[ "${failed}" -eq 0 ]] diff --git a/roles/svc-net-tor/templates/test.env.j2 b/roles/svc-net-tor/templates/test.env.j2 index 6e0bec9054..51288356b2 100644 --- a/roles/svc-net-tor/templates/test.env.j2 +++ b/roles/svc-net-tor/templates/test.env.j2 @@ -5,5 +5,7 @@ ONION_PORT_RETRIES=2 ONION_PORT_TIMEOUT=60 RETRIES=5 SLEEP_SECONDS=10 +TOR_CONTAINER={{ lookup('container_address', application_id, 'tor') }} TOR_DNSMASQ_CONF={{ TOR_DNSMASQ_CONF }} +TOR_FLAVOR={{ TOR_FLAVOR }} TOR_SOCKS={{ lookup('tor_socks') }} diff --git a/tests/unit/python/roles/svc-net-tor/test_chutney_worker_check.py b/tests/unit/python/roles/svc-net-tor/test_chutney_worker_check.py new file mode 100644 index 0000000000..b520ffe58e --- /dev/null +++ b/tests/unit/python/roles/svc-net-tor/test_chutney_worker_check.py @@ -0,0 +1,107 @@ +from __future__ import annotations + +import os +import shutil +import subprocess +import tempfile +import unittest +from pathlib import Path + +from . import PROJECT_ROOT + +TEST_SH = PROJECT_ROOT / "roles" / "svc-net-tor" / "files" / "test" / "test.sh" +BOUNDED = "DataDirectory /data\nNumCPUs 1\n" +UNBOUNDED = "DataDirectory /data\n" +CONTAINER_STUB = """\ +in_container=/opt/chutney/net/nodes +exec sh -c "${5//"${in_container}"/"${NODES_DIR}"}" +""" + + +def _stub(path: Path, body: str) -> None: + path.write_text(f"#!/usr/bin/env bash\n{body}\n", encoding="utf-8") + path.chmod(0o755) + + +def _run( + torrcs: dict[str, str], flavor: str = "chutney", unreadable: str = "" +) -> subprocess.CompletedProcess[str]: + with tempfile.TemporaryDirectory() as tmp: + root = Path(tmp) + nodes = root / "nodes" + nodes.mkdir() + for node, torrc in torrcs.items(): + (nodes / node).mkdir() + (nodes / node / "torrc").write_text(torrc, encoding="utf-8") + if unreadable: + (nodes / unreadable / "torrc").mkdir(parents=True) + shutil.copy(TEST_SH, root / "test.sh") + _stub(root / "onion_ports.py", "exit 0") + bin_dir = root / "bin" + bin_dir.mkdir() + _stub(bin_dir / "curl", "printf 200") + _stub(bin_dir / "container", CONTAINER_STUB) + return subprocess.run( + ["bash", str(root / "test.sh"), "example.onion"], + env={ + "PATH": f"{bin_dir}{os.pathsep}{os.environ['PATH']}", + "NODES_DIR": str(nodes), + "TOR_SOCKS": "127.0.0.1:9050", + "NGINX_SERVERS_DIR": str(root), + "RETRIES": "1", + "SLEEP_SECONDS": "0", + "TOR_DNSMASQ_CONF": str(root / "tor-onion.conf"), + "TOR_CONTAINER": "tor", + "TOR_FLAVOR": flavor, + }, + capture_output=True, + text=True, + check=False, + ) + + +def _verdict(result: subprocess.CompletedProcess[str]) -> str: + return result.stdout.splitlines()[-1] + + +class TestChutneyWorkerCheck(unittest.TestCase): + def test_nodes_that_all_carry_the_limit_pass(self) -> None: + result = _run({"000a": BOUNDED, "001a": BOUNDED}) + self.assertEqual(_verdict(result), "[OK] every chutney node runs one worker") + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) + + def test_a_node_without_the_limit_is_named_and_fails(self) -> None: + result = _run({"000a": BOUNDED, "001a": UNBOUNDED}) + self.assertIn("start one worker per host core", _verdict(result)) + self.assertTrue(_verdict(result).endswith("/001a/torrc"), _verdict(result)) + self.assertNotIn("000a", _verdict(result)) + self.assertEqual(result.returncode, 1) + + def test_nodes_that_all_lack_the_limit_fail(self) -> None: + result = _run({"000a": UNBOUNDED, "001a": UNBOUNDED}) + self.assertIn("/000a/torrc", _verdict(result)) + self.assertIn("/001a/torrc", _verdict(result)) + self.assertEqual(result.returncode, 1) + + def test_no_torrc_to_inspect_fails(self) -> None: + result = _run({}) + self.assertEqual( + _verdict(result), "[FAIL] cannot read the chutney node torrcs in tor" + ) + self.assertEqual(result.returncode, 1) + + def test_a_torrc_that_cannot_be_read_fails_beside_bounded_nodes(self) -> None: + result = _run({"000a": BOUNDED}, unreadable="001a") + self.assertEqual( + _verdict(result), "[FAIL] cannot read the chutney node torrcs in tor" + ) + self.assertEqual(result.returncode, 1) + + def test_the_public_flavor_does_not_run_the_check(self) -> None: + result = _run({}, flavor="public") + self.assertEqual(_verdict(result), "[INFO] 1 probed, 0 failed") + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/unit/python/roles/svc-net-tor/test_test_env_sourcing.py b/tests/unit/python/roles/svc-net-tor/test_test_env_sourcing.py index e0a3f8ed8c..ddb45113bd 100644 --- a/tests/unit/python/roles/svc-net-tor/test_test_env_sourcing.py +++ b/tests/unit/python/roles/svc-net-tor/test_test_env_sourcing.py @@ -26,6 +26,7 @@ PORTS = [25, 80, 587] def _lookup(name: str, *_args: str) -> object: return { "config": "example.onion", + "container_address": "tor", "nginx": "/etc/nginx/conf.d/servers", "tor_ports": [ {"onion_port": port, "target": f"127.0.0.1:{port}"} for port in PORTS @@ -42,8 +43,10 @@ def _render(mode: str) -> str: env.filters.update(FilterModule().filters()) return env.from_string(read_text(str(TEMPLATE))).render( lookup=_lookup, + application_id="svc-net-tor", DEPLOYMENT_MODE=mode, TOR_DNSMASQ_CONF="/etc/dnsmasq.d/tor-onion.conf", + TOR_FLAVOR="chutney", )