2020-01-12 19:52:40 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
# Modules integration test runner.
|
2020-12-24 10:29:14 +00:00
|
|
|
# The tests (defined in ./tests.nix) use the NixOS testing framework and are executed in a VM.
|
2020-01-12 19:52:40 +00:00
|
|
|
#
|
|
|
|
# Usage:
|
2020-08-21 20:36:13 +00:00
|
|
|
# Run all tests
|
|
|
|
# ./run-tests.sh
|
|
|
|
#
|
|
|
|
# Test specific scenario
|
2020-09-27 10:43:20 +00:00
|
|
|
# ./run-tests.sh --scenario|-s <scenario>
|
|
|
|
#
|
2021-01-30 09:47:01 +00:00
|
|
|
# - 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
|
2020-09-27 10:43:20 +00:00
|
|
|
# where services.<scenario> is enabled.
|
2020-01-12 19:52:40 +00:00
|
|
|
#
|
2020-09-27 10:43:32 +00:00
|
|
|
# Example:
|
|
|
|
# ./run-tests.sh -s electrs
|
|
|
|
#
|
2020-09-28 11:09:09 +00: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 19:52:40 +00:00
|
|
|
#
|
2020-08-21 20:36:15 +00:00
|
|
|
# Pass extra args to nix-build
|
|
|
|
# ./run-tests.sh build --builders 'ssh://mybuildhost - - 15'
|
|
|
|
#
|
2020-01-12 19:52:40 +00:00
|
|
|
# Run interactive test debugging
|
2020-08-21 20:36:13 +00:00
|
|
|
# ./run-tests.sh [--scenario <scenario>] debug
|
2020-01-12 19:52:40 +00:00
|
|
|
#
|
2020-09-27 10:43:20 +00: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 10:43:28 +00: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.
|
|
|
|
#
|
2021-03-26 22:23:22 +00:00
|
|
|
# Run a test scenario in a regular NixOS VM.
|
|
|
|
# No tests are executed, the machine's serial console is attached to your terminal.
|
|
|
|
# ./run-tests.sh [--scenario <scenario>] vm
|
|
|
|
#
|
|
|
|
# This is useful for directly exploring a test configuration without the
|
|
|
|
# intermediate Python REPL layer.
|
|
|
|
# Run command 'q' inside the machine for instant poweroff.
|
|
|
|
#
|
2021-02-06 08:38:58 +00:00
|
|
|
# Run tests from a snapshot copy of the source files
|
|
|
|
# ./run-tests.sh --copy-src|-c ...
|
|
|
|
#
|
|
|
|
# This allows you to continue editing the nix-bitcoin sources while tests are running
|
|
|
|
# and reading source files.
|
|
|
|
# Files are copied to /tmp, a caching scheme helps minimizing copies.
|
|
|
|
#
|
2022-12-12 10:57:08 +00:00
|
|
|
# Add custom scenarios from a file
|
|
|
|
# ./run-tests.sh --extra-scenarios ~/my/scenarios.nix ...
|
2020-01-12 19:52:40 +00:00
|
|
|
|
|
|
|
set -eo pipefail
|
|
|
|
|
2020-12-11 12:26:08 +00:00
|
|
|
scriptDir=$(cd "${BASH_SOURCE[0]%/*}" && pwd)
|
|
|
|
|
2021-02-06 08:38:58 +00:00
|
|
|
args=("$@")
|
2020-07-16 14:48:41 +00:00
|
|
|
scenario=
|
2020-08-21 20:36:14 +00:00
|
|
|
outLinkPrefix=
|
2022-12-12 10:57:08 +00:00
|
|
|
scenarioOverridesFile=
|
2020-08-21 20:36:14 +00:00
|
|
|
while :; do
|
|
|
|
case $1 in
|
|
|
|
--scenario|-s)
|
|
|
|
if [[ $2 ]]; then
|
|
|
|
scenario=$2
|
|
|
|
shift
|
|
|
|
shift
|
|
|
|
else
|
2020-09-27 10:43:25 +00:00
|
|
|
>&2 echo "Error: $1 requires an argument."
|
2020-08-21 20:36:14 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
--out-link-prefix|-o)
|
|
|
|
if [[ $2 ]]; then
|
|
|
|
outLinkPrefix=$2
|
|
|
|
shift
|
|
|
|
shift
|
|
|
|
else
|
2020-09-27 10:43:25 +00:00
|
|
|
>&2 echo "Error: $1 requires an argument."
|
2020-08-21 20:36:14 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
;;
|
2022-12-12 10:57:08 +00:00
|
|
|
--extra-scenarios)
|
|
|
|
if [[ $2 ]]; then
|
|
|
|
scenarioOverridesFile=$2
|
|
|
|
shift
|
|
|
|
shift
|
|
|
|
else
|
|
|
|
>&2 echo "Error: $1 requires an argument."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
;;
|
2021-02-06 08:38:58 +00:00
|
|
|
--copy-src|-c)
|
|
|
|
shift
|
2021-12-14 18:51:17 +00:00
|
|
|
if [[ ! $_nixBitcoinInCopiedSrc ]]; then
|
2021-02-06 08:38:58 +00:00
|
|
|
. "$scriptDir/lib/copy-src.sh"
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
;;
|
2020-08-21 20:36:14 +00:00
|
|
|
*)
|
|
|
|
break
|
|
|
|
esac
|
|
|
|
done
|
2020-07-16 14:48:41 +00:00
|
|
|
|
2022-10-22 17:37:58 +00:00
|
|
|
tmpDir=
|
|
|
|
# Sets global var `tmpDir`
|
|
|
|
makeTmpDir() {
|
|
|
|
if [[ ! $tmpDir ]]; then
|
|
|
|
tmpDir=$(mktemp -d /tmp/nix-bitcoin-tests.XXX)
|
|
|
|
# shellcheck disable=SC2064
|
|
|
|
trap "rm -rf '$tmpDir'" EXIT
|
|
|
|
fi
|
|
|
|
}
|
2021-01-30 09:47:01 +00:00
|
|
|
|
|
|
|
# Support explicit scenario definitions
|
|
|
|
if [[ $scenario = *' '* ]]; then
|
2022-10-22 17:37:58 +00:00
|
|
|
makeTmpDir
|
2022-12-12 10:57:08 +00:00
|
|
|
scenarioOverridesFile=$tmpDir/scenario-overrides.nix
|
2022-10-22 17:37:58 +00:00
|
|
|
echo "{ scenarios, pkgs, lib, nix-bitcoin }: with lib; { tmp = $scenario; }" > "$scenarioOverridesFile"
|
2021-01-30 09:47:01 +00:00
|
|
|
scenario=tmp
|
|
|
|
fi
|
2020-01-12 19:52:40 +00:00
|
|
|
|
|
|
|
# Run the test. No temporary files are left on the host system.
|
|
|
|
run() {
|
2022-10-22 17:37:58 +00:00
|
|
|
makeTmpDir
|
|
|
|
buildTestAttr .run --out-link "$tmpDir/run-vm"
|
|
|
|
NIX_BITCOIN_VM_DATADIR=$tmpDir "$tmpDir/run-vm/bin/run-vm" "$@"
|
2020-01-12 19:52:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
debug() {
|
2022-10-22 17:37:58 +00:00
|
|
|
run --debug
|
2020-12-11 12:26:04 +00:00
|
|
|
}
|
|
|
|
|
2020-09-27 10:43:28 +00:00
|
|
|
container() {
|
2022-10-22 17:37:58 +00:00
|
|
|
local nixosContainer
|
|
|
|
if ! nixosContainer=$(type -p nixos-container) \
|
|
|
|
|| grep -q '"/etc/nixos-containers"' "$nixosContainer"; then
|
|
|
|
local attr=container
|
|
|
|
else
|
|
|
|
# NixOS with `system.stateVersion` <22.05
|
|
|
|
local attr=containerLegacy
|
|
|
|
fi
|
|
|
|
echo "Building container"
|
|
|
|
makeTmpDir
|
|
|
|
export container=$tmpDir/container
|
|
|
|
buildTestAttr ".$attr" --out-link "$container"
|
|
|
|
export scriptDir
|
2021-01-30 09:47:01 +00:00
|
|
|
"$scriptDir/lib/make-container.sh" "$@"
|
2020-09-27 10:43:28 +00:00
|
|
|
}
|
|
|
|
|
2021-03-26 22:23:22 +00:00
|
|
|
# Run a regular NixOS VM
|
|
|
|
vm() {
|
2022-10-22 17:37:58 +00:00
|
|
|
makeTmpDir
|
|
|
|
buildTestAttr .vm --out-link "$tmpDir/vm"
|
|
|
|
NIX_BITCOIN_VM_DATADIR=$tmpDir "$tmpDir/vm/bin/run-vm-in-tmpdir"
|
|
|
|
}
|
2021-03-26 22:23:22 +00:00
|
|
|
|
2022-10-22 17:37:58 +00:00
|
|
|
# Run the test by building the test derivation
|
|
|
|
buildTest() {
|
|
|
|
buildTestAttr "" "$@"
|
|
|
|
}
|
2021-03-26 22:23:22 +00:00
|
|
|
|
2022-10-22 17:37:58 +00:00
|
|
|
evalTest() {
|
|
|
|
nixInstantiateTest "" "$@"
|
|
|
|
# Print out path
|
|
|
|
nix-store -q "$drv"
|
|
|
|
# Print drv path
|
|
|
|
realpath "$drv"
|
2021-03-26 22:23:22 +00:00
|
|
|
}
|
|
|
|
|
2022-10-22 17:37:58 +00:00
|
|
|
buildTestAttr() {
|
|
|
|
local attr=$1
|
2020-12-11 12:26:09 +00:00
|
|
|
shift
|
2022-10-22 17:37:58 +00:00
|
|
|
# TODO-EXTERNAL:
|
|
|
|
# Simplify and switch to pure build when `nix build` can build flake function outputs
|
|
|
|
nixInstantiateTest "$attr"
|
|
|
|
nixBuild "$scenario" "$drv" "$@"
|
2020-12-11 12:26:09 +00:00
|
|
|
}
|
|
|
|
|
2022-10-22 17:37:58 +00:00
|
|
|
buildTests() {
|
|
|
|
local -n tests=$1
|
|
|
|
shift
|
|
|
|
# TODO-EXTERNAL:
|
|
|
|
# Simplify and switch to pure build when `nix build` can instantiate flake function outputs
|
|
|
|
# shellcheck disable=SC2207
|
|
|
|
drvs=($(nixInstantiate "pkgs.instantiateTests \"${tests[*]}\""))
|
|
|
|
for i in "${!tests[@]}"; do
|
|
|
|
testName=${tests[$i]}
|
|
|
|
drv=${drvs[$i]}
|
|
|
|
echo
|
|
|
|
echo "Building test '$testName'"
|
|
|
|
nixBuild "$testName" "$drv" "$@"
|
|
|
|
done
|
2020-01-12 19:52:40 +00:00
|
|
|
}
|
|
|
|
|
2022-10-22 17:37:58 +00:00
|
|
|
# Instantiate an attr of the test defined in global var `scenario`
|
|
|
|
nixInstantiateTest() {
|
|
|
|
local attr=$1
|
|
|
|
shift
|
2022-12-12 10:57:08 +00:00
|
|
|
if [[ $scenarioOverridesFile ]]; then
|
2022-10-22 17:37:58 +00:00
|
|
|
local file="extraScenariosFile = \"$scenarioOverridesFile\";"
|
|
|
|
else
|
|
|
|
local file=
|
2020-12-11 12:26:10 +00:00
|
|
|
fi
|
2022-10-22 17:37:58 +00:00
|
|
|
nixInstantiate "(pkgs.getTest { name = \"$scenario\"; $file })$attr" "$@" >/dev/null
|
|
|
|
}
|
2020-12-11 12:26:10 +00:00
|
|
|
|
2022-10-22 17:37:58 +00:00
|
|
|
drv=
|
|
|
|
# Sets global var `drv` to the gcroot link of the instantiated derivation
|
|
|
|
nixInstantiate() {
|
|
|
|
local expr=$1
|
|
|
|
shift
|
|
|
|
makeTmpDir
|
|
|
|
drv="$tmpDir/drv"
|
|
|
|
nix-instantiate --add-root "$drv" -E "
|
|
|
|
let
|
|
|
|
pkgs = (builtins.getFlake \"git+file://$scriptDir/..\").legacyPackages.\${builtins.currentSystem};
|
|
|
|
in
|
|
|
|
$expr
|
|
|
|
" "$@"
|
2020-01-12 19:52:40 +00:00
|
|
|
}
|
|
|
|
|
2022-10-22 17:37:58 +00:00
|
|
|
nixBuild() {
|
|
|
|
local outLinkName=$1
|
|
|
|
shift
|
|
|
|
args=(--print-out-paths -L)
|
|
|
|
if [[ $outLinkPrefix ]]; then
|
|
|
|
args+=(--out-link "$outLinkPrefix-$outLinkName")
|
|
|
|
else
|
|
|
|
args+=(--no-link)
|
2021-12-08 03:07:30 +00:00
|
|
|
fi
|
2022-10-22 17:37:58 +00:00
|
|
|
nix build "${args[@]}" "$@"
|
2021-12-08 03:07:30 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 03:07:31 +00:00
|
|
|
flake() {
|
|
|
|
nix flake check "$scriptDir/.."
|
|
|
|
}
|
|
|
|
|
2021-12-08 03:07:32 +00:00
|
|
|
# Test generating module documentation for search.nixos.org
|
|
|
|
nixosSearch() {
|
|
|
|
if [[ $outLinkPrefix ]]; then
|
|
|
|
# Add gcroots for flake-info
|
2022-08-16 19:00:00 +00:00
|
|
|
nix build "$scriptDir/nixos-search#flake-info" -o "$outLinkPrefix-flake-info"
|
2021-12-08 03:07:32 +00:00
|
|
|
fi
|
2022-09-23 07:03:58 +00:00
|
|
|
"$scriptDir/nixos-search/flake-info-sandboxed.sh"
|
2021-12-08 03:07:32 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 11:09:10 +00:00
|
|
|
# A basic subset of tests to keep the total runtime within
|
2022-10-22 17:37:58 +00:00
|
|
|
# manageable bounds.
|
2020-09-28 11:09:10 +00:00
|
|
|
# These are also run on the CI server.
|
2022-10-22 17:37:58 +00:00
|
|
|
basic=(
|
|
|
|
default
|
|
|
|
netns
|
|
|
|
netnsRegtest
|
|
|
|
)
|
|
|
|
basic() { buildTests basic "$@"; }
|
2020-09-28 11:09:10 +00:00
|
|
|
|
2020-12-18 12:27:21 +00:00
|
|
|
# All tests that only consist of building a nix derivation.
|
2022-10-22 17:37:58 +00:00
|
|
|
# shellcheck disable=2034
|
|
|
|
buildable=(
|
|
|
|
"${basic[@]}"
|
|
|
|
full
|
|
|
|
regtest
|
|
|
|
hardened
|
|
|
|
clightningReplication
|
|
|
|
lndPruned
|
|
|
|
)
|
|
|
|
buildable() { buildTests buildable "$@"; }
|
2020-09-28 11:09:10 +00:00
|
|
|
|
2020-12-18 12:27:21 +00:00
|
|
|
examples() {
|
2022-10-22 17:37:58 +00:00
|
|
|
# shellcheck disable=SC2016
|
2022-10-22 17:37:51 +00:00
|
|
|
script='
|
2020-12-18 12:27:21 +00:00
|
|
|
set -e
|
2022-10-22 17:37:51 +00:00
|
|
|
runExample() { echo; echo Running example $1; ./$1; }
|
|
|
|
runExample deploy-container.sh
|
|
|
|
runExample deploy-container-minimal.sh
|
|
|
|
runExample deploy-qemu-vm.sh
|
|
|
|
runExample deploy-krops.sh
|
|
|
|
'
|
2021-08-15 09:28:39 +00:00
|
|
|
(cd "$scriptDir/../examples" && nix-shell --run "$script")
|
2020-12-18 12:27:21 +00:00
|
|
|
}
|
|
|
|
|
2022-08-16 19:00:00 +00:00
|
|
|
shellcheck() {
|
internal scripts: use pinned, cached pkgs
Instead of setting up the script PATH via nix-shell, use
`nix shell` with inputs from the nix-bitcoin flake.
Advantages:
- Uses the nixpkgs version from the nix-bitcoin flake instead of
`<nixpkgs>` from the user env (NIX_PATH), so the script runtime
env is reproducible.
- The pkg derivations for the runtime env are cached, which greatly
increases script startup speed.
This commit was generated by running the following script inside the
repo root dir:
def transform(path, src)
if src =~ /#! *nix-shell +-i +bash +-p +(.*)/
pkgs = $1
if src =~ /^.*?(set -e.*?pipefail)\n/
set_statement = $1
src.sub!($&, '')
end
src.sub!(/\A.*?#! *nix-shell.*?\n/m, '')
parents = ([ '..' ] * (path.split('/').count - 1)).join('/')
[
'#!/usr/bin/env bash',
*set_statement,
%(. "${BASH_SOURCE[0]%/*}/#{parents}/helper/run-in-nix-env" "#{pkgs}" "$@"),
nil,
src
].join("\n")
end
end
Dir['**/*.sh'].each do |f|
src = File.read(f)
if new_src = transform(f, src)
puts "Changed file #{f}"
File.write(f, new_src)
end
end
2022-08-22 12:57:39 +00:00
|
|
|
"$scriptDir/shellcheck.sh"
|
2022-08-16 19:00:00 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 12:27:21 +00:00
|
|
|
all() {
|
2022-08-16 19:00:00 +00:00
|
|
|
buildable "$@"
|
|
|
|
shellcheck
|
2020-12-18 12:27:21 +00:00
|
|
|
examples
|
2021-08-16 08:42:07 +00:00
|
|
|
flake
|
2021-12-08 03:07:32 +00:00
|
|
|
nixosSearch
|
2020-12-18 12:27:21 +00:00
|
|
|
}
|
|
|
|
|
2020-12-11 12:26:03 +00:00
|
|
|
# An alias for buildTest
|
2020-08-21 20:36:13 +00:00
|
|
|
build() {
|
2020-12-11 12:26:03 +00:00
|
|
|
buildTest "$@"
|
2020-08-21 20:36:13 +00:00
|
|
|
}
|
|
|
|
|
2022-08-16 19:00:00 +00:00
|
|
|
if [[ $# -gt 0 && $1 != -* ]]; then
|
2020-12-11 12:26:03 +00:00
|
|
|
# An explicit command was provided
|
|
|
|
command=$1
|
|
|
|
shift
|
|
|
|
if [[ $command == eval ]]; then
|
|
|
|
command=evalTest
|
|
|
|
fi
|
2020-09-28 11:09:09 +00:00
|
|
|
: ${scenario:=default}
|
2020-12-11 12:26:03 +00:00
|
|
|
elif [[ $scenario ]]; then
|
|
|
|
command=buildTest
|
|
|
|
else
|
|
|
|
command=basic
|
2020-09-27 10:43:34 +00:00
|
|
|
fi
|
2020-09-27 10:43:27 +00:00
|
|
|
$command "$@"
|