From 44546561fcae6fd5161c64709d4328c8e3d872fd Mon Sep 17 00:00:00 2001 From: Erik Arvstedt Date: Sat, 30 Jan 2021 10:47:01 +0100 Subject: [PATCH] run-tests: allow defining scenarios via cmdline args This simplifies running self-contained scenarios for testing and debugging. --- examples/README.md | 6 ++++++ test/run-tests.sh | 26 ++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/examples/README.md b/examples/README.md index 328c6aa..dba4e53 100644 --- a/examples/README.md +++ b/examples/README.md @@ -55,5 +55,11 @@ c systemctl status bitcoind # Explore a single feature ./run-tests.sh --scenario electrs container + +# Run a command in a container +./run-tests.sh --scenario '{ + services.clightning.enable = true; + nix-bitcoin.nodeinfo.enable = true; +}' container --run c nodeinfo ``` See [`run-tests.sh`](../test/run-tests.sh) for a complete documentation. diff --git a/test/run-tests.sh b/test/run-tests.sh index b4da8d2..2593a3f 100755 --- a/test/run-tests.sh +++ b/test/run-tests.sh @@ -10,7 +10,13 @@ # Test specific scenario # ./run-tests.sh --scenario|-s # -# When is undefined, the test is run with an adhoc scenario +# - When contains a space, is treated as nix code defining +# a scenario. It is evaluated in the same context as other scenarios in ./tests.nix +# +# Example: +# ./run-tests.sh -s "{ nix-bitcoin.nodeinfo.enable = true; }" container --run c nodeinfo +# +# - When does not name a scenario, the test is run with an adhoc scenario # where services. is enabled. # # Example: @@ -80,13 +86,24 @@ numCPUs=${numCPUs:-$(nproc)} # Min. 800 MiB needed to avoid 'out of memory' errors memoryMiB=${memoryMiB:-2048} -export NIX_PATH=nixpkgs=$(nix eval --raw -f "$scriptDir/../pkgs/nixpkgs-pinned.nix" nixpkgs) +export NIX_PATH=nixpkgs=$(nix eval --raw -f "$scriptDir/../pkgs/nixpkgs-pinned.nix" nixpkgs):nix-bitcoin=$(realpath "$scriptDir/..") + +runAtExit= +trap 'eval "$runAtExit"' EXIT + +# Support explicit scenario definitions +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" + scenario=tmp +fi # Run the test. No temporary files are left on the host system. run() { # TMPDIR is also used by the test driver for VM tmp files export TMPDIR=$(mktemp -d /tmp/nix-bitcoin-test.XXX) - trap "rm -rf $TMPDIR" EXIT + runAtExit+="rm -rf $TMPDIR;" nix-build --out-link $TMPDIR/driver -E "(import \"$scriptDir/tests.nix\" { scenario = \"$scenario\"; }).vm" -A driver @@ -134,7 +151,8 @@ instantiate() { } container() { - . "$scriptDir/lib/make-container.sh" "$@" + export scriptDir scenario + "$scriptDir/lib/make-container.sh" "$@" } doBuild() {