2022-06-06 13:29:04 +02:00
|
|
|
{ config, lib, hostPkgs, ... }:
|
|
|
|
let
|
2022-06-27 20:06:30 +02:00
|
|
|
inherit (lib) mkOption types literalMD mdDoc;
|
2022-06-06 13:29:04 +02:00
|
|
|
|
|
|
|
# Reifies and correctly wraps the python test driver for
|
|
|
|
# the respective qemu version and with or without ocr support
|
|
|
|
testDriver = hostPkgs.callPackage ../test-driver {
|
|
|
|
inherit (config) enableOCR extraPythonPackages;
|
|
|
|
qemu_pkg = config.qemu.package;
|
|
|
|
imagemagick_light = hostPkgs.imagemagick_light.override { inherit (hostPkgs) libtiff; };
|
|
|
|
tesseract4 = hostPkgs.tesseract4.override { enableLanguages = [ "eng" ]; };
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2023-05-22 05:38:52 +02:00
|
|
|
vlans = map (m: (
|
|
|
|
m.virtualisation.vlans ++
|
|
|
|
(lib.mapAttrsToList (_: v: v.vlan) m.virtualisation.interfaces))) (lib.attrValues config.nodes);
|
2022-06-06 13:29:04 +02:00
|
|
|
vms = map (m: m.system.build.vm) (lib.attrValues config.nodes);
|
|
|
|
|
|
|
|
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";
|
|
|
|
|
2023-04-25 10:48:47 +02:00
|
|
|
pythonizeName = name:
|
|
|
|
let
|
|
|
|
head = lib.substring 0 1 name;
|
|
|
|
tail = lib.substring 1 (-1) name;
|
|
|
|
in
|
|
|
|
(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;
|
2022-06-06 13:29:04 +02:00
|
|
|
|
|
|
|
uniqueVlans = lib.unique (builtins.concatLists vlans);
|
|
|
|
vlanNames = map (i: "vlan${toString i}: VLan;") uniqueVlans;
|
2023-04-25 10:48:47 +02:00
|
|
|
pythonizedNames = map pythonizeName nodeHostNames;
|
|
|
|
machineNames = map (name: "${name}: Machine;") pythonizedNames;
|
2022-06-06 13:29:04 +02:00
|
|
|
|
2023-04-25 10:48:47 +02:00
|
|
|
withChecks = lib.warnIf config.skipLint "Linting is disabled";
|
2022-06-06 13:29:04 +02:00
|
|
|
|
|
|
|
driver =
|
|
|
|
hostPkgs.runCommand "nixos-test-driver-${config.name}"
|
|
|
|
{
|
|
|
|
# inherit testName; TODO (roberth): need this?
|
|
|
|
nativeBuildInputs = [
|
|
|
|
hostPkgs.makeWrapper
|
|
|
|
] ++ lib.optionals (!config.skipTypeCheck) [ hostPkgs.mypy ];
|
2022-06-26 13:57:58 +02:00
|
|
|
buildInputs = [ testDriver ];
|
2022-06-06 13:29:04 +02:00
|
|
|
testScript = config.testScriptString;
|
|
|
|
preferLocalBuild = true;
|
|
|
|
passthru = config.passthru;
|
|
|
|
meta = config.meta // {
|
|
|
|
mainProgram = "nixos-test-driver";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
''
|
|
|
|
mkdir -p $out/bin
|
|
|
|
|
|
|
|
vmStartScripts=($(for i in ${toString vms}; do echo $i/bin/run-*-vm; done))
|
|
|
|
|
|
|
|
${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 "${builtins.toString vlanNames}" >> testScriptWithTypes
|
|
|
|
echo -n "$testScript" >> testScriptWithTypes
|
|
|
|
|
2023-07-07 16:02:58 +02:00
|
|
|
echo "Running type check (enable/disable: config.skipTypeCheck)"
|
2023-07-07 23:01:55 +02:00
|
|
|
echo "See https://nixos.org/manual/nixos/stable/#test-opt-skipTypeCheck"
|
2022-06-06 13:29:04 +02:00
|
|
|
|
|
|
|
mypy --no-implicit-optional \
|
|
|
|
--pretty \
|
|
|
|
--no-color-output \
|
|
|
|
testScriptWithTypes
|
|
|
|
''}
|
|
|
|
|
|
|
|
echo -n "$testScript" >> $out/test-script
|
|
|
|
|
|
|
|
ln -s ${testDriver}/bin/nixos-test-driver $out/bin/nixos-test-driver
|
|
|
|
|
|
|
|
${testDriver}/bin/generate-driver-symbols
|
|
|
|
${lib.optionalString (!config.skipLint) ''
|
2023-07-07 16:02:58 +02:00
|
|
|
echo "Linting test script (enable/disable: config.skipLint)"
|
2023-07-07 23:01:55 +02:00
|
|
|
echo "See https://nixos.org/manual/nixos/stable/#test-opt-skipLint"
|
2023-07-07 16:02:58 +02:00
|
|
|
|
2022-06-06 13:29:04 +02:00
|
|
|
PYFLAKES_BUILTINS="$(
|
2023-04-25 10:48:47 +02:00
|
|
|
echo -n ${lib.escapeShellArg (lib.concatStringsSep "," pythonizedNames)},
|
2022-06-06 13:29:04 +02:00
|
|
|
< ${lib.escapeShellArg "driver-symbols"}
|
|
|
|
)" ${hostPkgs.python3Packages.pyflakes}/bin/pyflakes $out/test-script
|
|
|
|
''}
|
|
|
|
|
|
|
|
# set defaults through environment
|
|
|
|
# see: ./test-driver/test-driver.py argparse implementation
|
|
|
|
wrapProgram $out/bin/nixos-test-driver \
|
|
|
|
--set startScripts "''${vmStartScripts[*]}" \
|
|
|
|
--set testScript "$out/test-script" \
|
2023-10-23 02:11:11 +02:00
|
|
|
--set globalTimeout "${toString config.globalTimeout}" \
|
2022-06-06 13:29:04 +02:00
|
|
|
--set vlans '${toString vlans}' \
|
|
|
|
${lib.escapeShellArgs (lib.concatMap (arg: ["--add-flags" arg]) config.extraDriverArgs)}
|
|
|
|
'';
|
|
|
|
|
|
|
|
in
|
|
|
|
{
|
|
|
|
options = {
|
|
|
|
|
|
|
|
driver = mkOption {
|
2022-06-27 20:06:30 +02:00
|
|
|
description = mdDoc "Package containing a script that runs the test.";
|
2022-06-06 13:29:04 +02:00
|
|
|
type = types.package;
|
2022-06-27 20:06:30 +02:00
|
|
|
defaultText = literalMD "set by the test framework";
|
2022-06-06 13:29:04 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
hostPkgs = mkOption {
|
2022-06-27 20:06:30 +02:00
|
|
|
description = mdDoc "Nixpkgs attrset used outside the nodes.";
|
2022-06-06 13:29:04 +02:00
|
|
|
type = types.raw;
|
|
|
|
example = lib.literalExpression ''
|
|
|
|
import nixpkgs { inherit system config overlays; }
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
qemu.package = mkOption {
|
2022-09-29 12:41:59 +02:00
|
|
|
description = mdDoc "Which qemu package to use for the virtualisation of [{option}`nodes`](#test-opt-nodes).";
|
2022-06-06 13:29:04 +02:00
|
|
|
type = types.package;
|
|
|
|
default = hostPkgs.qemu_test;
|
|
|
|
defaultText = "hostPkgs.qemu_test";
|
|
|
|
};
|
|
|
|
|
2023-10-23 02:11:11 +02:00
|
|
|
globalTimeout = mkOption {
|
|
|
|
description = mdDoc ''
|
|
|
|
A global timeout for the complete test, expressed in seconds.
|
|
|
|
Beyond that timeout, every resource will be killed and released and the test will fail.
|
|
|
|
|
|
|
|
By default, we use a 1 hour timeout.
|
|
|
|
'';
|
|
|
|
type = types.int;
|
|
|
|
default = 60 * 60;
|
|
|
|
example = 10 * 60;
|
|
|
|
};
|
|
|
|
|
2022-06-06 13:29:04 +02:00
|
|
|
enableOCR = mkOption {
|
2022-06-27 20:06:30 +02:00
|
|
|
description = mdDoc ''
|
2022-06-06 13:29:04 +02:00
|
|
|
Whether to enable Optical Character Recognition functionality for
|
2022-06-27 20:06:30 +02:00
|
|
|
testing graphical programs. See [Machine objects](`ssec-machine-objects`).
|
2022-06-06 13:29:04 +02:00
|
|
|
'';
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
extraPythonPackages = mkOption {
|
2022-06-27 20:06:30 +02:00
|
|
|
description = mdDoc ''
|
2022-06-06 13:29:04 +02:00
|
|
|
Python packages to add to the test driver.
|
|
|
|
|
|
|
|
The argument is a Python package set, similar to `pkgs.pythonPackages`.
|
|
|
|
'';
|
2022-06-27 20:06:30 +02:00
|
|
|
example = lib.literalExpression ''
|
|
|
|
p: [ p.numpy ]
|
|
|
|
'';
|
2022-06-06 13:29:04 +02:00
|
|
|
type = types.functionTo (types.listOf types.package);
|
|
|
|
default = ps: [ ];
|
|
|
|
};
|
|
|
|
|
|
|
|
extraDriverArgs = mkOption {
|
2022-06-27 20:06:30 +02:00
|
|
|
description = mdDoc ''
|
2022-06-06 13:29:04 +02:00
|
|
|
Extra arguments to pass to the test driver.
|
2022-06-27 20:06:30 +02:00
|
|
|
|
2022-09-29 12:41:59 +02:00
|
|
|
They become part of [{option}`driver`](#test-opt-driver) via `wrapProgram`.
|
2022-06-06 13:29:04 +02:00
|
|
|
'';
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [];
|
|
|
|
};
|
|
|
|
|
|
|
|
skipLint = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-06-27 20:06:30 +02:00
|
|
|
description = mdDoc ''
|
|
|
|
Do not run the linters. This may speed up your iteration cycle, but it is not something you should commit.
|
|
|
|
'';
|
2022-06-06 13:29:04 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
skipTypeCheck = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-06-27 20:06:30 +02:00
|
|
|
description = mdDoc ''
|
|
|
|
Disable type checking. This must not be enabled for new NixOS tests.
|
|
|
|
|
2022-09-29 12:41:59 +02:00
|
|
|
This may speed up your iteration cycle, unless you're working on the [{option}`testScript`](#test-opt-testScript).
|
2022-06-27 20:06:30 +02:00
|
|
|
'';
|
2022-06-06 13:29:04 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = {
|
2023-08-20 11:14:08 +02:00
|
|
|
_module.args = {
|
|
|
|
hostPkgs =
|
|
|
|
# Comment is in nixos/modules/misc/nixpkgs.nix
|
|
|
|
lib.mkOverride lib.modules.defaultOverridePriority
|
|
|
|
config.hostPkgs.__splicedPackages;
|
|
|
|
};
|
2022-06-06 13:29:04 +02:00
|
|
|
|
|
|
|
driver = withChecks driver;
|
|
|
|
|
|
|
|
# make available on the test runner
|
|
|
|
passthru.driver = config.driver;
|
|
|
|
};
|
|
|
|
}
|