git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #101598 from andir/nixos-build-vms-qemu

nixos/tests: follow-up to the closure reduction PR

commit1088f059401c43450ed85e4a4ec3f57dc8aa992f authorAndreas Rammhold <andreas@rammhold.de> dateMon 26 Oct 2020, 14:19 (6 years ago) parent4c07f77d4fb7da
2 files changed+148−127
nixos/lib/testing-python.nix+146−120 nixos/modules/installer/tools/nixos-build-vms/build-vms.nix+2−7
nixos/lib/testing-python.nix+146−120
@@ -3,13 +3,13 @@
33 # Use a minimal kernel?
44, minimal ? false
55 # Ignored
6−, config ? {}
6+, config ? { }
77 # !!! See comment about args in lib/modules.nix
8−, specialArgs ? {}
8+, specialArgs ? { }
99 # Modules to add to each VM
10−, extraConfigurations ? [] }:
10+, extraConfigurations ? [ ]
11+}:
1112
12−with import ./build-vms.nix { inherit system pkgs minimal specialArgs extraConfigurations; };
1313with pkgs;
1414
1515rec {
@@ -17,42 +17,41 @@ rec {
1717 inherit pkgs;
1818
1919
20− mkTestDriver = let
21− testDriverScript = ./test-driver/test-driver.py;
22− in qemu_pkg: stdenv.mkDerivation {
23− name = "nixos-test-driver";
24−
25− nativeBuildInputs = [ makeWrapper ];
26− buildInputs = [ (python3.withPackages (p: [ p.ptpython ])) ];
27− checkInputs = with python3Packages; [ pylint black mypy ];
28−
29− dontUnpack = true;
20+ mkTestDriver =
21+ let
22+ testDriverScript = ./test-driver/test-driver.py;
23+ in
24+ qemu_pkg: stdenv.mkDerivation {
25+ name = "nixos-test-driver";
3026
31− preferLocalBuild = true;
27+ nativeBuildInputs = [ makeWrapper ];
28+ buildInputs = [ (python3.withPackages (p: [ p.ptpython ])) ];
29+ checkInputs = with python3Packages; [ pylint black mypy ];
3230
33− doCheck = true;
34− checkPhase = ''
35− mypy --disallow-untyped-defs \
36− --no-implicit-optional \
37− --ignore-missing-imports ${testDriverScript}
38− pylint --errors-only ${testDriverScript}
39− black --check --diff ${testDriverScript}
40− '';
31+ dontUnpack = true;
4132
42− installPhase =
43− ''
44− mkdir -p $out/bin
45− cp ${testDriverScript} $out/bin/nixos-test-driver
46− chmod u+x $out/bin/nixos-test-driver
47− # TODO: copy user script part into this file (append)
33+ preferLocalBuild = true;
4834
49− wrapProgram $out/bin/nixos-test-driver \
50− --prefix PATH : "${lib.makeBinPath [ qemu_pkg vde2 netpbm coreutils ]}" \
35+ doCheck = true;
36+ checkPhase = ''
37+ mypy --disallow-untyped-defs \
38+ --no-implicit-optional \
39+ --ignore-missing-imports ${testDriverScript}
40+ pylint --errors-only ${testDriverScript}
41+ black --check --diff ${testDriverScript}
5142 '';
52− };
5343
54− testDriver = mkTestDriver qemu_test;
55− testDriverInteractive = mkTestDriver qemu_kvm;
44+ installPhase =
45+ ''
46+ mkdir -p $out/bin
47+ cp ${testDriverScript} $out/bin/nixos-test-driver
48+ chmod u+x $out/bin/nixos-test-driver
49+ # TODO: copy user script part into this file (append)
50+
51+ wrapProgram $out/bin/nixos-test-driver \
52+ --prefix PATH : "${lib.makeBinPath [ qemu_pkg vde2 netpbm coreutils ]}" \
53+ '';
54+ };
5655
5756 # Run an automated test suite in the given virtual network.
5857 # `driver' is the script that runs the network.
@@ -75,11 +74,10 @@ rec {
7574 { testScript
7675 , enableOCR ? false
7776 , name ? "unnamed"
78− # Skip linting (mainly intended for faster dev cycles)
77+ # Skip linting (mainly intended for faster dev cycles)
7978 , skipLint ? false
8079 , ...
8180 } @ t:
82−
8381 let
8482 # A standard store path to the vm monitor is built like this:
8583 # /tmp/nix-build-vm-test-run-$name.drv-0/vm-state-machine/monitor
@@ -88,25 +86,7 @@ rec {
8886 maxTestNameLen = 50;
8987 testNameLen = builtins.stringLength name;
9088
91− testDriverName = with builtins;
92− if testNameLen > maxTestNameLen then
93− abort ("The name of the test '${name}' must not be longer than ${toString maxTestNameLen} " +
94− "it's currently ${toString testNameLen} characters long.")
95− else
96− "nixos-test-driver-${name}";
97−
98− nodes = buildVirtualNetwork (
99− t.nodes or (if t ? machine then { machine = t.machine; } else { }));
10089
101− testScript' =
102− # Call the test script with the computed nodes.
103− if lib.isFunction testScript
104− then testScript { inherit nodes; }
105− else testScript;
106−
107− vlans = map (m: m.config.virtualisation.vlans) (lib.attrValues nodes);
108−
109− vms = map (m: m.config.system.build.vm) (lib.attrValues nodes);
11090
11191 ocrProg = tesseract4.override { enableLanguages = [ "eng" ]; };
11292
@@ -115,78 +95,124 @@ rec {
11595 # Generate convenience wrappers for running the test driver
11696 # interactively with the specified network, and for starting the
11797 # VMs from the command line.
118− driver = testDriver:
98+ mkDriver = qemu_pkg:
11999 let
100+ build-vms = import ./build-vms.nix {
101+ inherit system pkgs minimal specialArgs;
102+ extraConfigurations = extraConfigurations ++ (pkgs.lib.optional (qemu_pkg != null)
103+ {
104+ virtualisation.qemu.package = qemu_pkg;
105+ }
106+ );
107+ };
108+
109+ # FIXME: get this pkg from the module system
110+ testDriver = mkTestDriver (if qemu_pkg == null then pkgs.qemu_test else qemu_pkg);
111+
112+ nodes = build-vms.buildVirtualNetwork (
113+ t.nodes or (if t ? machine then { machine = t.machine; } else { })
114+ );
115+ vlans = map (m: m.config.virtualisation.vlans) (lib.attrValues nodes);
116+ vms = map (m: m.config.system.build.vm) (lib.attrValues nodes);
117+
118+ testScript' =
119+ # Call the test script with the computed nodes.
120+ if lib.isFunction testScript
121+ then testScript { inherit nodes; }
122+ else testScript;
123+
124+ testDriverName = with builtins;
125+ if testNameLen > maxTestNameLen then
126+ abort
127+ ("The name of the test '${name}' must not be longer than ${toString maxTestNameLen} " +
128+ "it's currently ${toString testNameLen} characters long.")
129+ else
130+ "nixos-test-driver-${name}";
131+
120132 warn = if skipLint then lib.warn "Linting is disabled!" else lib.id;
121133 in
122134 warn (runCommand testDriverName
123− { buildInputs = [ makeWrapper];
124− testScript = testScript';
125− preferLocalBuild = true;
126− testName = name;
127− }
128− ''
129− mkdir -p $out/bin
130−
131− echo -n "$testScript" > $out/test-script
132− ${lib.optionalString (!skipLint) ''
133− ${python3Packages.black}/bin/black --check --diff $out/test-script
134− ''}
135−
136− ln -s ${testDriver}/bin/nixos-test-driver $out/bin/
137− vms=($(for i in ${toString vms}; do echo $i/bin/run-*-vm; done))
138− wrapProgram $out/bin/nixos-test-driver \
139− --add-flags "''${vms[*]}" \
140− ${lib.optionalString enableOCR
141− "--prefix PATH : '${ocrProg}/bin:${imagemagick_tiff}/bin'"} \
142− --run "export testScript=\"\$(${coreutils}/bin/cat $out/test-script)\"" \
143− --set VLANS '${toString vlans}'
144− ln -s ${testDriver}/bin/nixos-test-driver $out/bin/nixos-run-vms
145− wrapProgram $out/bin/nixos-run-vms \
146− --add-flags "''${vms[*]}" \
147− ${lib.optionalString enableOCR "--prefix PATH : '${ocrProg}/bin'"} \
148− --set tests 'start_all(); join_all();' \
149− --set VLANS '${toString vlans}' \
150− ${lib.optionalString (builtins.length vms == 1) "--set USE_SERIAL 1"}
151− ''); # "
135+ {
136+ buildInputs = [ makeWrapper ];
137+ testScript = testScript';
138+ preferLocalBuild = true;
139+ testName = name;
140+ passthru = {
141+ inherit nodes;
142+ };
143+ }
144+ ''
145+ mkdir -p $out/bin
146+
147+ echo -n "$testScript" > $out/test-script
148+ ${lib.optionalString (!skipLint) ''
149+ ${python3Packages.black}/bin/black --check --diff $out/test-script
150+ ''}
151+
152+ ln -s ${testDriver}/bin/nixos-test-driver $out/bin/
153+ vms=($(for i in ${toString vms}; do echo $i/bin/run-*-vm; done))
154+ wrapProgram $out/bin/nixos-test-driver \
155+ --add-flags "''${vms[*]}" \
156+ ${lib.optionalString enableOCR
157+ "--prefix PATH : '${ocrProg}/bin:${imagemagick_tiff}/bin'"} \
158+ --run "export testScript=\"\$(${coreutils}/bin/cat $out/test-script)\"" \
159+ --set VLANS '${toString vlans}'
160+ ln -s ${testDriver}/bin/nixos-test-driver $out/bin/nixos-run-vms
161+ wrapProgram $out/bin/nixos-run-vms \
162+ --add-flags "''${vms[*]}" \
163+ ${lib.optionalString enableOCR "--prefix PATH : '${ocrProg}/bin'"} \
164+ --set tests 'start_all(); join_all();' \
165+ --set VLANS '${toString vlans}' \
166+ ${lib.optionalString (builtins.length vms == 1) "--set USE_SERIAL 1"}
167+ ''); # "
152168
153169 passMeta = drv: drv // lib.optionalAttrs (t ? meta) {
154− meta = (drv.meta or {}) // t.meta;
170+ meta = (drv.meta or { }) // t.meta;
155171 };
156172
157− test = passMeta (runTests (driver testDriver));
173+ driver = mkDriver null;
174+ driverInteractive = mkDriver pkgs.qemu;
158175
159− nodeNames = builtins.attrNames nodes;
176+ test = passMeta (runTests driver);
177+
178+ nodeNames = builtins.attrNames driver.nodes;
160179 invalidNodeNames = lib.filter
161− (node: builtins.match "^[A-z_]([A-z0-9_]+)?$" node == null) nodeNames;
180+ (node: builtins.match "^[A-z_]([A-z0-9_]+)?$" node == null)
181+ nodeNames;
162182
163183 in
164− if lib.length invalidNodeNames > 0 then
165− throw ''
166− Cannot create machines out of (${lib.concatStringsSep ", " invalidNodeNames})!
167− All machines are referenced as python variables in the testing framework which will break the
168− script when special characters are used.
184+ if lib.length invalidNodeNames > 0 then
185+ throw ''
186+ Cannot create machines out of (${lib.concatStringsSep ", " invalidNodeNames})!
187+ All machines are referenced as python variables in the testing framework which will break the
188+ script when special characters are used.
169189
170− Please stick to alphanumeric chars and underscores as separation.
171− ''
172− else
173− test // {
174− inherit nodes test;
175− driver = driver testDriver;
176− driverInteractive = driver testDriverInteractive;
177− };
190+ Please stick to alphanumeric chars and underscores as separation.
191+ ''
192+ else
193+ test // {
194+ inherit test driver driverInteractive;
195+ inherit (test) nodes;
196+ };
178197
179198 runInMachine =
180199 { drv
181200 , machine
182201 , preBuild ? ""
183202 , postBuild ? ""
203+ , qemu ? pkgs.qemu_test
184204 , ... # ???
185205 }:
186206 let
187− vm = buildVM { }
188− [ machine
189− { key = "run-in-machine";
207+ build-vms = import ./build-vms.nix {
208+ inherit system pkgs minimal specialArgs extraConfigurations;
209+ };
210+
211+ vm = build-vms.buildVM { }
212+ [
213+ machine
214+ {
215+ key = "run-in-machine";
190216 networking.hostName = "client";
191217 nix.readOnlyStore = false;
192218 virtualisation.writableStore = false;
@@ -229,20 +255,20 @@ rec {
229255 unset xchg
230256
231257 export tests='${testScript}'
232− ${testDriver}/bin/nixos-test-driver ${vm.config.system.build.vm}/bin/run-*-vm
258+ ${mkTestDriver qemu}/bin/nixos-test-driver --keep-vm-state ${vm.config.system.build.vm}/bin/run-*-vm
233259 ''; # */
234260
235261 in
236− lib.overrideDerivation drv (attrs: {
237− requiredSystemFeatures = [ "kvm" ];
238− builder = "${bash}/bin/sh";
239− args = ["-e" vmRunCommand];
240− origArgs = attrs.args;
241− origBuilder = attrs.builder;
242− });
262+ lib.overrideDerivation drv (attrs: {
263+ requiredSystemFeatures = [ "kvm" ];
264+ builder = "${bash}/bin/sh";
265+ args = [ "-e" vmRunCommand ];
266+ origArgs = attrs.args;
267+ origBuilder = attrs.builder;
268+ });
243269
244270
245− runInMachineWithX = { require ? [], ... } @ args:
271+ runInMachineWithX = { require ? [ ], ... } @ args:
246272 let
247273 client =
248274 { ... }:
@@ -258,13 +284,13 @@ rec {
258284 services.xserver.windowManager.icewm.enable = true;
259285 };
260286 in
261− runInMachine ({
262− machine = client;
263− preBuild =
264− ''
265− client.wait_for_x()
266− '';
267− } // args);
287+ runInMachine ({
288+ machine = client;
289+ preBuild =
290+ ''
291+ client.wait_for_x()
292+ '';
293+ } // args);
268294
269295
270296 simpleTest = as: (makeTest as).test;
nixos/modules/installer/tools/nixos-build-vms/build-vms.nix+2−7
@@ -6,12 +6,7 @@
66let
77 nodes = builtins.mapAttrs (vm: module: {
88 _file = "${networkExpr}@node-${vm}";
9− imports = [
10− module
11− ({ pkgs, ... }: {
12− virtualisation.qemu.package = pkgs.qemu;
13− })
14− ];
9+ imports = [ module ];
1510 }) (import networkExpr);
1611in
1712
@@ -20,4 +15,4 @@ with import ../../../../lib/testing-python.nix {
2015 pkgs = import ../../../../.. { inherit system config; };
2116};
2217
23−(makeTest { inherit nodes; testScript = ""; }).driver
18+(makeTest { inherit nodes; testScript = ""; }).driverInteractive