1
0
Fork 0
mirror of https://github.com/NixOS/nixos-hardware synced 2024-09-20 05:17:22 +02:00
nixos-hardware/tests/run.py

102 lines
2.6 KiB
Python
Raw Normal View History

2024-08-19 09:56:10 +02:00
#!/usr/bin/env python3
import argparse
2023-12-25 22:45:38 +01:00
import json
import multiprocessing
import re
2024-08-19 09:56:10 +02:00
import shlex
import subprocess
import sys
from pathlib import Path
2023-12-25 22:45:38 +01:00
from tempfile import TemporaryDirectory
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/([^>]+)>")
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)",
)
parser.add_argument(
2023-12-25 21:21:26 +01:00
"--verbose",
action="store_true",
help="Print evaluation commands executed",
)
2024-08-19 09:56:10 +02:00
parser.add_argument(
"--nixos-hardware",
help="Print evaluation commands executed",
)
return parser.parse_args()
2024-08-19 09:56:10 +02:00
def run_eval_test(nixos_hardware: str, gcroot_dir: Path, jobs: int) -> list[str]:
2023-12-25 22:45:38 +01:00
failed_profiles = []
cmd = [
"nix-eval-jobs",
2024-08-19 09:56:10 +02:00
"--extra-experimental-features",
"flakes",
"--override-input",
"nixos-hardware",
nixos_hardware,
2023-12-25 22:45:38 +01:00
"--gc-roots-dir",
2024-08-19 09:56:10 +02:00
str(gcroot_dir),
2023-12-25 22:45:38 +01:00
"--max-memory-size",
"2048",
"--workers",
str(jobs),
2024-08-19 09:56:10 +02:00
"--flake",
str(TEST_ROOT) + "#checks",
"--force-recurse",
2023-12-25 22:45:38 +01:00
]
2024-08-19 09:56:10 +02:00
print(" ".join(map(shlex.quote,cmd)))
2023-12-25 22:45:38 +01:00
proc = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
text=True,
)
2024-08-19 09:56:10 +02:00
2023-12-25 22:45:38 +01:00
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
def main() -> None:
args = parse_args()
failed_profiles = []
2024-08-19 09:56:10 +02:00
2023-12-25 22:45:38 +01:00
with TemporaryDirectory() as tmpdir:
gcroot_dir = Path(tmpdir) / "gcroot"
2024-08-19 09:56:10 +02:00
failed_profiles = run_eval_test(args.nixos_hardware, gcroot_dir, args.jobs)
if len(failed_profiles) > 0:
print(f"\n{RED}The following {len(failed_profiles)} test(s) failed:{RESET}")
for profile in failed_profiles:
2024-08-19 09:56:10 +02:00
print(f" '{profile}'")
sys.exit(1)
if __name__ == "__main__":
main()