From fce8b018f06431e7684b725a520416ff3862db9f Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Sat, 25 Jun 2022 13:53:02 +0200 Subject: [PATCH 01/38] lib: Add lazyDerivation --- lib/default.nix | 2 + lib/derivations.nix | 101 ++++++++++++++++++++++++++++++++++++++++++++ lib/tests/misc.nix | 53 +++++++++++++++++++++++ 3 files changed, 156 insertions(+) create mode 100644 lib/derivations.nix diff --git a/lib/default.nix b/lib/default.nix index e2a93e63ac1f..0c0e2d5e1021 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -23,6 +23,7 @@ let # packaging customisation = callLibs ./customisation.nix; + derivations = callLibs ./derivations.nix; maintainers = import ../maintainers/maintainer-list.nix; teams = callLibs ../maintainers/team-list.nix; meta = callLibs ./meta.nix; @@ -108,6 +109,7 @@ let inherit (self.customisation) overrideDerivation makeOverridable callPackageWith callPackagesWith extendDerivation hydraJob makeScope makeScopeWithSplicing; + inherit (self.derivations) lazyDerivation; inherit (self.meta) addMetaAttrs dontDistribute setName updateName appendToName mapDerivationAttrset setPrio lowPrio lowPrioSet hiPrio hiPrioSet getLicenseFromSpdxId getExe; diff --git a/lib/derivations.nix b/lib/derivations.nix new file mode 100644 index 000000000000..9a88087f2e34 --- /dev/null +++ b/lib/derivations.nix @@ -0,0 +1,101 @@ +{ lib }: + +let + inherit (lib) throwIfNot; +in +{ + /* + Restrict a derivation to a predictable set of attribute names, so + that the returned attrset is not strict in the actual derivation, + saving a lot of computation when the derivation is non-trivial. + + This is useful in situations where a derivation might only be used for its + passthru attributes, improving evaluation performance. + + The returned attribute set is lazy in `derivation`. Specifically, this + means that the derivation will not be evaluated in at least the + situations below. + + For illustration and/or testing, we define derivation such that its + evaluation is very noticable. + + let derivation = throw "This won't be evaluated."; + + In the following expressions, `derivation` will _not_ be evaluated: + + (lazyDerivation { inherit derivation; }).type + + attrNames (lazyDerivation { inherit derivation; }) + + (lazyDerivation { inherit derivation; } // { foo = true; }).foo + + (lazyDerivation { inherit derivation; meta.foo = true; }).meta + + In these expressions, it `derivation` _will_ be evaluated: + + "${lazyDerivation { inherit derivation }}" + + (lazyDerivation { inherit derivation }).outPath + + (lazyDerivation { inherit derivation }).meta + + And the following expressions are not valid, because the refer to + implementation details and/or attributes that may not be present on + some derivations: + + (lazyDerivation { inherit derivation }).buildInputs + + (lazyDerivation { inherit derivation }).passthru + + (lazyDerivation { inherit derivation }).pythonPath + + */ + lazyDerivation = + args@{ + # The derivation to be wrapped. + derivation + , # Optional meta attribute. + # + # While this function is primarily about derivations, it can improve + # the `meta` package attribute, which is usually specified through + # `mkDerivation`. + meta ? null + , # Optional extra values to add to the returned attrset. + # + # This can be used for adding package attributes, such as `tests`. + passthru ? { } + }: + let + # These checks are strict in `drv` and some `drv` attributes, but the + # attrset spine returned by lazyDerivation does not depend on it. + # Instead, the individual derivation attributes do depend on it. + checked = + throwIfNot (derivation.type or null == "derivation") + "lazySimpleDerivation: input must be a derivation." + throwIfNot + (derivation.outputs == [ "out" ]) + # Supporting multiple outputs should be a matter of inheriting more attrs. + "The derivation ${derivation.name or ""} has multiple outputs. This is not supported by lazySimpleDerivation yet. Support could be added, and be useful as long as the set of outputs is known in advance, without evaluating the actual derivation." + derivation; + in + { + # Hardcoded `type` + # + # `lazyDerivation` requires its `derivation` argument to be a derivation, + # so if it is not, that is a programming error by the caller and not + # something that `lazyDerivation` consumers should be able to correct + # for after the fact. + # So, to improve laziness, we assume correctness here and check it only + # when actual derivation values are accessed later. + type = "derivation"; + + # A fixed set of derivation values, so that `lazyDerivation` can return + # its attrset before evaluating `derivation`. + # This must only list attributes that are available on _all_ derivations. + inherit (checked) outputs out outPath outputName drvPath name system; + + # The meta attribute can either be taken from the derivation, or if the + # `lazyDerivation` caller knew a shortcut, be taken from there. + meta = args.meta or checked.meta; + } // passthru; +} diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix index 9b1397a7915a..74020bc7c8e5 100644 --- a/lib/tests/misc.nix +++ b/lib/tests/misc.nix @@ -1207,6 +1207,59 @@ runTests { expected = true; }; + # lazyDerivation + + testLazyDerivationIsLazyInDerivationForAttrNames = { + expr = attrNames (lazyDerivation { + derivation = throw "not lazy enough"; + }); + # It's ok to add attribute names here when lazyDerivation is improved + # in accordance with its inline comments. + expected = [ "drvPath" "meta" "name" "out" "outPath" "outputName" "outputs" "system" "type" ]; + }; + + testLazyDerivationIsLazyInDerivationForPassthruAttr = { + expr = (lazyDerivation { + derivation = throw "not lazy enough"; + passthru.tests = "whatever is in tests"; + }).tests; + expected = "whatever is in tests"; + }; + + testLazyDerivationIsLazyInDerivationForPassthruAttr2 = { + # passthru.tests is not a special case. It works for any attr. + expr = (lazyDerivation { + derivation = throw "not lazy enough"; + passthru.foo = "whatever is in foo"; + }).foo; + expected = "whatever is in foo"; + }; + + testLazyDerivationIsLazyInDerivationForMeta = { + expr = (lazyDerivation { + derivation = throw "not lazy enough"; + meta = "whatever is in meta"; + }).meta; + expected = "whatever is in meta"; + }; + + testLazyDerivationReturnsDerivationAttrs = let + derivation = { + type = "derivation"; + outputs = ["out"]; + out = "test out"; + outPath = "test outPath"; + outputName = "out"; + drvPath = "test drvPath"; + name = "test name"; + system = "test system"; + meta = "test meta"; + }; + in { + expr = lazyDerivation { inherit derivation; }; + expected = derivation; + }; + testTypeDescriptionInt = { expr = (with types; int).description; expected = "signed integer"; From 1ffa30b0559a05e810a3db663da5066953d4f05a Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Mon, 6 Jun 2022 16:05:21 +0200 Subject: [PATCH 02/38] lib/modules: Fix meta duplication in shorthand syntax --- lib/modules.nix | 3 ++- lib/tests/modules.sh | 3 +++ lib/tests/modules/shorthand-meta.nix | 19 +++++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 lib/tests/modules/shorthand-meta.nix diff --git a/lib/modules.nix b/lib/modules.nix index d3a7fac82c4a..28c8da9e923a 100644 --- a/lib/modules.nix +++ b/lib/modules.nix @@ -440,13 +440,14 @@ rec { config = addFreeformType (addMeta (m.config or {})); } else + # shorthand syntax lib.throwIfNot (isAttrs m) "module ${file} (${key}) does not look like a module." { _file = toString m._file or file; key = toString m.key or key; disabledModules = m.disabledModules or []; imports = m.require or [] ++ m.imports or []; options = {}; - config = addFreeformType (addMeta (removeAttrs m ["_file" "key" "disabledModules" "require" "imports" "freeformType"])); + config = addFreeformType (removeAttrs m ["_file" "key" "disabledModules" "require" "imports" "freeformType"]); }; applyModuleArgsIfFunction = key: f: args@{ config, options, lib, ... }: if isFunction f then diff --git a/lib/tests/modules.sh b/lib/tests/modules.sh index 2ef7c4806595..57d3b5a76cec 100755 --- a/lib/tests/modules.sh +++ b/lib/tests/modules.sh @@ -58,6 +58,9 @@ checkConfigError() { fi } +# Shorthand meta attribute does not duplicate the config +checkConfigOutput '^"one two"$' config.result ./shorthand-meta.nix + # Check boolean option. checkConfigOutput '^false$' config.enable ./declare-enable.nix checkConfigError 'The option .* does not exist. Definition values:\n\s*- In .*: true' config.enable ./define-enable.nix diff --git a/lib/tests/modules/shorthand-meta.nix b/lib/tests/modules/shorthand-meta.nix new file mode 100644 index 000000000000..8c9619e18a2a --- /dev/null +++ b/lib/tests/modules/shorthand-meta.nix @@ -0,0 +1,19 @@ +{ lib, ... }: +let + inherit (lib) types mkOption; +in +{ + imports = [ + ({ config, ... }: { + options = { + meta.foo = mkOption { + type = types.listOf types.str; + }; + result = mkOption { default = lib.concatStringsSep " " config.meta.foo; }; + }; + }) + { + meta.foo = [ "one" "two" ]; + } + ]; +} From b3de22483cfd40e52dbe6fe7317b0f4901bd957d Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Mon, 6 Jun 2022 13:29:04 +0200 Subject: [PATCH 03/38] nixos/testing-python.nix: Add evalTest This is a decomposition of the testing-python.nix and build-vms.nix files into modules. By refactoring the glue, we accomplish the following: - NixOS tests can now use `imports` and other module system features. - Network-wide test setup can now be reusable; example: - A setup with all VMs configured to use a DNS server - Split long, slow tests into multiple tests that import a common module that has most of the setup. - Type checking for the test arguments - (TBD) "generated" options reference docs - Aspects that had to be wired through all the glue are now in their own files. - Chief example: interactive.nix. - Also: network.nix In rewriting this, I've generally stuck as close as possible to the existing code; copying pieces of logic and rewiring them, without changing the logic itself. I've made two exceptions to this rule - Introduction of `extraDriverArgs` instead of hardcoded interactivity logic. - Incorporation of https://github.com/NixOS/nixpkgs/pull/144110 in testScript.nix. I might revert the latter and split it into a new commit. --- nixos/lib/testing-python.nix | 20 ++++ nixos/lib/testing/call-test.nix | 16 +++ nixos/lib/testing/driver.nix | 177 ++++++++++++++++++++++++++++++ nixos/lib/testing/interactive.nix | 18 +++ nixos/lib/testing/legacy.nix | 25 +++++ nixos/lib/testing/meta.nix | 12 ++ nixos/lib/testing/name.nix | 7 ++ nixos/lib/testing/network.nix | 75 +++++++++++++ nixos/lib/testing/nodes.nix | 91 +++++++++++++++ nixos/lib/testing/run.nix | 51 +++++++++ nixos/lib/testing/testScript.nix | 78 +++++++++++++ 11 files changed, 570 insertions(+) create mode 100644 nixos/lib/testing/call-test.nix create mode 100644 nixos/lib/testing/driver.nix create mode 100644 nixos/lib/testing/interactive.nix create mode 100644 nixos/lib/testing/legacy.nix create mode 100644 nixos/lib/testing/meta.nix create mode 100644 nixos/lib/testing/name.nix create mode 100644 nixos/lib/testing/network.nix create mode 100644 nixos/lib/testing/nodes.nix create mode 100644 nixos/lib/testing/run.nix create mode 100644 nixos/lib/testing/testScript.nix diff --git a/nixos/lib/testing-python.nix b/nixos/lib/testing-python.nix index 4bb1689ffd78..ab509c098d24 100644 --- a/nixos/lib/testing-python.nix +++ b/nixos/lib/testing-python.nix @@ -166,6 +166,26 @@ rec { ${lib.optionalString (interactive) "--add-flags --interactive"} ''); + evalTest = module: lib.evalModules { modules = testModules ++ [ module ]; }; + runTest = module: (evalTest module).config.run; + + testModules = [ + ./testing/driver.nix + ./testing/interactive.nix + ./testing/legacy.nix + ./testing/meta.nix + ./testing/name.nix + ./testing/network.nix + ./testing/nodes.nix + ./testing/run.nix + ./testing/testScript.nix + { + config = { + hostPkgs = pkgs; + }; + } + ]; + # Make a full-blown test makeTest = { machine ? null diff --git a/nixos/lib/testing/call-test.nix b/nixos/lib/testing/call-test.nix new file mode 100644 index 000000000000..3e137e78cd47 --- /dev/null +++ b/nixos/lib/testing/call-test.nix @@ -0,0 +1,16 @@ +{ config, lib, ... }: +let + inherit (lib) mkOption types; +in +{ + options = { + callTest = mkOption { + internal = true; + type = types.functionTo types.raw; + }; + result = mkOption { + internal = true; + default = config; + }; + }; +} diff --git a/nixos/lib/testing/driver.nix b/nixos/lib/testing/driver.nix new file mode 100644 index 000000000000..9473d888cbb8 --- /dev/null +++ b/nixos/lib/testing/driver.nix @@ -0,0 +1,177 @@ +{ config, lib, hostPkgs, ... }: +let + inherit (lib) mkOption types; + + # 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" ]; }; + }; + + + vlans = map (m: m.virtualisation.vlans) (lib.attrValues config.nodes); + 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"; + + # TODO: This is an implementation error and needs fixing + # the testing famework cannot legitimately restrict hostnames further + # beyond RFC1035 + invalidNodeNames = lib.filter + (node: builtins.match "^[A-z_]([A-z0-9_]+)?$" node == null) + nodeHostNames; + + uniqueVlans = lib.unique (builtins.concatLists vlans); + vlanNames = map (i: "vlan${toString i}: VLan;") uniqueVlans; + machineNames = map (name: "${name}: Machine;") nodeHostNames; + + withChecks = + if lib.length invalidNodeNames > 0 then + throw '' + Cannot create machines out of (${lib.concatStringsSep ", " invalidNodeNames})! + All machines are referenced as python variables in the testing framework which will break the + script when special characters are used. + + This is an IMPLEMENTATION ERROR and needs to be fixed. Meanwhile, + please stick to alphanumeric chars and underscores as separation. + '' + else + lib.warnIf config.skipLint "Linting is disabled"; + + driver = + hostPkgs.runCommand "nixos-test-driver-${config.name}" + { + # inherit testName; TODO (roberth): need this? + nativeBuildInputs = [ + hostPkgs.makeWrapper + ] ++ lib.optionals (!config.skipTypeCheck) [ hostPkgs.mypy ]; + 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 + + cat -n testScriptWithTypes + + # set pythonpath so mypy knows where to find the imports. this requires the py.typed file. + export PYTHONPATH='${../test-driver}' + mypy --no-implicit-optional \ + --pretty \ + --no-color-output \ + testScriptWithTypes + unset PYTHONPATH + ''} + + 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) '' + PYFLAKES_BUILTINS="$( + echo -n ${lib.escapeShellArg (lib.concatStringsSep "," nodeHostNames)}, + < ${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" \ + --set vlans '${toString vlans}' \ + ${lib.escapeShellArgs (lib.concatMap (arg: ["--add-flags" arg]) config.extraDriverArgs)} + ''; + +in +{ + options = { + + driver = mkOption { + description = "Script that runs the test."; + type = types.package; + defaultText = lib.literalDocBook "set by the test framework"; + }; + + hostPkgs = mkOption { + description = "Nixpkgs attrset used outside the nodes."; + type = types.raw; + example = lib.literalExpression '' + import nixpkgs { inherit system config overlays; } + ''; + }; + + qemu.package = mkOption { + description = "Which qemu package to use."; + type = types.package; + default = hostPkgs.qemu_test; + defaultText = "hostPkgs.qemu_test"; + }; + + enableOCR = mkOption { + description = '' + Whether to enable Optical Character Recognition functionality for + testing graphical programs. + ''; + type = types.bool; + default = false; + }; + + extraPythonPackages = mkOption { + description = '' + Python packages to add to the test driver. + + The argument is a Python package set, similar to `pkgs.pythonPackages`. + ''; + type = types.functionTo (types.listOf types.package); + default = ps: [ ]; + }; + + extraDriverArgs = mkOption { + description = '' + Extra arguments to pass to the test driver. + ''; + type = types.listOf types.str; + default = []; + }; + + skipLint = mkOption { + type = types.bool; + default = false; + }; + + skipTypeCheck = mkOption { + type = types.bool; + default = false; + }; + }; + + config = { + _module.args.hostPkgs = config.hostPkgs; + + driver = withChecks driver; + + # make available on the test runner + passthru.driver = config.driver; + }; +} diff --git a/nixos/lib/testing/interactive.nix b/nixos/lib/testing/interactive.nix new file mode 100644 index 000000000000..fd4d481a3f82 --- /dev/null +++ b/nixos/lib/testing/interactive.nix @@ -0,0 +1,18 @@ +{ config, lib, moduleType, hostPkgs, ... }: +let + inherit (lib) mkOption types; +in +{ + options = { + interactive = mkOption { + description = "All the same options, but configured for interactive use."; + type = moduleType; + }; + }; + + config = { + interactive.qemu.package = hostPkgs.qemu; + interactive.extraDriverArgs = [ "--interactive" ]; + passthru.driverInteractive = config.interactive.driver; + }; +} diff --git a/nixos/lib/testing/legacy.nix b/nixos/lib/testing/legacy.nix new file mode 100644 index 000000000000..868b8b65b17d --- /dev/null +++ b/nixos/lib/testing/legacy.nix @@ -0,0 +1,25 @@ +{ config, options, lib, ... }: +let + inherit (lib) mkIf mkOption types; +in +{ + # This needs options.warnings, which we don't have (yet?). + # imports = [ + # (lib.mkRenamedOptionModule [ "machine" ] [ "nodes" "machine" ]) + # ]; + + options = { + machine = mkOption { + internal = true; + type = types.raw; + }; + }; + + config = { + nodes = mkIf options.machine.isDefined ( + lib.warn + "In test `${config.name}': The `machine' attribute in NixOS tests (pkgs.nixosTest / make-test-python.nix / testing-python.nix / makeTest) is deprecated. Please set the equivalent `nodes.machine'." + { inherit (config) machine; } + ); + }; +} diff --git a/nixos/lib/testing/meta.nix b/nixos/lib/testing/meta.nix new file mode 100644 index 000000000000..1312d6a986ec --- /dev/null +++ b/nixos/lib/testing/meta.nix @@ -0,0 +1,12 @@ +{ lib, ... }: +let + inherit (lib) types mkOption; +in +{ + options = { + meta.maintainers = lib.mkOption { + type = types.listOf types.raw; + default = []; + }; + }; +} diff --git a/nixos/lib/testing/name.nix b/nixos/lib/testing/name.nix new file mode 100644 index 000000000000..f9fa488511c0 --- /dev/null +++ b/nixos/lib/testing/name.nix @@ -0,0 +1,7 @@ +{ lib, ... }: +{ + options.name = lib.mkOption { + description = "The name of the test."; + type = lib.types.str; + }; +} diff --git a/nixos/lib/testing/network.nix b/nixos/lib/testing/network.nix new file mode 100644 index 000000000000..e0446846fb09 --- /dev/null +++ b/nixos/lib/testing/network.nix @@ -0,0 +1,75 @@ +{ lib, nodes, ... }: + +with lib; + + +let + machines = attrNames nodes; + + machinesNumbered = zipLists machines (range 1 254); + + nodes_ = forEach machinesNumbered (m: nameValuePair m.fst + [ + ({ config, nodes, pkgs, ... }: + let + interfacesNumbered = zipLists config.virtualisation.vlans (range 1 255); + interfaces = forEach interfacesNumbered ({ fst, snd }: + nameValuePair "eth${toString snd}" { + ipv4.addresses = + [{ + address = "192.168.${toString fst}.${toString m.snd}"; + prefixLength = 24; + }]; + }); + + networkConfig = + { + networking.hostName = mkDefault m.fst; + + networking.interfaces = listToAttrs interfaces; + + networking.primaryIPAddress = + optionalString (interfaces != [ ]) (head (head interfaces).value.ipv4.addresses).address; + + # Put the IP addresses of all VMs in this machine's + # /etc/hosts file. If a machine has multiple + # interfaces, use the IP address corresponding to + # the first interface (i.e. the first network in its + # virtualisation.vlans option). + networking.extraHosts = flip concatMapStrings machines + (m': + let config = (getAttr m' nodes).config; in + optionalString (config.networking.primaryIPAddress != "") + ("${config.networking.primaryIPAddress} " + + optionalString (config.networking.domain != null) + "${config.networking.hostName}.${config.networking.domain} " + + "${config.networking.hostName}\n")); + + virtualisation.qemu.options = + let qemu-common = import ../qemu-common.nix { inherit lib pkgs; }; + in + flip concatMap interfacesNumbered + ({ fst, snd }: qemu-common.qemuNICFlags snd fst m.snd); + }; + + in + { + key = "ip-address"; + config = networkConfig // { + # Expose the networkConfig items for tests like nixops + # that need to recreate the network config. + system.build.networkConfig = networkConfig; + }; + } + ) + ]); + + extraNodeConfigs = lib.listToAttrs nodes_; +in +{ + config = { + defaults = { config, name, ... }: { + imports = extraNodeConfigs.${name}; + }; + }; +} diff --git a/nixos/lib/testing/nodes.nix b/nixos/lib/testing/nodes.nix new file mode 100644 index 000000000000..98580d5dc4f8 --- /dev/null +++ b/nixos/lib/testing/nodes.nix @@ -0,0 +1,91 @@ +testModuleArgs@{ config, lib, hostPkgs, nodes, ... }: + +let + inherit (lib) mkOption mkForce optional types mapAttrs mkDefault; + + system = hostPkgs.stdenv.hostPlatform.system; + + baseOS = + import ../eval-config.nix { + inherit system; + inherit (config.node) specialArgs; + modules = [ config.defaults ]; + baseModules = (import ../../modules/module-list.nix) ++ + [ + ../../modules/virtualisation/qemu-vm.nix + ../../modules/testing/test-instrumentation.nix # !!! should only get added for automated test runs + { key = "no-manual"; documentation.nixos.enable = false; } + { + key = "no-revision"; + # Make the revision metadata constant, in order to avoid needless retesting. + # The human version (e.g. 21.05-pre) is left as is, because it is useful + # for external modules that test with e.g. testers.nixosTest and rely on that + # version number. + config.system.nixos.revision = mkForce "constant-nixos-revision"; + } + { key = "nodes"; _module.args.nodes = nodes; } + + ({ config, ... }: + { + virtualisation.qemu.package = testModuleArgs.config.qemu.package; + + # Ensure we do not use aliases. Ideally this is only set + # when the test framework is used by Nixpkgs NixOS tests. + nixpkgs.config.allowAliases = false; + }) + ] ++ optional config.minimal ../../modules/testing/minimal-kernel.nix; + }; + + +in + +{ + + options = { + node.type = mkOption { + type = types.raw; + default = baseOS.type; + internal = true; + }; + + nodes = mkOption { + type = types.lazyAttrsOf config.node.type; + }; + + defaults = mkOption { + description = '' + NixOS configuration that is applied to all {option}`nodes`. + ''; + type = types.deferredModule; + default = { }; + }; + + node.specialArgs = mkOption { + type = types.lazyAttrsOf types.raw; + default = { }; + }; + + minimal = mkOption { + type = types.bool; + default = false; + }; + + nodesCompat = mkOption { + internal = true; + }; + }; + + config = { + _module.args.nodes = config.nodesCompat; + nodesCompat = + mapAttrs + (name: config: config // { + config = lib.warn + "Module argument `nodes.${name}.config` is deprecated. Use `nodes.${name}` instead." + config; + }) + config.nodes; + + passthru.nodes = config.nodesCompat; + }; +} diff --git a/nixos/lib/testing/run.nix b/nixos/lib/testing/run.nix new file mode 100644 index 000000000000..65bcbe720bf3 --- /dev/null +++ b/nixos/lib/testing/run.nix @@ -0,0 +1,51 @@ +{ config, hostPkgs, lib, ... }: +let + inherit (lib) types mkOption; +in +{ + options = { + passthru = mkOption { + type = types.lazyAttrsOf types.raw; + description = '' + Attributes to add to the returned derivations, + which are not necessarily part of the build. + + This is a bit like doing `drv // { myAttr = true; }` (which would be lost by `overrideAttrs`). + It does not change the actual derivation, but adds the attribute nonetheless, so that + consumers of what would be `drv` have more information. + ''; + }; + + run = mkOption { + type = types.package; + description = '' + Derivation that runs the test. + ''; + }; + }; + + config = { + run = hostPkgs.stdenv.mkDerivation { + name = "vm-test-run-${config.name}"; + + requiredSystemFeatures = [ "kvm" "nixos-test" ]; + + buildCommand = + '' + mkdir -p $out + + # effectively mute the XMLLogger + export LOGFILE=/dev/null + + ${config.driver}/bin/nixos-test-driver -o $out + ''; + + passthru = config.passthru; + + meta = config.meta; + }; + + # useful for inspection (debugging / exploration) + passthru.config = config; + }; +} diff --git a/nixos/lib/testing/testScript.nix b/nixos/lib/testing/testScript.nix new file mode 100644 index 000000000000..08e87b626b3b --- /dev/null +++ b/nixos/lib/testing/testScript.nix @@ -0,0 +1,78 @@ +testModuleArgs@{ config, lib, hostPkgs, nodes, moduleType, ... }: +let + inherit (lib) mkOption types; + inherit (types) either str functionTo; +in +{ + options = { + testScript = mkOption { + type = either str (functionTo str); + }; + testScriptString = mkOption { + type = str; + readOnly = true; + internal = true; + }; + + includeTestScriptReferences = mkOption { + type = types.bool; + default = true; + internal = true; + }; + withoutTestScriptReferences = mkOption { + type = moduleType; + description = '' + A parallel universe where the testScript is invalid and has no references. + ''; + }; + }; + config = { + withoutTestScriptReferences.includeTestScriptReferences = false; + withoutTestScriptReferences.testScript = lib.mkForce "testscript omitted"; + + testScriptString = + if lib.isFunction config.testScript + then + config.testScript + { + nodes = + lib.mapAttrs + (k: v: + if v.virtualisation.useNixStoreImage + then + # prevent infinite recursion when testScript would + # reference v's toplevel + config.withoutTestScriptReferences.nodesCompat.${k} + else + # reuse memoized config + v + ) + config.nodesCompat; + } + else config.testScript; + + defaults = { config, name, ... }: { + # Make sure all derivations referenced by the test + # script are available on the nodes. When the store is + # accessed through 9p, this isn't important, since + # everything in the store is available to the guest, + # but when building a root image it is, as all paths + # that should be available to the guest has to be + # copied to the image. + virtualisation.additionalPaths = + lib.optional + # A testScript may evaluate nodes, which has caused + # infinite recursions. The demand cycle involves: + # testScript --> + # nodes --> + # toplevel --> + # additionalPaths --> + # hasContext testScript' --> + # testScript (ad infinitum) + # If we don't need to build an image, we can break this + # cycle by short-circuiting when useNixStoreImage is false. + (config.virtualisation.useNixStoreImage && builtins.hasContext testModuleArgs.config.testScriptString && testModuleArgs.config.includeTestScriptReferences) + (hostPkgs.writeStringReferencesToFile testModuleArgs.config.testScriptString); + }; + }; +} From 3c09cb23639c185f3b168b56bfc83ab3768dd1e0 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Mon, 6 Jun 2022 20:19:59 +0200 Subject: [PATCH 04/38] nixos/all-tests.nix: Improve runTest for release.nix ... and add runTestOn. --- nixos/tests/all-tests.nix | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/nixos/tests/all-tests.nix b/nixos/tests/all-tests.nix index e0121fe6b6b8..eb13e33bd9aa 100644 --- a/nixos/tests/all-tests.nix +++ b/nixos/tests/all-tests.nix @@ -27,6 +27,22 @@ let }; evalMinimalConfig = module: nixosLib.evalModules { modules = [ module ]; }; + inherit + (rec { + doRunTest = (import ../lib/testing-python.nix { inherit system pkgs; }).runTest; + findTests = tree: + if tree?recurseForDerivations && tree.recurseForDerivations + then mapAttrs (k: findTests) (builtins.removeAttrs tree ["recurseForDerivations"]) + else callTest ({ test = tree; }); + runTest = arg: let r = doRunTest arg; in findTests r; + runTestOn = systems: arg: + if elem system systems then runTest arg + else {}; + }) + runTest + runTestOn + ; + in { _3proxy = handleTest ./3proxy.nix {}; acme = handleTest ./acme.nix {}; From 124b0c4abcd4ee093c7cee95f36f576bc6abffa5 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Mon, 6 Jun 2022 20:30:17 +0200 Subject: [PATCH 05/38] nixos/testing/network.nix: Avoid deprecated .config --- nixos/lib/testing/network.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nixos/lib/testing/network.nix b/nixos/lib/testing/network.nix index e0446846fb09..dfdd9b8314a2 100644 --- a/nixos/lib/testing/network.nix +++ b/nixos/lib/testing/network.nix @@ -38,7 +38,7 @@ let # virtualisation.vlans option). networking.extraHosts = flip concatMapStrings machines (m': - let config = (getAttr m' nodes).config; in + let config = getAttr m' nodes; in optionalString (config.networking.primaryIPAddress != "") ("${config.networking.primaryIPAddress} " + optionalString (config.networking.domain != null) From a958a4aa009e371d38936ef86a327cb9e6fb154a Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Mon, 6 Jun 2022 21:19:22 +0200 Subject: [PATCH 06/38] nixos/testing: Add pkgs parameter This parameter is for packages to use in VMs, unlike hostPkgs. --- nixos/lib/testing-python.nix | 1 + nixos/lib/testing/pkgs.nix | 11 +++++++++++ nixos/tests/3proxy.nix | 4 ++-- nixos/tests/all-tests.nix | 2 +- 4 files changed, 15 insertions(+), 3 deletions(-) create mode 100644 nixos/lib/testing/pkgs.nix diff --git a/nixos/lib/testing-python.nix b/nixos/lib/testing-python.nix index ab509c098d24..1c331a3c5162 100644 --- a/nixos/lib/testing-python.nix +++ b/nixos/lib/testing-python.nix @@ -177,6 +177,7 @@ rec { ./testing/name.nix ./testing/network.nix ./testing/nodes.nix + ./testing/pkgs.nix ./testing/run.nix ./testing/testScript.nix { diff --git a/nixos/lib/testing/pkgs.nix b/nixos/lib/testing/pkgs.nix new file mode 100644 index 000000000000..22dd586868e3 --- /dev/null +++ b/nixos/lib/testing/pkgs.nix @@ -0,0 +1,11 @@ +{ config, lib, hostPkgs, ... }: +{ + config = { + # default pkgs for use in VMs + _module.args.pkgs = hostPkgs; + + defaults = { + # TODO: a module to set a shared pkgs, if options.nixpkgs.* is untouched by user (highestPrio) */ + }; + }; +} diff --git a/nixos/tests/3proxy.nix b/nixos/tests/3proxy.nix index 8127438fabd9..d367295e538b 100644 --- a/nixos/tests/3proxy.nix +++ b/nixos/tests/3proxy.nix @@ -1,4 +1,4 @@ -import ./make-test-python.nix ({ pkgs, ...} : { +{ lib, pkgs, ... }: { name = "3proxy"; meta = with pkgs.lib.maintainers; { maintainers = [ misuzu ]; @@ -186,4 +186,4 @@ import ./make-test-python.nix ({ pkgs, ...} : { "${pkgs.wget}/bin/wget -e use_proxy=yes -e http_proxy=http://192.168.0.4:3128 -S -O /dev/null http://127.0.0.1:9999" ) ''; -}) +} diff --git a/nixos/tests/all-tests.nix b/nixos/tests/all-tests.nix index eb13e33bd9aa..b4f77557a40b 100644 --- a/nixos/tests/all-tests.nix +++ b/nixos/tests/all-tests.nix @@ -44,7 +44,7 @@ let ; in { - _3proxy = handleTest ./3proxy.nix {}; + _3proxy = runTest ./3proxy.nix; acme = handleTest ./acme.nix {}; adguardhome = handleTest ./adguardhome.nix {}; aesmd = handleTest ./aesmd.nix {}; From 0691b450b1d1c5e2846e23fd145e7c08d242c0e1 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Mon, 6 Jun 2022 21:05:41 +0200 Subject: [PATCH 07/38] nixosTests._3proxy: Use module system based runner --- nixos/tests/3proxy.nix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nixos/tests/3proxy.nix b/nixos/tests/3proxy.nix index d367295e538b..647d9d57c7ff 100644 --- a/nixos/tests/3proxy.nix +++ b/nixos/tests/3proxy.nix @@ -1,6 +1,6 @@ { lib, pkgs, ... }: { name = "3proxy"; - meta = with pkgs.lib.maintainers; { + meta = with lib.maintainers; { maintainers = [ misuzu ]; }; @@ -92,7 +92,7 @@ networking.firewall.allowedTCPPorts = [ 3128 9999 ]; }; - peer3 = { lib, ... }: { + peer3 = { lib, pkgs, ... }: { networking.useDHCP = false; networking.interfaces.eth1 = { ipv4.addresses = [ From 9e4277b970d96ddc1c2bbe4686757ae761baa18a Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Mon, 6 Jun 2022 23:46:39 +0200 Subject: [PATCH 08/38] nixos/testing/network.nix: Fix for specialisations and specialArgs.name Before this, it relied on being able to `imports` from the `name` module argument, which wasn't really necessary and required a potentially quite breaking change. We can revert that now. --- nixos/lib/testing/network.nix | 159 +++++++++++++++++++++------------- 1 file changed, 98 insertions(+), 61 deletions(-) diff --git a/nixos/lib/testing/network.nix b/nixos/lib/testing/network.nix index dfdd9b8314a2..c87abee30c93 100644 --- a/nixos/lib/testing/network.nix +++ b/nixos/lib/testing/network.nix @@ -1,75 +1,112 @@ { lib, nodes, ... }: -with lib; - - let - machines = attrNames nodes; + inherit (lib) + attrNames concatMap concatMapStrings flip forEach head + listToAttrs mkDefault mkOption nameValuePair optionalString + range types zipListsWith zipLists + ; - machinesNumbered = zipLists machines (range 1 254); + nodeNumbers = + listToAttrs + (zipListsWith + nameValuePair + (attrNames nodes) + (range 1 254) + ); - nodes_ = forEach machinesNumbered (m: nameValuePair m.fst - [ - ({ config, nodes, pkgs, ... }: - let - interfacesNumbered = zipLists config.virtualisation.vlans (range 1 255); - interfaces = forEach interfacesNumbered ({ fst, snd }: - nameValuePair "eth${toString snd}" { - ipv4.addresses = - [{ - address = "192.168.${toString fst}.${toString m.snd}"; - prefixLength = 24; - }]; - }); + networkModule = { config, nodes, pkgs, ... }: + let + interfacesNumbered = zipLists config.virtualisation.vlans (range 1 255); + interfaces = forEach interfacesNumbered ({ fst, snd }: + nameValuePair "eth${toString snd}" { + ipv4.addresses = + [{ + address = "192.168.${toString fst}.${toString config.virtualisation.test.nodeNumber}"; + prefixLength = 24; + }]; + }); - networkConfig = - { - networking.hostName = mkDefault m.fst; - - networking.interfaces = listToAttrs interfaces; - - networking.primaryIPAddress = - optionalString (interfaces != [ ]) (head (head interfaces).value.ipv4.addresses).address; - - # Put the IP addresses of all VMs in this machine's - # /etc/hosts file. If a machine has multiple - # interfaces, use the IP address corresponding to - # the first interface (i.e. the first network in its - # virtualisation.vlans option). - networking.extraHosts = flip concatMapStrings machines - (m': - let config = getAttr m' nodes; in - optionalString (config.networking.primaryIPAddress != "") - ("${config.networking.primaryIPAddress} " + - optionalString (config.networking.domain != null) - "${config.networking.hostName}.${config.networking.domain} " + - "${config.networking.hostName}\n")); - - virtualisation.qemu.options = - let qemu-common = import ../qemu-common.nix { inherit lib pkgs; }; - in - flip concatMap interfacesNumbered - ({ fst, snd }: qemu-common.qemuNICFlags snd fst m.snd); - }; - - in + networkConfig = { - key = "ip-address"; - config = networkConfig // { - # Expose the networkConfig items for tests like nixops - # that need to recreate the network config. - system.build.networkConfig = networkConfig; + networking.hostName = mkDefault config.virtualisation.test.nodeName; + + networking.interfaces = listToAttrs interfaces; + + networking.primaryIPAddress = + optionalString (interfaces != [ ]) (head (head interfaces).value.ipv4.addresses).address; + + # Put the IP addresses of all VMs in this machine's + # /etc/hosts file. If a machine has multiple + # interfaces, use the IP address corresponding to + # the first interface (i.e. the first network in its + # virtualisation.vlans option). + networking.extraHosts = flip concatMapStrings (attrNames nodes) + (m': + let config = nodes.${m'}; in + optionalString (config.networking.primaryIPAddress != "") + ("${config.networking.primaryIPAddress} " + + optionalString (config.networking.domain != null) + "${config.networking.hostName}.${config.networking.domain} " + + "${config.networking.hostName}\n")); + + virtualisation.qemu.options = + let qemu-common = import ../qemu-common.nix { inherit lib pkgs; }; + in + flip concatMap interfacesNumbered + ({ fst, snd }: qemu-common.qemuNICFlags snd fst config.virtualisation.test.nodeNumber); + }; + + in + { + key = "ip-address"; + config = networkConfig // { + # Expose the networkConfig items for tests like nixops + # that need to recreate the network config. + system.build.networkConfig = networkConfig; + }; + }; + + nodeNumberModule = (regular@{ config, name, ... }: { + options = { + virtualisation.test.nodeName = mkOption { + internal = true; + default = name; + # We need to force this in specilisations, otherwise it'd be + # readOnly = true; + description = '' + The `name` in `nodes.`; stable across `specialisations`. + ''; + }; + virtualisation.test.nodeNumber = mkOption { + internal = true; + type = types.int; + readOnly = true; + default = nodeNumbers.${config.virtualisation.test.nodeName}; + description = '' + A unique number assigned for each node in `nodes`. + ''; + }; + + # specialisations override the `name` module argument, + # so we push the real `virtualisation.test.nodeName`. + specialisation = mkOption { + type = types.attrsOf (types.submodule { + options.configuration = mkOption { + type = types.submodule ({ + config.virtualisation.test.nodeName = + # assert regular.config.virtualisation.test.nodeName != "configuration"; + regular.config.virtualisation.test.nodeName; + }); }; - } - ) - ]); + }); + }; + }; + }); - extraNodeConfigs = lib.listToAttrs nodes_; in { config = { - defaults = { config, name, ... }: { - imports = extraNodeConfigs.${name}; - }; + defaults = { imports = [ networkModule nodeNumberModule ]; }; }; } From b7ffe4446903a014ad37b26b6a206217c27da7fd Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Mon, 6 Jun 2022 23:49:59 +0200 Subject: [PATCH 09/38] nixosTests.acme: Use module system based runner --- nixos/tests/acme.nix | 14 +++++++------- nixos/tests/all-tests.nix | 2 +- nixos/tests/common/acme/client/default.nix | 4 ++-- nixos/tests/common/acme/server/default.nix | 6 +++--- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/nixos/tests/acme.nix b/nixos/tests/acme.nix index c07f99c5db3a..d3a436080ebf 100644 --- a/nixos/tests/acme.nix +++ b/nixos/tests/acme.nix @@ -1,7 +1,7 @@ -import ./make-test-python.nix ({ pkgs, lib, ... }: let +{ pkgs, lib, ... }: let commonConfig = ./common/acme/client; - dnsServerIP = nodes: nodes.dnsserver.config.networking.primaryIPAddress; + dnsServerIP = nodes: nodes.dnsserver.networking.primaryIPAddress; dnsScript = nodes: let dnsAddress = dnsServerIP nodes; @@ -153,7 +153,7 @@ in { description = "Pebble ACME challenge test server"; wantedBy = [ "network.target" ]; serviceConfig = { - ExecStart = "${pkgs.pebble}/bin/pebble-challtestsrv -dns01 ':53' -defaultIPv6 '' -defaultIPv4 '${nodes.webserver.config.networking.primaryIPAddress}'"; + ExecStart = "${pkgs.pebble}/bin/pebble-challtestsrv -dns01 ':53' -defaultIPv6 '' -defaultIPv4 '${nodes.webserver.networking.primaryIPAddress}'"; # Required to bind on privileged ports. AmbientCapabilities = [ "CAP_NET_BIND_SERVICE" ]; }; @@ -175,7 +175,7 @@ in { specialisation = { # First derivation used to test general ACME features general.configuration = { ... }: let - caDomain = nodes.acme.config.test-support.acme.caDomain; + caDomain = nodes.acme.test-support.acme.caDomain; email = config.security.acme.defaults.email; # Exit 99 to make it easier to track if this is the reason a renew failed accountCreateTester = '' @@ -316,7 +316,7 @@ in { testScript = { nodes, ... }: let - caDomain = nodes.acme.config.test-support.acme.caDomain; + caDomain = nodes.acme.test-support.acme.caDomain; newServerSystem = nodes.webserver.config.system.build.toplevel; switchToNewServer = "${newServerSystem}/bin/switch-to-configuration test"; in @@ -438,7 +438,7 @@ in { client.wait_for_unit("default.target") client.succeed( - 'curl --data \'{"host": "${caDomain}", "addresses": ["${nodes.acme.config.networking.primaryIPAddress}"]}\' http://${dnsServerIP nodes}:8055/add-a' + 'curl --data \'{"host": "${caDomain}", "addresses": ["${nodes.acme.networking.primaryIPAddress}"]}\' http://${dnsServerIP nodes}:8055/add-a' ) acme.wait_for_unit("network-online.target") @@ -594,4 +594,4 @@ in { wait_for_server() check_connection_key_bits(client, test_domain, "384") ''; -}) +} diff --git a/nixos/tests/all-tests.nix b/nixos/tests/all-tests.nix index b4f77557a40b..463bab49e1bb 100644 --- a/nixos/tests/all-tests.nix +++ b/nixos/tests/all-tests.nix @@ -45,7 +45,7 @@ let in { _3proxy = runTest ./3proxy.nix; - acme = handleTest ./acme.nix {}; + acme = runTest ./acme.nix; adguardhome = handleTest ./adguardhome.nix {}; aesmd = handleTest ./aesmd.nix {}; agate = handleTest ./web-servers/agate.nix {}; diff --git a/nixos/tests/common/acme/client/default.nix b/nixos/tests/common/acme/client/default.nix index 9dbe345e7a01..503e610d1ac9 100644 --- a/nixos/tests/common/acme/client/default.nix +++ b/nixos/tests/common/acme/client/default.nix @@ -1,7 +1,7 @@ { lib, nodes, pkgs, ... }: let - caCert = nodes.acme.config.test-support.acme.caCert; - caDomain = nodes.acme.config.test-support.acme.caDomain; + caCert = nodes.acme.test-support.acme.caCert; + caDomain = nodes.acme.test-support.acme.caDomain; in { security.acme = { diff --git a/nixos/tests/common/acme/server/default.nix b/nixos/tests/common/acme/server/default.nix index fa1b9b545d09..b81f860125c8 100644 --- a/nixos/tests/common/acme/server/default.nix +++ b/nixos/tests/common/acme/server/default.nix @@ -18,10 +18,10 @@ # # example = { nodes, ... }: { # networking.nameservers = [ -# nodes.acme.config.networking.primaryIPAddress +# nodes.acme.networking.primaryIPAddress # ]; # security.pki.certificateFiles = [ -# nodes.acme.config.test-support.acme.caCert +# nodes.acme.test-support.acme.caCert # ]; # }; # } @@ -36,7 +36,7 @@ # acme = { nodes, lib, ... }: { # imports = [ ./common/acme/server ]; # networking.nameservers = lib.mkForce [ -# nodes.myresolver.config.networking.primaryIPAddress +# nodes.myresolver.networking.primaryIPAddress # ]; # }; # From edf8be37af44fa214811c9165c4f45eef0fadd1c Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Thu, 9 Jun 2022 17:43:22 +0200 Subject: [PATCH 10/38] nixosTests.adguardhome: Use module based runner --- nixos/tests/adguardhome.nix | 2 +- nixos/tests/all-tests.nix | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/nixos/tests/adguardhome.nix b/nixos/tests/adguardhome.nix index ddbe8ff9c117..1a220f996998 100644 --- a/nixos/tests/adguardhome.nix +++ b/nixos/tests/adguardhome.nix @@ -1,4 +1,4 @@ -import ./make-test-python.nix { +{ name = "adguardhome"; nodes = { diff --git a/nixos/tests/all-tests.nix b/nixos/tests/all-tests.nix index 463bab49e1bb..229886370cb1 100644 --- a/nixos/tests/all-tests.nix +++ b/nixos/tests/all-tests.nix @@ -46,7 +46,7 @@ let in { _3proxy = runTest ./3proxy.nix; acme = runTest ./acme.nix; - adguardhome = handleTest ./adguardhome.nix {}; + adguardhome = runTest ./adguardhome.nix; aesmd = handleTest ./aesmd.nix {}; agate = handleTest ./web-servers/agate.nix {}; agda = handleTest ./agda.nix {}; From 15dcbc2514f019276f9cd73c804deab76fa1031f Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Thu, 9 Jun 2022 17:45:02 +0200 Subject: [PATCH 11/38] nixosTests.aesmd: Use module based runner --- nixos/tests/aesmd.nix | 4 ++-- nixos/tests/all-tests.nix | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/nixos/tests/aesmd.nix b/nixos/tests/aesmd.nix index 9f07426be8d8..5da661afd548 100644 --- a/nixos/tests/aesmd.nix +++ b/nixos/tests/aesmd.nix @@ -1,4 +1,4 @@ -import ./make-test-python.nix ({ pkgs, lib, ... }: { +{ pkgs, lib, ... }: { name = "aesmd"; meta = { maintainers = with lib.maintainers; [ veehaitch ]; @@ -59,4 +59,4 @@ import ./make-test-python.nix ({ pkgs, lib, ... }: { assert aesmd_config == "whitelist url = http://nixos.org\nproxy type = direct\ndefault quoting type = ecdsa_256\n", "aesmd.conf differs" ''; -}) +} diff --git a/nixos/tests/all-tests.nix b/nixos/tests/all-tests.nix index 229886370cb1..877efb001895 100644 --- a/nixos/tests/all-tests.nix +++ b/nixos/tests/all-tests.nix @@ -47,7 +47,7 @@ in { _3proxy = runTest ./3proxy.nix; acme = runTest ./acme.nix; adguardhome = runTest ./adguardhome.nix; - aesmd = handleTest ./aesmd.nix {}; + aesmd = runTest ./aesmd.nix; agate = handleTest ./web-servers/agate.nix {}; agda = handleTest ./agda.nix {}; airsonic = handleTest ./airsonic.nix {}; From 5727fd3e6f17684eb8bace334564cda302077524 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Thu, 9 Jun 2022 17:46:51 +0200 Subject: [PATCH 12/38] nixosTests.agate: Use module based runner --- nixos/tests/all-tests.nix | 2 +- nixos/tests/web-servers/agate.nix | 46 +++++++++++++++---------------- 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/nixos/tests/all-tests.nix b/nixos/tests/all-tests.nix index 877efb001895..5cd58cb5c3fd 100644 --- a/nixos/tests/all-tests.nix +++ b/nixos/tests/all-tests.nix @@ -48,7 +48,7 @@ in { acme = runTest ./acme.nix; adguardhome = runTest ./adguardhome.nix; aesmd = runTest ./aesmd.nix; - agate = handleTest ./web-servers/agate.nix {}; + agate = runTest ./web-servers/agate.nix; agda = handleTest ./agda.nix {}; airsonic = handleTest ./airsonic.nix {}; allTerminfo = handleTest ./all-terminfo.nix {}; diff --git a/nixos/tests/web-servers/agate.nix b/nixos/tests/web-servers/agate.nix index e364e134cfda..e8d789a9ca44 100644 --- a/nixos/tests/web-servers/agate.nix +++ b/nixos/tests/web-servers/agate.nix @@ -1,29 +1,27 @@ -import ../make-test-python.nix ( - { pkgs, lib, ... }: - { - name = "agate"; - meta = with lib.maintainers; { maintainers = [ jk ]; }; +{ pkgs, lib, ... }: +{ + name = "agate"; + meta = with lib.maintainers; { maintainers = [ jk ]; }; - nodes = { - geminiserver = { pkgs, ... }: { - services.agate = { - enable = true; - hostnames = [ "localhost" ]; - contentDir = pkgs.writeTextDir "index.gmi" '' - # Hello NixOS! - ''; - }; + nodes = { + geminiserver = { pkgs, ... }: { + services.agate = { + enable = true; + hostnames = [ "localhost" ]; + contentDir = pkgs.writeTextDir "index.gmi" '' + # Hello NixOS! + ''; }; }; + }; - testScript = { nodes, ... }: '' - geminiserver.wait_for_unit("agate") - geminiserver.wait_for_open_port(1965) + testScript = { nodes, ... }: '' + geminiserver.wait_for_unit("agate") + geminiserver.wait_for_open_port(1965) - with subtest("check is serving over gemini"): - response = geminiserver.succeed("${pkgs.gmni}/bin/gmni -j once -i -N gemini://localhost:1965") - print(response) - assert "Hello NixOS!" in response - ''; - } -) + with subtest("check is serving over gemini"): + response = geminiserver.succeed("${pkgs.gmni}/bin/gmni -j once -i -N gemini://localhost:1965") + print(response) + assert "Hello NixOS!" in response + ''; +} From b0c781cc4136e4678db1864875750c916b78ad33 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Wed, 15 Jun 2022 16:59:21 +0200 Subject: [PATCH 13/38] nixos/testing: Move entrypoint to nixos/lib + doc --- .../writing-nixos-tests.section.md | 73 +++++- .../writing-nixos-tests.section.xml | 217 ++++++++++++------ nixos/lib/default.nix | 8 + nixos/lib/testing-python.nix | 31 +-- nixos/lib/testing/default.nix | 24 ++ 5 files changed, 256 insertions(+), 97 deletions(-) create mode 100644 nixos/lib/testing/default.nix diff --git a/nixos/doc/manual/development/writing-nixos-tests.section.md b/nixos/doc/manual/development/writing-nixos-tests.section.md index 6934bb0face7..8dd3e6fb7597 100644 --- a/nixos/doc/manual/development/writing-nixos-tests.section.md +++ b/nixos/doc/manual/development/writing-nixos-tests.section.md @@ -1,9 +1,9 @@ # Writing Tests {#sec-writing-nixos-tests} -A NixOS test is a Nix expression that has the following structure: +A NixOS test is a module that has the following structure: ```nix -import ./make-test-python.nix { +{ # One or more machines: nodes = @@ -21,7 +21,10 @@ import ./make-test-python.nix { } ``` -The attribute `testScript` is a bit of Python code that executes the +We refer to the whole test above as a test module, whereas the values +in `nodes.` are NixOS modules. (A NixOS configuration is a module.) + +The option `testScript` is a bit of Python code that executes the test (described below). During the test, it will start one or more virtual machines, the configuration of which is described by the attribute `nodes`. @@ -34,7 +37,64 @@ when switching between consoles, and so on. An interesting multi-node test is [`nfs/simple.nix`](https://github.com/NixOS/nixpkgs/blob/master/nixos/tests/nfs/simple.nix). It uses two client nodes to test correct locking across server crashes. -There are a few special NixOS configuration options for test VMs: +## Calling a test {#sec-calling-nixos-tests} + +Tests are invoked a bit differently depending on whether the test lives in NixOS or in another project. + +### Testing within NixOS {#sec-call-nixos-test-in-nixos} + +Test modules can be instantiated into derivations in multiple ways. + +Tests that are part of NixOS are added to [`nixos/tests/all-tests.nix`](https://github.com/NixOS/nixpkgs/blob/master/nixos/tests/all-tests.nix). + +```nix + hostname = runTest ./hostname.nix; +``` + +Overrides can be added by defining an anonymous module in `all-tests.nix`. +For the purpose of constructing a test matrix, use the `matrix` options instead. + +```nix + hostname = runTest { imports = [ ./hostname.nix ]; defaults.networking.firewall.enable = false; }; +``` + +You can run a test with attribute name `mytest` in `all-tests.nix` by invoking: + +```shell +nix-build -A nixosTests.mytest +``` + +### Testing outside the NixOS project {#sec-call-nixos-test-outside-nixos} + +Outside the `nixpkgs` repository, you can instantiate the test by first acquiring the NixOS library, + +```nix +# regular nix +let nixos-lib = import (nixpkgs + "/nixos/lib") { }; +in +``` + +```nix +# flake +let nixos-lib = nixpkgs.lib.nixos; +in +``` + +... and then invoking `runTest`, for example: + +```nix +nixos-lib.runTest { + imports = [ ./test.nix ]; + hostPkgs = pkgs; # the Nixpkgs package set used outside the VMs + defaults.services.foo.package = mypkg; +} +``` + +`runTest` returns a derivation that runs the test. + +## Configuring the nodes {#sec-nixos-test-nodes} + +There are a few special NixOS options for test VMs: `virtualisation.memorySize` @@ -304,7 +364,7 @@ For faster dev cycles it\'s also possible to disable the code-linters (this shouldn\'t be commited though): ```nix -import ./make-test-python.nix { +{ skipLint = true; nodes.machine = { config, pkgs, ... }: @@ -336,7 +396,7 @@ Similarly, the type checking of test scripts can be disabled in the following way: ```nix -import ./make-test-python.nix { +{ skipTypeCheck = true; nodes.machine = { config, pkgs, ... }: @@ -400,7 +460,6 @@ added using the parameter `extraPythonPackages`. For example, you could add `numpy` like this: ```nix -import ./make-test-python.nix { extraPythonPackages = p: [ p.numpy ]; diff --git a/nixos/doc/manual/from_md/development/writing-nixos-tests.section.xml b/nixos/doc/manual/from_md/development/writing-nixos-tests.section.xml index d6f4f61c0645..6d0465e4230b 100644 --- a/nixos/doc/manual/from_md/development/writing-nixos-tests.section.xml +++ b/nixos/doc/manual/from_md/development/writing-nixos-tests.section.xml @@ -1,10 +1,10 @@
Writing Tests - A NixOS test is a Nix expression that has the following structure: + A NixOS test is a module that has the following structure: -import ./make-test-python.nix { +{ # One or more machines: nodes = @@ -22,7 +22,12 @@ import ./make-test-python.nix { } - The attribute testScript is a bit of Python code + We refer to the whole test above as a test module, whereas the + values in nodes.<name> are NixOS modules. + (A NixOS configuration is a module.) + + + The option testScript is a bit of Python code that executes the test (described below). During the test, it will start one or more virtual machines, the configuration of which is described by the attribute nodes. @@ -38,78 +43,149 @@ import ./make-test-python.nix { It uses two client nodes to test correct locking across server crashes. - - There are a few special NixOS configuration options for test VMs: - - - - - virtualisation.memorySize - - - - The memory of the VM in megabytes. - - - - - - virtualisation.vlans - - - - The virtual networks to which the VM is connected. See - nat.nix - for an example. - - - - - - virtualisation.writableStore - - - - By default, the Nix store in the VM is not writable. If you - enable this option, a writable union file system is mounted on - top of the Nix store to make it appear writable. This is - necessary for tests that run Nix operations that modify the - store. - - - - - - For more options, see the module - qemu-vm.nix. - - - The test script is a sequence of Python statements that perform - various actions, such as starting VMs, executing commands in the - VMs, and so on. Each virtual machine is represented as an object - stored in the variable name if this is also the - identifier of the machine in the declarative config. If you - specified a node nodes.machine, the following - example starts the machine, waits until it has finished booting, - then executes a command and checks that the output is more-or-less - correct: - - +
+ Calling a test + + Tests are invoked a bit differently depending on whether the test + lives in NixOS or in another project. + +
+ Testing within NixOS + + Test modules can be instantiated into derivations in multiple + ways. + + + Tests that are part of NixOS are added to + nixos/tests/all-tests.nix. + + + hostname = runTest ./hostname.nix; + + + Overrides can be added by defining an anonymous module in + all-tests.nix. For the purpose of + constructing a test matrix, use the matrix + options instead. + + + hostname = runTest { imports = [ ./hostname.nix ]; defaults.networking.firewall.enable = false; }; + + + You can run a test with attribute name mytest + in all-tests.nix by invoking: + + +nix-build -A nixosTests.mytest + +
+
+ Testing outside the NixOS project + + Outside the nixpkgs repository, you can + instantiate the test by first acquiring the NixOS library, + + +# regular nix +let nixos-lib = import (nixpkgs + "/nixos/lib") { }; +in + + +# flake +let nixos-lib = nixpkgs.lib.nixos; +in + + + … and then invoking runTest, for example: + + +nixos-lib.runTest { + imports = [ ./test.nix ]; + hostPkgs = pkgs; # the Nixpkgs package set used outside the VMs + defaults.services.foo.package = mypkg; +} + + + runTest returns a derivation that runs the + test. + +
+
+
+ Configuring the nodes + + There are a few special NixOS options for test VMs: + + + + + virtualisation.memorySize + + + + The memory of the VM in megabytes. + + + + + + virtualisation.vlans + + + + The virtual networks to which the VM is connected. See + nat.nix + for an example. + + + + + + virtualisation.writableStore + + + + By default, the Nix store in the VM is not writable. If you + enable this option, a writable union file system is mounted + on top of the Nix store to make it appear writable. This is + necessary for tests that run Nix operations that modify the + store. + + + + + + For more options, see the module + qemu-vm.nix. + + + The test script is a sequence of Python statements that perform + various actions, such as starting VMs, executing commands in the + VMs, and so on. Each virtual machine is represented as an object + stored in the variable name if this is also the + identifier of the machine in the declarative config. If you + specified a node nodes.machine, the following + example starts the machine, waits until it has finished booting, + then executes a command and checks that the output is more-or-less + correct: + + machine.start() machine.wait_for_unit("default.target") if not "Linux" in machine.succeed("uname"): raise Exception("Wrong OS") - - The first line is technically unnecessary; machines are implicitly - started when you first execute an action on them (such as - wait_for_unit or succeed). If - you have multiple machines, you can speed up the test by starting - them in parallel: - - + + The first line is technically unnecessary; machines are implicitly + started when you first execute an action on them (such as + wait_for_unit or succeed). + If you have multiple machines, you can speed up the test by + starting them in parallel: + + start_all() +
Machine objects @@ -563,7 +639,7 @@ machine.wait_for_unit("xautolock.service", "x-session-user") code-linters (this shouldn't be commited though): -import ./make-test-python.nix { +{ skipLint = true; nodes.machine = { config, pkgs, ... }: @@ -595,7 +671,7 @@ import ./make-test-python.nix { the following way: -import ./make-test-python.nix { +{ skipTypeCheck = true; nodes.machine = { config, pkgs, ... }: @@ -669,7 +745,6 @@ def foo_running(): numpy like this: -import ./make-test-python.nix { extraPythonPackages = p: [ p.numpy ]; diff --git a/nixos/lib/default.nix b/nixos/lib/default.nix index 2b3056e01457..65d91342d4d1 100644 --- a/nixos/lib/default.nix +++ b/nixos/lib/default.nix @@ -21,6 +21,8 @@ let seqAttrsIf = cond: a: lib.mapAttrs (_: v: seqIf cond a v); eval-config-minimal = import ./eval-config-minimal.nix { inherit lib; }; + + testing-lib = import ./testing/default.nix { inherit lib; }; in /* This attribute set appears as lib.nixos in the flake, or can be imported @@ -30,4 +32,10 @@ in inherit (seqAttrsIf (!featureFlags?minimalModules) minimalModulesWarning eval-config-minimal) evalModules ; + + inherit (testing-lib) + evalTest + runTest + ; + } diff --git a/nixos/lib/testing-python.nix b/nixos/lib/testing-python.nix index 1c331a3c5162..e72e5d476bfa 100644 --- a/nixos/lib/testing-python.nix +++ b/nixos/lib/testing-python.nix @@ -12,6 +12,10 @@ with pkgs; +let + nixos-lib = import ./default.nix { inherit (pkgs) lib; }; +in + rec { inherit pkgs; @@ -166,26 +170,15 @@ rec { ${lib.optionalString (interactive) "--add-flags --interactive"} ''); - evalTest = module: lib.evalModules { modules = testModules ++ [ module ]; }; - runTest = module: (evalTest module).config.run; + evalTest = module: nixos-lib.evalTest { imports = [ extraTestModule module ]; }; + runTest = module: nixos-lib.runTest { imports = [ extraTestModule module ]; }; - testModules = [ - ./testing/driver.nix - ./testing/interactive.nix - ./testing/legacy.nix - ./testing/meta.nix - ./testing/name.nix - ./testing/network.nix - ./testing/nodes.nix - ./testing/pkgs.nix - ./testing/run.nix - ./testing/testScript.nix - { - config = { - hostPkgs = pkgs; - }; - } - ]; + extraTestModule = { + config = { + hostPkgs = pkgs; + minimalResult = hydra; + }; + }; # Make a full-blown test makeTest = diff --git a/nixos/lib/testing/default.nix b/nixos/lib/testing/default.nix new file mode 100644 index 000000000000..676d52f5c3fb --- /dev/null +++ b/nixos/lib/testing/default.nix @@ -0,0 +1,24 @@ +{ lib }: +let + + evalTest = module: lib.evalModules { modules = testModules ++ [ module ]; }; + runTest = module: (evalTest module).config.result; + + testModules = [ + ./call-test.nix + ./driver.nix + ./interactive.nix + ./legacy.nix + ./meta.nix + ./name.nix + ./network.nix + ./nodes.nix + ./pkgs.nix + ./run.nix + ./testScript.nix + ]; + +in +{ + inherit evalTest runTest testModules; +} From 6e2f7539893ebabc92ccb1421a84fccf09b2052b Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Wed, 15 Jun 2022 18:04:41 +0200 Subject: [PATCH 14/38] nixos/doc/running-nixos-tests-interactively: Describe interactive option --- ...nning-nixos-tests-interactively.section.md | 8 +++++ ...ning-nixos-tests-interactively.section.xml | 32 +++++++++++++------ 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/nixos/doc/manual/development/running-nixos-tests-interactively.section.md b/nixos/doc/manual/development/running-nixos-tests-interactively.section.md index a1431859ff59..6170057442df 100644 --- a/nixos/doc/manual/development/running-nixos-tests-interactively.section.md +++ b/nixos/doc/manual/development/running-nixos-tests-interactively.section.md @@ -24,6 +24,8 @@ back into the test driver command line upon its completion. This allows you to inspect the state of the VMs after the test (e.g. to debug the test script). +## Reuse VM state {#sec-nixos-test-reuse-vm-state} + You can re-use the VM states coming from a previous run by setting the `--keep-vm-state` flag. @@ -33,3 +35,9 @@ $ ./result/bin/nixos-test-driver --keep-vm-state The machine state is stored in the `$TMPDIR/vm-state-machinename` directory. + +## Interactive-only test configuration {#sec-nixos-test-interactive-configuration} + +You can add configuration that is specific to the interactive test driver, by adding to the `interactive` option. +`interactive` is a copy of the regular test options namespace, and is used by the interactive test driver. +It can be helpful for troubleshooting changes that you don't want to apply to regular test runs. diff --git a/nixos/doc/manual/from_md/development/running-nixos-tests-interactively.section.xml b/nixos/doc/manual/from_md/development/running-nixos-tests-interactively.section.xml index 0e47350a0d24..edd3c33ff401 100644 --- a/nixos/doc/manual/from_md/development/running-nixos-tests-interactively.section.xml +++ b/nixos/doc/manual/from_md/development/running-nixos-tests-interactively.section.xml @@ -25,15 +25,29 @@ $ ./result/bin/nixos-test-driver completion. This allows you to inspect the state of the VMs after the test (e.g. to debug the test script). - - You can re-use the VM states coming from a previous run by setting - the --keep-vm-state flag. - - +
+ Reuse VM state + + You can re-use the VM states coming from a previous run by setting + the --keep-vm-state flag. + + $ ./result/bin/nixos-test-driver --keep-vm-state - - The machine state is stored in the - $TMPDIR/vm-state-machinename directory. - + + The machine state is stored in the + $TMPDIR/vm-state-machinename directory. + +
+
+ Interactive-only test configuration + + You can add configuration that is specific to the interactive test + driver, by adding to the interactive option. + interactive is a copy of the regular test + options namespace, and is used by the interactive test driver. It + can be helpful for troubleshooting changes that you don’t want to + apply to regular test runs. + +
From 537f45637367352b33d2979095765a0bdd2b0d00 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Wed, 15 Jun 2022 18:05:48 +0200 Subject: [PATCH 15/38] nixos/doc/running-nixos-tests: Simplify running instructions with nixosTests --- .../development/running-nixos-tests.section.md | 17 +++-------------- .../development/running-nixos-tests.section.xml | 17 +++-------------- 2 files changed, 6 insertions(+), 28 deletions(-) diff --git a/nixos/doc/manual/development/running-nixos-tests.section.md b/nixos/doc/manual/development/running-nixos-tests.section.md index 1bec023b613a..33076f5dc2a7 100644 --- a/nixos/doc/manual/development/running-nixos-tests.section.md +++ b/nixos/doc/manual/development/running-nixos-tests.section.md @@ -2,22 +2,11 @@ You can run tests using `nix-build`. For example, to run the test [`login.nix`](https://github.com/NixOS/nixpkgs/blob/master/nixos/tests/login.nix), -you just do: +you do: ```ShellSession -$ nix-build '' -``` - -or, if you don't want to rely on `NIX_PATH`: - -```ShellSession -$ cd /my/nixpkgs/nixos/tests -$ nix-build login.nix -… -running the VM test script -machine: QEMU running (pid 8841) -… -6 out of 6 tests succeeded +$ cd /my/git/clone/of/nixpkgs +$ nix-build -A nixosTests.login ``` After building/downloading all required dependencies, this will perform diff --git a/nixos/doc/manual/from_md/development/running-nixos-tests.section.xml b/nixos/doc/manual/from_md/development/running-nixos-tests.section.xml index da2e5076c956..23abb546899f 100644 --- a/nixos/doc/manual/from_md/development/running-nixos-tests.section.xml +++ b/nixos/doc/manual/from_md/development/running-nixos-tests.section.xml @@ -4,22 +4,11 @@ You can run tests using nix-build. For example, to run the test login.nix, - you just do: + you do: -$ nix-build '<nixpkgs/nixos/tests/login.nix>' - - - or, if you don’t want to rely on NIX_PATH: - - -$ cd /my/nixpkgs/nixos/tests -$ nix-build login.nix -… -running the VM test script -machine: QEMU running (pid 8841) -… -6 out of 6 tests succeeded +$ cd /my/git/clone/of/nixpkgs +$ nix-build -A nixosTests.login After building/downloading all required dependencies, this will From 38fb09e4278ca3e836da2c175dbff815c06b0632 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Wed, 22 Jun 2022 00:41:12 +0200 Subject: [PATCH 16/38] testing-python.nix: Replace makeTest implementation --- nixos/lib/testing-python.nix | 242 +----------------- .../tools/nixos-build-vms/build-vms.nix | 2 +- 2 files changed, 11 insertions(+), 233 deletions(-) diff --git a/nixos/lib/testing-python.nix b/nixos/lib/testing-python.nix index e72e5d476bfa..cdaed5fa514c 100644 --- a/nixos/lib/testing-python.nix +++ b/nixos/lib/testing-python.nix @@ -20,156 +20,6 @@ rec { inherit pkgs; - # Run an automated test suite in the given virtual network. - runTests = { driver, driverInteractive, pos }: - stdenv.mkDerivation { - name = "vm-test-run-${driver.testName}"; - - requiredSystemFeatures = [ "kvm" "nixos-test" ]; - - buildCommand = - '' - mkdir -p $out - - # effectively mute the XMLLogger - export LOGFILE=/dev/null - - ${driver}/bin/nixos-test-driver -o $out - ''; - - passthru = driver.passthru // { - inherit driver driverInteractive; - }; - - inherit pos; # for better debugging - }; - - # Generate convenience wrappers for running the test driver - # has vlans, vms and test script defaulted through env variables - # also instantiates test script with nodes, if it's a function (contract) - setupDriverForTest = { - testScript - , testName - , nodes - , qemu_pkg ? pkgs.qemu_test - , enableOCR ? false - , skipLint ? false - , skipTypeCheck ? false - , passthru ? {} - , interactive ? false - , extraPythonPackages ? (_ :[]) - }: - let - # Reifies and correctly wraps the python test driver for - # the respective qemu version and with or without ocr support - testDriver = pkgs.callPackage ./test-driver { - inherit enableOCR extraPythonPackages; - qemu_pkg = qemu_test; - imagemagick_light = imagemagick_light.override { inherit libtiff; }; - tesseract4 = tesseract4.override { enableLanguages = [ "eng" ]; }; - }; - - - testDriverName = - let - # A standard store path to the vm monitor is built like this: - # /tmp/nix-build-vm-test-run-$name.drv-0/vm-state-machine/monitor - # The max filename length of a unix domain socket is 108 bytes. - # This means $name can at most be 50 bytes long. - maxTestNameLen = 50; - testNameLen = builtins.stringLength testName; - in with builtins; - if testNameLen > maxTestNameLen then - abort - ("The name of the test '${testName}' must not be longer than ${toString maxTestNameLen} " + - "it's currently ${toString testNameLen} characters long.") - else - "nixos-test-driver-${testName}"; - - vlans = map (m: m.config.virtualisation.vlans) (lib.attrValues nodes); - vms = map (m: m.config.system.build.vm) (lib.attrValues nodes); - - nodeHostNames = let - nodesList = map (c: c.config.system.name) (lib.attrValues nodes); - in nodesList ++ lib.optional (lib.length nodesList == 1 && !lib.elem "machine" nodesList) "machine"; - - # TODO: This is an implementation error and needs fixing - # the testing famework cannot legitimately restrict hostnames further - # beyond RFC1035 - invalidNodeNames = lib.filter - (node: builtins.match "^[A-z_]([A-z0-9_]+)?$" node == null) - nodeHostNames; - - testScript' = - # Call the test script with the computed nodes. - if lib.isFunction testScript - then testScript { inherit nodes; } - else testScript; - - uniqueVlans = lib.unique (builtins.concatLists vlans); - vlanNames = map (i: "vlan${toString i}: VLan;") uniqueVlans; - machineNames = map (name: "${name}: Machine;") nodeHostNames; - in - if lib.length invalidNodeNames > 0 then - throw '' - Cannot create machines out of (${lib.concatStringsSep ", " invalidNodeNames})! - All machines are referenced as python variables in the testing framework which will break the - script when special characters are used. - - This is an IMPLEMENTATION ERROR and needs to be fixed. Meanwhile, - please stick to alphanumeric chars and underscores as separation. - '' - else lib.warnIf skipLint "Linting is disabled" (runCommand testDriverName - { - inherit testName; - nativeBuildInputs = [ makeWrapper mypy ]; - buildInputs = [ testDriver ]; - testScript = testScript'; - preferLocalBuild = true; - passthru = passthru // { - inherit nodes; - }; - meta.mainProgram = "nixos-test-driver"; - } - '' - mkdir -p $out/bin - - vmStartScripts=($(for i in ${toString vms}; do echo $i/bin/run-*-vm; done)) - - ${lib.optionalString (!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 - - 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 (!skipLint) '' - PYFLAKES_BUILTINS="$( - echo -n ${lib.escapeShellArg (lib.concatStringsSep "," nodeHostNames)}, - < ${lib.escapeShellArg "driver-symbols"} - )" ${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" \ - --set vlans '${toString vlans}' \ - ${lib.optionalString (interactive) "--add-flags --interactive"} - ''); - evalTest = module: nixos-lib.evalTest { imports = [ extraTestModule module ]; }; runTest = module: nixos-lib.runTest { imports = [ extraTestModule module ]; }; @@ -199,89 +49,17 @@ rec { else builtins.unsafeGetAttrPos "testScript" t) , extraPythonPackages ? (_ : []) } @ t: - let - mkNodes = qemu_pkg: - let - testScript' = - # Call the test script with the computed nodes. - if lib.isFunction testScript - then testScript { nodes = mkNodes qemu_pkg; } - else testScript; - - build-vms = import ./build-vms.nix { - inherit system lib pkgs minimal specialArgs; - extraConfigurations = extraConfigurations ++ [( - { config, ... }: - { - virtualisation.qemu.package = qemu_pkg; - - # Make sure all derivations referenced by the test - # script are available on the nodes. When the store is - # accessed through 9p, this isn't important, since - # everything in the store is available to the guest, - # but when building a root image it is, as all paths - # that should be available to the guest has to be - # copied to the image. - virtualisation.additionalPaths = - lib.optional - # A testScript may evaluate nodes, which has caused - # infinite recursions. The demand cycle involves: - # testScript --> - # nodes --> - # toplevel --> - # additionalPaths --> - # hasContext testScript' --> - # testScript (ad infinitum) - # If we don't need to build an image, we can break this - # cycle by short-circuiting when useNixStoreImage is false. - (config.virtualisation.useNixStoreImage && builtins.hasContext testScript') - (pkgs.writeStringReferencesToFile testScript'); - - # Ensure we do not use aliases. Ideally this is only set - # when the test framework is used by Nixpkgs NixOS tests. - nixpkgs.config.allowAliases = false; - } - )]; - }; - in - lib.warnIf (t?machine) "In test `${name}': The `machine' attribute in NixOS tests (pkgs.nixosTest / make-test-python.nix / testing-python.nix / makeTest) is deprecated. Please use the equivalent `nodes.machine'." - build-vms.buildVirtualNetwork ( - nodes // lib.optionalAttrs (machine != null) { inherit machine; } - ); - - driver = setupDriverForTest { - inherit testScript enableOCR skipTypeCheck skipLint passthru extraPythonPackages; - testName = name; - qemu_pkg = pkgs.qemu_test; - nodes = mkNodes pkgs.qemu_test; + runTest { + imports = [ + { _file = "makeTest parameters"; config = t; } + { + defaults = { + _file = "makeTest: extraConfigurations"; + imports = extraConfigurations; + }; + } + ]; }; - driverInteractive = setupDriverForTest { - inherit testScript enableOCR skipTypeCheck skipLint passthru extraPythonPackages; - testName = name; - qemu_pkg = pkgs.qemu; - nodes = mkNodes pkgs.qemu; - interactive = true; - }; - - test = lib.addMetaAttrs meta (runTests { inherit driver pos driverInteractive; }); - - in - test // { - inherit test driver driverInteractive; - inherit (driver) nodes; - }; - - abortForFunction = functionName: abort ''The ${functionName} function was - removed because it is not an essential part of the NixOS testing - infrastructure. It had no usage in NixOS or Nixpkgs and it had no designated - maintainer. You are free to reintroduce it by documenting it in the manual - and adding yourself as maintainer. It was removed in - https://github.com/NixOS/nixpkgs/pull/137013 - ''; - - runInMachine = abortForFunction "runInMachine"; - - runInMachineWithX = abortForFunction "runInMachineWithX"; simpleTest = as: (makeTest as).test; diff --git a/nixos/modules/installer/tools/nixos-build-vms/build-vms.nix b/nixos/modules/installer/tools/nixos-build-vms/build-vms.nix index b4a94f62ad93..ced344bce234 100644 --- a/nixos/modules/installer/tools/nixos-build-vms/build-vms.nix +++ b/nixos/modules/installer/tools/nixos-build-vms/build-vms.nix @@ -15,7 +15,7 @@ let inherit system pkgs; }; - interactiveDriver = (testing.makeTest { inherit nodes; testScript = "start_all(); join_all();"; }).driverInteractive; + interactiveDriver = (testing.makeTest { inherit nodes; name = "network"; testScript = "start_all(); join_all();"; }).driverInteractive; in From 24d1d74e4e61e53326b7ed059528693afd17cc50 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Wed, 22 Jun 2022 01:12:01 +0200 Subject: [PATCH 17/38] nixos/testing: Extract nixos-test-base.nix NixOS module --- nixos/lib/testing/nixos-test-base.nix | 23 +++++++++++++++++++++++ nixos/lib/testing/nodes.nix | 13 +------------ 2 files changed, 24 insertions(+), 12 deletions(-) create mode 100644 nixos/lib/testing/nixos-test-base.nix diff --git a/nixos/lib/testing/nixos-test-base.nix b/nixos/lib/testing/nixos-test-base.nix new file mode 100644 index 000000000000..59e6e3843367 --- /dev/null +++ b/nixos/lib/testing/nixos-test-base.nix @@ -0,0 +1,23 @@ +# A module containing the base imports and overrides that +# are always applied in NixOS VM tests, unconditionally, +# even in `inheritParentConfig = false` specialisations. +{ lib, ... }: +let + inherit (lib) mkForce; +in +{ + imports = [ + ../../modules/virtualisation/qemu-vm.nix + ../../modules/testing/test-instrumentation.nix # !!! should only get added for automated test runs + { key = "no-manual"; documentation.nixos.enable = false; } + { + key = "no-revision"; + # Make the revision metadata constant, in order to avoid needless retesting. + # The human version (e.g. 21.05-pre) is left as is, because it is useful + # for external modules that test with e.g. testers.nixosTest and rely on that + # version number. + config.system.nixos.revision = mkForce "constant-nixos-revision"; + } + + ]; +} diff --git a/nixos/lib/testing/nodes.nix b/nixos/lib/testing/nodes.nix index 98580d5dc4f8..a83c2a52d3ac 100644 --- a/nixos/lib/testing/nodes.nix +++ b/nixos/lib/testing/nodes.nix @@ -12,19 +12,8 @@ let modules = [ config.defaults ]; baseModules = (import ../../modules/module-list.nix) ++ [ - ../../modules/virtualisation/qemu-vm.nix - ../../modules/testing/test-instrumentation.nix # !!! should only get added for automated test runs - { key = "no-manual"; documentation.nixos.enable = false; } - { - key = "no-revision"; - # Make the revision metadata constant, in order to avoid needless retesting. - # The human version (e.g. 21.05-pre) is left as is, because it is useful - # for external modules that test with e.g. testers.nixosTest and rely on that - # version number. - config.system.nixos.revision = mkForce "constant-nixos-revision"; - } + ./nixos-test-base.nix { key = "nodes"; _module.args.nodes = nodes; } - ({ config, ... }: { virtualisation.qemu.package = testModuleArgs.config.qemu.package; From 57ac9dd3aeae93b9c4fa6e4a58792b7981535180 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Wed, 22 Jun 2022 00:50:53 +0200 Subject: [PATCH 18/38] nixos/lib/build-vms.nix: Remove It has been replaced by the modular test framework in nixos/lib/testing. If you are looking for a way to produce a VM-test-like configuration outside of the test framework, use the nixos/lib/testing/nixos-test-base.nix NixOS module, possibly in combination with { _module.args.nodes = .....; }. --- nixos/lib/build-vms.nix | 113 ---------------------------------------- 1 file changed, 113 deletions(-) delete mode 100644 nixos/lib/build-vms.nix diff --git a/nixos/lib/build-vms.nix b/nixos/lib/build-vms.nix deleted file mode 100644 index 18af49db1777..000000000000 --- a/nixos/lib/build-vms.nix +++ /dev/null @@ -1,113 +0,0 @@ -{ system -, # Use a minimal kernel? - minimal ? false -, # Ignored - config ? null -, # Nixpkgs, for qemu, lib and more - pkgs, lib -, # !!! See comment about args in lib/modules.nix - specialArgs ? {} -, # NixOS configuration to add to the VMs - extraConfigurations ? [] -}: - -with lib; - -rec { - - inherit pkgs; - - # Build a virtual network from an attribute set `{ machine1 = - # config1; ... machineN = configN; }', where `machineX' is the - # hostname and `configX' is a NixOS system configuration. Each - # machine is given an arbitrary IP address in the virtual network. - buildVirtualNetwork = - nodes: let nodesOut = mapAttrs (n: buildVM nodesOut) (assignIPAddresses nodes); in nodesOut; - - - buildVM = - nodes: configurations: - - import ./eval-config.nix { - inherit system specialArgs; - modules = configurations ++ extraConfigurations; - baseModules = (import ../modules/module-list.nix) ++ - [ ../modules/virtualisation/qemu-vm.nix - ../modules/testing/test-instrumentation.nix # !!! should only get added for automated test runs - { key = "no-manual"; documentation.nixos.enable = false; } - { key = "no-revision"; - # Make the revision metadata constant, in order to avoid needless retesting. - # The human version (e.g. 21.05-pre) is left as is, because it is useful - # for external modules that test with e.g. testers.nixosTest and rely on that - # version number. - config.system.nixos.revision = mkForce "constant-nixos-revision"; - } - { key = "nodes"; _module.args.nodes = nodes; } - ] ++ optional minimal ../modules/testing/minimal-kernel.nix; - }; - - - # Given an attribute set { machine1 = config1; ... machineN = - # configN; }, sequentially assign IP addresses in the 192.168.1.0/24 - # range to each machine, and set the hostname to the attribute name. - assignIPAddresses = nodes: - - let - - machines = attrNames nodes; - - machinesNumbered = zipLists machines (range 1 254); - - nodes_ = forEach machinesNumbered (m: nameValuePair m.fst - [ ( { config, nodes, ... }: - let - interfacesNumbered = zipLists config.virtualisation.vlans (range 1 255); - interfaces = forEach interfacesNumbered ({ fst, snd }: - nameValuePair "eth${toString snd}" { ipv4.addresses = - [ { address = "192.168.${toString fst}.${toString m.snd}"; - prefixLength = 24; - } ]; - }); - - networkConfig = - { networking.hostName = mkDefault m.fst; - - networking.interfaces = listToAttrs interfaces; - - networking.primaryIPAddress = - optionalString (interfaces != []) (head (head interfaces).value.ipv4.addresses).address; - - # Put the IP addresses of all VMs in this machine's - # /etc/hosts file. If a machine has multiple - # interfaces, use the IP address corresponding to - # the first interface (i.e. the first network in its - # virtualisation.vlans option). - networking.extraHosts = flip concatMapStrings machines - (m': let config = (getAttr m' nodes).config; in - optionalString (config.networking.primaryIPAddress != "") - ("${config.networking.primaryIPAddress} " + - optionalString (config.networking.domain != null) - "${config.networking.hostName}.${config.networking.domain} " + - "${config.networking.hostName}\n")); - - virtualisation.qemu.options = - let qemu-common = import ../lib/qemu-common.nix { inherit lib pkgs; }; - in flip concatMap interfacesNumbered - ({ fst, snd }: qemu-common.qemuNICFlags snd fst m.snd); - }; - - in - { key = "ip-address"; - config = networkConfig // { - # Expose the networkConfig items for tests like nixops - # that need to recreate the network config. - system.build.networkConfig = networkConfig; - }; - } - ) - (getAttr m.fst nodes) - ] ); - - in listToAttrs nodes_; - -} From 5297d584bcc5f95c8e87c631813b4e2ab7f19ecc Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Wed, 22 Jun 2022 01:01:34 +0200 Subject: [PATCH 19/38] nixos/lib/eval-config: Document the use of baseModules --- nixos/lib/eval-config.nix | 2 ++ 1 file changed, 2 insertions(+) diff --git a/nixos/lib/eval-config.nix b/nixos/lib/eval-config.nix index 791a03a3ba3c..1e086271e523 100644 --- a/nixos/lib/eval-config.nix +++ b/nixos/lib/eval-config.nix @@ -17,6 +17,8 @@ evalConfigArgs@ # be set modularly anyway. pkgs ? null , # !!! what do we gain by making this configurable? + # we can add modules that are included in specialisations, regardless + # of inheritParentConfig. baseModules ? import ../modules/module-list.nix , # !!! See comment about args in lib/modules.nix extraArgs ? {} From 9886db059a822f9bc1f8fbdacfd1ca1938fe9ebf Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Sat, 25 Jun 2022 12:47:50 +0200 Subject: [PATCH 20/38] nixos/testing: Embrace callTest My conception of its input was wrong. It is quite a useful construct, even if its name is a bit weird. --- nixos/lib/testing-python.nix | 1 - nixos/lib/testing/run.nix | 20 +++++++++++--------- nixos/release.nix | 8 ++++---- nixos/tests/all-tests.nix | 21 +++++++++++++++++---- pkgs/top-level/all-packages.nix | 4 ++-- 5 files changed, 34 insertions(+), 20 deletions(-) diff --git a/nixos/lib/testing-python.nix b/nixos/lib/testing-python.nix index cdaed5fa514c..fd34fe23d767 100644 --- a/nixos/lib/testing-python.nix +++ b/nixos/lib/testing-python.nix @@ -26,7 +26,6 @@ rec { extraTestModule = { config = { hostPkgs = pkgs; - minimalResult = hydra; }; }; diff --git a/nixos/lib/testing/run.nix b/nixos/lib/testing/run.nix index 65bcbe720bf3..cc31914f745d 100644 --- a/nixos/lib/testing/run.nix +++ b/nixos/lib/testing/run.nix @@ -16,22 +16,22 @@ in ''; }; - run = mkOption { + test = mkOption { type = types.package; description = '' - Derivation that runs the test. + Derivation that runs the test as its "build" process. ''; }; }; config = { - run = hostPkgs.stdenv.mkDerivation { - name = "vm-test-run-${config.name}"; + test = lib.lazyDerivation { # lazyDerivation improves performance when only passthru items and/or meta are used. + derivation = hostPkgs.stdenv.mkDerivation { + name = "vm-test-run-${config.name}"; - requiredSystemFeatures = [ "kvm" "nixos-test" ]; + requiredSystemFeatures = [ "kvm" "nixos-test" ]; - buildCommand = - '' + buildCommand = '' mkdir -p $out # effectively mute the XMLLogger @@ -40,9 +40,11 @@ in ${config.driver}/bin/nixos-test-driver -o $out ''; - passthru = config.passthru; + passthru = config.passthru; - meta = config.meta; + meta = config.meta; + }; + inherit (config) passthru meta; }; # useful for inspection (debugging / exploration) diff --git a/nixos/release.nix b/nixos/release.nix index f70b02c4292b..4f27e5dbb215 100644 --- a/nixos/release.nix +++ b/nixos/release.nix @@ -22,8 +22,8 @@ let import ./tests/all-tests.nix { inherit system; pkgs = import ./.. { inherit system; }; - callTest = t: { - ${system} = hydraJob t.test; + callTest = config: { + ${system} = hydraJob config.test; }; } // { # for typechecking of the scripts and evaluation of @@ -32,8 +32,8 @@ let import ./tests/all-tests.nix { inherit system; pkgs = import ./.. { inherit system; }; - callTest = t: { - ${system} = hydraJob t.test.driver; + callTest = config: { + ${system} = hydraJob config.driver; }; }; }; diff --git a/nixos/tests/all-tests.nix b/nixos/tests/all-tests.nix index 5cd58cb5c3fd..62e05fcf2b1e 100644 --- a/nixos/tests/all-tests.nix +++ b/nixos/tests/all-tests.nix @@ -1,4 +1,11 @@ -{ system, pkgs, callTest }: +{ system, + pkgs, + + # Projects the test configuration into a the desired value; usually + # the test runner: `config: config.test`. + callTest, + +}: # The return value of this function will be an attrset with arbitrary depth and # the `anything` returned by callTest at its test leafs. # The tests not supported by `system` will be replaced with `{}`, so that @@ -29,11 +36,17 @@ let inherit (rec { - doRunTest = (import ../lib/testing-python.nix { inherit system pkgs; }).runTest; + doRunTest = arg: (import ../lib/testing-python.nix { inherit system pkgs; }).runTest { + imports = [ arg { inherit callTest; } ]; + }; findTests = tree: if tree?recurseForDerivations && tree.recurseForDerivations - then mapAttrs (k: findTests) (builtins.removeAttrs tree ["recurseForDerivations"]) - else callTest ({ test = tree; }); + then + mapAttrs + (k: findTests) + (builtins.removeAttrs tree ["recurseForDerivations"]) + else callTest tree; + runTest = arg: let r = doRunTest arg; in findTests r; runTestOn = systems: arg: if elem system systems then runTest arg diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index ec53eedd77ff..d369fafdcc49 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -140,14 +140,14 @@ with pkgs; nixosTests = import ../../nixos/tests/all-tests.nix { inherit pkgs; system = stdenv.hostPlatform.system; - callTest = t: t.test; + callTest = config: config.test; } // { # for typechecking of the scripts and evaluation of # the nodes, without running VMs. allDrivers = import ../../nixos/tests/all-tests.nix { inherit pkgs; system = stdenv.hostPlatform.system; - callTest = t: t.test.driver; + callTest = config: config.test.driver; }; }; From e77913a680bc9e7bfd2c93047ecfd6697e2402d7 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Sat, 25 Jun 2022 21:26:50 +0200 Subject: [PATCH 21/38] nixos/all-tests.nix: Invoke tests based on make-test-python.nix --- nixos/tests/all-tests.nix | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/nixos/tests/all-tests.nix b/nixos/tests/all-tests.nix index 62e05fcf2b1e..21e75561964b 100644 --- a/nixos/tests/all-tests.nix +++ b/nixos/tests/all-tests.nix @@ -18,9 +18,18 @@ with pkgs.lib; let discoverTests = val: - if !isAttrs val then val - else if hasAttr "test" val then callTest val - else mapAttrs (n: s: discoverTests s) val; + if isAttrs val + then + if hasAttr "test" val then callTest val + else mapAttrs (n: s: discoverTests s) val + else if isFunction val + then + # Tests based on make-test-python.nix will return the second lambda + # in that file, which are then forwarded to the test definition + # following the `import make-test-python.nix` expression + # (if it is a function). + discoverTests (val { inherit system pkgs; }) + else val; handleTest = path: args: discoverTests (import path ({ inherit system pkgs; } // args)); handleTestOn = systems: path: args: From f01fec40994463da0157e8833969c37e4c03ba25 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Sat, 25 Jun 2022 23:29:49 +0200 Subject: [PATCH 22/38] nixos/testing/network.nix: Fix specialisations onlyShorthand --- nixos/lib/testing/network.nix | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/nixos/lib/testing/network.nix b/nixos/lib/testing/network.nix index c87abee30c93..4c00a0ba45d3 100644 --- a/nixos/lib/testing/network.nix +++ b/nixos/lib/testing/network.nix @@ -93,11 +93,15 @@ let specialisation = mkOption { type = types.attrsOf (types.submodule { options.configuration = mkOption { - type = types.submodule ({ - config.virtualisation.test.nodeName = - # assert regular.config.virtualisation.test.nodeName != "configuration"; - regular.config.virtualisation.test.nodeName; - }); + type = types.submoduleWith { + modules = [ + { + config.virtualisation.test.nodeName = + # assert regular.config.virtualisation.test.nodeName != "configuration"; + regular.config.virtualisation.test.nodeName; + } + ]; + }; }; }); }; From 0af6e6b0e5c3919190c0eca0b42dc10cab82458f Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Sun, 26 Jun 2022 00:13:03 +0200 Subject: [PATCH 23/38] nixos/testing/meta.nix: Add options, some optional --- nixos/lib/testing/meta.nix | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/nixos/lib/testing/meta.nix b/nixos/lib/testing/meta.nix index 1312d6a986ec..07ab27b11f8f 100644 --- a/nixos/lib/testing/meta.nix +++ b/nixos/lib/testing/meta.nix @@ -4,9 +4,25 @@ let in { options = { - meta.maintainers = lib.mkOption { - type = types.listOf types.raw; - default = []; + meta = lib.mkOption { + apply = lib.filterAttrs (k: v: v != null); + type = types.submodule { + options = { + maintainers = lib.mkOption { + type = types.listOf types.raw; + default = []; + }; + timeout = lib.mkOption { + type = types.nullOr types.int; + default = null; + }; + broken = lib.mkOption { + type = types.bool; + default = false; + }; + }; + }; + default = {}; }; }; } From e260018f9c356ff6f82d19b37f0501f8a19f29c0 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Sun, 26 Jun 2022 00:14:38 +0200 Subject: [PATCH 24/38] nixos/tests: Add names --- nixos/tests/corerad.nix | 1 + nixos/tests/ghostunnel.nix | 1 + nixos/tests/lorri/default.nix | 2 ++ nixos/tests/matomo.nix | 2 ++ nixos/tests/matrix/conduit.nix | 2 ++ nixos/tests/nixops/default.nix | 1 + nixos/tests/pam/pam-file-contents.nix | 1 + nixos/tests/pppd.nix | 2 ++ nixos/tests/thelounge.nix | 2 ++ nixos/tests/zrepl.nix | 2 ++ 10 files changed, 16 insertions(+) diff --git a/nixos/tests/corerad.nix b/nixos/tests/corerad.nix index 638010f92f44..b6f5d7fc6f75 100644 --- a/nixos/tests/corerad.nix +++ b/nixos/tests/corerad.nix @@ -1,5 +1,6 @@ import ./make-test-python.nix ( { + name = "corerad"; nodes = { router = {config, pkgs, ...}: { config = { diff --git a/nixos/tests/ghostunnel.nix b/nixos/tests/ghostunnel.nix index 8bea64854021..91a7b7085f67 100644 --- a/nixos/tests/ghostunnel.nix +++ b/nixos/tests/ghostunnel.nix @@ -1,4 +1,5 @@ import ./make-test-python.nix ({ pkgs, ... }: { + name = "ghostunnel"; nodes = { backend = { pkgs, ... }: { services.nginx.enable = true; diff --git a/nixos/tests/lorri/default.nix b/nixos/tests/lorri/default.nix index 209b87f9f26a..a4bdc92490ce 100644 --- a/nixos/tests/lorri/default.nix +++ b/nixos/tests/lorri/default.nix @@ -1,4 +1,6 @@ import ../make-test-python.nix { + name = "lorri"; + nodes.machine = { pkgs, ... }: { imports = [ ../../modules/profiles/minimal.nix ]; environment.systemPackages = [ pkgs.lorri ]; diff --git a/nixos/tests/matomo.nix b/nixos/tests/matomo.nix index 526a24fc4db7..0e09ad295f95 100644 --- a/nixos/tests/matomo.nix +++ b/nixos/tests/matomo.nix @@ -7,6 +7,8 @@ with pkgs.lib; let matomoTest = package: makeTest { + name = "matomo"; + nodes.machine = { config, pkgs, ... }: { services.matomo = { package = package; diff --git a/nixos/tests/matrix/conduit.nix b/nixos/tests/matrix/conduit.nix index 780837f962fa..2b81c23598eb 100644 --- a/nixos/tests/matrix/conduit.nix +++ b/nixos/tests/matrix/conduit.nix @@ -3,6 +3,8 @@ import ../make-test-python.nix ({ pkgs, ... }: name = "conduit"; in { + name = "matrix-conduit"; + nodes = { conduit = args: { services.matrix-conduit = { diff --git a/nixos/tests/nixops/default.nix b/nixos/tests/nixops/default.nix index 227b38815073..b77ac2476398 100644 --- a/nixos/tests/nixops/default.nix +++ b/nixos/tests/nixops/default.nix @@ -19,6 +19,7 @@ let }); testLegacyNetwork = { nixopsPkg }: pkgs.nixosTest ({ + name = "nixops-legacy-network"; nodes = { deployer = { config, lib, nodes, pkgs, ... }: { imports = [ ../../modules/installer/cd-dvd/channel.nix ]; diff --git a/nixos/tests/pam/pam-file-contents.nix b/nixos/tests/pam/pam-file-contents.nix index 86c61003aeb6..2bafd90618e9 100644 --- a/nixos/tests/pam/pam-file-contents.nix +++ b/nixos/tests/pam/pam-file-contents.nix @@ -2,6 +2,7 @@ let name = "pam"; in import ../make-test-python.nix ({ pkgs, ... }: { + name = "pam-file-contents"; nodes.machine = { ... }: { imports = [ ../../modules/profiles/minimal.nix ]; diff --git a/nixos/tests/pppd.nix b/nixos/tests/pppd.nix index bda0aa75bb50..e714a6c21a6c 100644 --- a/nixos/tests/pppd.nix +++ b/nixos/tests/pppd.nix @@ -5,6 +5,8 @@ import ./make-test-python.nix ( mode = "0640"; }; in { + name = "pppd"; + nodes = { server = {config, pkgs, ...}: { config = { diff --git a/nixos/tests/thelounge.nix b/nixos/tests/thelounge.nix index e9b85685bf2d..8d5a37d46c46 100644 --- a/nixos/tests/thelounge.nix +++ b/nixos/tests/thelounge.nix @@ -1,4 +1,6 @@ import ./make-test-python.nix { + name = "thelounge"; + nodes = { private = { config, pkgs, ... }: { services.thelounge = { diff --git a/nixos/tests/zrepl.nix b/nixos/tests/zrepl.nix index 85dd834a6aaf..0ed73fea34b0 100644 --- a/nixos/tests/zrepl.nix +++ b/nixos/tests/zrepl.nix @@ -1,5 +1,7 @@ import ./make-test-python.nix ( { + name = "zrepl"; + nodes.host = {config, pkgs, ...}: { config = { # Prerequisites for ZFS and tests. From 583a4f0275c0c06e7c99758b0aa66aa738a098ab Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Sun, 26 Jun 2022 00:15:00 +0200 Subject: [PATCH 25/38] nixos/tests/cri-o: Fix maintainers --- nixos/tests/cri-o.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nixos/tests/cri-o.nix b/nixos/tests/cri-o.nix index d3a8713d6a9b..08e1e8f36b06 100644 --- a/nixos/tests/cri-o.nix +++ b/nixos/tests/cri-o.nix @@ -1,7 +1,7 @@ # This test runs CRI-O and verifies via critest import ./make-test-python.nix ({ pkgs, ... }: { name = "cri-o"; - meta.maintainers = with pkgs.lib.maintainers; teams.podman.members; + meta.maintainers = with pkgs.lib; teams.podman.members; nodes = { crio = { From 9303a3c73bd45f95997ea52597f901bb8759fd2d Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Sun, 26 Jun 2022 00:15:17 +0200 Subject: [PATCH 26/38] nixos/tests/installed-tests: Fix maintainers --- nixos/tests/installed-tests/default.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nixos/tests/installed-tests/default.nix b/nixos/tests/installed-tests/default.nix index 3bb678d36782..b2c1b43f90ee 100644 --- a/nixos/tests/installed-tests/default.nix +++ b/nixos/tests/installed-tests/default.nix @@ -40,7 +40,7 @@ let name = tested.name; meta = { - maintainers = tested.meta.maintainers; + maintainers = tested.meta.maintainers or []; }; nodes.machine = { ... }: { From 52bfa318e8797cffbd4750efbb59cc4d276187a6 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Sun, 26 Jun 2022 13:57:58 +0200 Subject: [PATCH 27/38] nixos/testing: Support mypy through regular mechanisms Rebase / forward port of 2c8bbf33fd84d2fd9de70d66c1f50ac1b6123dd8 --- nixos/lib/testing/driver.nix | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/nixos/lib/testing/driver.nix b/nixos/lib/testing/driver.nix index 9473d888cbb8..b041693686eb 100644 --- a/nixos/lib/testing/driver.nix +++ b/nixos/lib/testing/driver.nix @@ -52,6 +52,7 @@ let nativeBuildInputs = [ hostPkgs.makeWrapper ] ++ lib.optionals (!config.skipTypeCheck) [ hostPkgs.mypy ]; + buildInputs = [ testDriver ]; testScript = config.testScriptString; preferLocalBuild = true; passthru = config.passthru; @@ -73,13 +74,10 @@ let cat -n testScriptWithTypes - # set pythonpath so mypy knows where to find the imports. this requires the py.typed file. - export PYTHONPATH='${../test-driver}' mypy --no-implicit-optional \ --pretty \ --no-color-output \ testScriptWithTypes - unset PYTHONPATH ''} echo -n "$testScript" >> $out/test-script From 618f82406fb5ef9d57b04c965516c35f41d0e7a3 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Sun, 26 Jun 2022 14:59:21 +0200 Subject: [PATCH 28/38] nixos/tests/installer: Fix docbook dependency --- nixos/tests/installer.nix | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/nixos/tests/installer.nix b/nixos/tests/installer.nix index 8bef4fad3dd2..9e2eca8daa3a 100644 --- a/nixos/tests/installer.nix +++ b/nixos/tests/installer.nix @@ -324,6 +324,9 @@ let desktop-file-utils docbook5 docbook_xsl_ns + (docbook-xsl-ns.override { + withManOptDedupPatch = true; + }) kmod.dev libarchive.dev libxml2.bin @@ -333,6 +336,7 @@ let perlPackages.ListCompare perlPackages.XMLLibXML python3Minimal + python3Packages.mistune shared-mime-info sudo texinfo From 4ce93fbae87bac1b4054b4875d607bcc00ba92bb Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Mon, 27 Jun 2022 13:05:59 +0200 Subject: [PATCH 29/38] nixos/testing-python: Add interactive variant support to makeTest --- nixos/lib/testing-python.nix | 1 + 1 file changed, 1 insertion(+) diff --git a/nixos/lib/testing-python.nix b/nixos/lib/testing-python.nix index fd34fe23d767..c303b0bf17bc 100644 --- a/nixos/lib/testing-python.nix +++ b/nixos/lib/testing-python.nix @@ -47,6 +47,7 @@ rec { then builtins.unsafeGetAttrPos "description" meta else builtins.unsafeGetAttrPos "testScript" t) , extraPythonPackages ? (_ : []) + , interactive ? {} } @ t: runTest { imports = [ From 6205d377477042582adc533197b1a05fbac6ddd0 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Mon, 27 Jun 2022 20:06:30 +0200 Subject: [PATCH 30/38] nixos/testing: Improve option docs --- doc/stdenv/meta.chapter.md | 4 ++ nixos/doc/manual/default.nix | 2 + .../writing-nixos-tests.section.md | 14 +++---- .../writing-nixos-tests.section.xml | 32 +++++++++------- nixos/lib/testing/driver.nix | 31 +++++++++++----- nixos/lib/testing/interactive.nix | 11 +++++- nixos/lib/testing/meta.nix | 18 ++++++++- nixos/lib/testing/name.nix | 13 +++++-- nixos/lib/testing/network.nix | 5 ++- nixos/lib/testing/nodes.nix | 37 +++++++++++++++++-- nixos/lib/testing/run.nix | 10 +++-- nixos/lib/testing/testScript.nix | 10 ++++- 12 files changed, 141 insertions(+), 46 deletions(-) diff --git a/doc/stdenv/meta.chapter.md b/doc/stdenv/meta.chapter.md index 51ad29b4b16e..a83aa0bd90f8 100644 --- a/doc/stdenv/meta.chapter.md +++ b/doc/stdenv/meta.chapter.md @@ -213,6 +213,10 @@ runCommand "my-package-test" { A timeout (in seconds) for building the derivation. If the derivation takes longer than this time to build, it can fail due to breaking the timeout. However, all computers do not have the same computing power, hence some builders may decide to apply a multiplicative factor to this value. When filling this value in, try to keep it approximately consistent with other values already present in `nixpkgs`. +`meta` attributes are not stored in the instantiated derivation. +Therefore, this setting may be lost when the package is used as a dependency. +To be effective, it must be presented directly to an evaluation process that handles the `meta.timeout` attribute. + ### `hydraPlatforms` {#var-meta-hydraPlatforms} The list of Nix platform types for which the Hydra instance at `hydra.nixos.org` will build the package. (Hydra is the Nix-based continuous build system.) It defaults to the value of `meta.platforms`. Thus, the only reason to set `meta.hydraPlatforms` is if you want `hydra.nixos.org` to build the package on a subset of `meta.platforms`, or not at all, e.g. diff --git a/nixos/doc/manual/default.nix b/nixos/doc/manual/default.nix index d61bbaddf764..5eaa44106012 100644 --- a/nixos/doc/manual/default.nix +++ b/nixos/doc/manual/default.nix @@ -13,6 +13,8 @@ with pkgs; let + inherit (lib) hasPrefix removePrefix; + lib = pkgs.lib; docbook_xsl_ns = pkgs.docbook-xsl-ns.override { diff --git a/nixos/doc/manual/development/writing-nixos-tests.section.md b/nixos/doc/manual/development/writing-nixos-tests.section.md index 8dd3e6fb7597..1d8c0a2a664a 100644 --- a/nixos/doc/manual/development/writing-nixos-tests.section.md +++ b/nixos/doc/manual/development/writing-nixos-tests.section.md @@ -22,12 +22,12 @@ A NixOS test is a module that has the following structure: ``` We refer to the whole test above as a test module, whereas the values -in `nodes.` are NixOS modules. (A NixOS configuration is a module.) +in [`nodes.`](#opt-nodes) are NixOS modules themselves. -The option `testScript` is a bit of Python code that executes the +The option [`testScript`](#opt-testScript) is a piece of Python code that executes the test (described below). During the test, it will start one or more virtual machines, the configuration of which is described by -the attribute `nodes`. +the option [`nodes`](#opt-nodes). An example of a single-node test is [`login.nix`](https://github.com/NixOS/nixpkgs/blob/master/nixos/tests/login.nix). @@ -58,7 +58,7 @@ For the purpose of constructing a test matrix, use the `matrix` options instead. hostname = runTest { imports = [ ./hostname.nix ]; defaults.networking.firewall.enable = false; }; ``` -You can run a test with attribute name `mytest` in `all-tests.nix` by invoking: +You can run a test with attribute name `mytest` in `nixos/tests/all-tests.nix` by invoking: ```shell nix-build -A nixosTests.mytest @@ -181,7 +181,7 @@ The following methods are available on machine objects: least one will be returned. ::: {.note} - This requires passing `enableOCR` to the test attribute set. + This requires [`enableOCR`](#opt-enableOCR) to be set to `true`. ::: `get_screen_text` @@ -190,7 +190,7 @@ The following methods are available on machine objects: machine\'s screen using optical character recognition. ::: {.note} - This requires passing `enableOCR` to the test attribute set. + This requires [`enableOCR`](#opt-enableOCR) to be set to `true`. ::: `send_monitor_command` @@ -301,7 +301,7 @@ The following methods are available on machine objects: `get_screen_text` and `get_screen_text_variants`). ::: {.note} - This requires passing `enableOCR` to the test attribute set. + This requires [`enableOCR`](#opt-enableOCR) to be set to `true`. ::: `wait_for_console_text` diff --git a/nixos/doc/manual/from_md/development/writing-nixos-tests.section.xml b/nixos/doc/manual/from_md/development/writing-nixos-tests.section.xml index 6d0465e4230b..4910f0e863b5 100644 --- a/nixos/doc/manual/from_md/development/writing-nixos-tests.section.xml +++ b/nixos/doc/manual/from_md/development/writing-nixos-tests.section.xml @@ -23,14 +23,17 @@
We refer to the whole test above as a test module, whereas the - values in nodes.<name> are NixOS modules. - (A NixOS configuration is a module.) + values in + nodes.<name> + are NixOS modules themselves. - The option testScript is a bit of Python code - that executes the test (described below). During the test, it will - start one or more virtual machines, the configuration of which is - described by the attribute nodes. + The option + testScript + is a piece of Python code that executes the test (described below). + During the test, it will start one or more virtual machines, the + configuration of which is described by the option + nodes. An example of a single-node test is @@ -73,7 +76,7 @@ You can run a test with attribute name mytest - in all-tests.nix by invoking: + in nixos/tests/all-tests.nix by invoking: nix-build -A nixosTests.mytest @@ -270,8 +273,9 @@ start_all() - This requires passing enableOCR to the - test attribute set. + This requires + enableOCR + to be set to true. @@ -287,8 +291,9 @@ start_all() - This requires passing enableOCR to the - test attribute set. + This requires + enableOCR + to be set to true. @@ -527,8 +532,9 @@ start_all() - This requires passing enableOCR to the - test attribute set. + This requires + enableOCR + to be set to true. diff --git a/nixos/lib/testing/driver.nix b/nixos/lib/testing/driver.nix index b041693686eb..04e99f9e21d6 100644 --- a/nixos/lib/testing/driver.nix +++ b/nixos/lib/testing/driver.nix @@ -1,6 +1,6 @@ { config, lib, hostPkgs, ... }: let - inherit (lib) mkOption types; + inherit (lib) mkOption types literalMD mdDoc; # Reifies and correctly wraps the python test driver for # the respective qemu version and with or without ocr support @@ -106,13 +106,13 @@ in options = { driver = mkOption { - description = "Script that runs the test."; + description = mdDoc "Package containing a script that runs the test."; type = types.package; - defaultText = lib.literalDocBook "set by the test framework"; + defaultText = literalMD "set by the test framework"; }; hostPkgs = mkOption { - description = "Nixpkgs attrset used outside the nodes."; + description = mdDoc "Nixpkgs attrset used outside the nodes."; type = types.raw; example = lib.literalExpression '' import nixpkgs { inherit system config overlays; } @@ -120,34 +120,39 @@ in }; qemu.package = mkOption { - description = "Which qemu package to use."; + description = mdDoc "Which qemu package to use for the virtualisation of [{option}`nodes`](#opt-nodes)."; type = types.package; default = hostPkgs.qemu_test; defaultText = "hostPkgs.qemu_test"; }; enableOCR = mkOption { - description = '' + description = mdDoc '' Whether to enable Optical Character Recognition functionality for - testing graphical programs. + testing graphical programs. See [Machine objects](`ssec-machine-objects`). ''; type = types.bool; default = false; }; extraPythonPackages = mkOption { - description = '' + description = mdDoc '' Python packages to add to the test driver. The argument is a Python package set, similar to `pkgs.pythonPackages`. ''; + example = lib.literalExpression '' + p: [ p.numpy ] + ''; type = types.functionTo (types.listOf types.package); default = ps: [ ]; }; extraDriverArgs = mkOption { - description = '' + description = mdDoc '' Extra arguments to pass to the test driver. + + They become part of [{option}`driver`](#opt-driver) via `wrapProgram`. ''; type = types.listOf types.str; default = []; @@ -156,11 +161,19 @@ in skipLint = mkOption { type = types.bool; default = false; + description = mdDoc '' + Do not run the linters. This may speed up your iteration cycle, but it is not something you should commit. + ''; }; skipTypeCheck = mkOption { type = types.bool; default = false; + description = mdDoc '' + Disable type checking. This must not be enabled for new NixOS tests. + + This may speed up your iteration cycle, unless you're working on the [{option}`testScript`](#opt-testScript). + ''; }; }; diff --git a/nixos/lib/testing/interactive.nix b/nixos/lib/testing/interactive.nix index fd4d481a3f82..43886fa72678 100644 --- a/nixos/lib/testing/interactive.nix +++ b/nixos/lib/testing/interactive.nix @@ -1,12 +1,19 @@ { config, lib, moduleType, hostPkgs, ... }: let - inherit (lib) mkOption types; + inherit (lib) mkOption types mdDoc; in { options = { interactive = mkOption { - description = "All the same options, but configured for interactive use."; + description = mdDoc '' + Tests [can be run interactively](#sec-running-nixos-tests-interactively). + + When they are, the configuration will include anything set in this submodule. + + You can set any top-level test option here. + ''; type = moduleType; + visible = "shallow"; }; }; diff --git a/nixos/lib/testing/meta.nix b/nixos/lib/testing/meta.nix index 07ab27b11f8f..4d8b0e0f1c43 100644 --- a/nixos/lib/testing/meta.nix +++ b/nixos/lib/testing/meta.nix @@ -1,24 +1,38 @@ { lib, ... }: let - inherit (lib) types mkOption; + inherit (lib) types mkOption mdDoc; in { options = { meta = lib.mkOption { + description = mdDoc '' + The [`meta`](https://nixos.org/manual/nixpkgs/stable/#chap-meta) attributes that will be set on the returned derivations. + + Not all [`meta`](https://nixos.org/manual/nixpkgs/stable/#chap-meta) attributes are supported, but more can be added as desired. + ''; apply = lib.filterAttrs (k: v: v != null); type = types.submodule { options = { maintainers = lib.mkOption { type = types.listOf types.raw; default = []; + description = mdDoc '' + The [list of maintainers](https://nixos.org/manual/nixpkgs/stable/#var-meta-maintainers) for this test. + ''; }; timeout = lib.mkOption { type = types.nullOr types.int; - default = null; + default = null; # NOTE: null values are filtered out by `meta`. + description = mdDoc '' + The [{option}`test`](#opt-test)'s [`meta.timeout`](https://nixos.org/manual/nixpkgs/stable/#var-meta-timeout) in seconds. + ''; }; broken = lib.mkOption { type = types.bool; default = false; + description = mdDoc '' + Sets the [`meta.broken`](https://nixos.org/manual/nixpkgs/stable/#var-meta-broken) attribute on the [{option}`test`](#opt-test) derivation. + ''; }; }; }; diff --git a/nixos/lib/testing/name.nix b/nixos/lib/testing/name.nix index f9fa488511c0..a54622e139bf 100644 --- a/nixos/lib/testing/name.nix +++ b/nixos/lib/testing/name.nix @@ -1,7 +1,14 @@ { lib, ... }: +let + inherit (lib) mkOption types mdDoc; +in { - options.name = lib.mkOption { - description = "The name of the test."; - type = lib.types.str; + options.name = mkOption { + description = mdDoc '' + The name of the test. + + This is used in the derivation names of the [{option}`driver`](#opt-driver) and [{option}`test`](#opt-test) runner. + ''; + type = types.str; }; } diff --git a/nixos/lib/testing/network.nix b/nixos/lib/testing/network.nix index 4c00a0ba45d3..513ec7a14092 100644 --- a/nixos/lib/testing/network.nix +++ b/nixos/lib/testing/network.nix @@ -5,6 +5,7 @@ let attrNames concatMap concatMapStrings flip forEach head listToAttrs mkDefault mkOption nameValuePair optionalString range types zipListsWith zipLists + mdDoc ; nodeNumbers = @@ -74,7 +75,7 @@ let default = name; # We need to force this in specilisations, otherwise it'd be # readOnly = true; - description = '' + description = mdDoc '' The `name` in `nodes.`; stable across `specialisations`. ''; }; @@ -83,7 +84,7 @@ let type = types.int; readOnly = true; default = nodeNumbers.${config.virtualisation.test.nodeName}; - description = '' + description = mdDoc '' A unique number assigned for each node in `nodes`. ''; }; diff --git a/nixos/lib/testing/nodes.nix b/nixos/lib/testing/nodes.nix index a83c2a52d3ac..624fc384c7ec 100644 --- a/nixos/lib/testing/nodes.nix +++ b/nixos/lib/testing/nodes.nix @@ -1,7 +1,7 @@ testModuleArgs@{ config, lib, hostPkgs, nodes, ... }: let - inherit (lib) mkOption mkForce optional types mapAttrs mkDefault; + inherit (lib) mkOption mkForce optional types mapAttrs mkDefault mdDoc; system = hostPkgs.stdenv.hostPlatform.system; @@ -39,11 +39,29 @@ in nodes = mkOption { type = types.lazyAttrsOf config.node.type; + visible = "shallow"; + description = mdDoc '' + An attribute set of NixOS configuration modules. + + The configurations are augmented by the [`defaults`](#opt-defaults) option. + + They are assigned network addresses according to the `nixos/lib/testing/network.nix` module. + + A few special options are available, that aren't in a plain NixOS configuration. See [Configuring the nodes](#sec-nixos-test-nodes) + ''; }; defaults = mkOption { - description = '' - NixOS configuration that is applied to all {option}`nodes`. + description = mdDoc '' + NixOS configuration that is applied to all [{option}`nodes`](#opt-nodes). + ''; + type = types.deferredModule; + default = { }; + }; + + extraBaseModules = mkOption { + description = mdDoc '' + NixOS configuration that, like [{option}`defaults`](#opt-defaults), is applied to all [{option}`nodes`](#opt-nodes) and can not be undone with [`specialisation..inheritParentConfig`](https://search.nixos.org/options?show=specialisation.%3Cname%3E.inheritParentConfig&from=0&size=50&sort=relevance&type=packages&query=specialisation). ''; type = types.deferredModule; default = { }; @@ -52,15 +70,28 @@ in node.specialArgs = mkOption { type = types.lazyAttrsOf types.raw; default = { }; + description = mdDoc '' + An attribute set of arbitrary values that will be made available as module arguments during the resolution of module `imports`. + + Note that it is not possible to override these from within the NixOS configurations. If you argument is not relevant to `imports`, consider setting {option}`defaults._module.args.` instead. + ''; }; minimal = mkOption { type = types.bool; default = false; + description = mdDoc '' + Enable to configure all [{option}`nodes`](#opt-nodes) to run with a minimal kernel. + ''; }; nodesCompat = mkOption { internal = true; + description = mdDoc '' + Basically `_module.args.nodes`, but with backcompat and warnings added. + + This will go away. + ''; }; }; diff --git a/nixos/lib/testing/run.nix b/nixos/lib/testing/run.nix index cc31914f745d..0cd07d8afd21 100644 --- a/nixos/lib/testing/run.nix +++ b/nixos/lib/testing/run.nix @@ -1,12 +1,12 @@ { config, hostPkgs, lib, ... }: let - inherit (lib) types mkOption; + inherit (lib) types mkOption mdDoc; in { options = { passthru = mkOption { type = types.lazyAttrsOf types.raw; - description = '' + description = mdDoc '' Attributes to add to the returned derivations, which are not necessarily part of the build. @@ -18,8 +18,12 @@ in test = mkOption { type = types.package; - description = '' + # TODO: can the interactive driver be configured to access the network? + description = mdDoc '' Derivation that runs the test as its "build" process. + + This implies that NixOS tests run isolated from the network, making them + more dependable. ''; }; }; diff --git a/nixos/lib/testing/testScript.nix b/nixos/lib/testing/testScript.nix index 08e87b626b3b..5d4181c5f5dd 100644 --- a/nixos/lib/testing/testScript.nix +++ b/nixos/lib/testing/testScript.nix @@ -1,12 +1,16 @@ testModuleArgs@{ config, lib, hostPkgs, nodes, moduleType, ... }: let - inherit (lib) mkOption types; + inherit (lib) mkOption types mdDoc; inherit (types) either str functionTo; in { options = { testScript = mkOption { type = either str (functionTo str); + description = '' + A series of python declarations and statements that you write to perform + the test. + ''; }; testScriptString = mkOption { type = str; @@ -21,9 +25,11 @@ in }; withoutTestScriptReferences = mkOption { type = moduleType; - description = '' + description = mdDoc '' A parallel universe where the testScript is invalid and has no references. ''; + internal = true; + visible = false; }; }; config = { From ac03757eb214edf3de5d36761ea4a3f2b0dfd370 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Mon, 27 Jun 2022 17:52:08 +0200 Subject: [PATCH 31/38] nixos/doc: Wire up the test options reference --- nixos/doc/manual/default.nix | 28 +++++++++++++++++++ .../writing-nixos-tests.section.md | 9 +++++- .../writing-nixos-tests.section.xml | 13 ++++++--- 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/nixos/doc/manual/default.nix b/nixos/doc/manual/default.nix index 5eaa44106012..ecd62eb4e848 100644 --- a/nixos/doc/manual/default.nix +++ b/nixos/doc/manual/default.nix @@ -38,6 +38,33 @@ let }; }; + nixos-lib = import ../../lib { }; + + testOptionsDoc = let + eval = nixos-lib.evalTest { + # Avoid evaluating a NixOS config prototype. + config.node.type = lib.types.deferredModule; + options._module.args = lib.mkOption { internal = true; }; + }; + in buildPackages.nixosOptionsDoc { + inherit (eval) options; + inherit (revision); + transformOptions = opt: opt // { + # Clean up declaration sites to not refer to the NixOS source tree. + declarations = + map + (decl: + if hasPrefix (toString ../../..) (toString decl) + then + let subpath = removePrefix "/" (removePrefix (toString ../../..) (toString decl)); + in { url = "https://github.com/NixOS/nixpkgs/blob/master/${subpath}"; name = subpath; } + else decl) + opt.declarations; + }; + documentType = "none"; + variablelistId = "test-options-list"; + }; + sources = lib.sourceFilesBySuffices ./. [".xml"]; modulesDoc = builtins.toFile "modules.xml" '' @@ -52,6 +79,7 @@ let mkdir $out ln -s ${modulesDoc} $out/modules.xml ln -s ${optionsDoc.optionsDocBook} $out/options-db.xml + ln -s ${testOptionsDoc.optionsDocBook} $out/test-options-db.xml printf "%s" "${version}" > $out/version ''; diff --git a/nixos/doc/manual/development/writing-nixos-tests.section.md b/nixos/doc/manual/development/writing-nixos-tests.section.md index 1d8c0a2a664a..043da45d517f 100644 --- a/nixos/doc/manual/development/writing-nixos-tests.section.md +++ b/nixos/doc/manual/development/writing-nixos-tests.section.md @@ -52,7 +52,6 @@ Tests that are part of NixOS are added to [`nixos/tests/all-tests.nix`](https:// ``` Overrides can be added by defining an anonymous module in `all-tests.nix`. -For the purpose of constructing a test matrix, use the `matrix` options instead. ```nix hostname = runTest { imports = [ ./hostname.nix ]; defaults.networking.firewall.enable = false; }; @@ -476,3 +475,11 @@ added using the parameter `extraPythonPackages`. For example, you could add ``` In that case, `numpy` is chosen from the generic `python3Packages`. + +## Test Options Reference {#sec-test-options-reference} + +The following options can be used when writing tests. + +```{=docbook} + +``` diff --git a/nixos/doc/manual/from_md/development/writing-nixos-tests.section.xml b/nixos/doc/manual/from_md/development/writing-nixos-tests.section.xml index 4910f0e863b5..ed6662f637fc 100644 --- a/nixos/doc/manual/from_md/development/writing-nixos-tests.section.xml +++ b/nixos/doc/manual/from_md/development/writing-nixos-tests.section.xml @@ -1,4 +1,4 @@ -
+
Writing Tests A NixOS test is a module that has the following structure: @@ -67,9 +67,7 @@ Overrides can be added by defining an anonymous module in - all-tests.nix. For the purpose of - constructing a test matrix, use the matrix - options instead. + all-tests.nix. hostname = runTest { imports = [ ./hostname.nix ]; defaults.networking.firewall.enable = false; }; @@ -770,4 +768,11 @@ def foo_running(): python3Packages.
+
+ Test Options Reference + + The following options can be used when writing tests. + + +
From 666e969da02e809ccd9f933e50d5759712286f8b Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Mon, 27 Jun 2022 20:07:08 +0200 Subject: [PATCH 32/38] nixos/testing: Add nodes.config backcompat to nodes module argument --- nixos/lib/testing/nodes.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nixos/lib/testing/nodes.nix b/nixos/lib/testing/nodes.nix index 624fc384c7ec..c2d4ad6e61bc 100644 --- a/nixos/lib/testing/nodes.nix +++ b/nixos/lib/testing/nodes.nix @@ -13,7 +13,7 @@ let baseModules = (import ../../modules/module-list.nix) ++ [ ./nixos-test-base.nix - { key = "nodes"; _module.args.nodes = nodes; } + { key = "nodes"; _module.args.nodes = config.nodesCompat; } ({ config, ... }: { virtualisation.qemu.package = testModuleArgs.config.qemu.package; From b2caf7965c0ba5526956ad05d340168a77fd799e Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Mon, 11 Jul 2022 11:44:15 +0200 Subject: [PATCH 33/38] nixos/doc/writing-nixos-tests: Clarify working directory Co-authored-by: christian-burger --- nixos/doc/manual/development/writing-nixos-tests.section.md | 1 + .../manual/from_md/development/writing-nixos-tests.section.xml | 1 + 2 files changed, 2 insertions(+) diff --git a/nixos/doc/manual/development/writing-nixos-tests.section.md b/nixos/doc/manual/development/writing-nixos-tests.section.md index 043da45d517f..f6f6c960b071 100644 --- a/nixos/doc/manual/development/writing-nixos-tests.section.md +++ b/nixos/doc/manual/development/writing-nixos-tests.section.md @@ -60,6 +60,7 @@ Overrides can be added by defining an anonymous module in `all-tests.nix`. You can run a test with attribute name `mytest` in `nixos/tests/all-tests.nix` by invoking: ```shell +cd /my/git/clone/of/nixpkgs nix-build -A nixosTests.mytest ``` diff --git a/nixos/doc/manual/from_md/development/writing-nixos-tests.section.xml b/nixos/doc/manual/from_md/development/writing-nixos-tests.section.xml index ed6662f637fc..c5343f0a711f 100644 --- a/nixos/doc/manual/from_md/development/writing-nixos-tests.section.xml +++ b/nixos/doc/manual/from_md/development/writing-nixos-tests.section.xml @@ -77,6 +77,7 @@ in nixos/tests/all-tests.nix by invoking: +cd /my/git/clone/of/nixpkgs nix-build -A nixosTests.mytest
From 6a78b414768de3f8107e1eecdf79121f3ee0c8a0 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Fri, 29 Jul 2022 11:50:12 +0200 Subject: [PATCH 34/38] nixos/doc/writing-nixos-tests: Various improvements Thanks to fricklerhandwerk for the many suggestions, most of which I have fixupped into preceding commits. --- .../writing-nixos-tests.section.md | 15 +++++++------ .../writing-nixos-tests.section.xml | 22 +++++++++---------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/nixos/doc/manual/development/writing-nixos-tests.section.md b/nixos/doc/manual/development/writing-nixos-tests.section.md index f6f6c960b071..62da4f33d818 100644 --- a/nixos/doc/manual/development/writing-nixos-tests.section.md +++ b/nixos/doc/manual/development/writing-nixos-tests.section.md @@ -39,12 +39,10 @@ It uses two client nodes to test correct locking across server crashes. ## Calling a test {#sec-calling-nixos-tests} -Tests are invoked a bit differently depending on whether the test lives in NixOS or in another project. +Tests are invoked differently depending on whether the test is part of NixOS or lives in a different project. ### Testing within NixOS {#sec-call-nixos-test-in-nixos} -Test modules can be instantiated into derivations in multiple ways. - Tests that are part of NixOS are added to [`nixos/tests/all-tests.nix`](https://github.com/NixOS/nixpkgs/blob/master/nixos/tests/all-tests.nix). ```nix @@ -54,19 +52,22 @@ Tests that are part of NixOS are added to [`nixos/tests/all-tests.nix`](https:// Overrides can be added by defining an anonymous module in `all-tests.nix`. ```nix - hostname = runTest { imports = [ ./hostname.nix ]; defaults.networking.firewall.enable = false; }; + hostname = runTest { + imports = [ ./hostname.nix ]; + defaults.networking.firewall.enable = false; + }; ``` -You can run a test with attribute name `mytest` in `nixos/tests/all-tests.nix` by invoking: +You can run a test with attribute name `hostname` in `nixos/tests/all-tests.nix` by invoking: ```shell cd /my/git/clone/of/nixpkgs -nix-build -A nixosTests.mytest +nix-build -A nixosTests.hostname ``` ### Testing outside the NixOS project {#sec-call-nixos-test-outside-nixos} -Outside the `nixpkgs` repository, you can instantiate the test by first acquiring the NixOS library, +Outside the `nixpkgs` repository, you can instantiate the test by first importing the NixOS library, ```nix # regular nix diff --git a/nixos/doc/manual/from_md/development/writing-nixos-tests.section.xml b/nixos/doc/manual/from_md/development/writing-nixos-tests.section.xml index c5343f0a711f..6e27845b598d 100644 --- a/nixos/doc/manual/from_md/development/writing-nixos-tests.section.xml +++ b/nixos/doc/manual/from_md/development/writing-nixos-tests.section.xml @@ -49,15 +49,11 @@
Calling a test - Tests are invoked a bit differently depending on whether the test - lives in NixOS or in another project. + Tests are invoked differently depending on whether the test is + part of NixOS or lives in a different project.
Testing within NixOS - - Test modules can be instantiated into derivations in multiple - ways. - Tests that are part of NixOS are added to nixos/tests/all-tests.nix. @@ -70,22 +66,26 @@ all-tests.nix. - hostname = runTest { imports = [ ./hostname.nix ]; defaults.networking.firewall.enable = false; }; + hostname = runTest { + imports = [ ./hostname.nix ]; + defaults.networking.firewall.enable = false; + }; - You can run a test with attribute name mytest - in nixos/tests/all-tests.nix by invoking: + You can run a test with attribute name + hostname in + nixos/tests/all-tests.nix by invoking: cd /my/git/clone/of/nixpkgs -nix-build -A nixosTests.mytest +nix-build -A nixosTests.hostname
Testing outside the NixOS project Outside the nixpkgs repository, you can - instantiate the test by first acquiring the NixOS library, + instantiate the test by first importing the NixOS library, # regular nix From 7cdc9bc34012e367522b6ac7ed98631fa95c5deb Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Fri, 19 Aug 2022 12:09:58 +0200 Subject: [PATCH 35/38] nixos/testing: Improve interactive docs --- ...nning-nixos-tests-interactively.section.md | 12 +++++++--- ...ning-nixos-tests-interactively.section.xml | 23 ++++++++++++++----- nixos/lib/testing/interactive.nix | 22 +++++++++++++++++- 3 files changed, 47 insertions(+), 10 deletions(-) diff --git a/nixos/doc/manual/development/running-nixos-tests-interactively.section.md b/nixos/doc/manual/development/running-nixos-tests-interactively.section.md index 6170057442df..d9c316f4b139 100644 --- a/nixos/doc/manual/development/running-nixos-tests-interactively.section.md +++ b/nixos/doc/manual/development/running-nixos-tests-interactively.section.md @@ -38,6 +38,12 @@ directory. ## Interactive-only test configuration {#sec-nixos-test-interactive-configuration} -You can add configuration that is specific to the interactive test driver, by adding to the `interactive` option. -`interactive` is a copy of the regular test options namespace, and is used by the interactive test driver. -It can be helpful for troubleshooting changes that you don't want to apply to regular test runs. +The `.driverInteractive` attribute combines the regular test configuration with +definitions from the [`interactive` submodule](#opt-interactive). This gives you +a more usable, graphical, but slightly different configuration. + +You can add your own interactive-only test configuration by adding extra +configuration to the [`interactive` submodule](#opt-interactive). + +To interactively run only the regular configuration, build the `.driver` attribute +instead, and call it with the flag `result/bin/nixos-test-driver --interactive`. diff --git a/nixos/doc/manual/from_md/development/running-nixos-tests-interactively.section.xml b/nixos/doc/manual/from_md/development/running-nixos-tests-interactively.section.xml index edd3c33ff401..35d9bbd1c1fe 100644 --- a/nixos/doc/manual/from_md/development/running-nixos-tests-interactively.section.xml +++ b/nixos/doc/manual/from_md/development/running-nixos-tests-interactively.section.xml @@ -42,12 +42,23 @@ $ ./result/bin/nixos-test-driver --keep-vm-state
Interactive-only test configuration - You can add configuration that is specific to the interactive test - driver, by adding to the interactive option. - interactive is a copy of the regular test - options namespace, and is used by the interactive test driver. It - can be helpful for troubleshooting changes that you don’t want to - apply to regular test runs. + The .driverInteractive attribute combines the + regular test configuration with definitions from the + interactive + submodule. This gives you a more usable, graphical, but + slightly different configuration. + + + You can add your own interactive-only test configuration by adding + extra configuration to the + interactive + submodule. + + + To interactively run only the regular configuration, build the + <test>.driver attribute instead, and call + it with the flag + result/bin/nixos-test-driver --interactive.
diff --git a/nixos/lib/testing/interactive.nix b/nixos/lib/testing/interactive.nix index 43886fa72678..317ed4241882 100644 --- a/nixos/lib/testing/interactive.nix +++ b/nixos/lib/testing/interactive.nix @@ -6,11 +6,31 @@ in options = { interactive = mkOption { description = mdDoc '' - Tests [can be run interactively](#sec-running-nixos-tests-interactively). + Tests [can be run interactively](#sec-running-nixos-tests-interactively) + using the program in the test derivation's `.driverInteractive` attribute. When they are, the configuration will include anything set in this submodule. You can set any top-level test option here. + + Example test module: + + ```nix + { config, lib, ... }: { + + nodes.rabbitmq = { + services.rabbitmq.enable = true; + }; + + # When running interactively ... + interactive.nodes.rabbitmq = { + # ... enable the web ui. + services.rabbitmq.managementPlugin.enable = true; + }; + } + ``` + + For details, see the section about [running tests interactively](#sec-running-nixos-tests-interactively). ''; type = moduleType; visible = "shallow"; From 1c0b9c4a482b745040be828eafa8bb6b4e3b2200 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Sat, 17 Sep 2022 14:34:14 +0100 Subject: [PATCH 36/38] nixos/testing/network.nix: Add network config to specialisations --- nixos/lib/testing/network.nix | 2 +- nixos/lib/testing/nodes.nix | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/nixos/lib/testing/network.nix b/nixos/lib/testing/network.nix index 513ec7a14092..04ea9a2bc9f7 100644 --- a/nixos/lib/testing/network.nix +++ b/nixos/lib/testing/network.nix @@ -112,6 +112,6 @@ let in { config = { - defaults = { imports = [ networkModule nodeNumberModule ]; }; + extraBaseModules = { imports = [ networkModule nodeNumberModule ]; }; }; } diff --git a/nixos/lib/testing/nodes.nix b/nixos/lib/testing/nodes.nix index c2d4ad6e61bc..765af2878dfe 100644 --- a/nixos/lib/testing/nodes.nix +++ b/nixos/lib/testing/nodes.nix @@ -22,6 +22,7 @@ let # when the test framework is used by Nixpkgs NixOS tests. nixpkgs.config.allowAliases = false; }) + testModuleArgs.config.extraBaseModules ] ++ optional config.minimal ../../modules/testing/minimal-kernel.nix; }; From f40ff7f1f12b49ca1aa5c9a0e3bad6cfdcf3ccaa Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Sun, 18 Sep 2022 12:59:14 +0100 Subject: [PATCH 37/38] nixos/tests/installer: Add make-options-doc dep --- nixos/tests/installer.nix | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/nixos/tests/installer.nix b/nixos/tests/installer.nix index 9e2eca8daa3a..d9f64a781c57 100644 --- a/nixos/tests/installer.nix +++ b/nixos/tests/installer.nix @@ -336,7 +336,13 @@ let perlPackages.ListCompare perlPackages.XMLLibXML python3Minimal - python3Packages.mistune + # make-options-doc/default.nix + (let + self = (pkgs.python3Minimal.override { + inherit self; + includeSiteCustomize = true; + }); + in self.withPackages (p: [ p.mistune ])) shared-mime-info sudo texinfo From 3ce4179374352b1407b1800cb97c1e08a07db4ca Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Sat, 24 Sep 2022 17:32:56 +0100 Subject: [PATCH 38/38] nixos/doc/writing-nixos-tests: Remove flake info for now --- .../development/writing-nixos-tests.section.md | 11 ----------- .../development/writing-nixos-tests.section.xml | 12 +----------- 2 files changed, 1 insertion(+), 22 deletions(-) diff --git a/nixos/doc/manual/development/writing-nixos-tests.section.md b/nixos/doc/manual/development/writing-nixos-tests.section.md index 62da4f33d818..99704ec3c141 100644 --- a/nixos/doc/manual/development/writing-nixos-tests.section.md +++ b/nixos/doc/manual/development/writing-nixos-tests.section.md @@ -70,20 +70,9 @@ nix-build -A nixosTests.hostname Outside the `nixpkgs` repository, you can instantiate the test by first importing the NixOS library, ```nix -# regular nix let nixos-lib = import (nixpkgs + "/nixos/lib") { }; in -``` -```nix -# flake -let nixos-lib = nixpkgs.lib.nixos; -in -``` - -... and then invoking `runTest`, for example: - -```nix nixos-lib.runTest { imports = [ ./test.nix ]; hostPkgs = pkgs; # the Nixpkgs package set used outside the VMs diff --git a/nixos/doc/manual/from_md/development/writing-nixos-tests.section.xml b/nixos/doc/manual/from_md/development/writing-nixos-tests.section.xml index 6e27845b598d..32f5fdb77f50 100644 --- a/nixos/doc/manual/from_md/development/writing-nixos-tests.section.xml +++ b/nixos/doc/manual/from_md/development/writing-nixos-tests.section.xml @@ -88,19 +88,9 @@ nix-build -A nixosTests.hostname instantiate the test by first importing the NixOS library, -# regular nix let nixos-lib = import (nixpkgs + "/nixos/lib") { }; in - - -# flake -let nixos-lib = nixpkgs.lib.nixos; -in - - - … and then invoking runTest, for example: - - + nixos-lib.runTest { imports = [ ./test.nix ]; hostPkgs = pkgs; # the Nixpkgs package set used outside the VMs