team-ljyzipelo

공개
264줄13 KB
1from contextlib import redirect_stdout
2from io import StringIO
3import json
4import os
5from pathlib import Path, PurePosixPath, PureWindowsPath
6import tempfile
7import subprocess
8import sys
9import unittest
10from unittest.mock import ANY, patch
11
12import run_rust
13
14
15TEST_WORK = run_rust.work_directory(
16 run_rust.ROOT, os.environ.get("ZIPELO_CI_JOB", "rust-local")
17)
18
19
20class RunnerTests(unittest.TestCase):
21 def test_job_directory_is_restricted_to_one_work_child(self):
22 for name in ("../escape", "other/job", "", ".hidden", "bad name"):
23 with self.subTest(name=name), self.assertRaises(ValueError):
24 run_rust.work_directory(run_rust.ROOT, name)
25 self.assertEqual(run_rust.work_directory(run_rust.ROOT, os.environ["ZIPELO_CI_JOB"]), TEST_WORK)
26
27 def test_cargo_and_temp_paths_are_kept_in_job_directory(self):
28 for key in ("CARGO_TARGET_DIR", "TMPDIR", "TMP", "TEMP", "ZIPELO_TEST_WORK_DIR"):
29 with self.subTest(key=key):
30 self.assertTrue(Path(os.environ[key]).resolve().is_relative_to(TEST_WORK))
31
32 def test_all_manifests_have_locks(self):
33 paths = run_rust.manifests(run_rust.ROOT)
34 expected = set(run_rust.ROOT.glob("experiments/*/Cargo.toml"))
35 expected |= set(run_rust.ROOT.glob("tests/*/Cargo.toml"))
36 self.assertEqual(set(paths), expected)
37
38 def test_failure_does_not_hide_later_crates(self):
39 paths = [
40 Path("/repo/experiments/a/Cargo.toml"),
41 Path("/repo/experiments/b/Cargo.toml"),
42 ]
43 with (
44 patch.object(run_rust, "manifests", return_value=paths),
45 patch.object(run_rust.subprocess, "run", side_effect=[
46 subprocess.CompletedProcess([], 7),
47 subprocess.CompletedProcess([], 0),
48 ]),
49 ):
50 results = run_rust.run(Path("/repo"), "build")
51 self.assertEqual([row["status"] for row in results], ["failed", "passed"])
52 self.assertEqual(results[0]["exit_code"], 7)
53
54 def test_windows_exclusion_is_explicit_and_only_for_linux_tests(self):
55 paths = [
56 Path("/repo/tests/rust-file-publication/Cargo.toml"),
57 Path("/repo/experiments/rust-file-publication/Cargo.toml"),
58 ]
59 with (
60 patch.object(run_rust, "manifests", return_value=paths),
61 patch.object(run_rust.sys, "platform", "win32"),
62 patch.object(
63 run_rust.subprocess, "run",
64 return_value=subprocess.CompletedProcess([], 0),
65 ) as execute,
66 ):
67 results = run_rust.run(Path("/repo"), "test")
68 self.assertEqual(
69 [row["status"] for row in results], ["not-applicable", "passed"]
70 )
71 self.assertEqual(execute.call_count, 1)
72
73 def test_fixture_bytes_match_git_blobs(self):
74 names = subprocess.check_output(
75 ["git", "ls-files", "-z", "tests/**/fixtures/*"], cwd=run_rust.ROOT
76 ).decode("utf-8").split("\0")
77 names = [name for name in names if name]
78 self.assertTrue(names, "검증할 fixture가 없음")
79 for name in names:
80 with self.subTest(path=name):
81 expected = subprocess.check_output(
82 ["git", "show", f"HEAD:{name}"], cwd=run_rust.ROOT
83 )
84 self.assertEqual((run_rust.ROOT / name).read_bytes(), expected)
85
86 def test_cross_build_never_executes_tests(self):
87 args = run_rust.command(Path("tests/example/Cargo.toml"), "cross-build")
88 self.assertEqual(args[2], "build")
89 self.assertIn(run_rust.WINDOWS_TARGET, args)
90 self.assertNotIn("--test-threads=1", args)
91
92 def test_preflight_failure_is_reported_without_hiding_other_phase(self):
93 with (
94 patch.object(run_rust, "prepare_cross_build", side_effect=ValueError("missing linker")),
95 patch.object(run_rust, "run", return_value=[{"manifest": "sample", "status": "passed"}]),
96 ):
97 results = run_rust.execute_phases(Path("/repo"), ["build", "cross-build"])
98 self.assertEqual([row["status"] for row in results], ["passed", "failed"])
99 self.assertEqual(results[1]["reason"], "missing linker")
100
101 def test_self_check_failure_replaces_previous_success_report(self):
102 for phase, platform in (("local", "linux"), ("test", "win32")):
103 with self.subTest(phase=phase), tempfile.TemporaryDirectory(
104 dir=TEST_WORK
105 ) as directory:
106 root = Path(directory)
107 (root / "work/tmp").mkdir(parents=True)
108 report_path = root / f"work/{phase}-results.json"
109 with (
110 patch.object(run_rust, "ROOT", root),
111 patch.object(run_rust, "work_directory", return_value=root / "work"),
112 patch.object(run_rust, "prepare_tools"),
113 patch.object(run_rust, "prepare_local_tools"),
114 patch.object(run_rust.sys, "argv", ["run_rust.py", phase]),
115 patch.object(run_rust.sys, "platform", platform),
116 patch.dict(run_rust.os.environ, {"GITHUB_STEP_SUMMARY": ""}),
117 patch.object(run_rust.subprocess, "run", side_effect=[
118 subprocess.CompletedProcess([], 0),
119 subprocess.CompletedProcess([], 7),
120 ]),
121 patch.object(run_rust.subprocess, "check_output", side_effect=[
122 "successful-head\n", "failed-head\n",
123 ]),
124 patch.object(run_rust, "execute_phases", return_value=[
125 {"phase": "test", "manifest": "sample", "status": "passed"},
126 ]) as execute,
127 redirect_stdout(StringIO()),
128 ):
129 self.assertEqual(run_rust.main(), 0)
130 successful = json.loads(report_path.read_text(encoding="utf-8"))
131 self.assertEqual(successful["results"][0]["status"], "passed")
132 self.assertEqual(run_rust.main(), 7)
133 failed = json.loads(report_path.read_text(encoding="utf-8"))
134 self.assertEqual(failed["head"], "failed-head")
135 self.assertEqual(failed["results"], [{
136 "phase": "self-check",
137 "manifest": "tests/ci",
138 "status": "failed",
139 "exit_code": 7,
140 }])
141 self.assertFalse(failed["windows_runtime_tested"])
142 execute.assert_called_once()
143
144 @unittest.skipUnless(sys.platform == "linux", "Linux MinGW 실행 파일 탐색 시험임")
145 def test_linked_worktree_finds_setup_tools_without_path_export(self):
146 with tempfile.TemporaryDirectory(dir=TEST_WORK) as directory:
147 base = Path(directory)
148 root = base / "linked"
149 tools = base / "tests/ci/tools"
150 binaries = tools / "mingw/usr/bin"
151 binaries.mkdir(parents=True)
152 for name in ("gcc", "g++"):
153 executable = binaries / f"x86_64-w64-mingw32-{name}-posix"
154 executable.write_text("#!/bin/sh\nexit 0\n")
155 executable.chmod(0o700)
156 rustup = tools / "rustup"
157 (rustup / "toolchains/1.99.1-x86_64-unknown-linux-gnu").mkdir(parents=True)
158 with (
159 patch.dict(run_rust.os.environ, {"PATH": ""}, clear=True),
160 patch.object(run_rust.subprocess, "check_output", return_value=str(base / ".git")),
161 ):
162 run_rust.prepare_tools(root)
163 run_rust.prepare_cross_build()
164 self.assertEqual(run_rust.os.environ["RUSTUP_HOME"], str(rustup))
165 self.assertEqual(
166 run_rust.os.environ["CARGO_TARGET_X86_64_PC_WINDOWS_GNU_LINKER"],
167 str(binaries / "x86_64-w64-mingw32-gcc-posix"),
168 )
169
170 def test_local_bootstraps_missing_cross_tools_and_reloads_paths(self):
171 for root in (Path("/repo"), PurePosixPath("/repo"), PureWindowsPath("C:/repo")):
172 with self.subTest(root=root):
173 with (
174 patch.object(run_rust, "prepare_cross_build", side_effect=[ValueError("missing"), None]) as detect,
175 patch.object(run_rust.subprocess, "run") as setup,
176 patch.object(run_rust, "prepare_tools") as reload,
177 ):
178 run_rust.prepare_local_tools(root)
179 setup.assert_called_once_with(
180 ["bash", str(root / "tests/ci/setup_windows.sh")], cwd=root, env=ANY, check=True,
181 )
182 reload.assert_called_once_with(root)
183 self.assertEqual(detect.call_count, 2)
184
185 def test_existing_cross_tools_do_not_trigger_downloads(self):
186 with (
187 patch.object(run_rust, "prepare_cross_build"),
188 patch.object(run_rust.subprocess, "run") as setup,
189 ):
190 run_rust.prepare_local_tools(Path("/repo"))
191 setup.assert_not_called()
192
193 def test_explicit_tools_directory_does_not_fall_back_to_other_worktree(self):
194 with (
195 patch.dict(run_rust.os.environ, {"PATH": "", "ZIPELO_CI_TOOLS": "/missing"}, clear=True),
196 patch.object(run_rust.subprocess, "check_output") as git,
197 ):
198 run_rust.prepare_tools(Path("/repo"))
199 git.assert_not_called()
200 with self.assertRaisesRegex(ValueError, "setup_windows.sh"):
201 run_rust.prepare_cross_build()
202
203 @unittest.skipUnless(sys.platform == "linux", "Linux 파일시스템과 소유권 검사 시험임")
204 def test_filesystem_skip_is_limited_to_publication_test_crates(self):
205 with tempfile.TemporaryDirectory(dir=TEST_WORK) as directory:
206 root = Path(directory)
207 work = root / "work"
208 work.mkdir()
209 paths = [
210 root / "tests/rust-file-publication/Cargo.toml",
211 root / "tests/rust-gzip-file-job/Cargo.toml",
212 root / "tests/rust-file-snapshot/Cargo.toml",
213 root / "experiments/rust-file-publication/Cargo.toml",
214 ]
215 with (
216 patch.object(run_rust, "manifests", return_value=paths),
217 patch.dict(run_rust.os.environ, {"ZIPELO_TEST_WORK_DIR": str(work)}),
218 patch.object(run_rust.subprocess, "check_output", return_value="1021994\n"),
219 patch.object(run_rust.subprocess, "run", return_value=subprocess.CompletedProcess([], 0)) as cargo,
220 ):
221 results = run_rust.run(root, "test")
222 self.assertEqual([row["status"] for row in results], [
223 "not-applicable", "not-applicable", "passed", "passed",
224 ])
225 self.assertIn("0x1021994", results[0]["reason"])
226 self.assertEqual(cargo.call_count, 2)
227 cargo.reset_mock()
228 results = run_rust.run(root, "cross-build")
229 self.assertTrue(all(row["status"] == "passed" for row in results))
230 self.assertEqual(cargo.call_count, 4)
231
232 @unittest.skipUnless(sys.platform == "linux", "Linux 소켓 경로 회귀 시험임")
233 def test_long_checkout_does_not_block_file_fixtures(self):
234 with tempfile.TemporaryDirectory(dir=TEST_WORK) as directory:
235 root = Path(directory) / ("long-checkout-" * 10)
236 work = Path(directory) / "work"
237 socket = root / "tests/rust-file-publication/runs/case-1-1/parent/output"
238 self.assertGreaterEqual(len(run_rust.os.fsencode(socket)), 108)
239 with (
240 patch.dict(run_rust.os.environ, {"ZIPELO_TEST_WORK_DIR": str(work)}),
241 patch.object(run_rust.subprocess, "check_output", return_value="ef53\n"),
242 ):
243 self.assertEqual(run_rust.prepare_fixtures(root), {})
244 for name in run_rust.FILE_FIXTURES:
245 self.assertEqual(list((work / "fixtures" / name).iterdir()), [])
246 self.assertFalse((root / "tests" / name / "runs").exists())
247
248 def test_unknown_filesystem_probe_error_is_failure(self):
249 with (
250 patch.object(run_rust, "prepare_fixtures", side_effect=subprocess.CalledProcessError(1, "stat")),
251 patch.object(run_rust.sys, "platform", "linux"),
252 ):
253 results = run_rust.execute_phases(run_rust.ROOT, ["test"])
254 self.assertEqual(results[0]["status"], "failed")
255
256 def test_cargo_contract(self):
257 args = run_rust.command(Path("tests/example/Cargo.toml"), "test")
258 self.assertIn("--locked", args)
259 self.assertIn("--all-features", args)
260 self.assertEqual(args[-2:], ["--", "--test-threads=1"])
261
262
263if __name__ == "__main__":
264 unittest.main()