2020-02-19 12:01:02 +01:00
|
|
|
#!/usr/bin/env nix-shell
|
2023-12-25 22:45:38 +01:00
|
|
|
#!nix-shell --quiet -p nix-eval-jobs -p nix -p python3 -i python
|
2020-02-19 12:01:02 +01:00
|
|
|
|
|
|
|
import argparse
|
2023-12-25 22:45:38 +01:00
|
|
|
import json
|
2020-02-19 12:01:02 +01:00
|
|
|
import multiprocessing
|
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
import sys
|
2023-12-25 22:45:38 +01:00
|
|
|
import textwrap
|
2020-02-19 12:01:02 +01:00
|
|
|
from pathlib import Path
|
2023-12-25 22:45:38 +01:00
|
|
|
from tempfile import TemporaryDirectory
|
|
|
|
from typing import IO
|
2020-02-19 12:01:02 +01:00
|
|
|
|
|
|
|
TEST_ROOT = Path(__file__).resolve().parent
|
|
|
|
ROOT = TEST_ROOT.parent
|
|
|
|
|
|
|
|
GREEN = "\033[92m"
|
|
|
|
RED = "\033[91m"
|
|
|
|
RESET = "\033[0m"
|
|
|
|
|
2023-12-25 22:45:38 +01:00
|
|
|
re_nixos_hardware = re.compile(r"<nixos-hardware/([^>]+)>")
|
|
|
|
|
2020-02-19 12:01:02 +01:00
|
|
|
|
2023-12-25 21:27:11 +01:00
|
|
|
def parse_readme() -> list[str]:
|
2020-02-19 12:01:02 +01:00
|
|
|
profiles = set()
|
2023-12-25 21:27:11 +01:00
|
|
|
with ROOT.joinpath("README.md").open() as f:
|
2020-02-19 12:01:02 +01:00
|
|
|
for line in f:
|
2023-12-25 22:45:38 +01:00
|
|
|
if (m := re_nixos_hardware.search(line)) is not None:
|
|
|
|
profiles.add(m.group(1).strip())
|
2020-02-19 12:01:02 +01:00
|
|
|
return list(profiles)
|
|
|
|
|
|
|
|
|
|
|
|
def parse_args() -> argparse.Namespace:
|
|
|
|
parser = argparse.ArgumentParser(description="Run hardware tests")
|
|
|
|
parser.add_argument(
|
|
|
|
"--jobs",
|
|
|
|
type=int,
|
|
|
|
default=multiprocessing.cpu_count(),
|
|
|
|
help="Number of parallel evaluations."
|
|
|
|
"If set to 1 it disable multi processing (suitable for debugging)",
|
|
|
|
)
|
2020-05-18 12:38:36 +02:00
|
|
|
parser.add_argument(
|
2023-12-25 21:21:26 +01:00
|
|
|
"--verbose",
|
|
|
|
action="store_true",
|
|
|
|
help="Print evaluation commands executed",
|
2020-05-18 12:38:36 +02:00
|
|
|
)
|
2020-02-19 12:01:02 +01:00
|
|
|
parser.add_argument("profiles", nargs="*")
|
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
2023-12-25 22:45:38 +01:00
|
|
|
def write_eval_test(f: IO[str], profiles: list[str]) -> None:
|
|
|
|
build_profile = TEST_ROOT.joinpath("build-profile.nix")
|
|
|
|
f.write(
|
|
|
|
textwrap.dedent(
|
|
|
|
f"""
|
|
|
|
let
|
|
|
|
purePkgs = system: import <nixpkgs> {{
|
|
|
|
config = {{
|
|
|
|
allowBroken = true;
|
|
|
|
allowUnfree = true;
|
|
|
|
nvidia.acceptLicense = true;
|
|
|
|
}};
|
|
|
|
overlays = [];
|
|
|
|
inherit system;
|
|
|
|
}};
|
|
|
|
pkgs.x86_64-linux = purePkgs "x86_64-linux";
|
|
|
|
pkgs.aarch64-linux = purePkgs "aarch64-linux";
|
|
|
|
buildProfile = import {build_profile};
|
|
|
|
in
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
)
|
|
|
|
f.write("{\n")
|
|
|
|
for profile in profiles:
|
|
|
|
# does import-from-derivation
|
|
|
|
if profile == "toshiba/swanky":
|
|
|
|
continue
|
|
|
|
# uses custom nixpkgs config
|
|
|
|
if profile == "raspberry-pi/2":
|
|
|
|
continue
|
|
|
|
|
|
|
|
system = "x86_64-linux"
|
2024-06-26 23:58:14 +02:00
|
|
|
if profile in ("raspberry-pi/3", "raspberry-pi/4", "raspberry-pi/5"):
|
2023-12-25 22:45:38 +01:00
|
|
|
system = "aarch64-linux"
|
|
|
|
|
|
|
|
f.write(
|
|
|
|
f' "{profile}" = buildProfile {{ profile = import {ROOT}/{profile}; pkgs = pkgs.{system}; }};\n'
|
|
|
|
)
|
|
|
|
f.write("}\n")
|
|
|
|
|
|
|
|
|
|
|
|
def run_eval_test(eval_test: Path, gcroot_dir: Path, jobs: int) -> list[str]:
|
|
|
|
failed_profiles = []
|
|
|
|
cmd = [
|
|
|
|
"nix-eval-jobs",
|
|
|
|
"--gc-roots-dir",
|
|
|
|
gcroot_dir,
|
|
|
|
"--max-memory-size",
|
|
|
|
"2048",
|
|
|
|
"--workers",
|
|
|
|
str(jobs),
|
|
|
|
str(eval_test),
|
|
|
|
]
|
|
|
|
proc = subprocess.Popen(
|
|
|
|
cmd,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
text=True,
|
|
|
|
)
|
|
|
|
with proc as p:
|
|
|
|
assert p.stdout is not None
|
|
|
|
for line in p.stdout:
|
|
|
|
data = json.loads(line)
|
|
|
|
attr = data.get("attr")
|
|
|
|
if "error" in data:
|
|
|
|
failed_profiles.append(attr)
|
|
|
|
print(f"{RED}FAIL {attr}:{RESET}", file=sys.stderr)
|
|
|
|
print(f"{RED}{data['error']}{RESET}", file=sys.stderr)
|
|
|
|
else:
|
|
|
|
print(f"{GREEN}OK {attr}{RESET}")
|
|
|
|
return failed_profiles
|
|
|
|
|
|
|
|
|
2020-02-19 12:01:02 +01:00
|
|
|
def main() -> None:
|
|
|
|
args = parse_args()
|
2023-12-25 21:27:11 +01:00
|
|
|
profiles = parse_readme() if len(args.profiles) == 0 else args.profiles
|
2020-02-19 12:01:02 +01:00
|
|
|
|
|
|
|
failed_profiles = []
|
2023-12-25 22:45:38 +01:00
|
|
|
with TemporaryDirectory() as tmpdir:
|
|
|
|
eval_test = Path(tmpdir) / "eval-test.nix"
|
|
|
|
gcroot_dir = Path(tmpdir) / "gcroot"
|
|
|
|
with eval_test.open("w") as f:
|
|
|
|
write_eval_test(f, profiles)
|
|
|
|
failed_profiles = run_eval_test(eval_test, gcroot_dir, args.jobs)
|
2020-02-19 12:01:02 +01:00
|
|
|
|
|
|
|
if len(failed_profiles) > 0:
|
|
|
|
print(f"\n{RED}The following {len(failed_profiles)} test(s) failed:{RESET}")
|
|
|
|
for profile in failed_profiles:
|
|
|
|
print(f"{sys.argv[0]} '{profile}'")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|