|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +import os |
| 8 | +import shutil |
| 9 | +import subprocess |
| 10 | +import tempfile |
| 11 | +import unittest |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | + |
| 15 | +ROOT = Path(__file__).resolve().parents[3] |
| 16 | +IMAGE = "executorch-ubuntu-22.04-clang12" |
| 17 | +PACKAGES = { |
| 18 | + "pytorch": "TORCH_VERSION", |
| 19 | + "torchvision": "TORCHVISION_VERSION", |
| 20 | + "torchaudio": "TORCHAUDIO_VERSION", |
| 21 | +} |
| 22 | + |
| 23 | + |
| 24 | +class TorchciImageOverrideTest(unittest.TestCase): |
| 25 | + def run_build(self, image=IMAGE, invalid=None): |
| 26 | + with tempfile.TemporaryDirectory() as tmp: |
| 27 | + fixture = Path(tmp) |
| 28 | + docker = fixture / ".ci/docker" |
| 29 | + pins = docker / "ci_commit_pins" |
| 30 | + pins.mkdir(parents=True) |
| 31 | + (pins / "pytorch.txt").write_text("shared-pin\n") |
| 32 | + (pins / "buck2.txt").write_text("buck-pin\n") |
| 33 | + for package in PACKAGES: |
| 34 | + source = ( |
| 35 | + ROOT / ".ci/docker/ci_commit_pins" / f"{package}-linux-clang12.txt" |
| 36 | + ) |
| 37 | + value = invalid if invalid is not None else source.read_text() |
| 38 | + (pins / source.name).write_text(value) |
| 39 | + (fixture / "requirements-lintrunner.txt").write_text("") |
| 40 | + arm = fixture / "backends/arm/scripts" |
| 41 | + arm.mkdir(parents=True) |
| 42 | + for name in ("utils", "fvp_utils", "toolchain_utils"): |
| 43 | + (arm / f"{name}.sh").write_text("") |
| 44 | + shutil.copyfile(ROOT / ".ci/docker/build.sh", docker / "build.sh") |
| 45 | + binary = fixture / "bin" |
| 46 | + binary.mkdir() |
| 47 | + mock = binary / "docker" |
| 48 | + mock.write_text('#!/bin/sh\nprintf "%s\\n" "$@"\n') |
| 49 | + mock.chmod(0o755) |
| 50 | + return subprocess.run( |
| 51 | + ["bash", "build.sh", image], |
| 52 | + cwd=docker, |
| 53 | + env={ |
| 54 | + **os.environ, |
| 55 | + "PATH": f"{binary}:{os.environ['PATH']}", |
| 56 | + "REMOTE_BUILDKIT": "", |
| 57 | + }, |
| 58 | + text=True, |
| 59 | + capture_output=True, |
| 60 | + ) |
| 61 | + |
| 62 | + def test_clang_image_uses_all_published_pins(self): |
| 63 | + result = self.run_build() |
| 64 | + self.assertEqual(result.returncode, 0, result.stderr) |
| 65 | + for package, variable in PACKAGES.items(): |
| 66 | + pin = ( |
| 67 | + (ROOT / ".ci/docker/ci_commit_pins" / f"{package}-linux-clang12.txt") |
| 68 | + .read_text() |
| 69 | + .strip() |
| 70 | + ) |
| 71 | + self.assertRegex(pin, r"^[0-9a-f]{40}$") |
| 72 | + self.assertIn(f"{variable}={pin}", result.stdout.splitlines()) |
| 73 | + self.assertIn("TORCHCI_REPORTS=1", result.stdout.splitlines()) |
| 74 | + |
| 75 | + def test_other_images_keep_the_shared_pin(self): |
| 76 | + result = self.run_build("executorch-ubuntu-22.04-gcc11", invalid="invalid") |
| 77 | + self.assertEqual(result.returncode, 0, result.stderr) |
| 78 | + for value in ( |
| 79 | + "TORCH_VERSION=shared-pin", |
| 80 | + "TORCHVISION_VERSION=", |
| 81 | + "TORCHAUDIO_VERSION=", |
| 82 | + "TORCHCI_REPORTS=", |
| 83 | + ): |
| 84 | + self.assertIn(value, result.stdout.splitlines()) |
| 85 | + |
| 86 | + def test_missing_or_mutable_pin_stops_before_docker(self): |
| 87 | + for pin in ("", "main", "a" * 39, "A" * 40): |
| 88 | + with self.subTest(pin=pin): |
| 89 | + result = self.run_build(invalid=pin) |
| 90 | + self.assertNotEqual(result.returncode, 0) |
| 91 | + self.assertNotIn("--no-cache", result.stdout.splitlines()) |
| 92 | + |
| 93 | + def test_docker_forwards_the_domain_pins_and_checks_installed_torch(self): |
| 94 | + dockerfile = (ROOT / ".ci/docker/ubuntu/Dockerfile").read_text() |
| 95 | + script = (ROOT / ".ci/docker/common/install_pytorch.sh").read_text() |
| 96 | + for variable in (*PACKAGES.values(), "TORCHCI_REPORTS"): |
| 97 | + self.assertIn(f"ARG {variable}\n", dockerfile) |
| 98 | + self.assertIn("TORCHAUDIO_VERSION=${TORCHAUDIO_VERSION:-release/2.11}", script) |
| 99 | + self.assertIn( |
| 100 | + "TORCHVISION_VERSION=${TORCHVISION_VERSION:-release/0.29}", script |
| 101 | + ) |
| 102 | + self.assertLess( |
| 103 | + script.index(" install_domains\n"), |
| 104 | + script.index("if torch.version.git_version"), |
| 105 | + ) |
| 106 | + self.assertIn('report.SCHEMA_VERSION != "1.0"', script) |
| 107 | + self.assertIn("environment.capture()", script) |
0 commit comments