diff --git a/tests/unit/utils/diagnostics/test_container.py b/tests/unit/utils/diagnostics/test_container.py index 307f4547f2..6b10a197d3 100644 --- a/tests/unit/utils/diagnostics/test_container.py +++ b/tests/unit/utils/diagnostics/test_container.py @@ -133,6 +133,48 @@ class CollectTests(unittest.TestCase): self.assertTrue((out / "resolve-ghcr-io-via-10-0-0-2.txt").is_file()) self.assertTrue((out / "resolve-ghcr-io.txt").is_file()) + def test_daemon_json_dns_servers_are_probed_and_deduped(self): + mod = _load() + + def fake_run(cmd, **_kw): + if cmd == ["cat", "/etc/resolv.conf"]: + return _cp(cmd, stdout=b"nameserver 127.0.0.1\n") + if cmd == ["cat", "/etc/docker/daemon.json"]: + return _cp(cmd, stdout=b'{"dns": ["172.30.0.53", "127.0.0.1"]}') + return _cp(cmd) + + with tempfile.TemporaryDirectory() as td: + out = Path(td) + with ( + mock.patch.object( + mod.shutil, "which", side_effect=lambda b: b == "dig" + ), + mock.patch.object(mod, "run", side_effect=fake_run) as runner, + ): + mod.collect_resolver_probes(out) + self.assertTrue((out / "resolve-ghcr-io-via-172-30-0-53.txt").is_file()) + loopback_digs = [ + c.args[0] + for c in runner.call_args_list + if c.args[0][:1] == ["dig"] + and "@127.0.0.1" in c.args[0] + and "ghcr.io" in c.args[0] + ] + self.assertEqual(len(loopback_digs), 1, loopback_digs) + + def test_a_broken_daemon_json_adds_no_probes(self): + mod = _load() + with mock.patch.object( + mod, "run", return_value=_cp([], rc=0, stdout=b"not json") + ): + self.assertEqual(mod.daemon_json_dns(), []) + with mock.patch.object(mod, "run", return_value=_cp([], rc=1)): + self.assertEqual(mod.daemon_json_dns(), []) + with mock.patch.object( + mod, "run", return_value=_cp([], rc=0, stdout=b'{"dns": "1.1.1.1"}') + ): + self.assertEqual(mod.daemon_json_dns(), []) + def test_a_distro_without_a_query_tool_still_gets_the_per_name_verdicts(self): mod = _load() with tempfile.TemporaryDirectory() as td: diff --git a/utils/diagnostics/container.py b/utils/diagnostics/container.py index d40c567759..0d845b368d 100755 --- a/utils/diagnostics/container.py +++ b/utils/diagnostics/container.py @@ -24,6 +24,7 @@ Usage: from __future__ import annotations import contextlib +import json import os import re import shutil @@ -151,6 +152,25 @@ def collect_host(out: Path, app_id: str, context: str, stamp: str) -> None: collect_service_state(out) +def daemon_json_dns() -> list[str]: + """Nameservers pinned under the ``dns`` key of /etc/docker/daemon.json. + + A Tor node's resolv.conf names only the local dnsmasq, so probing its + entries alone cannot say whether the pinned upstream was still alive when + that dnsmasq went silent. Anything unreadable or keyless yields []. + """ + proc = run(["cat", "/etc/docker/daemon.json"]) + if proc.returncode != 0: + return [] + try: + dns = json.loads(proc.stdout.decode(errors="replace")).get("dns") + except ValueError: + return [] + if not isinstance(dns, list): + return [] + return [str(server) for server in dns] + + def collect_resolver_probes(out: Path) -> None: """Resolve each probed name, once through the stack and once per nameserver. @@ -175,6 +195,7 @@ def collect_resolver_probes(out: Path) -> None: for parts in (line.split() for line in list_lines(["cat", "/etc/resolv.conf"])) if parts[:1] == ["nameserver"] and len(parts) > 1 ] + servers += [server for server in daemon_json_dns() if server not in servers] for host in _PROBE_HOSTS: capture(out, f"resolve-{source_name(host)}", ["getent", "hosts", host]) if not tool: