Files
computer-playbook/tests/unit/cli/contributing/network/diagnose/test_cli.py
T
kevinveenbirkenbachandClaude Opus 4.7 e4e861c261 refactor(cli): move ports/networks suggesters under cli/contributing/network and add diagnose subcommand
Relocates `cli/meta/{ports,networks/suggest}` to
`cli/contributing/network/{ports,address/suggest}` so port and subnet
allocators live alongside the rest of the contributor-side tooling
(mirror, update, changelog) instead of `meta`. Mirrors the tests
into `tests/unit/cli/contributing/network/` and renames the contract
docs to `docs/contributing/tools/network/{port,address}.md`. Prog
names update from `cli meta ports suggest` / `cli meta networks
suggest` to `cli contributing network ports suggest` / `cli
contributing network address suggest`; the corresponding lint
references and `utils.meta.scan` docstring follow.

Adds a third subcommand: `cli contributing network diagnose`
(stdlib-only DNS / TCP / TLS / PMTU probe against the standard
infrastructure hosts plus interface MTU, `/etc/resolv.conf`,
`/etc/hosts`, proxy env, CA-bundle summaries). Split across `cli.py`
(orchestration), `config.py` (SPOT for hosts / per-distro install
commands), `format.py` (output + subprocess helpers), `tools.py`
(distro-aware auto-install of `iputils-ping` + `iproute2`),
`report.py` (system-info dumps + IPv6-default-route detection), and
`probes.py` (per-host probes). Exposed as `make debug-network`.

Companion `docs/contributing/actions/debugging/network.md` absorbs
the diagnose-tool walkthrough (incl. how to read MTU-induced TLS
handshake timeouts) so the merged page covers DNS / MTU / IPv6 /
connectivity end-to-end.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-21 14:55:10 +02:00

82 lines
2.9 KiB
Python

"""Unit tests for cli.contributing.network.diagnose.cli."""
from __future__ import annotations
import io
import socket
import unittest
from contextlib import redirect_stdout
from unittest.mock import patch
from cli.contributing.network.diagnose import cli as diag_cli
class TestResolveHosts(unittest.TestCase):
def test_returns_defaults_when_env_unset(self) -> None:
with patch.dict(diag_cli.os.environ, {}, clear=True):
hosts = diag_cli.resolve_hosts()
self.assertEqual(hosts, list(diag_cli.DEFAULT_HOSTS))
def test_appends_env_hosts(self) -> None:
with patch.dict(
diag_cli.os.environ,
{diag_cli.EXTRA_HOSTS_ENV: "a.example b.example"},
clear=True,
):
hosts = diag_cli.resolve_hosts()
self.assertEqual(hosts[-2:], ["a.example", "b.example"])
self.assertEqual(hosts[:-2], list(diag_cli.DEFAULT_HOSTS))
class TestMain(unittest.TestCase):
def test_returns_zero_and_runs_v4_only_when_no_v6(self) -> None:
buf = io.StringIO()
with (
patch.object(diag_cli, "show_identity") as si,
patch.object(diag_cli, "ensure_tools") as et,
patch.object(diag_cli, "show_iface_routes"),
patch.object(diag_cli, "show_resolv"),
patch.object(diag_cli, "show_hosts"),
patch.object(diag_cli, "show_proxies"),
patch.object(diag_cli, "show_ca_bundle"),
patch.object(diag_cli, "has_ipv6_default_route", return_value=False),
patch.object(diag_cli, "per_host_check") as phc,
redirect_stdout(buf),
):
rc = diag_cli.main()
self.assertEqual(rc, 0)
si.assert_called_once()
et.assert_called_once()
phc.assert_called_once()
args, _kwargs = phc.call_args
self.assertEqual(args[1], socket.AF_INET)
self.assertEqual(args[2], "IPv4")
self.assertIn("[SKIP] no IPv6 default route", buf.getvalue())
def test_runs_v6_when_route_present(self) -> None:
buf = io.StringIO()
with (
patch.object(diag_cli, "show_identity"),
patch.object(diag_cli, "ensure_tools"),
patch.object(diag_cli, "show_iface_routes"),
patch.object(diag_cli, "show_resolv"),
patch.object(diag_cli, "show_hosts"),
patch.object(diag_cli, "show_proxies"),
patch.object(diag_cli, "show_ca_bundle"),
patch.object(diag_cli, "has_ipv6_default_route", return_value=True),
patch.object(diag_cli.socket, "has_ipv6", True),
patch.object(diag_cli, "per_host_check") as phc,
redirect_stdout(buf),
):
rc = diag_cli.main()
self.assertEqual(rc, 0)
self.assertEqual(phc.call_count, 2)
families = [call.args[1] for call in phc.call_args_list]
self.assertEqual(families, [socket.AF_INET, socket.AF_INET6])
if __name__ == "__main__":
unittest.main()