Repository navigation
Expand file tree
/
Copy pathsetup_bazel_test.py
More file actions
424 lines (367 loc) · 15 KB
/
Copy pathsetup_bazel_test.py
File metadata and controls
424 lines (367 loc) · 15 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
"""Tests for the .github/actions/setup-bazel composite action."""
from __future__ import annotations
import asyncio
import hashlib
import os
import shutil
import stat
import subprocess
import sys
import tarfile
from pathlib import Path
import download_bazel
import pytest
import yaml
from python.runfiles import runfiles
def _action_dir() -> Path:
return runfiles.CreateOrRaise().root() / "rules_python/.github/actions/setup-bazel"
def _write_fake_bazel(bin_dir: Path, body: str, name: str = "bazel") -> Path:
bin_dir.mkdir(parents=True, exist_ok=True)
fake_bazel = bin_dir / name
fake_bazel.write_text(
f"#!/usr/bin/env bash\nset -euo pipefail\n{body}\n", encoding="utf-8"
)
fake_bazel.chmod(0o755)
return fake_bazel
def _sha256_file(path: Path) -> str:
return hashlib.sha256(path.read_bytes()).hexdigest()
def test_action_yaml_invokes_executable_download_script(tmp_path: Path):
"""Verifies setup-bazel action.yaml runs dev/download_bazel.py and omits bazelisk-version."""
rf = runfiles.CreateOrRaise()
action_dir = _action_dir()
script = rf.root() / "rules_python/dev/download_bazel.py"
assert script.is_file()
assert bool(script.stat().st_mode & stat.S_IXUSR), (
"download_bazel.py must be executable"
)
action = yaml.safe_load((action_dir / "action.yaml").read_text(encoding="utf-8"))
steps = action["runs"]["steps"]
assert any("dev/download_bazel.py" in step.get("run", "") for step in steps)
setup_step = next(s for s in steps if s.get("name") == "Setup Bazel")
assert "bazelisk-version" not in setup_step["with"]
# Stage dev/ scripts outside Bazel runfiles and verify dev/download_bazel.py
# resolves sibling dev/verified_download.py with an empty environment (as in
# Read the Docs and GitHub Actions).
staged_dev = tmp_path / "dev"
staged_dev.mkdir(parents=True)
staged_script = staged_dev / "download_bazel.py"
shutil.copy2(script, staged_script)
shutil.copy2(
rf.root() / "rules_python/dev/verified_download.py",
staged_dev / "verified_download.py",
)
subprocess.run(
[sys.executable, "-B", "-m", "doctest", "download_bazel.py"],
check=True,
cwd=staged_dev,
env={},
)
def test_action_yaml_enables_buildbuddy_on_all_os():
"""Verifies setup-bazel action.yaml enables BuildBuddy across all OSes."""
action = yaml.safe_load((_action_dir() / "action.yaml").read_text(encoding="utf-8"))
steps = action["runs"]["steps"]
setup_step = next(s for s in steps if s.get("name") == "Setup Bazel")
bazelrc = setup_step["with"]["bazelrc"]
assert "inputs.buildbuddy_api_key" in bazelrc
assert "runner.os" not in bazelrc
assert "ROLE=CI" in bazelrc
assert "VISIBILITY=PUBLIC" in bazelrc
assert "RUN_ID=" in bazelrc
assert "ACTOR=" in bazelrc
assert "COMMIT_SHA=" in bazelrc
assert "PR_NUMBER=" in bazelrc
assert "BRANCH_NAME=" in bazelrc
assert '--build_metadata="BUILDBUDDY_LINKS=' in bazelrc
secret_step = next(
s for s in steps if s.get("name") == "Write secret to user.bazelrc"
)
assert secret_step["if"] == "${{ inputs.buildbuddy_api_key != '' }}"
def test_buildbuddy_bazelrc_cache_and_diagnostics():
"""Verifies buildbuddy.bazelrc includes recommended cache, diagnostic, and
profiling flags.
"""
rf = runfiles.CreateOrRaise()
buildbuddy_path = rf.root() / "rules_python/buildbuddy.bazelrc"
buildbuddy_lines = [
line.strip()
for line in buildbuddy_path.read_text(encoding="utf-8").splitlines()
if line.strip() and not line.strip().startswith("#")
]
expected_flags = [
(
"build:_buildbuddy_base "
"--build_metadata=REPO_URL=https://github.com/bazel-contrib/rules_python"
),
"common:_buildbuddy_base --remote_cache_compression",
"common:_buildbuddy_base --experimental_remote_cache_chunking",
"common:_buildbuddy_base --rewind_lost_inputs",
"common:_buildbuddy_base --remote_grpc_log=bazel-remote-grpc.log",
(
"common:_buildbuddy_base "
"--execution_log_compact_file=bazel-execution-log.binpb.zst"
),
"common:_buildbuddy_base --noslim_profile",
"common:_buildbuddy_base --experimental_profile_include_target_label",
"common:_buildbuddy_base --experimental_profile_include_primary_output",
]
for flag in expected_flags:
assert flag in buildbuddy_lines, f"Expected '{flag}' in buildbuddy.bazelrc"
def test_buildbuddy_bazelrc_disables_remote_executor_on_macos_and_windows():
"""Verifies .bazelrc and buildbuddy.bazelrc disable remote_executor on macOS and Windows."""
rf = runfiles.CreateOrRaise()
buildbuddy_path = rf.root() / "rules_python/buildbuddy.bazelrc"
buildbuddy_lines = [
line.strip()
for line in buildbuddy_path.read_text(encoding="utf-8").splitlines()
if line.strip() and not line.strip().startswith("#")
]
executor_line = (
"common:_buildbuddy_base --remote_executor=grpcs://rules-python.buildbuddy.io"
)
rbe_platform_line = (
"build:_buildbuddy_base "
"--extra_execution_platforms=@toolchains_buildbuddy//platforms:linux_x86_64"
)
rbe_toolchain_line = (
"build:_buildbuddy_base "
"--extra_toolchains=@toolchains_buildbuddy//toolchains/cc:ubuntu_gcc_x86_64"
)
platform_config_line = "common:_buildbuddy_base --enable_platform_specific_config"
assert executor_line in buildbuddy_lines
assert rbe_platform_line in buildbuddy_lines
assert rbe_toolchain_line in buildbuddy_lines
assert platform_config_line in buildbuddy_lines
assert buildbuddy_lines.index(executor_line) < buildbuddy_lines.index(
platform_config_line
)
assert buildbuddy_lines.index(rbe_platform_line) < buildbuddy_lines.index(
platform_config_line
)
bazelrc_path = rf.root() / "rules_python/.bazelrc"
bazelrc_lines = [
line.strip()
for line in bazelrc_path.read_text(encoding="utf-8").splitlines()
if line.strip() and not line.strip().startswith("#")
]
assert "common:macos --remote_executor=" in bazelrc_lines
assert "build:macos --extra_execution_platforms=" in bazelrc_lines
assert "common:windows --remote_executor=" in bazelrc_lines
assert "build:windows --extra_execution_platforms=" in bazelrc_lines
def test_download_bazelisk_from_primary_url(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
):
"""Verifies download_bazel.py downloads Bazelisk, checks SHA-256, and updates GITHUB_PATH."""
calls_file = tmp_path / "calls.txt"
release_dir = tmp_path / "releases" / "v1.30.0"
asset = _write_fake_bazel(
release_dir,
f'echo "$@" >> "{calls_file}"\necho "bazel 9.1.1"\n',
name="bazelisk-linux-amd64",
)
github_path = tmp_path / "github_path"
home = tmp_path / "home"
monkeypatch.setenv("HOME", str(home))
monkeypatch.delenv("XDG_CACHE_HOME", raising=False)
monkeypatch.delenv("BAZELISK_HOME", raising=False)
monkeypatch.setenv("GITHUB_PATH", str(github_path))
monkeypatch.setattr(download_bazel, "_detect_os", lambda: "linux")
monkeypatch.setattr(download_bazel, "_detect_arch", lambda: "amd64")
monkeypatch.setitem(
download_bazel.BAZELISK_URLS,
"linux-amd64",
[(asset.as_uri(), _sha256_file(asset), "")],
)
asyncio.run(download_bazel.main())
out = capsys.readouterr().out
bin_dir = home / ".cache/bazelisk/bin"
assert (bin_dir / "bazel").is_file()
assert (bin_dir / "bazelisk").is_file()
assert github_path.read_text(encoding="utf-8").splitlines() == [str(bin_dir)]
assert calls_file.read_text(encoding="utf-8").splitlines() == ["--version"]
assert "::warning::" not in out
assert "::error::" not in out
def test_download_bazelisk_falls_back_to_npm_mirror(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
):
"""Verifies download_bazel.py falls back to the npm tarball mirror when primary fails."""
calls_file = tmp_path / "calls.txt"
pkg_dir = tmp_path / "npm" / "package"
asset = _write_fake_bazel(
pkg_dir,
f'echo "$@" >> "{calls_file}"\necho "bazel 9.1.1"\n',
name="bazelisk-linux_amd64",
)
tgz_path = tmp_path / "bazelisk-1.28.1.tgz"
with tarfile.open(tgz_path, "w:gz") as tar:
tar.add(asset, arcname="package/bazelisk-linux_amd64")
home = tmp_path / "home"
missing_url = (tmp_path / "missing").as_uri()
sha = _sha256_file(asset)
monkeypatch.setenv("HOME", str(home))
monkeypatch.delenv("XDG_CACHE_HOME", raising=False)
monkeypatch.delenv("BAZELISK_HOME", raising=False)
monkeypatch.setattr(download_bazel, "_detect_os", lambda: "linux")
monkeypatch.setattr(download_bazel, "_detect_arch", lambda: "amd64")
monkeypatch.setitem(
download_bazel.BAZELISK_URLS,
"linux-amd64",
[
(missing_url, sha, ""),
(tgz_path.as_uri(), sha, "package/bazelisk-linux_amd64"),
],
)
asyncio.run(download_bazel.main())
out = capsys.readouterr().out
assert f"::warning::Failed to fetch {missing_url}" in out
assert calls_file.read_text(encoding="utf-8").splitlines() == ["--version"]
assert (home / ".cache/bazelisk/bin/bazel").is_file()
def test_download_bazelisk_reuses_cached_binary(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
):
"""Verifies download_bazel.py reuses a cached Bazelisk binary with matching SHA-256."""
calls_file = tmp_path / "calls.txt"
home = tmp_path / "home"
cached_bazel = _write_fake_bazel(
home / ".cache/bazelisk/bin",
f'echo "$@" >> "{calls_file}"\necho "bazel 9.1.1"\n',
name="bazel",
)
monkeypatch.setenv("HOME", str(home))
monkeypatch.delenv("XDG_CACHE_HOME", raising=False)
monkeypatch.delenv("BAZELISK_HOME", raising=False)
monkeypatch.setattr(download_bazel, "_detect_os", lambda: "linux")
monkeypatch.setattr(download_bazel, "_detect_arch", lambda: "amd64")
monkeypatch.setitem(
download_bazel.BAZELISK_URLS,
"linux-amd64",
[((tmp_path / "missing").as_uri(), _sha256_file(cached_bazel), "")],
)
asyncio.run(download_bazel.main())
out = capsys.readouterr().out
assert (home / ".cache/bazelisk/bin/bazelisk").is_file()
assert calls_file.read_text(encoding="utf-8").splitlines() == ["--version"]
assert "::warning::" not in out
def test_download_bazelisk_exhausts_retries_and_cleans_cache(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
):
"""Verifies download_bazel.py cleans up cache dirs when Bazelisk download fails."""
home = tmp_path / "home"
(home / ".cache/bazelisk/partial").mkdir(parents=True, exist_ok=True)
monkeypatch.setenv("HOME", str(home))
monkeypatch.delenv("XDG_CACHE_HOME", raising=False)
monkeypatch.delenv("BAZELISK_HOME", raising=False)
monkeypatch.setattr(download_bazel, "_detect_os", lambda: "linux")
monkeypatch.setattr(download_bazel, "_detect_arch", lambda: "amd64")
monkeypatch.setitem(
download_bazel.BAZELISK_URLS,
"linux-amd64",
[
((tmp_path / "missing").as_uri(), "0" * 64, ""),
(
(tmp_path / "missing.tgz").as_uri(),
"0" * 64,
"package/bazelisk-linux_amd64",
),
],
)
with pytest.raises(SystemExit) as exc_info:
asyncio.run(download_bazel._setup_bazelisk(max_attempts=2, initial_delay=0))
assert exc_info.value.code == 1
out = capsys.readouterr().out
assert "::error::Failed to download Bazelisk after 2 attempts" in out
assert not (home / ".cache/bazelisk").exists()
def test_download_bazel_succeeds_first_try(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
):
"""Verifies _download_bazel succeeds without warnings when bazel --version succeeds."""
calls_file = tmp_path / "calls.txt"
_write_fake_bazel(
tmp_path / "bin",
f'echo "$@" >> "{calls_file}"\necho "bazel 9.1.1"\n',
)
monkeypatch.setenv(
"PATH", f"{tmp_path / 'bin'}{os.pathsep}{os.environ.get('PATH', '')}"
)
monkeypatch.setenv("HOME", str(tmp_path / "home"))
asyncio.run(download_bazel._download_bazel(max_attempts=5, initial_delay=0))
out = capsys.readouterr().out
assert calls_file.read_text(encoding="utf-8").splitlines() == ["--version"]
assert "::warning::" not in out
assert "::error::" not in out
def test_download_bazel_retries_transient_failure(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
):
"""Verifies _download_bazel retries transient failures and succeeds."""
calls_file = tmp_path / "calls.txt"
_write_fake_bazel(
tmp_path / "bin",
f"""
echo "$@" >> "{calls_file}"
count=$(wc -l < "{calls_file}")
if (( count < 3 )); then
echo "could not download Bazel: stream error: INTERNAL_ERROR" >&2
exit 1
fi
mkdir -p "$HOME/.cache/bazelisk/downloads"
echo "bazel 9.1.1"
""",
)
home = tmp_path / "home"
monkeypatch.setenv(
"PATH", f"{tmp_path / 'bin'}{os.pathsep}{os.environ.get('PATH', '')}"
)
monkeypatch.setenv("HOME", str(home))
asyncio.run(download_bazel._download_bazel(max_attempts=5, initial_delay=0))
out = capsys.readouterr().out
assert calls_file.read_text(encoding="utf-8").splitlines() == [
"--version",
"--version",
"--version",
]
assert "::warning::Failed to download Bazel (attempt 1/5)" in out
assert "::warning::Failed to download Bazel (attempt 2/5)" in out
assert (home / ".cache/bazelisk/downloads").is_dir()
def test_download_bazel_exhausts_retries_and_cleans_cache(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
):
"""Verifies _download_bazel cleans up partial cache dirs when all retries fail."""
calls_file = tmp_path / "calls.txt"
_write_fake_bazel(
tmp_path / "bin",
f"""
echo "$@" >> "{calls_file}"
mkdir -p "$HOME/.cache/bazelisk/downloads/_tmp"
mkdir -p "$BAZELISK_HOME/downloads/_tmp"
exit 1
""",
)
home = tmp_path / "home"
bazelisk_home = tmp_path / "bazelisk_home"
monkeypatch.setenv(
"PATH", f"{tmp_path / 'bin'}{os.pathsep}{os.environ.get('PATH', '')}"
)
monkeypatch.setenv("HOME", str(home))
monkeypatch.delenv("XDG_CACHE_HOME", raising=False)
monkeypatch.setenv("BAZELISK_HOME", str(bazelisk_home))
with pytest.raises(SystemExit) as exc_info:
asyncio.run(download_bazel._download_bazel(max_attempts=3, initial_delay=0))
assert exc_info.value.code == 1
out = capsys.readouterr().out
assert len(calls_file.read_text(encoding="utf-8").splitlines()) == 3
assert "::error::Failed to download Bazel after 3 attempts" in out
assert not (home / ".cache/bazelisk").exists()
assert not bazelisk_home.exists()