2019-09-06 09:25:22 +02:00
|
|
|
{ system
|
|
|
|
, pkgs ? import ../.. { inherit system config; }
|
|
|
|
# Use a minimal kernel?
|
|
|
|
, minimal ? false
|
|
|
|
# Ignored
|
2020-10-23 23:13:38 +02:00
|
|
|
, config ? { }
|
2020-06-02 16:27:07 +02:00
|
|
|
# !!! See comment about args in lib/modules.nix
|
2020-10-23 23:13:38 +02:00
|
|
|
, specialArgs ? { }
|
2019-09-06 09:25:22 +02:00
|
|
|
# Modules to add to each VM
|
2020-10-23 23:13:38 +02:00
|
|
|
, extraConfigurations ? [ ]
|
|
|
|
}:
|
2019-09-06 09:25:22 +02:00
|
|
|
|
|
|
|
with pkgs;
|
|
|
|
|
2022-06-15 16:59:21 +02:00
|
|
|
let
|
|
|
|
nixos-lib = import ./default.nix { inherit (pkgs) lib; };
|
|
|
|
in
|
|
|
|
|
2020-05-14 14:34:50 +02:00
|
|
|
rec {
|
2019-09-06 09:25:22 +02:00
|
|
|
|
|
|
|
inherit pkgs;
|
|
|
|
|
|
|
|
# Run an automated test suite in the given virtual network.
|
2022-01-02 23:48:56 +01:00
|
|
|
runTests = { driver, driverInteractive, pos }:
|
2019-09-06 09:25:22 +02:00
|
|
|
stdenv.mkDerivation {
|
|
|
|
name = "vm-test-run-${driver.testName}";
|
|
|
|
|
|
|
|
requiredSystemFeatures = [ "kvm" "nixos-test" ];
|
|
|
|
|
|
|
|
buildCommand =
|
|
|
|
''
|
2020-05-07 15:56:30 +02:00
|
|
|
mkdir -p $out
|
2019-09-06 09:25:22 +02:00
|
|
|
|
2021-06-06 19:00:12 +02:00
|
|
|
# effectively mute the XMLLogger
|
|
|
|
export LOGFILE=/dev/null
|
|
|
|
|
2022-01-26 22:26:19 +01:00
|
|
|
${driver}/bin/nixos-test-driver -o $out
|
2019-09-06 09:25:22 +02:00
|
|
|
'';
|
2020-12-09 12:59:39 +01:00
|
|
|
|
2021-05-08 17:24:47 +02:00
|
|
|
passthru = driver.passthru // {
|
2022-01-02 23:48:56 +01:00
|
|
|
inherit driver driverInteractive;
|
2021-05-08 17:24:47 +02:00
|
|
|
};
|
2021-02-09 12:02:35 +01:00
|
|
|
|
2021-06-06 18:36:07 +02:00
|
|
|
inherit pos; # for better debugging
|
2019-09-06 09:25:22 +02:00
|
|
|
};
|
|
|
|
|
2021-06-06 18:36:07 +02:00
|
|
|
# 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
|
2022-06-03 13:37:04 +02:00
|
|
|
, skipTypeCheck ? false
|
2021-06-06 18:36:07 +02:00
|
|
|
, passthru ? {}
|
2022-01-25 13:23:37 +01:00
|
|
|
, interactive ? false
|
2022-05-23 11:08:13 +02:00
|
|
|
, extraPythonPackages ? (_ :[])
|
2021-06-06 18:36:07 +02:00
|
|
|
}:
|
|
|
|
let
|
2021-12-06 13:49:23 +01:00
|
|
|
# Reifies and correctly wraps the python test driver for
|
|
|
|
# the respective qemu version and with or without ocr support
|
|
|
|
testDriver = pkgs.callPackage ./test-driver {
|
2022-05-23 11:08:13 +02:00
|
|
|
inherit enableOCR extraPythonPackages;
|
2021-12-06 13:49:23 +01:00
|
|
|
qemu_pkg = qemu_test;
|
|
|
|
imagemagick_light = imagemagick_light.override { inherit libtiff; };
|
|
|
|
tesseract4 = tesseract4.override { enableLanguages = [ "eng" ]; };
|
|
|
|
};
|
|
|
|
|
2021-06-06 18:36:07 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2021-11-19 23:02:30 +01:00
|
|
|
nodeHostNames = let
|
|
|
|
nodesList = map (c: c.config.system.name) (lib.attrValues nodes);
|
2022-06-02 09:58:46 +02:00
|
|
|
in nodesList ++ lib.optional (lib.length nodesList == 1 && !lib.elem "machine" nodesList) "machine";
|
2021-06-06 18:36:07 +02:00
|
|
|
|
2021-07-28 02:38:10 +02:00
|
|
|
# TODO: This is an implementation error and needs fixing
|
|
|
|
# the testing famework cannot legitimately restrict hostnames further
|
|
|
|
# beyond RFC1035
|
2021-06-06 18:36:07 +02:00
|
|
|
invalidNodeNames = lib.filter
|
|
|
|
(node: builtins.match "^[A-z_]([A-z0-9_]+)?$" node == null)
|
2021-07-28 02:38:10 +02:00
|
|
|
nodeHostNames;
|
2021-06-06 18:36:07 +02:00
|
|
|
|
|
|
|
testScript' =
|
|
|
|
# Call the test script with the computed nodes.
|
|
|
|
if lib.isFunction testScript
|
|
|
|
then testScript { inherit nodes; }
|
|
|
|
else testScript;
|
2019-09-06 09:25:22 +02:00
|
|
|
|
2022-06-02 09:58:46 +02:00
|
|
|
uniqueVlans = lib.unique (builtins.concatLists vlans);
|
|
|
|
vlanNames = map (i: "vlan${toString i}: VLan;") uniqueVlans;
|
|
|
|
machineNames = map (name: "${name}: Machine;") nodeHostNames;
|
2021-06-06 18:36:07 +02:00
|
|
|
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.
|
2021-07-28 02:38:10 +02:00
|
|
|
|
|
|
|
This is an IMPLEMENTATION ERROR and needs to be fixed. Meanwhile,
|
|
|
|
please stick to alphanumeric chars and underscores as separation.
|
2021-06-06 18:36:07 +02:00
|
|
|
''
|
|
|
|
else lib.warnIf skipLint "Linting is disabled" (runCommand testDriverName
|
|
|
|
{
|
|
|
|
inherit testName;
|
2022-06-02 09:58:46 +02:00
|
|
|
nativeBuildInputs = [ makeWrapper mypy ];
|
2022-06-17 12:22:57 +02:00
|
|
|
buildInputs = [ testDriver ];
|
2021-06-06 18:36:07 +02:00
|
|
|
testScript = testScript';
|
|
|
|
preferLocalBuild = true;
|
|
|
|
passthru = passthru // {
|
|
|
|
inherit nodes;
|
|
|
|
};
|
2022-05-05 12:33:57 +02:00
|
|
|
meta.mainProgram = "nixos-test-driver";
|
2021-06-06 18:36:07 +02:00
|
|
|
}
|
|
|
|
''
|
|
|
|
mkdir -p $out/bin
|
|
|
|
|
2021-06-06 19:00:12 +02:00
|
|
|
vmStartScripts=($(for i in ${toString vms}; do echo $i/bin/run-*-vm; done))
|
2022-06-02 09:58:46 +02:00
|
|
|
|
2022-06-03 13:37:04 +02:00
|
|
|
${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
|
|
|
|
''}
|
2022-06-02 09:58:46 +02:00
|
|
|
|
|
|
|
echo -n "$testScript" >> $out/test-script
|
|
|
|
|
2021-06-06 19:00:12 +02:00
|
|
|
ln -s ${testDriver}/bin/nixos-test-driver $out/bin/nixos-test-driver
|
|
|
|
|
2021-12-06 13:49:23 +01:00
|
|
|
${testDriver}/bin/generate-driver-symbols
|
2021-06-06 18:36:07 +02:00
|
|
|
${lib.optionalString (!skipLint) ''
|
|
|
|
PYFLAKES_BUILTINS="$(
|
|
|
|
echo -n ${lib.escapeShellArg (lib.concatStringsSep "," nodeHostNames)},
|
2021-12-06 13:49:23 +01:00
|
|
|
< ${lib.escapeShellArg "driver-symbols"}
|
2021-06-06 18:36:07 +02:00
|
|
|
)" ${python3Packages.pyflakes}/bin/pyflakes $out/test-script
|
|
|
|
''}
|
|
|
|
|
2021-06-06 19:00:12 +02:00
|
|
|
# set defaults through environment
|
|
|
|
# see: ./test-driver/test-driver.py argparse implementation
|
2021-06-06 18:36:07 +02:00
|
|
|
wrapProgram $out/bin/nixos-test-driver \
|
2021-06-06 19:00:12 +02:00
|
|
|
--set startScripts "''${vmStartScripts[*]}" \
|
|
|
|
--set testScript "$out/test-script" \
|
2022-01-25 13:23:37 +01:00
|
|
|
--set vlans '${toString vlans}' \
|
2022-01-25 13:43:56 +01:00
|
|
|
${lib.optionalString (interactive) "--add-flags --interactive"}
|
2021-06-06 18:36:07 +02:00
|
|
|
'');
|
|
|
|
|
2022-06-15 16:59:21 +02:00
|
|
|
evalTest = module: nixos-lib.evalTest { imports = [ extraTestModule module ]; };
|
|
|
|
runTest = module: nixos-lib.runTest { imports = [ extraTestModule module ]; };
|
|
|
|
|
|
|
|
extraTestModule = {
|
|
|
|
config = {
|
|
|
|
hostPkgs = pkgs;
|
|
|
|
minimalResult = hydra;
|
|
|
|
};
|
|
|
|
};
|
2022-06-06 13:29:04 +02:00
|
|
|
|
2021-06-06 18:36:07 +02:00
|
|
|
# Make a full-blown test
|
2019-09-06 09:25:22 +02:00
|
|
|
makeTest =
|
2022-03-18 01:20:21 +01:00
|
|
|
{ machine ? null
|
|
|
|
, nodes ? {}
|
|
|
|
, testScript
|
2019-09-06 09:25:22 +02:00
|
|
|
, enableOCR ? false
|
|
|
|
, name ? "unnamed"
|
2022-06-03 13:37:04 +02:00
|
|
|
, skipTypeCheck ? false
|
2020-10-23 23:13:38 +02:00
|
|
|
# Skip linting (mainly intended for faster dev cycles)
|
2019-12-22 14:50:08 +01:00
|
|
|
, skipLint ? false
|
2020-12-09 12:59:39 +01:00
|
|
|
, passthru ? {}
|
2022-03-18 01:20:21 +01:00
|
|
|
, meta ? {}
|
2021-02-09 12:02:35 +01:00
|
|
|
, # For meta.position
|
|
|
|
pos ? # position used in error messages and for meta.position
|
2022-03-18 01:20:21 +01:00
|
|
|
(if meta.description or null != null
|
|
|
|
then builtins.unsafeGetAttrPos "description" meta
|
2021-02-09 12:02:35 +01:00
|
|
|
else builtins.unsafeGetAttrPos "testScript" t)
|
2022-05-23 11:08:13 +02:00
|
|
|
, extraPythonPackages ? (_ : [])
|
2019-09-06 09:25:22 +02:00
|
|
|
} @ t:
|
|
|
|
let
|
2022-03-18 01:20:21 +01:00
|
|
|
mkNodes = qemu_pkg:
|
2020-10-23 23:13:38 +02:00
|
|
|
let
|
2021-10-06 18:47:56 +02:00
|
|
|
testScript' =
|
|
|
|
# Call the test script with the computed nodes.
|
|
|
|
if lib.isFunction testScript
|
2022-03-18 01:20:21 +01:00
|
|
|
then testScript { nodes = mkNodes qemu_pkg; }
|
2021-10-06 18:47:56 +02:00
|
|
|
else testScript;
|
|
|
|
|
2020-10-23 23:09:18 +02:00
|
|
|
build-vms = import ./build-vms.nix {
|
2021-06-23 17:46:46 +02:00
|
|
|
inherit system lib pkgs minimal specialArgs;
|
2021-06-06 18:36:07 +02:00
|
|
|
extraConfigurations = extraConfigurations ++ [(
|
2021-10-31 18:22:25 +01:00
|
|
|
{ config, ... }:
|
2020-10-23 23:09:18 +02:00
|
|
|
{
|
|
|
|
virtualisation.qemu.package = qemu_pkg;
|
2021-10-06 18:47:56 +02:00
|
|
|
|
|
|
|
# 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
|
2021-10-31 18:22:25 +01:00
|
|
|
# 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')
|
2021-10-06 18:47:56 +02:00
|
|
|
(pkgs.writeStringReferencesToFile testScript');
|
|
|
|
|
2020-11-07 13:04:50 +01:00
|
|
|
# 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;
|
|
|
|
}
|
|
|
|
)];
|
2020-10-23 23:09:18 +02:00
|
|
|
};
|
2020-10-05 15:52:27 +02:00
|
|
|
in
|
2022-05-27 15:00:25 +02:00
|
|
|
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'."
|
2021-06-06 18:36:07 +02:00
|
|
|
build-vms.buildVirtualNetwork (
|
2022-03-18 01:20:21 +01:00
|
|
|
nodes // lib.optionalAttrs (machine != null) { inherit machine; }
|
2021-06-06 18:36:07 +02:00
|
|
|
);
|
2019-09-06 09:25:22 +02:00
|
|
|
|
2021-06-06 18:36:07 +02:00
|
|
|
driver = setupDriverForTest {
|
2022-06-03 14:22:13 +02:00
|
|
|
inherit testScript enableOCR skipTypeCheck skipLint passthru extraPythonPackages;
|
2021-06-06 18:36:07 +02:00
|
|
|
testName = name;
|
|
|
|
qemu_pkg = pkgs.qemu_test;
|
2022-03-18 01:20:21 +01:00
|
|
|
nodes = mkNodes pkgs.qemu_test;
|
2021-06-06 18:36:07 +02:00
|
|
|
};
|
|
|
|
driverInteractive = setupDriverForTest {
|
2022-06-03 14:22:13 +02:00
|
|
|
inherit testScript enableOCR skipTypeCheck skipLint passthru extraPythonPackages;
|
2021-06-06 18:36:07 +02:00
|
|
|
testName = name;
|
|
|
|
qemu_pkg = pkgs.qemu;
|
2022-03-18 01:20:21 +01:00
|
|
|
nodes = mkNodes pkgs.qemu;
|
2022-01-25 13:23:37 +01:00
|
|
|
interactive = true;
|
2021-06-06 18:36:07 +02:00
|
|
|
};
|
2019-09-06 09:25:22 +02:00
|
|
|
|
2022-03-19 11:51:39 +01:00
|
|
|
test = lib.addMetaAttrs meta (runTests { inherit driver pos driverInteractive; });
|
2021-05-08 17:52:22 +02:00
|
|
|
|
2019-09-06 09:25:22 +02:00
|
|
|
in
|
2020-10-23 23:09:18 +02:00
|
|
|
test // {
|
2022-03-18 01:20:21 +01:00
|
|
|
inherit test driver driverInteractive;
|
|
|
|
inherit (driver) nodes;
|
2020-10-23 23:13:38 +02:00
|
|
|
};
|
2019-09-06 09:25:22 +02:00
|
|
|
|
2021-09-07 18:36:11 +02:00
|
|
|
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
|
|
|
|
'';
|
2019-09-06 09:25:22 +02:00
|
|
|
|
2021-09-07 18:36:11 +02:00
|
|
|
runInMachine = abortForFunction "runInMachine";
|
2019-09-06 09:25:22 +02:00
|
|
|
|
2021-09-07 18:36:11 +02:00
|
|
|
runInMachineWithX = abortForFunction "runInMachineWithX";
|
2019-09-06 09:25:22 +02:00
|
|
|
|
|
|
|
simpleTest = as: (makeTest as).test;
|
|
|
|
|
|
|
|
}
|