diff --git a/README.md b/README.md index e7f6989..802ec39 100644 --- a/README.md +++ b/README.md @@ -342,6 +342,7 @@ fix: ci [skip test] [skip sbom] # skip multiple steps | Variable | Default | Description | |----------|---------|-------------| | `DBUILD_REGISTRY` | `ghcr.io/daemonless` | Default container registry | +| `DBUILD_TEST_NETWORK` | `podman` (`host` inside a jail without vnet) | Network for `dbuild test` containers. `host` shares the caller's network and probes `127.0.0.1` | | `GITHUB_TOKEN` | | Forwarded as build secret + registry auth | | `GITHUB_ACTOR` | | Registry login username | | `DOCKERHUB_USERNAME` | | Enable Docker Hub mirroring | diff --git a/dbuild/docs.py b/dbuild/docs.py index 6f9dc70..e693d10 100644 --- a/dbuild/docs.py +++ b/dbuild/docs.py @@ -113,6 +113,10 @@ def _global_config_doc_path() -> str: ), "ENVIRONMENT": { "DBUILD_REGISTRY": "Override the target container registry (e.g., ghcr.io/myorg).", + "DBUILD_TEST_NETWORK": ( + "Network for the containers dbuild test starts. Default podman; host inside a " + "jail without vnet (a CI runner container), where the checks then probe 127.0.0.1." + ), "GITHUB_TOKEN": "Authentication token for GitHub Packages and build secrets.", "GITHUB_ACTOR": "The username associated with GITHUB_TOKEN.", "DOCKERHUB_USERNAME": "Enable mirroring by providing a Docker Hub username.", diff --git a/dbuild/podman.py b/dbuild/podman.py index cf2f9ea..ed23c2a 100644 --- a/dbuild/podman.py +++ b/dbuild/podman.py @@ -218,11 +218,37 @@ def history(image: str, *, quiet: bool = False) -> list[dict[str, Any]]: return json.loads(result.stdout) if result.stdout.strip() else [] +def _jailed_without_vnet() -> bool: + """True inside a FreeBSD jail that has no network stack of its own.""" + def read(name: str) -> str: + try: + out = subprocess.run( + ["sysctl", "-n", name], capture_output=True, text=True, check=False, + ) + except OSError: + return "" + return out.stdout.strip() + return read("security.jail.jailed") == "1" and read("security.jail.vnet") == "0" + + +def cit_network() -> str: + """Network for containers started by CIT. + + ``$DBUILD_TEST_NETWORK`` wins. Otherwise ``host`` inside a jail without + vnet (a CI runner container): no bridge can be created there, so the + container shares the runner's network. Otherwise ``podman``. + """ + env = os.environ.get("DBUILD_TEST_NETWORK") + if env: + return env + return "host" if _jailed_without_vnet() else "podman" + + def run_detached( image: str, *, name: str, - network: str = "podman", + network: str | None = None, annotations: dict[str, str] | None = None, env: dict[str, str] | None = None, volumes: list[str] | None = None, @@ -231,8 +257,9 @@ def run_detached( *env* maps environment variable names to values (``-e KEY=VALUE``). *volumes* is a list of ``-v`` mount specs (e.g. ``"vol:/config"``). + *network* defaults to :func:`cit_network`. """ - cmd = ["podman", "run", "-d", "--name", name, f"--network={network}"] + cmd = ["podman", "run", "-d", "--name", name, f"--network={network or cit_network()}"] for key, val in (annotations or {}).items(): cmd += ["--annotation", f"{key}={val}"] for key, val in (env or {}).items(): @@ -288,7 +315,15 @@ def inspect_ip(container_name: str) -> str: "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}", container_name, ]) - return result.stdout.strip() + ip = result.stdout.strip() + if ip: + return ip + # A host-network container has no address of its own. + mode = _run( + ["podman", "inspect", "--format", "{{.HostConfig.NetworkMode}}", container_name], + check=False, + ) + return "127.0.0.1" if (mode.stdout or "").strip() == "host" else "" def container_running(container_name: str, *, quiet: bool = False) -> bool: diff --git a/docs/dbuild.1 b/docs/dbuild.1 index 61964e1..f9e18ed 100644 --- a/docs/dbuild.1 +++ b/docs/dbuild.1 @@ -1,4 +1,4 @@ -.TH DBUILD 1 "2026-11-12" "dbuild 1.13.1" "User Commands" +.TH DBUILD 1 "2026-11-16" "dbuild 1.13.1" "User Commands" .SH NAME dbuild \- FreeBSD OCI container image build tool .SH SYNOPSIS @@ -376,6 +376,9 @@ enable/disable the PUID/PGID re-chown check for this run, overriding the `cit: p \fIDBUILD_REGISTRY\fR Override the target container registry (e.g., ghcr.io/myorg). .TP +\fIDBUILD_TEST_NETWORK\fR +Network for the containers dbuild test starts. Default podman; host inside a jail without vnet (a CI runner container), where the checks then probe 127.0.0.1. +.TP \fIGITHUB_TOKEN\fR Authentication token for GitHub Packages and build secrets. .TP diff --git a/tests/test_podman.py b/tests/test_podman.py index 694a246..fecc234 100644 --- a/tests/test_podman.py +++ b/tests/test_podman.py @@ -191,6 +191,67 @@ def test_with_env_and_volumes(self, mock_run): self.assertIn("myvol:/config", cmd) + @patch("dbuild.podman._jailed_without_vnet", return_value=False) + @patch("dbuild.podman._run") + def test_network_defaults_to_podman(self, mock_run, _jailed): + mock_run.return_value = subprocess.CompletedProcess( + args=[], returncode=0, stdout="abc123\n", stderr="" + ) + with patch.dict("os.environ", {}, clear=False) as env: + env.pop("DBUILD_TEST_NETWORK", None) + podman.run_detached("myimage:latest", name="test-ctr") + self.assertIn("--network=podman", mock_run.call_args[0][0]) + + @patch("dbuild.podman._jailed_without_vnet", return_value=True) + @patch("dbuild.podman._run") + def test_network_is_host_in_a_jail_without_vnet(self, mock_run, _jailed): + mock_run.return_value = subprocess.CompletedProcess( + args=[], returncode=0, stdout="abc123\n", stderr="" + ) + with patch.dict("os.environ", {}, clear=False) as env: + env.pop("DBUILD_TEST_NETWORK", None) + podman.run_detached("myimage:latest", name="test-ctr") + self.assertIn("--network=host", mock_run.call_args[0][0]) + + @patch("dbuild.podman._jailed_without_vnet", return_value=True) + @patch("dbuild.podman._run") + def test_network_env_wins(self, mock_run, _jailed): + mock_run.return_value = subprocess.CompletedProcess( + args=[], returncode=0, stdout="abc123\n", stderr="" + ) + with patch.dict("os.environ", {"DBUILD_TEST_NETWORK": "mynet"}): + podman.run_detached("myimage:latest", name="test-ctr") + self.assertIn("--network=mynet", mock_run.call_args[0][0]) + + +class TestInspectIp(unittest.TestCase): + """Tests for inspect_ip().""" + + @patch("dbuild.podman._run") + def test_bridge_address(self, mock_run): + mock_run.return_value = subprocess.CompletedProcess( + args=[], returncode=0, stdout="10.88.0.7\n", stderr="" + ) + self.assertEqual(podman.inspect_ip("ctr"), "10.88.0.7") + self.assertEqual(mock_run.call_count, 1) + + @patch("dbuild.podman._run") + def test_host_network_is_loopback(self, mock_run): + mock_run.side_effect = [ + subprocess.CompletedProcess(args=[], returncode=0, stdout="\n", stderr=""), + subprocess.CompletedProcess(args=[], returncode=0, stdout="host\n", stderr=""), + ] + self.assertEqual(podman.inspect_ip("ctr"), "127.0.0.1") + + @patch("dbuild.podman._run") + def test_no_address_on_other_networks(self, mock_run): + mock_run.side_effect = [ + subprocess.CompletedProcess(args=[], returncode=0, stdout="\n", stderr=""), + subprocess.CompletedProcess(args=[], returncode=0, stdout="none\n", stderr=""), + ] + self.assertEqual(podman.inspect_ip("ctr"), "") + + class TestContainerRunning(unittest.TestCase): """Tests for container_running()."""