git.iko.soy/typeable/nixpkgs
on master help sign in
nixos/tests: format the testing-python.nix file more consistenly
commit61b09f552c1b66befe83b114510c686ea98ffd0a authorAndreas Rammhold <andreas@rammhold.de> dateFri 23 Oct 2020, 23:13 (6 years ago) parent04100cd
1 file changed+106−98
nixos/lib/testing-python.nix+106−98
nixos/lib/testing-python.nix+106−98
@@ -3,11 +3,12 @@
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
1213with pkgs;
1314
@@ -16,39 +17,41 @@ rec {
1617 inherit pkgs;
1718
1819
19− mkTestDriver = let
20− testDriverScript = ./test-driver/test-driver.py;
21− in qemu_pkg: stdenv.mkDerivation {
22− name = "nixos-test-driver";
20+ mkTestDriver =
21+ let
22+ testDriverScript = ./test-driver/test-driver.py;
23+ in
24+ qemu_pkg: stdenv.mkDerivation {
25+ name = "nixos-test-driver";
2326
24− nativeBuildInputs = [ makeWrapper ];
25− buildInputs = [ (python3.withPackages (p: [ p.ptpython ])) ];
26− checkInputs = with python3Packages; [ pylint black mypy ];
27+ nativeBuildInputs = [ makeWrapper ];
28+ buildInputs = [ (python3.withPackages (p: [ p.ptpython ])) ];
29+ checkInputs = with python3Packages; [ pylint black mypy ];
2730
28− dontUnpack = true;
31+ dontUnpack = true;
2932
30− preferLocalBuild = true;
33+ preferLocalBuild = true;
3134
32− doCheck = true;
33− checkPhase = ''
34− mypy --disallow-untyped-defs \
35− --no-implicit-optional \
36− --ignore-missing-imports ${testDriverScript}
37− pylint --errors-only ${testDriverScript}
38− black --check --diff ${testDriverScript}
39− '';
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}
42+ '';
4043
41− installPhase =
42− ''
43− mkdir -p $out/bin
44− cp ${testDriverScript} $out/bin/nixos-test-driver
45− chmod u+x $out/bin/nixos-test-driver
46− # TODO: copy user script part into this file (append)
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)
4750
48− wrapProgram $out/bin/nixos-test-driver \
49− --prefix PATH : "${lib.makeBinPath [ qemu_pkg vde2 netpbm coreutils ]}" \
50− '';
51− };
51+ wrapProgram $out/bin/nixos-test-driver \
52+ --prefix PATH : "${lib.makeBinPath [ qemu_pkg vde2 netpbm coreutils ]}" \
53+ '';
54+ };
5255
5356 # Run an automated test suite in the given virtual network.
5457 # `driver' is the script that runs the network.
@@ -71,11 +74,10 @@ rec {
7174 { testScript
7275 , enableOCR ? false
7376 , name ? "unnamed"
74− # Skip linting (mainly intended for faster dev cycles)
77+ # Skip linting (mainly intended for faster dev cycles)
7578 , skipLint ? false
7679 , ...
7780 } @ t:
78−
7981 let
8082 # A standard store path to the vm monitor is built like this:
8183 # /tmp/nix-build-vm-test-run-$name.drv-0/vm-state-machine/monitor
@@ -94,7 +96,7 @@ rec {
9496 # interactively with the specified network, and for starting the
9597 # VMs from the command line.
9698 mkDriver = qemu_pkg:
97− let
99+ let
98100 build-vms = import ./build-vms.nix {
99101 inherit system pkgs minimal specialArgs;
100102 extraConfigurations = extraConfigurations ++ (pkgs.lib.optional (qemu_pkg != null)
@@ -108,7 +110,8 @@ rec {
108110 testDriver = mkTestDriver (if qemu_pkg == null then pkgs.qemu_test else qemu_pkg);
109111
110112 nodes = build-vms.buildVirtualNetwork (
111− t.nodes or (if t ? machine then { machine = t.machine; } else { }));
113+ t.nodes or (if t ? machine then { machine = t.machine; } else { })
114+ );
112115 vlans = map (m: m.config.virtualisation.vlans) (lib.attrValues nodes);
113116 vms = map (m: m.config.system.build.vm) (lib.attrValues nodes);
114117
@@ -120,49 +123,51 @@ rec {
120123
121124 testDriverName = with builtins;
122125 if testNameLen > maxTestNameLen then
123− abort ("The name of the test '${name}' must not be longer than ${toString maxTestNameLen} " +
124− "it's currently ${toString testNameLen} characters long.")
126+ abort
127+ ("The name of the test '${name}' must not be longer than ${toString maxTestNameLen} " +
128+ "it's currently ${toString testNameLen} characters long.")
125129 else
126130 "nixos-test-driver-${name}";
127131
128132 warn = if skipLint then lib.warn "Linting is disabled!" else lib.id;
129133 in
130134 warn (runCommand testDriverName
131− { buildInputs = [ makeWrapper];
132− testScript = testScript';
133− preferLocalBuild = true;
134− testName = name;
135− passthru = {
136− inherit nodes;
137− };
138− }
139− ''
140− mkdir -p $out/bin
141−
142− echo -n "$testScript" > $out/test-script
143− ${lib.optionalString (!skipLint) ''
144− ${python3Packages.black}/bin/black --check --diff $out/test-script
145− ''}
146−
147− ln -s ${testDriver}/bin/nixos-test-driver $out/bin/
148− vms=($(for i in ${toString vms}; do echo $i/bin/run-*-vm; done))
149− wrapProgram $out/bin/nixos-test-driver \
150− --add-flags "''${vms[*]}" \
151− ${lib.optionalString enableOCR
152− "--prefix PATH : '${ocrProg}/bin:${imagemagick_tiff}/bin'"} \
153− --run "export testScript=\"\$(${coreutils}/bin/cat $out/test-script)\"" \
154− --set VLANS '${toString vlans}'
155− ln -s ${testDriver}/bin/nixos-test-driver $out/bin/nixos-run-vms
156− wrapProgram $out/bin/nixos-run-vms \
157− --add-flags "''${vms[*]}" \
158− ${lib.optionalString enableOCR "--prefix PATH : '${ocrProg}/bin'"} \
159− --set tests 'start_all(); join_all();' \
160− --set VLANS '${toString vlans}' \
161− ${lib.optionalString (builtins.length vms == 1) "--set USE_SERIAL 1"}
162− ''); # "
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+ ''); # "
163168
164169 passMeta = drv: drv // lib.optionalAttrs (t ? meta) {
165− meta = (drv.meta or {}) // t.meta;
170+ meta = (drv.meta or { }) // t.meta;
166171 };
167172
168173 driver = mkDriver null;
@@ -172,22 +177,23 @@ rec {
172177
173178 nodeNames = builtins.attrNames driver.nodes;
174179 invalidNodeNames = lib.filter
175− (node: builtins.match "^[A-z_]([A-z0-9_]+)?$" node == null) nodeNames;
180+ (node: builtins.match "^[A-z_]([A-z0-9_]+)?$" node == null)
181+ nodeNames;
176182
177183 in
178− if lib.length invalidNodeNames > 0 then
179− throw ''
180− Cannot create machines out of (${lib.concatStringsSep ", " invalidNodeNames})!
181− All machines are referenced as python variables in the testing framework which will break the
182− 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.
183189
184− Please stick to alphanumeric chars and underscores as separation.
185− ''
186− else
190+ Please stick to alphanumeric chars and underscores as separation.
191+ ''
192+ else
187193 test // {
188− inherit test driver driverInteractive;
189− inherit (test) nodes;
190− };
194+ inherit test driver driverInteractive;
195+ inherit (test) nodes;
196+ };
191197
192198 runInMachine =
193199 { drv
@@ -202,8 +208,10 @@ rec {
202208 };
203209
204210 vm = build-vms.buildVM { }
205− [ machine
206− { key = "run-in-machine";
211+ [
212+ machine
213+ {
214+ key = "run-in-machine";
207215 networking.hostName = "client";
208216 nix.readOnlyStore = false;
209217 virtualisation.writableStore = false;
@@ -250,16 +258,16 @@ rec {
250258 ''; # */
251259
252260 in
253− lib.overrideDerivation drv (attrs: {
254− requiredSystemFeatures = [ "kvm" ];
255− builder = "${bash}/bin/sh";
256− args = ["-e" vmRunCommand];
257− origArgs = attrs.args;
258− origBuilder = attrs.builder;
259− });
261+ lib.overrideDerivation drv (attrs: {
262+ requiredSystemFeatures = [ "kvm" ];
263+ builder = "${bash}/bin/sh";
264+ args = [ "-e" vmRunCommand ];
265+ origArgs = attrs.args;
266+ origBuilder = attrs.builder;
267+ });
260268
261269
262− runInMachineWithX = { require ? [], ... } @ args:
270+ runInMachineWithX = { require ? [ ], ... } @ args:
263271 let
264272 client =
265273 { ... }:
@@ -275,13 +283,13 @@ rec {
275283 services.xserver.windowManager.icewm.enable = true;
276284 };
277285 in
278− runInMachine ({
279− machine = client;
280− preBuild =
281− ''
282− client.wait_for_x()
283− '';
284− } // args);
286+ runInMachine ({
287+ machine = client;
288+ preBuild =
289+ ''
290+ client.wait_for_x()
291+ '';
292+ } // args);
285293
286294
287295 simpleTest = as: (makeTest as).test;