Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/zig.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,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: |
Expand Down
1 change: 1 addition & 0 deletions changelog.d/+zero-skips-strict-env.misc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Require `zsh`, `eb-stack`, and `unshare` in `ZeroPythonSkips` when `EB_LOCAL_REQUIRE_TOOLS` is set, and skip with a reason otherwise.
108 changes: 100 additions & 8 deletions tests/test_zero_skips.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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"),
Expand All @@ -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",
Expand All @@ -154,14 +201,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:
Expand Down Expand Up @@ -201,6 +248,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)


class LiveEessiOutsideZeroSkipDiscovery(unittest.TestCase):
"""`tests/site` stays out of discovery, and the live file fails closed."""
Expand Down
Loading