From 44439e2a81d9366708bee92f1cf15ad22a5da0b6 Mon Sep 17 00:00:00 2001 From: Erik Arvstedt Date: Mon, 22 Mar 2021 13:19:48 +0100 Subject: [PATCH] tests: optimize building multiple tests at once The result of `import tests.nix {}` is now an attrset of tests. This makes it easier and more efficient to evaluate or build multiple tests in one call to `nix build`. Simplify tests.nix by removing the large module args scope in favor of self-contained scenario module definitions. Add CPU core and memory size defaults to the test configuration to enable building tests without `run-tests.sh`. Add the following top-level args to tests.nix: - `extraScenarios` to provide a nix-level way to define extra scenarios. - `pkgs` to allow building tests with custom pkgs or systems. --- test/lib/make-container.sh | 2 +- test/lib/make-test-vm.nix | 2 +- test/lib/make-test.nix | 15 +++++++-- test/run-tests.sh | 6 ++-- test/tests.nix | 66 ++++++++++++++++++++++++-------------- 5 files changed, 59 insertions(+), 32 deletions(-) diff --git a/test/lib/make-container.sh b/test/lib/make-container.sh index e039dc1..af19185 100755 --- a/test/lib/make-container.sh +++ b/test/lib/make-container.sh @@ -84,6 +84,6 @@ if [[ ! ($containerBin && $(realpath $containerBin) == *extra-container-0.6*) ]] fi read -d '' src < { config = {}; overlays = []; }; makeVM = import ./make-test-vm.nix pkgs; + inherit (pkgs) lib; in + name: testConfig: { vm = makeVM { @@ -9,8 +11,15 @@ name: testConfig: machine = { imports = [ testConfig ]; - # Needed because duplicity requires 270 MB of free temp space, regardless of backup size - virtualisation.diskSize = 1024; + virtualisation = { + # Needed because duplicity requires 270 MB of free temp space, regardless of backup size + diskSize = 1024; + + # Min. 800 MiB needed to avoid 'out of memory' errors + memorySize = lib.mkDefault 2048; + + cores = lib.mkDefault 2; + }; }; testScript = nodes: let diff --git a/test/run-tests.sh b/test/run-tests.sh index 25a13df..f07c835 100755 --- a/test/run-tests.sh +++ b/test/run-tests.sh @@ -110,7 +110,7 @@ trap 'eval "$runAtExit"' EXIT if [[ $scenario = *' '* ]]; then export scenarioOverridesFile=$(mktemp ${XDG_RUNTIME_DIR:-/tmp}/nb-scenario.XXX) runAtExit+='rm -f "$scenarioOverridesFile";' - echo "{ testEnv, config, pkgs, lib }: with testEnv; with lib; { tmp = $scenario; }" > "$scenarioOverridesFile" + echo "{ scenarios, pkgs, lib }: with lib; { tmp = $scenario; }" > "$scenarioOverridesFile" scenario=tmp fi @@ -120,7 +120,7 @@ run() { export TMPDIR=$(mktemp -d /tmp/nix-bitcoin-test.XXX) runAtExit+="rm -rf $TMPDIR;" - nix-build --out-link $TMPDIR/driver -E "(import \"$scriptDir/tests.nix\" { scenario = \"$scenario\"; }).vm" -A driver + nix-build --out-link $TMPDIR/driver -E "((import \"$scriptDir/tests.nix\" {}).getTest \"$scenario\").vm" -A driver # Variable 'tests' contains the Python code that is executed by the driver on startup if [[ $1 == --interactive ]]; then @@ -212,7 +212,7 @@ vmTestNixExpr() { fi cat <