-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathtest_scripts_run_task.py
More file actions
686 lines (585 loc) · 19.6 KB
/
test_scripts_run_task.py
File metadata and controls
686 lines (585 loc) · 19.6 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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
import functools
import json
import os
import site
import stat
import subprocess
import sys
import tempfile
from argparse import Namespace
from importlib.machinery import SourceFileLoader
from importlib.util import module_from_spec, spec_from_loader
from unittest.mock import Mock
import pytest
import taskgraph
from taskgraph.util.caches import CACHES
from .conftest import nowin
@pytest.fixture(scope="module")
def run_task_mod():
spec = spec_from_loader(
"run-task",
SourceFileLoader(
"run-task",
os.path.join(os.path.dirname(taskgraph.__file__), "run-task", "run-task"),
),
)
assert spec
assert spec.loader
mod = module_from_spec(spec)
spec.loader.exec_module(mod)
return mod
@pytest.fixture
def patch_run_command(monkeypatch, run_task_mod):
called_with = []
def fake_run_command(*args, **kwargs):
called_with.append((args, kwargs))
def inner():
monkeypatch.setattr(run_task_mod, "run_command", fake_run_command)
nonlocal called_with
called_with = []
return called_with
return inner
@pytest.fixture()
def mock_stdin(monkeypatch):
_stdin = Mock(
fileno=lambda: 3,
)
monkeypatch.setattr(
sys,
"stdin",
_stdin,
)
def test_install_pip_requirements(
mocker,
tmp_path,
patch_run_command,
run_task_mod,
):
mocker.patch("shutil.which", return_value=False)
# no requirements
repositories = [{"pip-requirements": None}]
called = patch_run_command()
run_task_mod.install_pip_requirements(repositories)
assert len(called) == 0
# single requirement
req = tmp_path.joinpath("requirements.txt")
req.write_text("taskcluster-taskgraph==1.0.0")
repositories = [{"pip-requirements": str(req)}]
called = patch_run_command()
run_task_mod.install_pip_requirements(repositories)
assert len(called) == 1
assert called[0][0] == (
b"pip-install",
[
sys.executable,
"-mpip",
"install",
"--user",
"--break-system-packages",
"--require-hashes",
"-r",
str(req),
],
)
# two requirements
req2 = tmp_path.joinpath("requirements2.txt")
req2.write_text("redo")
repositories.append({"pip-requirements": str(req2)})
called = patch_run_command()
run_task_mod.install_pip_requirements(repositories)
assert len(called) == 1
assert called[0][0] == (
b"pip-install",
[
sys.executable,
"-mpip",
"install",
"--user",
"--break-system-packages",
"--require-hashes",
"-r",
str(req),
"-r",
str(req2),
],
)
def test_install_pip_requirements_with_uv(
mocker,
tmp_path,
patch_run_command,
run_task_mod,
):
mocker.patch("shutil.which", return_value=True)
req = tmp_path.joinpath("requirements.txt")
req.write_text("taskcluster-taskgraph==1.0.0")
repositories = [{"pip-requirements": str(req)}]
called = patch_run_command()
run_task_mod.install_pip_requirements(repositories)
assert len(called) == 1
assert called[0][0] == (
b"pip-install",
[
"uv",
"pip",
"install",
"--python",
sys.executable,
"--prefix",
site.getuserbase(),
"--require-hashes",
"-r",
str(req),
],
)
@pytest.mark.parametrize(
"args,env,extra_expected",
[
pytest.param(
{},
{
"REPOSITORY_TYPE": "hg",
"BASE_REPOSITORY": "https://hg.mozilla.org/mozilla-central",
"HEAD_REPOSITORY": "https://hg.mozilla.org/mozilla-central",
"HEAD_REV": "abcdef",
"PIP_REQUIREMENTS": "taskcluster/requirements.txt",
},
{
"base-repo": "https://hg.mozilla.org/mozilla-unified",
},
id="hg",
),
pytest.param(
{"myrepo_shallow_clone": True},
{
"REPOSITORY_TYPE": "git",
"HEAD_REPOSITORY": "https://github.com/test/repo.git",
"HEAD_REV": "abc123",
},
{"shallow-clone": True},
id="git_with_shallow_clone",
),
pytest.param(
{},
{
"REPOSITORY_TYPE": "git",
"HEAD_REPOSITORY": "https://github.com/example/repo",
"HEAD_REV": "abc123",
"EXTRA_REFS": json.dumps(["refs/notes/taskgraph", "refs/notes/other"]),
},
{"extra-refs": ["refs/notes/taskgraph", "refs/notes/other"]},
id="git_with_extra_refs",
),
],
)
def test_collect_vcs_options(
monkeypatch,
run_task_mod,
args,
env,
extra_expected,
):
name = "myrepo"
checkout = "checkout"
monkeypatch.setattr(os, "environ", {})
for k, v in env.items():
monkeypatch.setenv(f"{name.upper()}_{k.upper()}", v)
args.setdefault(f"{name}_checkout", checkout)
args.setdefault(f"{name}_shallow_clone", False)
args = Namespace(**args)
result = run_task_mod.collect_vcs_options(args, name, name)
expected = {
"base-repo": env.get("BASE_REPOSITORY"),
"base-rev": env.get("BASE_REV"),
"checkout": os.path.join(os.getcwd(), "checkout"),
"env-prefix": name.upper(),
"head-repo": env.get("HEAD_REPOSITORY"),
"name": name,
"pip-requirements": None,
"project": name,
"head-ref": env.get("HEAD_REF"),
"head-rev": env.get("HEAD_REV"),
"repo-type": env.get("REPOSITORY_TYPE"),
"shallow-clone": False,
"ssh-secret-name": env.get("SSH_SECRET_NAME"),
"store-path": env.get("HG_STORE_PATH"),
"extra-refs": None,
}
if "PIP_REQUIREMENTS" in env:
expected["pip-requirements"] = os.path.join(
expected["checkout"], env.get("PIP_REQUIREMENTS")
)
expected.update(extra_expected)
assert result == expected
def test_remove_directory(monkeypatch, run_task_mod):
_tempdir = tempfile.TemporaryDirectory()
assert os.path.isdir(_tempdir.name) is True
run_task_mod.remove(_tempdir.name)
assert os.path.isdir(_tempdir.name) is False
def test_remove_closed_file(monkeypatch, run_task_mod):
_tempdir = tempfile.TemporaryDirectory()
_tempfile = tempfile.NamedTemporaryFile(dir=_tempdir.name, delete=False)
_tempfile.write(b"foo")
_tempfile.close()
assert os.path.isdir(_tempdir.name) is True
assert os.path.isfile(_tempfile.name) is True
run_task_mod.remove(_tempdir.name)
assert os.path.isdir(_tempdir.name) is False
assert os.path.isfile(_tempfile.name) is False
def test_remove_readonly_tree(monkeypatch, run_task_mod):
_tempdir = tempfile.TemporaryDirectory()
_mark_readonly(_tempdir.name)
assert os.path.isdir(_tempdir.name) is True
run_task_mod.remove(_tempdir.name)
assert os.path.isdir(_tempdir.name) is False
def test_remove_readonly_file(monkeypatch, run_task_mod):
_tempdir = tempfile.TemporaryDirectory()
_tempfile = tempfile.NamedTemporaryFile(dir=_tempdir.name, delete=False)
_tempfile.write(b"foo")
_tempfile.close()
_mark_readonly(_tempfile.name)
# should change write permission and then remove file
assert os.path.isfile(_tempfile.name) is True
run_task_mod.remove(_tempfile.name)
assert os.path.isfile(_tempfile.name) is False
_tempdir.cleanup()
def _mark_readonly(path):
"""Removes all write permissions from given file/directory.
:param path: path of directory/file of which modes must be changed
"""
mode = os.stat(path)[stat.ST_MODE]
os.chmod(path, mode & ~stat.S_IWUSR & ~stat.S_IWGRP & ~stat.S_IWOTH)
def test_clean_git_checkout(monkeypatch, mock_stdin, run_task_mod):
prefix = "Would remove "
root_dir = tempfile.TemporaryDirectory()
untracked_dir = tempfile.TemporaryDirectory(dir=root_dir.name)
tracked_dir = tempfile.TemporaryDirectory(dir=root_dir.name)
untracked_file = tempfile.NamedTemporaryFile(dir=tracked_dir.name, delete=False)
untracked_file.write(b"untracked")
untracked_file.close()
tracked_file = tempfile.NamedTemporaryFile(dir=tracked_dir.name, delete=False)
tracked_file.write(b"tracked")
tracked_file.close()
root_dir_prefix = root_dir.name + "/"
untracked_dir_rel_path = untracked_dir.name[len(root_dir_prefix) :]
untracked_file_rel_path = untracked_file.name[len(root_dir_prefix) :]
output_str = (
f"{prefix}{untracked_dir_rel_path}/\n{prefix}{untracked_file_rel_path}\n"
)
real_popen = subprocess.Popen
def _Popen(
args,
**kwargs,
):
kwargs["stdin"] = subprocess.PIPE
proc = real_popen(
["cat"],
**kwargs,
)
proc.stdin.write(output_str)
return proc
monkeypatch.setattr(
subprocess,
"Popen",
_Popen,
)
assert os.path.isdir(root_dir.name) is True
assert os.path.isdir(tracked_dir.name) is True
assert os.path.isdir(untracked_dir.name) is True
assert os.path.isfile(untracked_file.name) is True
assert os.path.isfile(tracked_file.name) is True
run_task_mod._clean_git_checkout(root_dir.name)
assert os.path.isdir(root_dir.name) is True
assert os.path.isdir(tracked_dir.name) is True
assert os.path.isfile(tracked_file.name) is True
assert os.path.isdir(untracked_dir.name) is False
assert os.path.isfile(untracked_file.name) is False
def git_current_rev(cwd):
return subprocess.check_output(
args=["git", "rev-parse", "--verify", "HEAD"],
cwd=cwd,
universal_newlines=True,
).strip()
@pytest.fixture(scope="session") # Tests shouldn't change this repo
def mock_git_repo():
"Mock repository with files, commits and branches for using as source"
with tempfile.TemporaryDirectory() as repo:
repo_path = str(repo)
# Init git repo and setup user config
subprocess.check_call(["git", "init", "-b", "main"], cwd=repo_path)
subprocess.check_call(["git", "config", "user.name", "pytest"], cwd=repo_path)
subprocess.check_call(
["git", "config", "user.email", "py@tes.t"], cwd=repo_path
)
def _commit_file(message, filename, content):
filepath = os.path.join(repo, filename)
os.makedirs(os.path.dirname(filepath), exist_ok=True)
with open(filepath, "w") as fout:
fout.write(content)
subprocess.check_call(["git", "add", filename], cwd=repo_path)
subprocess.check_call(["git", "commit", "-m", message], cwd=repo_path)
return git_current_rev(repo_path)
# Commit mainfile (to main branch)
main_commits = [_commit_file("Initial commit", "mainfile", "foo")]
# New branch mybranch
subprocess.check_call(["git", "checkout", "-b", "mybranch"], cwd=repo_path)
# Create two commits to mybranch
branch_commits = []
branch_commits.append(
_commit_file("Add file in mybranch2", "branchfile", "bar")
)
branch_commits.append(
_commit_file("Update file in mybranch", "branchfile", "baz")
)
# Set current branch back to main
subprocess.check_call(["git", "checkout", "main"], cwd=repo_path)
yield {"path": repo_path, "main": main_commits, "branch": branch_commits}
@pytest.mark.parametrize(
"base_rev,head_ref,files,hash_key,exc",
[
(None, None, ["mainfile"], "main", AssertionError),
(None, "main", ["mainfile"], "main", None),
(None, "mybranch", ["mainfile", "branchfile"], "branch", None),
("main", "main", ["mainfile"], "main", None),
("main", "mybranch", ["mainfile", "branchfile"], "branch", None),
],
)
def test_git_checkout(
mock_stdin,
run_task_mod,
mock_git_repo,
tmp_path,
base_rev,
head_ref,
files,
hash_key,
exc,
):
destination = tmp_path / "destination"
run_git_checkout = functools.partial(
run_task_mod.git_checkout,
destination_path=destination,
head_repo=mock_git_repo["path"],
base_repo=mock_git_repo["path"],
base_rev=base_rev,
head_ref=head_ref,
head_rev=None,
ssh_key_file=None,
ssh_known_hosts_file=None,
)
if exc:
with pytest.raises(exc):
run_git_checkout()
return
run_git_checkout()
# Check desired files exist
for filename in files:
assert os.path.exists(os.path.join(destination, filename))
# Check repo is on the right branch
if head_ref:
current_branch = subprocess.check_output(
args=["git", "rev-parse", "--abbrev-ref", "HEAD"],
cwd=destination,
universal_newlines=True,
).strip()
assert current_branch == head_ref
current_rev = git_current_rev(destination)
assert current_rev == mock_git_repo[hash_key][-1]
@pytest.mark.parametrize(
"head_ref,head_rev_index",
(
pytest.param("mybranch", 1, id="head"),
pytest.param("mybranch", 0, id="non tip"),
pytest.param(None, 0, id="non tip without head_ref"),
),
)
def test_git_checkout_with_commit(
mock_stdin,
run_task_mod,
mock_git_repo,
tmp_path,
head_ref,
head_rev_index,
):
destination = tmp_path / "destination"
base_rev = mock_git_repo["main"][-1]
head_rev = mock_git_repo["branch"][head_rev_index]
run_task_mod.git_checkout(
destination_path=str(destination),
head_repo=mock_git_repo["path"],
base_repo=mock_git_repo["path"],
base_rev=base_rev,
head_ref=head_ref,
head_rev=head_rev,
ssh_key_file=None,
ssh_known_hosts_file=None,
)
current_rev = subprocess.check_output(
args=["git", "rev-parse", "HEAD"],
cwd=str(destination),
universal_newlines=True,
).strip()
assert current_rev == head_rev
def test_git_checkout_shallow(
mock_stdin,
run_task_mod,
mock_git_repo,
tmp_path,
):
destination = tmp_path / "destination"
# Git ignores `--depth` when cloning from local directories, so use file://
# protocol to force shallow clone.
repo_url = f"file://{mock_git_repo['path']}"
base_rev = mock_git_repo["main"][-1]
head_rev = mock_git_repo["branch"][-1]
# Use shallow clone with head_ref != head_rev
run_task_mod.git_checkout(
destination_path=str(destination),
head_repo=repo_url,
base_repo=repo_url,
base_rev=base_rev,
head_ref="mybranch",
head_rev=head_rev,
ssh_key_file=None,
ssh_known_hosts_file=None,
shallow=True,
)
shallow_file = destination / ".git" / "shallow"
assert shallow_file.exists()
# Verify we're on the correct commit
final_rev = subprocess.check_output(
["git", "rev-parse", "HEAD"],
cwd=str(destination),
universal_newlines=True,
).strip()
assert final_rev == head_rev
# Verify both base_rev and head_rev are available.
for sha in (base_rev, head_rev):
result = subprocess.run(
["git", "cat-file", "-t", sha],
cwd=str(destination),
capture_output=True,
text=True,
)
assert result.returncode == 0, f"Commit {sha} should be available"
assert result.stdout.strip() == "commit"
def test_git_fetch_shallow(
mock_stdin,
run_task_mod,
mock_git_repo,
tmp_path,
):
destination = tmp_path / "destination"
# Git ignores `--depth` when cloning from local directories, so use file://
# protocol to force shallow clone.
repo_url = f"file://{mock_git_repo['path']}"
run_task_mod.run_command(
b"vcs",
[
"git",
"clone",
"--depth=1",
"--no-checkout",
repo_url,
str(destination),
],
)
shallow_file = destination / ".git" / "shallow"
assert shallow_file.exists()
# Verify base_rev doesn't exist yet
base_rev = mock_git_repo["branch"][-1]
result = subprocess.run(
["git", "cat-file", "-t", base_rev],
cwd=str(destination),
capture_output=True,
text=True,
)
assert result.returncode != 0
run_task_mod.git_fetch(str(destination), base_rev, remote=repo_url, shallow=True)
# Verify base_rev is now available
result = subprocess.run(
["git", "cat-file", "-t", base_rev],
cwd=str(destination),
capture_output=True,
text=True,
)
assert result.returncode == 0
assert result.stdout.strip() == "commit"
def test_display_python_version_should_output_python_versions_title(
run_task_mod, capsys
):
run_task_mod._display_python_versions()
output = capsys.readouterr().out
assert ("Python version:" in output) is True
assert "Subprocess" in output and "version:" in output
def test_display_python_version_should_output_python_versions(run_task_mod, capsys):
run_task_mod._display_python_versions()
output = capsys.readouterr().out
assert ("Python version: 3." in output) or ("Python version: 2." in output) is True
assert "Subprocess" in output and "version:" in output
@pytest.fixture
def run_main(tmp_path, mocker, mock_stdin, run_task_mod):
base_args = [
f"--task-cwd={str(tmp_path)}",
]
base_command_args = [
"bash",
"-c",
"echo hello",
]
m = mocker.patch.object(run_task_mod.os, "getcwd")
m.return_value = "/builds/worker"
def inner(extra_args=None, env=None):
extra_args = extra_args or []
env = env or {}
mocker.patch.object(run_task_mod.os, "environ", env)
args = base_args + extra_args
args.append("--")
args.extend(base_command_args)
result = run_task_mod.main(args)
return result, env
return inner
@nowin
def test_main_abspath_environment(mocker, run_main):
envvars = ["GECKO_PATH", "MOZ_FETCHES_DIR", "UPLOAD_DIR"]
envvars += [cache["env"] for cache in CACHES.values() if "env" in cache]
env = {key: "file" for key in envvars}
env["FOO"] = "file"
mocker.patch("os.sep", "\\")
env["MOZ_PYTHON_HOME"] = "dir\\python"
env["MOZ_UV_HOME"] = "dir/uv"
result, env = run_main(env=env)
assert result == 0
assert env.get("FOO") == "file"
assert env.get("MOZ_PYTHON_HOME") == "/builds/worker/dir/python"
assert env.get("MOZ_UV_HOME") == "/builds/worker/dir/uv"
for key in envvars:
assert env[key] == "/builds/worker/file"
def test_git_checkout_extra_refs(mock_stdin, run_task_mod, mock_git_repo, tmp_path):
"""extra_refs are fetched into the local repo during checkout."""
# Add a notes ref to the source repo
rev = mock_git_repo["main"][-1]
subprocess.check_call(
["git", "notes", "--ref=refs/notes/taskgraph", "add", "-m", "test", rev],
cwd=mock_git_repo["path"],
)
destination = tmp_path / "destination"
run_task_mod.git_checkout(
destination_path=str(destination),
head_repo=mock_git_repo["path"],
base_repo=mock_git_repo["path"],
base_rev=None,
head_ref="main",
head_rev=None,
ssh_key_file=None,
ssh_known_hosts_file=None,
extra_refs=["refs/notes/taskgraph"],
)
# Verify the notes ref is available locally
result = subprocess.run(
["git", "notes", "--ref=refs/notes/taskgraph", "show", rev],
cwd=str(destination),
capture_output=True,
text=True,
)
assert result.returncode == 0
assert "test" in result.stdout