"""Exercise the actual pre-checkout and always-run cleanup workflow bodies.""" import contextlib import io import os from pathlib import Path import shutil import tempfile import textwrap import unittest from unittest.mock import patch ROOT = Path(__file__).resolve().parents[3] WORKFLOWS = sorted((ROOT / ".github/workflows").glob("block-*.yml")) def step(text, name): marker = " - name: " + name + "\n" if text.count(marker) != 1: raise AssertionError("missing or duplicate " + name) return text.split(marker, 1)[1].split("\n - name:", 1)[0] def code(block): return textwrap.dedent(block.split(" run: |\n", 1)[1]).split("\n", 1)[1].rsplit("\nPY", 1)[0] def execute(source, workspace, temp, free=None): output = io.StringIO() with patch.dict(os.environ, {"GITHUB_WORKSPACE": str(workspace), "RUNNER_TEMP": str(temp)}), contextlib.redirect_stdout(output): with contextlib.ExitStack() as stack: if free is not None: stack.enter_context(patch("shutil.disk_usage", return_value=shutil._ntuple_diskusage(100_000_000_000, 0, free))) try: exec(compile(source, "", "exec"), {}) except SystemExit as err: return err.code, output.getvalue() return 0, output.getvalue() class RunnerWorkspaceTests(unittest.TestCase): def test_every_job_checks_before_checkout_and_cleans_last_even_on_failure(self): for path in WORKFLOWS: text = path.read_text() before = step(text, "Runner free space") after = step(text, "Clean job workspace") self.assertLess(text.index("- name: Runner free space"), text.index("- name: Checkout"), path.name) self.assertIn("if: always() && runner.environment == 'self-hosted'", after) self.assertNotIn("continue-on-error", after) self.assertNotIn("- name:", text.split("- name: Clean job workspace", 1)[1]) self.assertIn("working-directory: ${{ runner.temp }}", before) self.assertIn("working-directory: ${{ runner.temp }}", after) self.assertIn("python3 -I -", before) self.assertIn("python3 -I -", after) def test_low_space_refuses_and_boundary_admits(self): for path in WORKFLOWS: source = code(step(path.read_text(), "Runner free space")) with tempfile.TemporaryDirectory() as temp: root = Path(temp) rc, out = execute(source, root, root, 9_999_999_999) self.assertEqual(rc, 75) self.assertIn("runner_disk_low", out) self.assertIn("required_bytes=10000000000", out) self.assertEqual(execute(source, root, root, 10_000_000_000)[0], 0) with patch("shutil.disk_usage", side_effect=[shutil._ntuple_diskusage(100, 0, 10_000_000_000), shutil._ntuple_diskusage(100, 0, 1)]): rc, out = execute(source, root / "work", root / "temp") self.assertEqual(rc, 75) self.assertIn("runner_disk_low", out) with patch("shutil.disk_usage", side_effect=OSError("probe failed")): rc, out = execute(source, root, root) self.assertEqual(rc, 75) self.assertIn("runner_disk_check_failed", out) def test_cleanup_removes_hidden_and_build_files_but_not_symlink_targets(self): for path in WORKFLOWS: source = code(step(path.read_text(), "Clean job workspace")) with tempfile.TemporaryDirectory() as temp: root = Path(temp); work = root / "repo/repo"; work.mkdir(parents=True) (work / ".git").mkdir(); (work / ".git/config").write_text("config") (work / "binary").write_bytes(b"build") outside = root / "outside"; outside.mkdir(); (outside / "keep").write_text("keep") (work / "link").symlink_to(outside, target_is_directory=True) rc, out = execute(source, work, root) self.assertEqual(rc, 0, out) self.assertEqual(list(work.iterdir()), []) self.assertTrue((outside / "keep").is_file()) self.assertEqual(execute(source, root, root)[0], 1) self.assertEqual(execute(source, Path(root.anchor), root)[0], 1) alias = root / "alias"; alias.symlink_to(work, target_is_directory=True) self.assertEqual(execute(source, alias, root)[0], 1) def test_cleanup_refuses_symlinked_ancestors_and_preserves_external_sentinel(self): for path in WORKFLOWS: with self.subTest(workflow=path.name), tempfile.TemporaryDirectory() as temp: base = Path(temp) external = base / "external/repo/repo"; external.mkdir(parents=True) sentinel = external / "keep"; sentinel.write_text("outside checkout") alias = base / "alias"; alias.symlink_to(base / "external", target_is_directory=True) source = code(step(path.read_text(), "Clean job workspace")) rc, out = execute(source, alias / "repo/repo", base) self.assertTrue(sentinel.is_file(), "cleanup followed a workspace ancestor outside the checkout") self.assertEqual(rc, 1, out) self.assertIn("runner_workspace_cleanup_refused", out) if __name__ == "__main__": unittest.main()