|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import atexit |
| 4 | +import os |
| 5 | +import pathlib |
| 6 | +import shutil |
| 7 | +import socket |
| 8 | +import subprocess |
| 9 | +import tempfile |
| 10 | +import time |
| 11 | +import unittest |
| 12 | + |
| 13 | +import minio_server |
| 14 | +from python.runfiles import Runfiles |
| 15 | + |
| 16 | +MINIO_PORT = 9000 |
| 17 | + |
| 18 | +FILE_CONTENT = "go bears" |
| 19 | +PATCHED_CONTENT = "Go Bears!" |
| 20 | + |
| 21 | +# Always run in a new output_base to avoid false-negatives from caching. |
| 22 | +TEMP_OUTPUT_BASE = tempfile.mkdtemp() |
| 23 | +atexit.register(shutil.rmtree, TEMP_OUTPUT_BASE) |
| 24 | + |
| 25 | + |
| 26 | +def wait_for_port(host: str, port: int, timeout: int = 5) -> bool: |
| 27 | + start = time.time() |
| 28 | + while time.time() - start < timeout: |
| 29 | + try: |
| 30 | + with socket.create_connection((host, port), timeout=1): |
| 31 | + return True |
| 32 | + except OSError: # noqa: PERF203 |
| 33 | + time.sleep(0.1) |
| 34 | + return False |
| 35 | + |
| 36 | + |
| 37 | +class CloudArchiveTest(unittest.TestCase): |
| 38 | + @classmethod |
| 39 | + def setUpClass(cls) -> None: |
| 40 | + r = Runfiles.Create() |
| 41 | + mc_binary = r.Rlocation("mc_binary/file/mc") |
| 42 | + mc_config = r.Rlocation("my_workspace/mc/config.json") |
| 43 | + |
| 44 | + cls.env = { |
| 45 | + "PATH": f"{os.path.dirname(mc_binary)}:{os.environ['PATH']}", |
| 46 | + "MC_CONFIG_DIR": os.path.dirname(mc_config), |
| 47 | + } |
| 48 | + cls.workspace_dir = os.environ["BUILD_WORKING_DIRECTORY"] |
| 49 | + |
| 50 | + repository_cache = subprocess.check_output( |
| 51 | + ["bazel", "info", "repository_cache"], |
| 52 | + text=True, |
| 53 | + cwd=cls.workspace_dir, |
| 54 | + ).strip() |
| 55 | + cls.bazel_args = [ |
| 56 | + f"--output_base={TEMP_OUTPUT_BASE}", |
| 57 | + ] |
| 58 | + cls.bazel_build_args = [ |
| 59 | + f"--repository_cache={repository_cache}", |
| 60 | + ] |
| 61 | + |
| 62 | + execution_root = subprocess.check_output( |
| 63 | + ["bazel", *cls.bazel_args, "info", "execution_root"], |
| 64 | + text=True, |
| 65 | + cwd=cls.workspace_dir, |
| 66 | + ).strip() |
| 67 | + cls.output_dir = pathlib.Path(execution_root, "external") |
| 68 | + |
| 69 | + def test_minio(self) -> None: |
| 70 | + subtests = [ |
| 71 | + ( |
| 72 | + "@test_file//file", |
| 73 | + FILE_CONTENT, |
| 74 | + self.output_dir / "test_file" / "file" / "downloaded", |
| 75 | + ), |
| 76 | + ( |
| 77 | + "@test_archive//:file.txt", |
| 78 | + FILE_CONTENT, |
| 79 | + self.output_dir / "test_archive" / "file.txt", |
| 80 | + ), |
| 81 | + ( |
| 82 | + "@test_archive_p0_patch//:file.txt", |
| 83 | + PATCHED_CONTENT, |
| 84 | + self.output_dir / "test_archive_p0_patch" / "file.txt", |
| 85 | + ), |
| 86 | + ( |
| 87 | + "@test_archive_p1_patch//:file.txt", |
| 88 | + PATCHED_CONTENT, |
| 89 | + self.output_dir / "test_archive_p1_patch" / "file.txt", |
| 90 | + ), |
| 91 | + ] |
| 92 | + with minio_server.run(port=MINIO_PORT) as proc: |
| 93 | + if not wait_for_port("127.0.0.1", MINIO_PORT): |
| 94 | + proc.kill() |
| 95 | + print("MinIO server failed to start") |
| 96 | + raise SystemExit(1) |
| 97 | + |
| 98 | + for target, expected_content, output_path in subtests: |
| 99 | + with self.subTest(target=target): |
| 100 | + subprocess.check_call( |
| 101 | + ["bazel", *self.bazel_args, "build", *self.bazel_build_args, target], |
| 102 | + env=self.env, |
| 103 | + cwd=self.workspace_dir, |
| 104 | + ) |
| 105 | + assert expected_content in output_path.read_text() |
| 106 | + |
| 107 | + |
| 108 | +if __name__ == "__main__": |
| 109 | + unittest.main() |
0 commit comments