2024-03-17 18:48:49 +00:00
|
|
|
{
|
|
|
|
hostPkgs,
|
|
|
|
lib,
|
|
|
|
config,
|
|
|
|
...
|
|
|
|
}:
|
2023-10-14 13:54:56 +00:00
|
|
|
let
|
|
|
|
testDriver = hostPkgs.python3.pkgs.callPackage ./package.nix {
|
|
|
|
inherit (config) extraPythonPackages;
|
|
|
|
inherit (hostPkgs.pkgs) util-linux systemd;
|
|
|
|
};
|
|
|
|
containers = map (m: m.system.build.toplevel) (lib.attrValues config.nodes);
|
2024-03-17 18:48:49 +00:00
|
|
|
pythonizeName =
|
|
|
|
name:
|
2023-10-14 13:54:56 +00:00
|
|
|
let
|
|
|
|
head = lib.substring 0 1 name;
|
|
|
|
tail = lib.substring 1 (-1) name;
|
|
|
|
in
|
2024-03-17 18:48:49 +00:00
|
|
|
(if builtins.match "[A-z_]" head == null then "_" else head)
|
|
|
|
+ lib.stringAsChars (c: if builtins.match "[A-z0-9_]" c == null then "_" else c) tail;
|
2023-10-14 13:54:56 +00:00
|
|
|
nodeHostNames =
|
|
|
|
let
|
|
|
|
nodesList = map (c: c.system.name) (lib.attrValues config.nodes);
|
|
|
|
in
|
|
|
|
nodesList ++ lib.optional (lib.length nodesList == 1 && !lib.elem "machine" nodesList) "machine";
|
|
|
|
machineNames = map (name: "${name}: Machine;") pythonizedNames;
|
|
|
|
pythonizedNames = map pythonizeName nodeHostNames;
|
|
|
|
in
|
|
|
|
{
|
2024-03-17 18:48:49 +00:00
|
|
|
driver = lib.mkForce (
|
|
|
|
hostPkgs.runCommand "nixos-test-driver-${config.name}"
|
|
|
|
{
|
|
|
|
nativeBuildInputs = [
|
|
|
|
hostPkgs.makeWrapper
|
|
|
|
] ++ lib.optionals (!config.skipTypeCheck) [ hostPkgs.mypy ];
|
|
|
|
buildInputs = [ testDriver ];
|
|
|
|
testScript = config.testScriptString;
|
|
|
|
preferLocalBuild = true;
|
|
|
|
passthru = config.passthru;
|
|
|
|
meta = config.meta // {
|
|
|
|
mainProgram = "nixos-test-driver";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
''
|
|
|
|
mkdir -p $out/bin
|
2023-10-14 13:54:56 +00:00
|
|
|
|
2024-03-17 18:48:49 +00:00
|
|
|
containers=(${toString containers})
|
2023-10-14 13:54:56 +00:00
|
|
|
|
2024-03-17 18:48:49 +00:00
|
|
|
${lib.optionalString (!config.skipTypeCheck) ''
|
|
|
|
# prepend type hints so the test script can be type checked with mypy
|
|
|
|
cat "${./test-script-prepend.py}" >> testScriptWithTypes
|
|
|
|
echo "${builtins.toString machineNames}" >> testScriptWithTypes
|
|
|
|
echo -n "$testScript" >> testScriptWithTypes
|
2023-10-14 13:54:56 +00:00
|
|
|
|
2024-03-17 18:48:49 +00:00
|
|
|
echo "Running type check (enable/disable: config.skipTypeCheck)"
|
|
|
|
echo "See https://nixos.org/manual/nixos/stable/#test-opt-skipTypeCheck"
|
2023-10-14 13:54:56 +00:00
|
|
|
|
2024-03-17 18:48:49 +00:00
|
|
|
mypy --no-implicit-optional \
|
|
|
|
--pretty \
|
|
|
|
--no-color-output \
|
|
|
|
testScriptWithTypes
|
|
|
|
''}
|
2023-10-14 13:54:56 +00:00
|
|
|
|
2024-03-17 18:48:49 +00:00
|
|
|
echo -n "$testScript" >> $out/test-script
|
2023-10-14 13:54:56 +00:00
|
|
|
|
2024-03-17 18:48:49 +00:00
|
|
|
ln -s ${testDriver}/bin/nixos-test-driver $out/bin/nixos-test-driver
|
2023-10-14 13:54:56 +00:00
|
|
|
|
2024-03-17 18:48:49 +00:00
|
|
|
wrapProgram $out/bin/nixos-test-driver \
|
|
|
|
${lib.concatStringsSep " " (map (name: "--add-flags '--container ${name}'") containers)} \
|
|
|
|
--add-flags "--test-script '$out/test-script'"
|
|
|
|
''
|
|
|
|
);
|
2023-10-14 13:54:56 +00:00
|
|
|
|
2024-03-17 18:48:49 +00:00
|
|
|
test = lib.mkForce (
|
|
|
|
lib.lazyDerivation {
|
|
|
|
# lazyDerivation improves performance when only passthru items and/or meta are used.
|
|
|
|
derivation = hostPkgs.stdenv.mkDerivation {
|
|
|
|
name = "vm-test-run-${config.name}";
|
2023-10-14 13:54:56 +00:00
|
|
|
|
2024-03-17 18:48:49 +00:00
|
|
|
requiredSystemFeatures = [ "uid-range" ];
|
2023-10-14 13:54:56 +00:00
|
|
|
|
2024-03-17 18:48:49 +00:00
|
|
|
buildCommand = ''
|
|
|
|
mkdir -p $out
|
2023-10-14 13:54:56 +00:00
|
|
|
|
2024-03-17 18:48:49 +00:00
|
|
|
# effectively mute the XMLLogger
|
|
|
|
export LOGFILE=/dev/null
|
2023-10-14 13:54:56 +00:00
|
|
|
|
2024-03-17 18:48:49 +00:00
|
|
|
${config.driver}/bin/nixos-test-driver -o $out
|
|
|
|
'';
|
2023-10-14 13:54:56 +00:00
|
|
|
|
2024-03-17 18:48:49 +00:00
|
|
|
passthru = config.passthru;
|
2023-10-14 13:54:56 +00:00
|
|
|
|
2024-03-17 18:48:49 +00:00
|
|
|
meta = config.meta;
|
|
|
|
};
|
|
|
|
inherit (config) passthru meta;
|
|
|
|
}
|
|
|
|
);
|
2023-10-14 13:54:56 +00:00
|
|
|
}
|