test: improve modularization

This improves debugging and experimenting by making it easy to compose fine-grained
scenarios that have specific tests and features enabled.

The VM test output now includes the subtest name and duration.

Remove the 'raise Exception()' hack for interactive mode.

Run 'banlist-and-restart' test before 'backups'. This speeds up the test
by avoiding an extra shutdown of all bitcoin-related services.
This commit is contained in:
Erik Arvstedt 2020-09-27 12:43:20 +02:00
parent 14d2d97ba6
commit 1e18d3ea3b
No known key found for this signature in database
GPG key ID: 33312B944DD97846
8 changed files with 385 additions and 181 deletions

35
test/lib/make-test-vm.nix Normal file
View file

@ -0,0 +1,35 @@
testArgs:
let
pkgs = import <nixpkgs> { config = {}; overlays = []; };
pkgs19_09 = import (pkgs.fetchzip {
url = "https://github.com/NixOS/nixpkgs-channels/archive/a7ceb2536ab11973c59750c4c48994e3064a75fa.tar.gz";
sha256 = "0hka65f31njqpq7i07l22z5rs7lkdfcl4pbqlmlsvnysb74ynyg1";
}) { config = {}; overlays = []; };
test = (import "${pkgs.path}/nixos/tests/make-test-python.nix") testArgs;
fixedTest = { system ? builtins.currentSystem, ... }@args:
let
pkgsFixed = pkgs // {
# Fix the black Python code formatter that's used in the test to allow the test
# script to have longer lines. The default width of 88 chars is too restrictive for
# our script.
python3Packages = pkgs.python3Packages // {
black = pkgs.writeScriptBin "black" ''
fileToCheck=''${@:$#}
[[ $fileToCheck = *test-script ]] && extraArgs='--line-length 100'
exec ${pkgs.python3Packages.black}/bin/black $extraArgs "$@"
'';
};
# QEMU 4.20 from unstable fails on Travis build nodes with message
# "error: failed to set MSR 0x48b to 0x159ff00000000"
# Use version 4.0.1 instead.
inherit (pkgs19_09) qemu_test;
};
in
test (args // { pkgs = pkgsFixed; });
in
fixedTest

41
test/lib/make-test.nix Normal file
View file

@ -0,0 +1,41 @@
scenario: testConfig:
{
vm = import ./make-test-vm.nix {
name = "nix-bitcoin-${scenario}";
machine = {
imports = [ testConfig ];
# Needed because duplicity requires 270 MB of free temp space, regardless of backup size
virtualisation.diskSize = 1024;
};
testScript = nodes: let
cfg = nodes.nodes.machine.config;
data = {
data = cfg.test.data;
tests = cfg.tests;
};
dataFile = builtins.toFile "test-data" (builtins.toJSON data);
initData = ''
import json
with open("${dataFile}") as f:
data = json.load(f)
enabled_tests = set(test for (test, enabled) in data["tests"].items() if enabled)
test_data = data["data"]
'';
in
builtins.concatStringsSep "\n\n" [
initData
(builtins.readFile ./../tests.py)
# Don't run tests in interactive mode.
# is_interactive is set in ../run-tests.sh
''
if not "is_interactive" in vars():
run_tests()
''
];
};
}

30
test/lib/test-lib.nix Normal file
View file

@ -0,0 +1,30 @@
{ config, lib, ... }:
with lib;
{
options = {
test = {
noConnections = mkOption {
type = types.bool;
default = true;
description = ''
Whether services should be configured to not connect to external hosts.
This can silence some warnings while running the test in an offline environment.
'';
};
data = mkOption {
type = types.attrs;
default = {};
description = ''
Attrs that are available in the Python test script under the global
dictionary variable 'test_data'. The data is exported via JSON.
'';
};
};
tests = mkOption {
type = with types; attrsOf bool;
default = {};
description = "Python tests that should be run.";
};
};
}