team-ljyzipelo

공개
169줄6.9 KB
1import contextlib
2import importlib.util
3import io
4from pathlib import Path
5import subprocess
6import unittest
7from unittest.mock import patch
8
9
10ROOT = Path(__file__).resolve().parents[2]
11SPEC = importlib.util.spec_from_file_location("zipelo_runner_under_test", ROOT / "tests/run_checks.py")
12RUNNER = importlib.util.module_from_spec(SPEC)
13SPEC.loader.exec_module(RUNNER)
14
15
16class RunnerTests(unittest.TestCase):
17 def invoke(self, run, runtime=False):
18 with (
19 patch.object(RUNNER.shutil, "which", return_value="dotnet"),
20 patch.object(RUNNER.subprocess, "run", side_effect=run),
21 contextlib.redirect_stdout(io.StringIO()),
22 ):
23 return RUNNER.runtime_checks() if runtime else RUNNER.core_checks()
24
25 def create_assembly(self, command):
26 directory = Path(command[command.index("--output") + 1])
27 directory.mkdir()
28 (directory / "Zipelo.SecurityTests.dll").write_bytes(b"runner-test-only")
29
30 def test_build_options_are_separate_from_two_application_arguments(self):
31 calls = []
32
33 def run(command, **options):
34 calls.append((command, options))
35 sandbox = options["cwd"]
36 self.assertTrue(sandbox.is_relative_to(RUNNER.SECURITY / ".work"))
37 self.assertTrue(sandbox.is_dir())
38 self.assertFalse(options["check"])
39 for variable in (
40 "DOTNET_CLI_HOME", "NUGET_PACKAGES", "NUGET_HTTP_CACHE_PATH", "TMPDIR", "TMP", "TEMP"
41 ):
42 self.assertTrue(Path(options["env"][variable]).is_relative_to(sandbox))
43 if len(calls) == 1:
44 self.assertEqual(command[:3], [
45 "dotnet", "build", str(RUNNER.SECURITY / "Zipelo.SecurityTests.csproj")
46 ])
47 self.assertEqual(command[command.index("--configfile") + 1],
48 str(RUNNER.SECURITY / "NuGet.Config"))
49 self.assertTrue(Path(command[command.index("--artifacts-path") + 1]).is_relative_to(sandbox))
50 self.assertEqual(options["timeout"], RUNNER.BUILD_TIMEOUT)
51 self.create_assembly(command)
52 else:
53 self.assertEqual(command, [
54 "dotnet", str(sandbox / "bin/Zipelo.SecurityTests.dll"),
55 str(ROOT), str(sandbox),
56 ])
57 self.assertEqual(options["timeout"], RUNNER.CORE_TIMEOUT)
58 return subprocess.CompletedProcess(command, 0)
59
60 self.assertEqual(self.invoke(run), 0)
61 self.assertEqual(len(calls), 2)
62 self.assertFalse(calls[0][1]["cwd"].exists())
63
64 def test_runtime_builds_worker_and_keeps_only_two_app_arguments(self):
65 calls = []
66
67 def run(command, **options):
68 calls.append(command)
69 sandbox = options["cwd"]
70 if len(calls) == 1:
71 self.assertEqual(command[2], str(ROOT / "tests/runtime/Zipelo.RuntimeTests.csproj"))
72 self.assertEqual(command[command.index("--configfile") + 1], str(RUNNER.SECURITY / "NuGet.Config"))
73 directory = Path(command[command.index("--output") + 1])
74 self.assertTrue(directory.is_relative_to(sandbox))
75 directory.mkdir()
76 for name in ("Zipelo.RuntimeTests.dll", "Zipelo.Worker.dll", "Zipelo.Worker.deps.json",
77 "Zipelo.Worker.runtimeconfig.json"):
78 (directory / name).write_bytes(b"runner-test-only")
79 else:
80 self.assertEqual(command, ["dotnet", str(sandbox / "bin/Zipelo.RuntimeTests.dll"), str(ROOT), str(sandbox)])
81 self.assertEqual(options["timeout"], 120)
82 return subprocess.CompletedProcess(command, 0)
83
84 self.assertEqual(self.invoke(run, runtime=True), 0)
85 self.assertEqual(len(calls), 2)
86
87 def test_runtime_missing_worker_stops_before_application_launch(self):
88 calls = []
89
90 def run(command, **options):
91 calls.append(command)
92 directory = Path(command[command.index("--output") + 1])
93 directory.mkdir()
94 (directory / "Zipelo.RuntimeTests.dll").write_bytes(b"runner-test-only")
95 return subprocess.CompletedProcess(command, 0)
96
97 self.assertEqual(self.invoke(run, runtime=True), 2)
98 self.assertEqual(len(calls), 1)
99
100 def test_build_failure_stops_before_application_launch(self):
101 calls = []
102
103 def run(command, **options):
104 calls.append((command, options["cwd"]))
105 return subprocess.CompletedProcess(command, 17)
106
107 self.assertEqual(self.invoke(run), 17)
108 self.assertEqual(len(calls), 1)
109 self.assertFalse(calls[0][1].exists())
110
111 def test_application_failure_is_preserved(self):
112 calls = []
113
114 def run(command, **options):
115 calls.append(command)
116 if len(calls) == 1:
117 self.create_assembly(command)
118 return subprocess.CompletedProcess(command, 0 if len(calls) == 1 else 9)
119
120 self.assertEqual(self.invoke(run), 9)
121 self.assertEqual(len(calls), 2)
122
123 def test_missing_assembly_is_not_reported_as_success(self):
124 calls = []
125
126 def run(command, **options):
127 calls.append(command)
128 return subprocess.CompletedProcess(command, 0)
129
130 self.assertEqual(self.invoke(run), 2)
131 self.assertEqual(len(calls), 1)
132
133 def test_timeouts_fail_and_remove_owned_sandbox(self):
134 for stage in (1, 2):
135 with self.subTest(stage=stage):
136 calls = []
137
138 def run(command, **options):
139 calls.append(options["cwd"])
140 if len(calls) == stage:
141 raise subprocess.TimeoutExpired(command, options["timeout"])
142 self.create_assembly(command)
143 return subprocess.CompletedProcess(command, 0)
144
145 self.assertEqual(self.invoke(run), 124)
146 self.assertEqual(len(calls), stage)
147 self.assertFalse(calls[0].exists())
148
149 def test_missing_sdk_does_not_launch_commands(self):
150 with (
151 patch.object(RUNNER.shutil, "which", return_value=None),
152 patch.object(RUNNER.subprocess, "run") as run,
153 contextlib.redirect_stdout(io.StringIO()),
154 ):
155 self.assertEqual(RUNNER.core_checks(), 2)
156 run.assert_not_called()
157
158 def test_outside_or_linked_sandbox_is_rejected(self):
159 work = RUNNER.SECURITY / ".work"
160 for outside in (ROOT / "tests/security/unowned", work / "../escape"):
161 with self.subTest(path=outside), self.assertRaises(ValueError):
162 RUNNER.check_work_path(outside)
163 with patch.object(Path, "is_symlink", autospec=True, side_effect=lambda path: path == work):
164 with self.assertRaises(ValueError):
165 RUNNER.check_work_path(work / "run-test")
166
167
168if __name__ == "__main__":
169 unittest.main()