-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_install_base_python_locks.py
More file actions
453 lines (390 loc) · 14 KB
/
Copy pathtest_install_base_python_locks.py
File metadata and controls
453 lines (390 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
"""Regression tests for trusted base Python lock installation."""
from __future__ import annotations
import io
import json
import pathlib
import subprocess
import pytest
from scripts.ci import install_base_python_locks as installer
def write_candidate(
root: pathlib.Path,
*,
generated_file: str,
source: str,
content: str = "demo==1 --hash=sha256:" + ("a" * 64) + "\n",
) -> None:
"""Append one manifest entry and write its materialized lock."""
manifest_path = root / "manifest.json"
manifest = (
json.loads(manifest_path.read_text(encoding="utf-8"))
if manifest_path.exists()
else []
)
manifest.append({"file": generated_file, "source": source})
manifest_path.write_text(json.dumps(manifest), encoding="utf-8")
(root / generated_file).write_text(content, encoding="utf-8")
def test_recovers_partial_supplement_with_same_directory_lock(tmp_path) -> None:
"""An optional supplement can join its sibling lock without widening scope."""
write_candidate(
tmp_path,
generated_file="requirements-000.txt",
source="backend/requirements-agent.txt",
)
write_candidate(
tmp_path,
generated_file="requirements-001.txt",
source="backend/requirements-hashes.txt",
)
commands: list[list[str]] = []
def fake_runner(command: list[str], **kwargs):
commands.append(command)
requirements = [
command[index + 1]
for index, argument in enumerate(command)
if argument == "-r"
]
if (
"--dry-run" in command
and len(requirements) == 1
and requirements[0].endswith("requirements-000.txt")
):
return subprocess.CompletedProcess(
command,
1,
stdout=(
"ERROR: In --require-hashes mode, all requirements must have "
"their versions pinned with ==: httpx>=0.27"
),
)
return subprocess.CompletedProcess(command, 0, stdout="")
stdout = io.StringIO()
stderr = io.StringIO()
result = installer.install_materialized_locks(
tmp_path,
runner=fake_runner,
stdout=stdout,
stderr=stderr,
)
assert result == 0
assert len(commands) == 4
assert "--dry-run" in commands[0]
assert "--ignore-installed" in commands[0]
assert "--dry-run" in commands[1]
assert "--ignore-installed" in commands[1]
assert "--dry-run" in commands[2]
assert commands[2].count("-r") == 2
assert "--dry-run" not in commands[3]
assert commands[3].count("-r") == 2
assert stderr.getvalue() == ""
assert "Recovered trusted base Python supplement" in stdout.getvalue()
assert "candidates=2 installed=2 skipped=0" in stdout.getvalue()
def test_skips_partial_candidate_without_completing_sibling(tmp_path) -> None:
"""An unrecoverable hash-bearing supplement remains visible and non-fatal."""
write_candidate(
tmp_path,
generated_file="requirements-000.txt",
source="backend/requirements-agent.txt",
)
def fake_runner(command: list[str], **kwargs):
return subprocess.CompletedProcess(
command,
1,
stdout=(
"ERROR: In --require-hashes mode, all requirements must have "
"their versions pinned with ==: httpx>=0.27"
),
)
stdout = io.StringIO()
stderr = io.StringIO()
result = installer.install_materialized_locks(
tmp_path,
runner=fake_runner,
stdout=stdout,
stderr=stderr,
)
assert result == 0
assert "requirements-agent.txt" in stderr.getvalue()
assert "httpx>=0.27" in stderr.getvalue()
assert "candidates=1 installed=0 skipped=1" in stdout.getvalue()
def test_failed_same_directory_group_still_skips_partial_candidates(tmp_path) -> None:
"""A sibling group that remains incomplete cannot become an install plan."""
write_candidate(
tmp_path,
generated_file="requirements-000.txt",
source="backend/requirements-agent.txt",
)
write_candidate(
tmp_path,
generated_file="requirements-001.txt",
source="backend/requirements-extra.txt",
)
commands: list[list[str]] = []
def fake_runner(command: list[str], **kwargs):
commands.append(command)
return subprocess.CompletedProcess(
command,
1,
stdout=(
"ERROR: In --require-hashes mode, all requirements must have "
"their versions pinned with ==: httpx>=0.27"
),
)
stdout = io.StringIO()
stderr = io.StringIO()
result = installer.install_materialized_locks(
tmp_path,
runner=fake_runner,
stdout=stdout,
stderr=stderr,
)
assert result == 0
assert len(commands) == 3
assert commands[-1].count("-r") == 2
assert "installed=0 skipped=2" in stdout.getvalue()
assert stderr.getvalue().count("httpx>=0.27") == 2
def test_does_not_combine_multiple_independent_root_environments(tmp_path) -> None:
"""Independent root locks must not become one conflicting recovery closure."""
for index, source in enumerate(
(
"requirements-opencode-review-ci.txt",
"requirements-security-ci.txt",
"requirements-security-tools.txt",
"requirements.lock",
)
):
write_candidate(
tmp_path,
generated_file=f"requirements-{index:03d}.txt",
source=source,
)
commands: list[list[str]] = []
def fake_runner(command: list[str], **kwargs):
"""Make two environments complete and two independently incomplete."""
commands.append(command)
requirements = [
command[index + 1]
for index, argument in enumerate(command)
if argument == "-r"
]
if len(requirements) != 1:
raise AssertionError("independent root environments were combined")
if "--dry-run" in command and requirements[0].endswith(
("requirements-001.txt", "requirements-002.txt")
):
return subprocess.CompletedProcess(
command,
1,
stdout=(
"ERROR: In --require-hashes mode, all requirements must have "
"their versions pinned with ==: pip"
),
)
return subprocess.CompletedProcess(command, 0, stdout="")
stdout = io.StringIO()
stderr = io.StringIO()
result = installer.install_materialized_locks(
tmp_path,
runner=fake_runner,
stdout=stdout,
stderr=stderr,
)
assert result == 0
assert len(commands) == 6
assert "candidates=4 installed=2 skipped=2" in stdout.getvalue()
assert stderr.getvalue().count("pip") == 2
@pytest.mark.parametrize(
"failure_output",
[
"",
("ERROR: THESE PACKAGES DO NOT MATCH THE HASHES FROM THE REQUIREMENTS FILE"),
"WARNING: Retrying after connection broken by ConnectionError",
"ERROR: Could not fetch URL https://pypi.org/simple/demo/",
"pip resolver crashed without a classified dependency error",
],
)
def test_unclassified_preflight_failure_is_fatal(tmp_path, failure_output: str) -> None:
"""Hash, network, empty, and unknown preflight failures fail closed."""
write_candidate(
tmp_path,
generated_file="requirements-000.txt",
source="requirements-hashes.txt",
)
def fake_runner(command: list[str], **kwargs):
return subprocess.CompletedProcess(command, 23, stdout=failure_output)
stderr = io.StringIO()
result = installer.install_materialized_locks(
tmp_path,
runner=fake_runner,
stderr=stderr,
)
assert result == 23
assert "only incomplete hash closures" in stderr.getvalue()
assert "requirements-hashes.txt" in stderr.getvalue()
if failure_output:
assert failure_output in stderr.getvalue()
def test_explicit_python_incompatibility_is_visible_and_nonfatal(tmp_path) -> None:
"""A base lock for another interpreter may defer to coverage execution."""
write_candidate(
tmp_path,
generated_file="requirements-000.txt",
source="requirements-hashes.txt",
)
def fake_runner(command: list[str], **kwargs):
return subprocess.CompletedProcess(
command,
1,
stdout=(
"ERROR: Package 'demo' requires a different Python: "
"3.14.0 not in '<3.14,>=3.10'"
),
)
stdout = io.StringIO()
stderr = io.StringIO()
result = installer.install_materialized_locks(
tmp_path,
runner=fake_runner,
stdout=stdout,
stderr=stderr,
)
assert result == 0
assert "requires a different Python" in stderr.getvalue()
assert "candidates=1 installed=0 skipped=1" in stdout.getvalue()
def test_fatal_same_directory_group_failure_aborts(tmp_path) -> None:
"""A group cannot turn a registry or integrity failure into a skip."""
write_candidate(
tmp_path,
generated_file="requirements-000.txt",
source="backend/requirements-agent.txt",
)
write_candidate(
tmp_path,
generated_file="requirements-001.txt",
source="backend/requirements-hashes.txt",
)
call_count = 0
def fake_runner(command: list[str], **kwargs):
nonlocal call_count
call_count += 1
if call_count <= 2:
return subprocess.CompletedProcess(
command,
1,
stdout=(
"ERROR: In --require-hashes mode, all requirements must have "
"their versions pinned with ==: httpx>=0.27"
),
)
return subprocess.CompletedProcess(
command,
29,
stdout="ERROR: Could not fetch URL https://pypi.org/simple/httpx/",
)
stderr = io.StringIO()
result = installer.install_materialized_locks(
tmp_path,
runner=fake_runner,
stderr=stderr,
)
assert result == 29
assert call_count == 3
assert "Could not fetch URL" in stderr.getvalue()
@pytest.mark.parametrize(
("manifest_text", "candidate_files", "error"),
[
(None, (), "regular non-symlink"),
("{not-json", (), "manifest is invalid"),
("{}", (), "must be a JSON array"),
("[1]", (), "entries must be objects"),
(
'[{"file":"requirements-000.txt","source":"/absolute.txt"}]',
("requirements-000.txt",),
"unsafe source path",
),
(
(
'[{"file":"requirements-000.txt","source":"one.txt"},'
'{"file":"requirements-000.txt","source":"two.txt"}]'
),
("requirements-000.txt",),
"duplicate file names",
),
(
'[{"file":"requirements-000.txt","source":"missing.txt"}]',
(),
"must be a regular file",
),
],
)
def test_manifest_validation_failures(
tmp_path,
manifest_text: str | None,
candidate_files: tuple[str, ...],
error: str,
) -> None:
"""Malformed trusted materializer output fails before any pip command."""
if manifest_text is not None:
(tmp_path / "manifest.json").write_text(manifest_text, encoding="utf-8")
for candidate_file in candidate_files:
(tmp_path / candidate_file).write_text("lock", encoding="utf-8")
with pytest.raises(ValueError, match=error):
installer._manifest_entries(tmp_path)
def test_bounded_failure_output_preserves_root_and_tail() -> None:
"""Long resolver logs retain their leading context and final root cause."""
output = "\n".join(f"line-{index}" for index in range(150))
bounded = installer._bounded_failure_output(output)
assert bounded.splitlines()[0] == "line-0"
assert "30 dependency-resolution log lines omitted" in bounded
assert bounded.splitlines()[-1] == "line-149"
def test_rejects_unsafe_manifest_before_running_pip(tmp_path) -> None:
"""Generated and source paths must remain inside trusted materializer output."""
(tmp_path / "manifest.json").write_text(
json.dumps([{"file": "../escape.txt", "source": "/absolute/lock.txt"}]),
encoding="utf-8",
)
called = False
def fake_runner(command: list[str], **kwargs):
nonlocal called
called = True
return subprocess.CompletedProcess(command, 0, stdout="")
stderr = io.StringIO()
result = installer.install_materialized_locks(
tmp_path,
runner=fake_runner,
stderr=stderr,
)
assert result == 2
assert not called
assert "unsafe file name" in stderr.getvalue()
def test_install_failure_after_successful_preflight_is_fatal(tmp_path) -> None:
"""A registry or hash race after preflight must fail the image build."""
write_candidate(
tmp_path,
generated_file="requirements-000.txt",
source="requirements-hashes.txt",
)
call_count = 0
def fake_runner(command: list[str], **kwargs):
nonlocal call_count
call_count += 1
return subprocess.CompletedProcess(
command,
0 if call_count == 1 else 19,
stdout="",
)
stderr = io.StringIO()
result = installer.install_materialized_locks(
tmp_path,
runner=fake_runner,
stderr=stderr,
)
assert result == 19
assert "failed during installation" in stderr.getvalue()
def test_main_forwards_requirements_root(monkeypatch, tmp_path) -> None:
"""The CLI delegates the exact requirements root to the installer."""
seen: list[pathlib.Path] = []
def fake_install(root: pathlib.Path) -> int:
seen.append(root)
return 7
monkeypatch.setattr(installer, "install_materialized_locks", fake_install)
assert installer.main(["--requirements-root", str(tmp_path)]) == 7
assert seen == [tmp_path]