tests: improve make-test-vm.nix

1. fixedTest: Remove some unneeded layers of function calls.

2. Don't add a modified version of `black` to the global pkgs set.
   Tests should not affect the pkgs of the tested system modules.
   Fix the driver build script instead by adding an extra arg to the
   call to `black`.
This commit is contained in:
Erik Arvstedt 2021-03-22 13:19:47 +01:00
parent 08fe9ba84a
commit 9ca52af523
No known key found for this signature in database
GPG Key ID: 33312B944DD97846
3 changed files with 48 additions and 37 deletions

View File

@ -1,37 +1,45 @@
testArgs: pkgs:
let let
pkgs = import <nixpkgs> { config = {}; overlays = []; }; pythonTesting = import "${toString pkgs.path}/nixos/lib/testing-python.nix" {
system = builtins.currentSystem;
inherit pkgs;
};
in
test = (import "${pkgs.path}/nixos/tests/make-test-python.nix") (testArgs pkgs); args:
let
test = pythonTesting.makeTest args;
fixedTest = { ... }@args: fixedDriver = test.driver.overrideAttrs (old: let
let # Allow the test script to have longer lines by fixing the call to the 'black'
pkgsFixed = pkgs // { # code formatter.
# Fix the black Python code formatter that's used in the test to allow the test # The default width of 88 chars is too restrictive for our script.
# script to have longer lines. The default width of 88 chars is too restrictive for parts = builtins.split ''/nix/store/[^ ]+/black '' old.buildCommand;
# our script. preMatch = builtins.elemAt parts 0;
python3Packages = pkgs.python3Packages // { postMatch = builtins.elemAt parts 2;
black = pkgs.writeScriptBin "black" '' in {
fileToCheck=''${@:$#} # See `mkDriver` in nixpkgs/nixos/lib/testing-python.nix for the original definition of `buildCommand`
[[ $fileToCheck = *test-script ]] && extraArgs='--line-length 100' buildCommand = ''
exec ${pkgs.python3Packages.black}/bin/black $extraArgs "$@" ${preMatch}${pkgs.python3Packages.black}/bin/black --line-length 100 ${postMatch}
''; '';
}; # Keep reference to the `testDriver` derivation, required by `buildCommand`
}; testDriverReference = old.buildCommand;
test' = test (args // { pkgs = pkgsFixed; }); });
in
# See nixpkgs/nixos/lib/testing-python.nix for the original definition
test'.overrideAttrs (_: {
# 1. Save test output
# 2. Add link to driver so that a gcroot to a test prevents the driver from
# being garbage-collected
buildCommand = ''
mkdir $out
LOGFILE=$out/output.xml tests='exec(os.environ["testScript"])' ${test'.driver}/bin/nixos-test-driver
ln -s ${test'.driver} $out/driver
'';
}) // { inherit (test') nodes driver; } ;
# 1. Use fixed driver
# 2. Save test logging output
# 3. Add link to driver so that a gcroot to a test prevents the driver from
# being garbage-collected
fixedTest = test.overrideAttrs (_: {
# See `runTests` in nixpkgs/nixos/lib/testing-python.nix for the original definition of `buildCommand`
buildCommand = ''
mkdir $out
LOGFILE=$out/output.xml tests='exec(os.environ["testScript"])' ${fixedDriver}/bin/nixos-test-driver
ln -s ${fixedDriver} $out/driver
'';
}) // {
driver = fixedDriver;
inherit (test) nodes;
};
in in
fixedTest fixedTest

View File

@ -1,8 +1,11 @@
scenario: testConfig: let
pkgs = import <nixpkgs> { config = {}; overlays = []; };
makeVM = import ./make-test-vm.nix pkgs;
in
name: testConfig:
{ {
vm = import ./make-test-vm.nix (pkgs: { vm = makeVM {
name = "nix-bitcoin-${scenario}"; name = "nix-bitcoin-${name}";
machine = { machine = {
imports = [ testConfig ]; imports = [ testConfig ];
@ -37,7 +40,7 @@ scenario: testConfig:
run_tests() run_tests()
'' ''
]; ];
}); };
container = { container = {
# The container name has a 11 char length limit # The container name has a 11 char length limit

View File

@ -212,7 +212,7 @@ vmTestNixExpr() {
fi fi
cat <<EOF cat <<EOF
((import "$scriptDir/tests.nix" { scenario = "$scenario"; }).vm {}).overrideAttrs (old: rec { (import "$scriptDir/tests.nix" { scenario = "$scenario"; }).vm.overrideAttrs (old: rec {
buildCommand = '' buildCommand = ''
export QEMU_OPTS="-smp $numCPUs -m $memoryMiB $extraQEMUOpts" export QEMU_OPTS="-smp $numCPUs -m $memoryMiB $extraQEMUOpts"
echo "VM stats: CPUs: $numCPUs, memory: $memoryMiB MiB" echo "VM stats: CPUs: $numCPUs, memory: $memoryMiB MiB"