2019-09-06 07:25:22 +00:00
|
|
|
{ system
|
|
|
|
, pkgs ? import ../.. { inherit system config; }
|
|
|
|
# Use a minimal kernel?
|
|
|
|
, minimal ? false
|
|
|
|
# Ignored
|
2020-10-23 21:13:38 +00:00
|
|
|
, config ? { }
|
2020-06-02 14:27:07 +00:00
|
|
|
# !!! See comment about args in lib/modules.nix
|
2020-10-23 21:13:38 +00:00
|
|
|
, specialArgs ? { }
|
2019-09-06 07:25:22 +00:00
|
|
|
# Modules to add to each VM
|
2020-10-23 21:13:38 +00:00
|
|
|
, extraConfigurations ? [ ]
|
|
|
|
}:
|
2019-09-06 07:25:22 +00:00
|
|
|
|
|
|
|
with pkgs;
|
|
|
|
|
2020-05-14 12:34:50 +00:00
|
|
|
rec {
|
2019-09-06 07:25:22 +00:00
|
|
|
|
|
|
|
inherit pkgs;
|
|
|
|
|
2021-06-06 16:36:07 +00:00
|
|
|
# Reifies and correctly wraps the python test driver for
|
|
|
|
# the respective qemu version and with or without ocr support
|
|
|
|
pythonTestDriver = {
|
|
|
|
qemu_pkg ? pkgs.qemu_test
|
|
|
|
, enableOCR ? false
|
|
|
|
}:
|
2020-10-23 21:13:38 +00:00
|
|
|
let
|
|
|
|
name = "nixos-test-driver";
|
2021-06-06 16:36:07 +00:00
|
|
|
testDriverScript = ./test-driver/test-driver.py;
|
|
|
|
ocrProg = tesseract4.override { enableLanguages = [ "eng" ]; };
|
|
|
|
imagemagick_tiff = imagemagick_light.override { inherit libtiff; };
|
|
|
|
in stdenv.mkDerivation {
|
|
|
|
inherit name;
|
2019-09-06 07:25:22 +00:00
|
|
|
|
2020-10-23 21:13:38 +00:00
|
|
|
nativeBuildInputs = [ makeWrapper ];
|
2021-05-14 23:57:11 +00:00
|
|
|
buildInputs = [ (python3.withPackages (p: [ p.ptpython p.colorama ])) ];
|
2020-10-23 21:13:38 +00:00
|
|
|
checkInputs = with python3Packages; [ pylint black mypy ];
|
2019-09-06 07:25:22 +00:00
|
|
|
|
2020-10-23 21:13:38 +00:00
|
|
|
dontUnpack = true;
|
2019-09-06 07:25:22 +00:00
|
|
|
|
2020-10-23 21:13:38 +00:00
|
|
|
preferLocalBuild = true;
|
2019-09-06 07:25:22 +00:00
|
|
|
|
2021-05-08 15:52:22 +00:00
|
|
|
buildPhase = ''
|
|
|
|
python <<EOF
|
|
|
|
from pydoc import importfile
|
2021-06-06 16:36:07 +00:00
|
|
|
with open('driver-symbols', 'w') as fp:
|
2021-05-08 15:52:22 +00:00
|
|
|
fp.write(','.join(dir(importfile('${testDriverScript}'))))
|
|
|
|
EOF
|
|
|
|
'';
|
|
|
|
|
2020-10-23 21:13:38 +00:00
|
|
|
doCheck = true;
|
|
|
|
checkPhase = ''
|
|
|
|
mypy --disallow-untyped-defs \
|
|
|
|
--no-implicit-optional \
|
|
|
|
--ignore-missing-imports ${testDriverScript}
|
|
|
|
pylint --errors-only ${testDriverScript}
|
|
|
|
black --check --diff ${testDriverScript}
|
|
|
|
'';
|
2019-09-06 07:25:22 +00:00
|
|
|
|
2020-10-23 21:13:38 +00:00
|
|
|
installPhase =
|
|
|
|
''
|
|
|
|
mkdir -p $out/bin
|
|
|
|
cp ${testDriverScript} $out/bin/nixos-test-driver
|
|
|
|
chmod u+x $out/bin/nixos-test-driver
|
|
|
|
# TODO: copy user script part into this file (append)
|
2019-09-06 07:25:22 +00:00
|
|
|
|
2020-10-23 21:13:38 +00:00
|
|
|
wrapProgram $out/bin/nixos-test-driver \
|
2021-06-06 16:36:07 +00:00
|
|
|
--argv0 ${name} \
|
2021-06-22 06:51:39 +00:00
|
|
|
--prefix PATH : "${lib.makeBinPath [ qemu_pkg vde2 netpbm coreutils socat ]}" \
|
2021-06-06 16:36:07 +00:00
|
|
|
${lib.optionalString enableOCR
|
|
|
|
"--prefix PATH : '${ocrProg}/bin:${imagemagick_tiff}/bin'"} \
|
2021-05-08 15:52:22 +00:00
|
|
|
|
2021-06-06 16:36:07 +00:00
|
|
|
install -m 0644 -vD driver-symbols $out/nix-support/driver-symbols
|
2020-10-23 21:13:38 +00:00
|
|
|
'';
|
|
|
|
};
|
2019-09-06 07:25:22 +00:00
|
|
|
|
|
|
|
# Run an automated test suite in the given virtual network.
|
2021-06-06 16:36:07 +00:00
|
|
|
runTests = { driver, pos }:
|
2019-09-06 07:25:22 +00:00
|
|
|
stdenv.mkDerivation {
|
|
|
|
name = "vm-test-run-${driver.testName}";
|
|
|
|
|
|
|
|
requiredSystemFeatures = [ "kvm" "nixos-test" ];
|
|
|
|
|
|
|
|
buildCommand =
|
|
|
|
''
|
2020-05-07 13:56:30 +00:00
|
|
|
mkdir -p $out
|
2019-09-06 07:25:22 +00:00
|
|
|
|
2020-08-30 22:42:06 +00:00
|
|
|
LOGFILE=/dev/null tests='exec(os.environ["testScript"])' ${driver}/bin/nixos-test-driver
|
2019-09-06 07:25:22 +00:00
|
|
|
'';
|
2020-12-09 11:59:39 +00:00
|
|
|
|
2021-05-08 15:24:47 +00:00
|
|
|
passthru = driver.passthru // {
|
|
|
|
inherit driver;
|
|
|
|
};
|
2021-02-09 11:02:35 +00:00
|
|
|
|
2021-06-06 16:36:07 +00:00
|
|
|
inherit pos; # for better debugging
|
2019-09-06 07:25:22 +00:00
|
|
|
};
|
|
|
|
|
2021-06-06 16:36:07 +00: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
|
|
|
|
, passthru ? {}
|
|
|
|
}:
|
|
|
|
let
|
|
|
|
# FIXME: get this pkg from the module system
|
|
|
|
testDriver = pythonTestDriver { inherit qemu_pkg enableOCR;};
|
|
|
|
|
|
|
|
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 = map (c: c.config.system.name) (lib.attrValues nodes);
|
|
|
|
|
2021-07-28 00:38:10 +00:00
|
|
|
# TODO: This is an implementation error and needs fixing
|
|
|
|
# the testing famework cannot legitimately restrict hostnames further
|
|
|
|
# beyond RFC1035
|
2021-06-06 16:36:07 +00:00
|
|
|
invalidNodeNames = lib.filter
|
|
|
|
(node: builtins.match "^[A-z_]([A-z0-9_]+)?$" node == null)
|
2021-07-28 00:38:10 +00:00
|
|
|
nodeHostNames;
|
2021-06-06 16:36:07 +00:00
|
|
|
|
|
|
|
testScript' =
|
|
|
|
# Call the test script with the computed nodes.
|
|
|
|
if lib.isFunction testScript
|
|
|
|
then testScript { inherit nodes; }
|
|
|
|
else testScript;
|
2019-09-06 07:25:22 +00:00
|
|
|
|
2021-06-06 16:36:07 +00: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 00:38:10 +00:00
|
|
|
|
|
|
|
This is an IMPLEMENTATION ERROR and needs to be fixed. Meanwhile,
|
|
|
|
please stick to alphanumeric chars and underscores as separation.
|
2021-06-06 16:36:07 +00:00
|
|
|
''
|
|
|
|
else lib.warnIf skipLint "Linting is disabled" (runCommand testDriverName
|
|
|
|
{
|
|
|
|
inherit testName;
|
|
|
|
nativeBuildInputs = [ makeWrapper ];
|
|
|
|
testScript = testScript';
|
|
|
|
preferLocalBuild = true;
|
|
|
|
passthru = passthru // {
|
|
|
|
inherit nodes;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
''
|
|
|
|
mkdir -p $out/bin
|
|
|
|
|
|
|
|
echo -n "$testScript" > $out/test-script
|
|
|
|
${lib.optionalString (!skipLint) ''
|
|
|
|
PYFLAKES_BUILTINS="$(
|
|
|
|
echo -n ${lib.escapeShellArg (lib.concatStringsSep "," nodeHostNames)},
|
|
|
|
< ${lib.escapeShellArg "${testDriver}/nix-support/driver-symbols"}
|
|
|
|
)" ${python3Packages.pyflakes}/bin/pyflakes $out/test-script
|
|
|
|
''}
|
|
|
|
|
|
|
|
ln -s ${testDriver}/bin/nixos-test-driver $out/bin/
|
|
|
|
vms=($(for i in ${toString vms}; do echo $i/bin/run-*-vm; done))
|
|
|
|
wrapProgram $out/bin/nixos-test-driver \
|
|
|
|
--add-flags "''${vms[*]}" \
|
|
|
|
--run "export testScript=\"\$(${coreutils}/bin/cat $out/test-script)\"" \
|
|
|
|
--set VLANS '${toString vlans}'
|
|
|
|
ln -s ${testDriver}/bin/nixos-test-driver $out/bin/nixos-run-vms
|
|
|
|
wrapProgram $out/bin/nixos-run-vms \
|
|
|
|
--add-flags "''${vms[*]}" \
|
|
|
|
--set tests 'start_all(); join_all();' \
|
|
|
|
--set VLANS '${toString vlans}'
|
|
|
|
'');
|
|
|
|
|
|
|
|
# Make a full-blown test
|
2019-09-06 07:25:22 +00:00
|
|
|
makeTest =
|
|
|
|
{ testScript
|
|
|
|
, enableOCR ? false
|
|
|
|
, name ? "unnamed"
|
2020-10-23 21:13:38 +00:00
|
|
|
# Skip linting (mainly intended for faster dev cycles)
|
2019-12-22 13:50:08 +00:00
|
|
|
, skipLint ? false
|
2020-12-09 11:59:39 +00:00
|
|
|
, passthru ? {}
|
2021-02-09 11:02:35 +00:00
|
|
|
, # For meta.position
|
|
|
|
pos ? # position used in error messages and for meta.position
|
|
|
|
(if t.meta.description or null != null
|
|
|
|
then builtins.unsafeGetAttrPos "description" t.meta
|
|
|
|
else builtins.unsafeGetAttrPos "testScript" t)
|
2019-09-06 07:25:22 +00:00
|
|
|
, ...
|
|
|
|
} @ t:
|
|
|
|
let
|
2021-06-06 16:36:07 +00:00
|
|
|
nodes = qemu_pkg:
|
2020-10-23 21:13:38 +00:00
|
|
|
let
|
2020-10-23 21:09:18 +00:00
|
|
|
build-vms = import ./build-vms.nix {
|
|
|
|
inherit system pkgs minimal specialArgs;
|
2021-06-06 16:36:07 +00:00
|
|
|
extraConfigurations = extraConfigurations ++ [(
|
2020-10-23 21:09:18 +00:00
|
|
|
{
|
|
|
|
virtualisation.qemu.package = qemu_pkg;
|
2020-11-07 12:04:50 +00: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 21:09:18 +00:00
|
|
|
};
|
2020-10-05 13:52:27 +00:00
|
|
|
in
|
2021-06-06 16:36:07 +00:00
|
|
|
build-vms.buildVirtualNetwork (
|
|
|
|
t.nodes or (if t ? machine then { machine = t.machine; } else { })
|
|
|
|
);
|
2019-09-06 07:25:22 +00:00
|
|
|
|
2021-06-06 16:36:07 +00:00
|
|
|
driver = setupDriverForTest {
|
2021-06-18 12:16:27 +00:00
|
|
|
inherit testScript enableOCR skipLint passthru;
|
2021-06-06 16:36:07 +00:00
|
|
|
testName = name;
|
|
|
|
qemu_pkg = pkgs.qemu_test;
|
|
|
|
nodes = nodes pkgs.qemu_test;
|
|
|
|
};
|
|
|
|
driverInteractive = setupDriverForTest {
|
2021-06-18 12:16:27 +00:00
|
|
|
inherit testScript enableOCR skipLint passthru;
|
2021-06-06 16:36:07 +00:00
|
|
|
testName = name;
|
|
|
|
qemu_pkg = pkgs.qemu;
|
|
|
|
nodes = nodes pkgs.qemu;
|
|
|
|
};
|
2019-09-06 07:25:22 +00:00
|
|
|
|
2021-06-06 16:36:07 +00:00
|
|
|
test =
|
|
|
|
let
|
|
|
|
passMeta = drv: drv // lib.optionalAttrs (t ? meta) {
|
|
|
|
meta = (drv.meta or { }) // t.meta;
|
|
|
|
};
|
|
|
|
in passMeta (runTests { inherit driver pos; });
|
2021-05-08 15:52:22 +00:00
|
|
|
|
2019-09-06 07:25:22 +00:00
|
|
|
in
|
2020-10-23 21:09:18 +00:00
|
|
|
test // {
|
2021-06-06 16:36:07 +00:00
|
|
|
inherit test driver driverInteractive nodes;
|
2020-10-23 21:13:38 +00:00
|
|
|
};
|
2019-09-06 07:25:22 +00:00
|
|
|
|
|
|
|
runInMachine =
|
|
|
|
{ drv
|
|
|
|
, machine
|
|
|
|
, preBuild ? ""
|
|
|
|
, postBuild ? ""
|
2021-06-06 16:36:07 +00:00
|
|
|
, qemu_pkg ? pkgs.qemu_test
|
2019-09-06 07:25:22 +00:00
|
|
|
, ... # ???
|
|
|
|
}:
|
|
|
|
let
|
2020-10-23 21:09:18 +00:00
|
|
|
build-vms = import ./build-vms.nix {
|
|
|
|
inherit system pkgs minimal specialArgs extraConfigurations;
|
|
|
|
};
|
|
|
|
|
|
|
|
vm = build-vms.buildVM { }
|
2020-10-23 21:13:38 +00:00
|
|
|
[
|
|
|
|
machine
|
|
|
|
{
|
|
|
|
key = "run-in-machine";
|
2019-09-06 07:25:22 +00:00
|
|
|
networking.hostName = "client";
|
|
|
|
nix.readOnlyStore = false;
|
|
|
|
virtualisation.writableStore = false;
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
buildrunner = writeText "vm-build" ''
|
|
|
|
source $1
|
|
|
|
|
|
|
|
${coreutils}/bin/mkdir -p $TMPDIR
|
|
|
|
cd $TMPDIR
|
|
|
|
|
|
|
|
exec $origBuilder $origArgs
|
|
|
|
'';
|
|
|
|
|
|
|
|
testScript = ''
|
2020-02-09 21:21:52 +00:00
|
|
|
start_all()
|
|
|
|
client.wait_for_unit("multi-user.target")
|
2019-09-06 07:25:22 +00:00
|
|
|
${preBuild}
|
2020-02-09 21:21:52 +00:00
|
|
|
client.succeed("env -i ${bash}/bin/bash ${buildrunner} /tmp/xchg/saved-env >&2")
|
2019-09-06 07:25:22 +00:00
|
|
|
${postBuild}
|
2020-02-09 21:21:52 +00:00
|
|
|
client.succeed("sync") # flush all data before pulling the plug
|
2019-09-06 07:25:22 +00:00
|
|
|
'';
|
|
|
|
|
2021-06-06 16:36:07 +00:00
|
|
|
testDriver = pythonTestDriver { inherit qemu_pkg; };
|
|
|
|
|
2019-09-06 07:25:22 +00:00
|
|
|
vmRunCommand = writeText "vm-run" ''
|
|
|
|
xchg=vm-state-client/xchg
|
|
|
|
${coreutils}/bin/mkdir $out
|
|
|
|
${coreutils}/bin/mkdir -p $xchg
|
|
|
|
|
|
|
|
for i in $passAsFile; do
|
|
|
|
i2=''${i}Path
|
|
|
|
_basename=$(${coreutils}/bin/basename ''${!i2})
|
|
|
|
${coreutils}/bin/cp ''${!i2} $xchg/$_basename
|
|
|
|
eval $i2=/tmp/xchg/$_basename
|
|
|
|
${coreutils}/bin/ls -la $xchg
|
|
|
|
done
|
|
|
|
|
|
|
|
unset i i2 _basename
|
|
|
|
export | ${gnugrep}/bin/grep -v '^xchg=' > $xchg/saved-env
|
|
|
|
unset xchg
|
|
|
|
|
|
|
|
export tests='${testScript}'
|
2021-06-06 16:36:07 +00:00
|
|
|
${testDriver}/bin/nixos-test-driver --keep-vm-state ${vm.config.system.build.vm}/bin/run-*-vm
|
2019-09-06 07:25:22 +00:00
|
|
|
''; # */
|
|
|
|
|
|
|
|
in
|
2020-10-23 21:13:38 +00:00
|
|
|
lib.overrideDerivation drv (attrs: {
|
|
|
|
requiredSystemFeatures = [ "kvm" ];
|
|
|
|
builder = "${bash}/bin/sh";
|
|
|
|
args = [ "-e" vmRunCommand ];
|
|
|
|
origArgs = attrs.args;
|
|
|
|
origBuilder = attrs.builder;
|
|
|
|
});
|
2019-09-06 07:25:22 +00:00
|
|
|
|
|
|
|
|
2020-10-23 21:13:38 +00:00
|
|
|
runInMachineWithX = { require ? [ ], ... } @ args:
|
2019-09-06 07:25:22 +00:00
|
|
|
let
|
|
|
|
client =
|
|
|
|
{ ... }:
|
|
|
|
{
|
|
|
|
inherit require;
|
2020-02-09 21:23:35 +00:00
|
|
|
imports = [
|
|
|
|
../tests/common/auto.nix
|
|
|
|
];
|
2019-09-06 07:25:22 +00:00
|
|
|
virtualisation.memorySize = 1024;
|
|
|
|
services.xserver.enable = true;
|
2020-02-09 21:23:35 +00:00
|
|
|
test-support.displayManager.auto.enable = true;
|
2019-12-10 14:10:30 +00:00
|
|
|
services.xserver.displayManager.defaultSession = "none+icewm";
|
2019-09-06 07:25:22 +00:00
|
|
|
services.xserver.windowManager.icewm.enable = true;
|
|
|
|
};
|
|
|
|
in
|
2020-10-23 21:13:38 +00:00
|
|
|
runInMachine ({
|
|
|
|
machine = client;
|
|
|
|
preBuild =
|
|
|
|
''
|
|
|
|
client.wait_for_x()
|
|
|
|
'';
|
|
|
|
} // args);
|
2019-09-06 07:25:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
simpleTest = as: (makeTest as).test;
|
|
|
|
|
|
|
|
}
|