~magic_rb/dotfiles

cc362a70e4af4b157453d2bee4400420b1ffc109 — magic_rb a month ago 5243055
Add NixOS tests test

Signed-off-by: magic_rb <magic_rb@redalder.org>
2 files changed, 59 insertions(+), 0 deletions(-)

A nixos/tests/default.nix
A nixos/tests/router.py
A nixos/tests/default.nix => nixos/tests/default.nix +38 -0
@@ 0,0 1,38 @@
{self, ...}: {
  perSystem = {pkgs, ...}: {
    checks.router = pkgs.stdenv.mkDerivation {
      name = "router";
      phases = [
        "runPhase"
      ];

      env.ROUTER_CMD =
        (self.nixosConfigurations.liveusb.extendModules {
          modules = [
            "${pkgs.path}/nixos/modules/testing/test-instrumentation.nix"
          ];
        })
        .config
        .system
        .build
        .vm;

      nativeBuildInputs = with pkgs; [
        iproute2
        (python3.withPackages (ps:
          with ps; [
            ptpython
            colorama
            junit-xml
          ]))
      ];

      runPhase = ''
        PYTHONPATH=${pkgs.path}/nixos/lib/test-driver python ${./router.py}

        mkdir -p $out
        touch $out/success
      '';
    };
  };
}

A nixos/tests/router.py => nixos/tests/router.py +21 -0
@@ 0,0 1,21 @@
from test_driver.machine import Machine, StartCommand, NixStartScript
from test_driver.logger import TerminalLogger
from pathlib import Path
import os


def make_machine(out_dir: Path, tmp_dir: Path) -> Machine:
    out_dir.mkdir(parents=True, exist_ok=True)
    tmp_dir.mkdir(parents=True, exist_ok=True)
    return Machine(
        out_dir = out_dir.absolute(),
        tmp_dir = tmp_dir.absolute(),
        start_command = NixStartScript(os.getenv("ROUTER_CMD") + "/bin/run-nixos-vm"),
        logger = TerminalLogger(),
        name = "router",
    )

router = make_machine(Path("router/out"), Path("router/tmp"))
router.start()
router.wait_for_unit("multi-user.target")
router.shutdown()