···
1
+
{ config, lib, hostPkgs, ... }:
3
+
inherit (lib) mkOption types;
5
+
# Reifies and correctly wraps the python test driver for
6
+
# the respective qemu version and with or without ocr support
7
+
testDriver = hostPkgs.callPackage ../test-driver {
8
+
inherit (config) enableOCR extraPythonPackages;
9
+
qemu_pkg = config.qemu.package;
10
+
imagemagick_light = hostPkgs.imagemagick_light.override { inherit (hostPkgs) libtiff; };
11
+
tesseract4 = hostPkgs.tesseract4.override { enableLanguages = [ "eng" ]; };
15
+
vlans = map (m: m.virtualisation.vlans) (lib.attrValues config.nodes);
16
+
vms = map (m: m.system.build.vm) (lib.attrValues config.nodes);
20
+
nodesList = map (c: c.system.name) (lib.attrValues config.nodes);
22
+
nodesList ++ lib.optional (lib.length nodesList == 1 && !lib.elem "machine" nodesList) "machine";
24
+
# TODO: This is an implementation error and needs fixing
25
+
# the testing famework cannot legitimately restrict hostnames further
27
+
invalidNodeNames = lib.filter
28
+
(node: builtins.match "^[A-z_]([A-z0-9_]+)?$" node == null)
31
+
uniqueVlans = lib.unique (builtins.concatLists vlans);
32
+
vlanNames = map (i: "vlan${toString i}: VLan;") uniqueVlans;
33
+
machineNames = map (name: "${name}: Machine;") nodeHostNames;
36
+
if lib.length invalidNodeNames > 0 then
38
+
Cannot create machines out of (${lib.concatStringsSep ", " invalidNodeNames})!
39
+
All machines are referenced as python variables in the testing framework which will break the
40
+
script when special characters are used.
42
+
This is an IMPLEMENTATION ERROR and needs to be fixed. Meanwhile,
43
+
please stick to alphanumeric chars and underscores as separation.
46
+
lib.warnIf config.skipLint "Linting is disabled";
49
+
hostPkgs.runCommand "nixos-test-driver-${config.name}"
51
+
# inherit testName; TODO (roberth): need this?
52
+
nativeBuildInputs = [
53
+
hostPkgs.makeWrapper
54
+
] ++ lib.optionals (!config.skipTypeCheck) [ hostPkgs.mypy ];
55
+
testScript = config.testScriptString;
56
+
preferLocalBuild = true;
57
+
passthru = config.passthru;
58
+
meta = config.meta // {
59
+
mainProgram = "nixos-test-driver";
65
+
vmStartScripts=($(for i in ${toString vms}; do echo $i/bin/run-*-vm; done))
67
+
${lib.optionalString (!config.skipTypeCheck) ''
68
+
# prepend type hints so the test script can be type checked with mypy
69
+
cat "${../test-script-prepend.py}" >> testScriptWithTypes
70
+
echo "${builtins.toString machineNames}" >> testScriptWithTypes
71
+
echo "${builtins.toString vlanNames}" >> testScriptWithTypes
72
+
echo -n "$testScript" >> testScriptWithTypes
74
+
cat -n testScriptWithTypes
76
+
# set pythonpath so mypy knows where to find the imports. this requires the py.typed file.
77
+
export PYTHONPATH='${../test-driver}'
78
+
mypy --no-implicit-optional \
85
+
echo -n "$testScript" >> $out/test-script
87
+
ln -s ${testDriver}/bin/nixos-test-driver $out/bin/nixos-test-driver
89
+
${testDriver}/bin/generate-driver-symbols
90
+
${lib.optionalString (!config.skipLint) ''
91
+
PYFLAKES_BUILTINS="$(
92
+
echo -n ${lib.escapeShellArg (lib.concatStringsSep "," nodeHostNames)},
93
+
< ${lib.escapeShellArg "driver-symbols"}
94
+
)" ${hostPkgs.python3Packages.pyflakes}/bin/pyflakes $out/test-script
97
+
# set defaults through environment
98
+
# see: ./test-driver/test-driver.py argparse implementation
99
+
wrapProgram $out/bin/nixos-test-driver \
100
+
--set startScripts "''${vmStartScripts[*]}" \
101
+
--set testScript "$out/test-script" \
102
+
--set vlans '${toString vlans}' \
103
+
${lib.escapeShellArgs (lib.concatMap (arg: ["--add-flags" arg]) config.extraDriverArgs)}
110
+
driver = mkOption {
111
+
description = "Script that runs the test.";
112
+
type = types.package;
113
+
defaultText = lib.literalDocBook "set by the test framework";
116
+
hostPkgs = mkOption {
117
+
description = "Nixpkgs attrset used outside the nodes.";
119
+
example = lib.literalExpression ''
120
+
import nixpkgs { inherit system config overlays; }
124
+
qemu.package = mkOption {
125
+
description = "Which qemu package to use.";
126
+
type = types.package;
127
+
default = hostPkgs.qemu_test;
128
+
defaultText = "hostPkgs.qemu_test";
131
+
enableOCR = mkOption {
133
+
Whether to enable Optical Character Recognition functionality for
134
+
testing graphical programs.
140
+
extraPythonPackages = mkOption {
142
+
Python packages to add to the test driver.
144
+
The argument is a Python package set, similar to `pkgs.pythonPackages`.
146
+
type = types.functionTo (types.listOf types.package);
150
+
extraDriverArgs = mkOption {
152
+
Extra arguments to pass to the test driver.
154
+
type = types.listOf types.str;
158
+
skipLint = mkOption {
163
+
skipTypeCheck = mkOption {
170
+
_module.args.hostPkgs = config.hostPkgs;
172
+
driver = withChecks driver;
174
+
# make available on the test runner
175
+
passthru.driver = config.driver;