2020-01-12 11:52:40 -08:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
# Modules integration test runner.
|
2020-09-27 03:43:12 -07:00
|
|
|
# The tests (./tests.nix) use the NixOS testing framework and are executed in a VM.
|
2020-01-12 11:52:40 -08:00
|
|
|
#
|
|
|
|
# Usage:
|
2020-08-21 13:36:13 -07:00
|
|
|
# Run all tests
|
|
|
|
# ./run-tests.sh
|
|
|
|
#
|
|
|
|
# Test specific scenario
|
2020-09-27 03:43:20 -07:00
|
|
|
# ./run-tests.sh --scenario|-s <scenario>
|
|
|
|
#
|
|
|
|
# When <scenario> is undefined, the test is run with an adhoc scenario
|
|
|
|
# where services.<scenario> is enabled.
|
2020-01-12 11:52:40 -08:00
|
|
|
#
|
2020-09-27 03:43:32 -07:00
|
|
|
# Example:
|
|
|
|
# ./run-tests.sh -s electrs
|
|
|
|
#
|
2020-09-28 04:09:09 -07:00
|
|
|
# Run test(s) and link results to avoid garbage collection
|
|
|
|
# ./run-tests.sh [--scenario <scenario>] --out-link-prefix /tmp/nix-bitcoin-test
|
2020-01-12 11:52:40 -08:00
|
|
|
#
|
2020-08-21 13:36:15 -07:00
|
|
|
# Pass extra args to nix-build
|
|
|
|
# ./run-tests.sh build --builders 'ssh://mybuildhost - - 15'
|
|
|
|
#
|
2020-01-12 11:52:40 -08:00
|
|
|
# Run interactive test debugging
|
2020-08-21 13:36:13 -07:00
|
|
|
# ./run-tests.sh [--scenario <scenario>] debug
|
2020-01-12 11:52:40 -08:00
|
|
|
#
|
2020-09-27 03:43:20 -07:00
|
|
|
# This starts the testing VM and drops you into a Python REPL where you can
|
|
|
|
# manually execute the tests from ./tests.py
|
|
|
|
#
|
2020-09-27 03:43:28 -07:00
|
|
|
# Run a test scenario in a container
|
|
|
|
# sudo ./run-tests.sh [--scenario <scenario>] container
|
|
|
|
#
|
|
|
|
# This is useful for quick experiments; containers start much faster than VMs.
|
|
|
|
# Running the Python test suite in containers is not yet supported.
|
|
|
|
# For now, creating NixOS containers requires root permissions.
|
|
|
|
# See ./lib/make-container.sh for a complete documentation.
|
|
|
|
#
|
2020-09-27 03:43:20 -07:00
|
|
|
# To add custom scenarios, set the environment variable `scenarioOverridesFile`.
|
2020-01-12 11:52:40 -08:00
|
|
|
|
|
|
|
set -eo pipefail
|
|
|
|
|
2020-07-16 07:48:41 -07:00
|
|
|
scenario=
|
2020-08-21 13:36:14 -07:00
|
|
|
outLinkPrefix=
|
|
|
|
while :; do
|
|
|
|
case $1 in
|
|
|
|
--scenario|-s)
|
|
|
|
if [[ $2 ]]; then
|
|
|
|
scenario=$2
|
|
|
|
shift
|
|
|
|
shift
|
|
|
|
else
|
2020-09-27 03:43:25 -07:00
|
|
|
>&2 echo "Error: $1 requires an argument."
|
2020-08-21 13:36:14 -07:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
--out-link-prefix|-o)
|
|
|
|
if [[ $2 ]]; then
|
|
|
|
outLinkPrefix=$2
|
|
|
|
shift
|
|
|
|
shift
|
|
|
|
else
|
2020-09-27 03:43:25 -07:00
|
|
|
>&2 echo "Error: $1 requires an argument."
|
2020-08-21 13:36:14 -07:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
break
|
|
|
|
esac
|
|
|
|
done
|
2020-07-16 07:48:41 -07:00
|
|
|
|
2020-01-12 11:52:40 -08:00
|
|
|
numCPUs=${numCPUs:-$(nproc)}
|
|
|
|
# Min. 800 MiB needed to avoid 'out of memory' errors
|
|
|
|
memoryMiB=${memoryMiB:-2048}
|
|
|
|
|
2020-09-27 03:43:26 -07:00
|
|
|
testDir=$(cd "${BASH_SOURCE[0]%/*}" && pwd)
|
2020-01-12 11:52:40 -08:00
|
|
|
|
2020-09-27 03:43:26 -07:00
|
|
|
export NIX_PATH=nixpkgs=$(nix eval --raw -f "$testDir/../pkgs/nixpkgs-pinned.nix" nixpkgs)
|
2020-01-12 11:52:40 -08:00
|
|
|
|
|
|
|
# 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
|
2020-03-29 09:50:35 -07:00
|
|
|
export TMPDIR=$(mktemp -d /tmp/nix-bitcoin-test.XXX)
|
2020-01-12 11:52:40 -08:00
|
|
|
trap "rm -rf $TMPDIR" EXIT
|
|
|
|
|
2020-09-27 03:43:26 -07:00
|
|
|
nix-build --out-link $TMPDIR/driver -E "(import \"$testDir/tests.nix\" { scenario = \"$scenario\"; }).vm" -A driver
|
2020-01-12 11:52:40 -08:00
|
|
|
|
|
|
|
# Variable 'tests' contains the Python code that is executed by the driver on startup
|
|
|
|
if [[ $1 == --interactive ]]; then
|
|
|
|
echo "Running interactive testing environment"
|
|
|
|
tests=$(
|
|
|
|
echo 'is_interactive = True'
|
2020-09-27 03:43:20 -07:00
|
|
|
echo 'exec(os.environ["testScript"])'
|
2020-01-12 11:52:40 -08:00
|
|
|
# Start VM
|
|
|
|
echo 'start_all()'
|
|
|
|
# Start REPL
|
|
|
|
echo 'import code'
|
|
|
|
echo 'code.interact(local=globals())'
|
|
|
|
)
|
|
|
|
else
|
|
|
|
tests='exec(os.environ["testScript"])'
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "VM stats: CPUs: $numCPUs, memory: $memoryMiB MiB"
|
|
|
|
[[ $NB_TEST_ENABLE_NETWORK ]] || QEMU_NET_OPTS='restrict=on'
|
2020-03-29 09:50:35 -07:00
|
|
|
cd $TMPDIR # The VM creates a VDE control socket in $PWD
|
2020-01-12 11:52:40 -08:00
|
|
|
env -i \
|
|
|
|
NIX_PATH="$NIX_PATH" \
|
|
|
|
TMPDIR="$TMPDIR" \
|
2020-03-29 09:50:35 -07:00
|
|
|
USE_TMPDIR=1 \
|
|
|
|
NIX_DISK_IMAGE=$TMPDIR/img.qcow2 \
|
2020-01-12 11:52:40 -08:00
|
|
|
tests="$tests" \
|
|
|
|
QEMU_OPTS="-smp $numCPUs -m $memoryMiB -nographic $QEMU_OPTS" \
|
|
|
|
QEMU_NET_OPTS="$QEMU_NET_OPTS" \
|
|
|
|
$TMPDIR/driver/bin/nixos-test-driver
|
|
|
|
}
|
|
|
|
|
|
|
|
debug() {
|
|
|
|
run --interactive
|
|
|
|
}
|
|
|
|
|
2020-09-27 03:43:34 -07:00
|
|
|
evalTest() {
|
|
|
|
nix eval --raw "($(vmTestNixExpr)).outPath"
|
|
|
|
echo # nix eval doesn't print a newline
|
|
|
|
}
|
|
|
|
|
2020-09-27 03:43:28 -07:00
|
|
|
container() {
|
|
|
|
. "$testDir/lib/make-container.sh" "$@"
|
|
|
|
}
|
|
|
|
|
2020-01-12 11:52:40 -08:00
|
|
|
# Run the test by building the test derivation
|
2020-08-21 13:36:13 -07:00
|
|
|
buildTest() {
|
2020-08-21 13:36:14 -07:00
|
|
|
if [[ $outLinkPrefix ]]; then
|
|
|
|
buildArgs="--out-link $outLinkPrefix-$scenario"
|
|
|
|
else
|
|
|
|
buildArgs=--no-out-link
|
|
|
|
fi
|
|
|
|
vmTestNixExpr | nix-build $buildArgs "$@" -
|
2020-01-12 11:52:40 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
# On continuous integration nodes there are few other processes running alongside the
|
|
|
|
# test, so use more memory here for maximum performance.
|
|
|
|
exprForCI() {
|
|
|
|
memoryMiB=3072
|
|
|
|
memTotalKiB=$(awk '/MemTotal/ { print $2 }' /proc/meminfo)
|
|
|
|
memAvailableKiB=$(awk '/MemAvailable/ { print $2 }' /proc/meminfo)
|
|
|
|
# Round down to nearest multiple of 50 MiB for improved test build caching
|
|
|
|
((memAvailableMiB = memAvailableKiB / (1024 * 50) * 50))
|
|
|
|
((memAvailableMiB < memoryMiB)) && memoryMiB=$memAvailableMiB
|
2020-02-25 13:51:57 -08:00
|
|
|
>&2 echo "VM stats: CPUs: $numCPUs, memory: $memoryMiB MiB"
|
|
|
|
>&2 echo "Host memory total: $((memTotalKiB / 1024)) MiB, available: $memAvailableMiB MiB"
|
2020-09-27 03:43:37 -07:00
|
|
|
|
|
|
|
# VMX is usually not available on CI nodes due to recursive virtualisation.
|
|
|
|
# Explicitly disable VMX, otherwise QEMU 4.20 fails with message
|
|
|
|
# "error: failed to set MSR 0x48b to 0x159ff00000000"
|
|
|
|
vmTestNixExpr "-cpu host,-vmx"
|
2020-01-12 11:52:40 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
vmTestNixExpr() {
|
2020-09-27 03:43:37 -07:00
|
|
|
extraQEMUOpts="$1"
|
2020-01-12 11:52:40 -08:00
|
|
|
cat <<EOF
|
2020-09-27 03:43:26 -07:00
|
|
|
((import "$testDir/tests.nix" { scenario = "$scenario"; }).vm {}).overrideAttrs (old: rec {
|
2020-01-12 11:52:40 -08:00
|
|
|
buildCommand = ''
|
2020-09-27 03:43:37 -07:00
|
|
|
export QEMU_OPTS="-smp $numCPUs -m $memoryMiB $extraQEMUOpts"
|
2020-01-12 11:52:40 -08:00
|
|
|
echo "VM stats: CPUs: $numCPUs, memory: $memoryMiB MiB"
|
|
|
|
'' + old.buildCommand;
|
|
|
|
})
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2020-09-28 04:09:10 -07:00
|
|
|
# A basic subset of tests to keep the total runtime within
|
2020-10-29 13:20:28 -07:00
|
|
|
# manageable bounds (<4 min on desktop systems).
|
2020-09-28 04:09:10 -07:00
|
|
|
# These are also run on the CI server.
|
|
|
|
basic() {
|
|
|
|
scenario=default buildTest "$@"
|
|
|
|
scenario=netns buildTest "$@"
|
2020-10-29 13:20:28 -07:00
|
|
|
scenario=netnsRegtest buildTest "$@"
|
2020-09-28 04:09:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
all() {
|
|
|
|
scenario=default buildTest "$@"
|
|
|
|
scenario=netns buildTest "$@"
|
|
|
|
scenario=full buildTest "$@"
|
2020-10-16 08:43:19 -07:00
|
|
|
scenario=regtest buildTest "$@"
|
2020-10-29 13:20:28 -07:00
|
|
|
scenario=netnsRegtest buildTest "$@"
|
2020-09-28 04:09:10 -07:00
|
|
|
}
|
|
|
|
|
2020-08-21 13:36:13 -07:00
|
|
|
build() {
|
|
|
|
if [[ $scenario ]]; then
|
|
|
|
buildTest "$@"
|
|
|
|
else
|
2020-09-28 04:09:10 -07:00
|
|
|
basic "$@"
|
2020-08-21 13:36:13 -07:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2020-09-27 03:43:27 -07:00
|
|
|
command="${1:-build}"
|
|
|
|
shift || true
|
2020-09-28 04:09:09 -07:00
|
|
|
if [[ $command != build ]]; then
|
|
|
|
: ${scenario:=default}
|
|
|
|
fi
|
2020-09-27 03:43:34 -07:00
|
|
|
if [[ $command == eval ]]; then
|
|
|
|
command=evalTest
|
|
|
|
fi
|
2020-09-27 03:43:27 -07:00
|
|
|
$command "$@"
|