run-tests: allow defining scenarios via cmdline args

This simplifies running self-contained scenarios for testing and debugging.
This commit is contained in:
Erik Arvstedt 2021-01-30 10:47:01 +01:00
parent fc40776689
commit 44546561fc
No known key found for this signature in database
GPG Key ID: 33312B944DD97846
2 changed files with 28 additions and 4 deletions

View File

@ -55,5 +55,11 @@ c systemctl status bitcoind
# Explore a single feature # Explore a single feature
./run-tests.sh --scenario electrs container ./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. See [`run-tests.sh`](../test/run-tests.sh) for a complete documentation.

View File

@ -10,7 +10,13 @@
# Test specific scenario # Test specific scenario
# ./run-tests.sh --scenario|-s <scenario> # ./run-tests.sh --scenario|-s <scenario>
# #
# When <scenario> is undefined, the test is run with an adhoc scenario # - When <scenario> contains a space, <scenario> 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 <scenario> does not name a scenario, the test is run with an adhoc scenario
# where services.<scenario> is enabled. # where services.<scenario> is enabled.
# #
# Example: # Example:
@ -80,13 +86,24 @@ numCPUs=${numCPUs:-$(nproc)}
# Min. 800 MiB needed to avoid 'out of memory' errors # Min. 800 MiB needed to avoid 'out of memory' errors
memoryMiB=${memoryMiB:-2048} 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 the test. No temporary files are left on the host system.
run() { run() {
# TMPDIR is also used by the test driver for VM tmp files # TMPDIR is also used by the test driver for VM tmp files
export TMPDIR=$(mktemp -d /tmp/nix-bitcoin-test.XXX) 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 nix-build --out-link $TMPDIR/driver -E "(import \"$scriptDir/tests.nix\" { scenario = \"$scenario\"; }).vm" -A driver
@ -134,7 +151,8 @@ instantiate() {
} }
container() { container() {
. "$scriptDir/lib/make-container.sh" "$@" export scriptDir scenario
"$scriptDir/lib/make-container.sh" "$@"
} }
doBuild() { doBuild() {