From 7b071ef51c293165e76399956075d454d61f7343 Mon Sep 17 00:00:00 2001 From: Rohit Goswami Date: Fri, 20 Nov 2026 12:17:09 +0000 Subject: [PATCH] test: skip tool meta-tests outside EB_LOCAL_REQUIRE_TOOLS The ZeroPythonSkips meta-tests in tests/test_zero_skips.py asserted unconditionally that eb-stack, zsh, and unshare were installed on PATH, failing local test runs on hosts without those tools. Check EB_LOCAL_REQUIRE_TOOLS in ZeroPythonSkips.require_tool. Set EB_LOCAL_REQUIRE_TOOLS=1 in zig.yml so missing tools or unavailable user-namespace bind mounts fail in CI, and call skipTest with an explicit reason when EB_LOCAL_REQUIRE_TOOLS is unset, 0, or false. --- .github/workflows/zig.yml | 2 +- changelog.d/+zero-skips-strict-env.misc.md | 1 + tests/test_zero_skips.py | 108 +++++++++++++++++++-- 3 files changed, 102 insertions(+), 9 deletions(-) create mode 100644 changelog.d/+zero-skips-strict-env.misc.md diff --git a/.github/workflows/zig.yml b/.github/workflows/zig.yml index ac7f90ad..48382975 100644 --- a/.github/workflows/zig.yml +++ b/.github/workflows/zig.yml @@ -68,7 +68,7 @@ jobs: set -euo pipefail log="$(mktemp)" trap 'rm -f "$log"' EXIT - EB_LOCAL_BIN="$PWD/zig-out/bin/eb-local" python3 -m unittest discover -s tests -v 2>&1 | tee "$log" + EB_LOCAL_REQUIRE_TOOLS=1 EB_LOCAL_BIN="$PWD/zig-out/bin/eb-local" python3 -m unittest discover -s tests -v 2>&1 | tee "$log" ! grep -Eq '\.\.\. skipped |skipped=[0-9]+' "$log" - name: a release build carries no test seam run: | diff --git a/changelog.d/+zero-skips-strict-env.misc.md b/changelog.d/+zero-skips-strict-env.misc.md new file mode 100644 index 00000000..f613f13b --- /dev/null +++ b/changelog.d/+zero-skips-strict-env.misc.md @@ -0,0 +1 @@ +Require `zsh`, `eb-stack`, and `unshare` in `ZeroPythonSkips` when `EB_LOCAL_REQUIRE_TOOLS` is set, and skip with a reason otherwise. diff --git a/tests/test_zero_skips.py b/tests/test_zero_skips.py index 288dc324..1e8f95f6 100644 --- a/tests/test_zero_skips.py +++ b/tests/test_zero_skips.py @@ -11,16 +11,39 @@ import binary import hostile_fs +import test_overlay_real_eb_stack ROOT = Path(__file__).resolve().parents[1] TESTS_DIR = Path(__file__).resolve().parent +REQUIRE_TOOLS_ENV = "EB_LOCAL_REQUIRE_TOOLS" + + +def require_tools_strict() -> bool: + val = os.environ.get(REQUIRE_TOOLS_ENV, "").strip().lower() + return val not in ("", "0", "false") + + +def which_on_env_path(cmd: str): + if "PATH" in os.environ: + return shutil.which(cmd, path=os.environ["PATH"]) + return shutil.which(cmd) @unittest.skipUnless(binary.BIN, "requires compiled eb-local binary") class ZeroPythonSkips(unittest.TestCase): + def require_tool(self, available: bool, reason: str) -> None: + if available: + return + if require_tools_strict(): + self.fail(f"{reason} ({REQUIRE_TOOLS_ENV}=1)") + self.skipTest(reason) + def test_overlay_real_eb_stack_runs_from_path_with_zero_skips(self): - eb_stack = shutil.which("eb-stack") - self.assertIsNotNone(eb_stack, "eb-stack must be installed on PATH") + eb_stack, reason = test_overlay_real_eb_stack.resolve_eb_stack( + env=os.environ, + which=which_on_env_path, + ) + self.require_tool(bool(eb_stack), reason or "eb-stack 0.8.0 or newer was not found on PATH") with tempfile.TemporaryDirectory(prefix="eb-local-zero-skips-") as tmp: env = { "PATH": os.environ.get("PATH", "/usr/local/bin:/usr/bin:/bin"), @@ -116,8 +139,8 @@ def patched_which(cmd, *args, **kwargs): self.assertEqual(len(logged), 1, f"expected only probe call, got: {logged}") def test_caller_shell_init_runs_with_zsh_and_zero_skips(self): - zsh = shutil.which("zsh") - self.assertIsNotNone(zsh, "zsh must be installed on PATH") + zsh = which_on_env_path("zsh") + self.require_tool(zsh is not None, "zsh was not found on PATH") with tempfile.TemporaryDirectory(prefix="eb-local-shell-init-") as tmp: env = { "PATH": os.environ.get("PATH", "/usr/local/bin:/usr/bin:/bin"), @@ -140,10 +163,34 @@ def test_caller_shell_init_runs_with_zsh_and_zero_skips(self): self.assertNotIn("skipped", combined) def test_host_lmod_and_site_files_are_isolated_instead_of_skipping(self): - unshare = shutil.which("unshare") - self.assertIsNotNone(unshare, "unshare must be available") + unshare = which_on_env_path("unshare") + self.require_tool(unshare is not None, "unshare was not found on PATH") uid = str(os.getuid()) gid = str(os.getgid()) + mount_bin = hostile_fs._mount_cmd() + umount_bin = hostile_fs._umount_cmd() + try: + probe = subprocess.run( + [ + unshare, + "-U", + "--keep-caps", + f"--map-user={uid}", + f"--map-group={gid}", + "-m", + "/bin/sh", + "-c", + f'd=$(mktemp -d) && "{mount_bin}" --bind "$d" "$d" && "{umount_bin}" "$d" && rmdir "$d"', + ], + stdin=subprocess.DEVNULL, + capture_output=True, + text=True, + timeout=10, + ) + probe_ok = probe.returncode == 0 + except (OSError, subprocess.SubprocessError): + probe_ok = False + self.require_tool(probe_ok, "user-namespace bind mounts unavailable") targets = [ "test_shell_init_cli.CallerShellInit.test_clean_callers_source_their_eessi_initializer", @@ -152,14 +199,14 @@ def test_host_lmod_and_site_files_are_isolated_instead_of_skipping(self): "test_list_doctor_cli.ListAndDoctor.test_doctor_without_a_site_file_judges_eb_local_eb", ] script = ( - 'd=$(mktemp -d) && trap \'umount /etc >/dev/null 2>&1 || true; rm -rf "$d"\' EXIT && ' + f'd=$(mktemp -d) && trap \'"{umount_bin}" /etc >/dev/null 2>&1 || true; rm -rf "$d"\' EXIT && ' 'cp -a /etc/. "$d/" 2>/dev/null || true; ' 'mkdir -p "$d/profile.d" "$d/eb-local" && ' 'printf "%s\\n" "module() { return 0; }" > "$d/profile.d/lmod.sh" && ' 'printf "[storage]\\nnode_local_root = \\"/local\\"\\n" > "$d/eb-local/config.toml" && ' 'printf "EB_LOCAL_NODE_LOCAL_ROOT=/local\\n" > "$d/eb-local.conf" && ' 'chmod 0644 "$d/eb-local/config.toml" "$d/eb-local.conf" && ' - 'mount --bind "$d" /etc && ' + f'"{mount_bin}" --bind "$d" /etc && ' 'exec "$@"' ) with tempfile.TemporaryDirectory(prefix="eb-local-host-mask-") as home: @@ -199,6 +246,51 @@ def test_host_lmod_and_site_files_are_isolated_instead_of_skipping(self): self.assertIn(f"Ran {len(targets)} tests", combined) self.assertNotIn("skipped", combined) + def _run_tool_meta_tests(self, extra_env: dict[str, str]): + test_ids = [ + "test_zero_skips.ZeroPythonSkips.test_overlay_real_eb_stack_runs_from_path_with_zero_skips", + "test_zero_skips.ZeroPythonSkips.test_caller_shell_init_runs_with_zsh_and_zero_skips", + "test_zero_skips.ZeroPythonSkips.test_host_lmod_and_site_files_are_isolated_instead_of_skipping", + ] + with tempfile.TemporaryDirectory(prefix="eb-local-empty-path-") as tmp: + empty_bin = Path(tmp) / "bin" + empty_bin.mkdir() + env = { + "PATH": str(empty_bin), + "HOME": tmp, + "PYTHONPATH": str(TESTS_DIR), + "EB_LOCAL_BIN": binary.BIN, + **extra_env, + } + run = subprocess.run( + [sys.executable, "-m", "unittest", "-v", *test_ids], + cwd=ROOT, + env=env, + stdin=subprocess.DEVNULL, + capture_output=True, + text=True, + timeout=30, + ) + return run.returncode, run.stdout + run.stderr + + def test_missing_tools_skip_with_reason_outside_ci(self): + rc, combined = self._run_tool_meta_tests({}) + self.assertEqual(rc, 0, combined) + self.assertIn("Ran 3 tests", combined) + self.assertIn("OK (skipped=3)", combined) + self.assertIn("eb-stack was not found on PATH", combined) + self.assertIn("zsh was not found on PATH", combined) + self.assertIn("unshare was not found on PATH", combined) + + def test_missing_tools_fail_when_require_tools_env_is_set(self): + rc, combined = self._run_tool_meta_tests({REQUIRE_TOOLS_ENV: "1"}) + self.assertNotEqual(rc, 0, combined) + self.assertIn("Ran 3 tests", combined) + self.assertIn("FAILED (failures=3)", combined) + self.assertIn("eb-stack was not found on PATH", combined) + self.assertIn("zsh was not found on PATH", combined) + self.assertIn("unshare was not found on PATH", combined) + if __name__ == "__main__": unittest.main()