From 9ca52af523eba8e58f38bf5ae02b515fe17fae82 Mon Sep 17 00:00:00 2001 From: Erik Arvstedt Date: Mon, 22 Mar 2021 13:19:47 +0100 Subject: [PATCH] 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`. --- test/lib/make-test-vm.nix | 70 ++++++++++++++++++++++----------------- test/lib/make-test.nix | 13 +++++--- test/run-tests.sh | 2 +- 3 files changed, 48 insertions(+), 37 deletions(-) diff --git a/test/lib/make-test-vm.nix b/test/lib/make-test-vm.nix index 8f9928b..7505199 100644 --- a/test/lib/make-test-vm.nix +++ b/test/lib/make-test-vm.nix @@ -1,37 +1,45 @@ -testArgs: - +pkgs: let - pkgs = import { 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: - let - pkgsFixed = pkgs // { - # Fix the black Python code formatter that's used in the test to allow the test - # script to have longer lines. The default width of 88 chars is too restrictive for - # our script. - python3Packages = pkgs.python3Packages // { - black = pkgs.writeScriptBin "black" '' - fileToCheck=''${@:$#} - [[ $fileToCheck = *test-script ]] && extraArgs='--line-length 100' - exec ${pkgs.python3Packages.black}/bin/black $extraArgs "$@" - ''; - }; - }; - 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; } ; + fixedDriver = test.driver.overrideAttrs (old: let + # Allow the test script to have longer lines by fixing the call to the 'black' + # code formatter. + # The default width of 88 chars is too restrictive for our script. + parts = builtins.split ''/nix/store/[^ ]+/black '' old.buildCommand; + preMatch = builtins.elemAt parts 0; + postMatch = builtins.elemAt parts 2; + in { + # See `mkDriver` in nixpkgs/nixos/lib/testing-python.nix for the original definition of `buildCommand` + buildCommand = '' + ${preMatch}${pkgs.python3Packages.black}/bin/black --line-length 100 ${postMatch} + ''; + # Keep reference to the `testDriver` derivation, required by `buildCommand` + testDriverReference = old.buildCommand; + }); + # 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 fixedTest diff --git a/test/lib/make-test.nix b/test/lib/make-test.nix index c3620fe..23d71d5 100644 --- a/test/lib/make-test.nix +++ b/test/lib/make-test.nix @@ -1,8 +1,11 @@ -scenario: testConfig: - +let + pkgs = import { config = {}; overlays = []; }; + makeVM = import ./make-test-vm.nix pkgs; +in +name: testConfig: { - vm = import ./make-test-vm.nix (pkgs: { - name = "nix-bitcoin-${scenario}"; + vm = makeVM { + name = "nix-bitcoin-${name}"; machine = { imports = [ testConfig ]; @@ -37,7 +40,7 @@ scenario: testConfig: run_tests() '' ]; - }); + }; container = { # The container name has a 11 char length limit diff --git a/test/run-tests.sh b/test/run-tests.sh index 7bfceb7..25a13df 100755 --- a/test/run-tests.sh +++ b/test/run-tests.sh @@ -212,7 +212,7 @@ vmTestNixExpr() { fi cat <