From fcc67da9f42928a8cd8e18b76a6f1cf702ca555a Mon Sep 17 00:00:00 2001 From: Erik Arvstedt Date: Sun, 27 Sep 2020 12:43:28 +0200 Subject: [PATCH] test: add container support --- test/lib/make-container.sh | 89 ++++++++++++++++++++++++++++++++++++++ test/lib/make-test.nix | 10 +++++ test/lib/test-lib.nix | 11 ++++- test/run-tests.sh | 12 +++++ test/tests.nix | 8 ++++ 5 files changed, 129 insertions(+), 1 deletion(-) create mode 100755 test/lib/make-container.sh diff --git a/test/lib/make-container.sh b/test/lib/make-container.sh new file mode 100755 index 0000000..327f13d --- /dev/null +++ b/test/lib/make-container.sh @@ -0,0 +1,89 @@ +#!/usr/bin/env bash + +# Usage: +# +# run-tests.sh [--scenario|-s ] container +# +# Start container and start a shell session with helper commands +# for accessing the container. +# A short command documentation is printed at the start of the session. +# The container is destroyed after exiting the shell. +# An existing container is destroyed before starting. +# +# Supported arguments: +# +# --destroy|-d to destroy +# +# When `run-tests.sh container` from inside an existing shell session, +# the current container is updated without restarting by switching +# its NixOS configuration. +# Use this arg to destroy and restart the container instead. +# +# --no-destroy|-n +# +# By default, all commands destroy an existing container before starting and, +# when appropriate, before exiting. +# This ensures that containers start with no leftover filesystem state from +# previous runs and that containers don't consume system resources after use. +# This args disables auto-destructing containers. +# +# +# run-tests.sh container --run|-r c systemctl status bitcoind +# +# Run a command in the shell session environmentand exit. +# Destroy the container afterwards. +# All arguments following `--run` are used as a command. +# Supports argument '--no-destroy|-n' (see above for an explanation). +# +# Example: Start shell inside container +# run-tests.sh container --run c +# +# +# run-tests.sh [--scenario|-s ] container --command|--c +# +# Provide a custom extra-container command. +# +# Example: +# run-tests.sh container --command create -s +# Create and start a container without a shell. +# +# +# All extra args are passed to extra-container (unless --command is used): +# run-tests.sh container --build-args --builders 'ssh://worker - - 8' + +set -euo pipefail + +if [[ $EUID != 0 ]]; then + # NixOS containers require root permissions. + # By using sudo here and not at the user's call-site extra-container can detect if it is running + # inside an existing shell session (by checking an internal environment variable). + exec sudo scenario="$scenario" testDir="$testDir" NIX_PATH="$NIX_PATH" PATH="$PATH" \ + scenarioOverridesFile="${scenarioOverridesFile:-}" "$testDir/lib/make-container.sh" "$@" +fi + +export containerName=nb-test +containerCommand=shell + +while [[ $# > 0 ]]; do + case $1 in + --command|-c) + shift + containerCommand=$1 + shift + ;; + *) + break + esac +done + +containerBin=$(type -P extra-container) || true +if [[ ! ($containerBin && $(realpath $containerBin) == *extra-container-0.5*) ]]; then + echo "Building extra-container. Skip this step by adding extra-container 0.5 to PATH." + nix-build --out-link /tmp/extra-container "$testDir"/../pkgs -A extra-container >/dev/null + export PATH="/tmp/extra-container/bin${PATH:+:}$PATH" +fi + +read -d '' src <] 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. +# # To add custom scenarios, set the environment variable `scenarioOverridesFile`. set -eo pipefail @@ -108,6 +116,10 @@ debug() { run --interactive } +container() { + . "$testDir/lib/make-container.sh" "$@" +} + # Run the test by building the test derivation buildTest() { if [[ $outLinkPrefix ]]; then diff --git a/test/tests.nix b/test/tests.nix index cb8aa4e..db073a1 100644 --- a/test/tests.nix +++ b/test/tests.nix @@ -115,6 +115,14 @@ let testEnv = rec { }; }; + # Container-specific features + containerFeatures = { + # Container has WAN access and bitcoind connects to external nodes + test.container.enableWAN = true; + # See ./lib/test-lib.nix for a description + test.container.exposeLocalhost = true; + }; + adhoc = { # # You can also set the env var `scenarioOverridesFile` (used below) to define custom scenarios.