2022-10-22 17:37:58 +00:00
|
|
|
flake: pkgs: makeTestVM:
|
2021-03-22 12:19:47 +00:00
|
|
|
let
|
2022-10-22 17:37:58 +00:00
|
|
|
inherit (flake.inputs) extra-container;
|
|
|
|
inherit (pkgs.stdenv.hostPlatform) system;
|
2021-03-22 12:19:47 +00:00
|
|
|
in
|
2021-03-22 12:19:48 +00:00
|
|
|
|
2022-10-22 17:37:58 +00:00
|
|
|
{ name ? "nix-bitcoin-test", config }:
|
|
|
|
let
|
|
|
|
inherit (pkgs) lib;
|
2022-09-05 16:12:09 +00:00
|
|
|
|
2022-10-22 17:37:58 +00:00
|
|
|
testConfig = config;
|
2021-03-22 12:19:48 +00:00
|
|
|
|
2022-10-22 17:37:58 +00:00
|
|
|
test = makeTestVM {
|
|
|
|
inherit name;
|
2021-03-22 12:19:48 +00:00
|
|
|
|
2022-10-22 17:37:58 +00:00
|
|
|
nodes.machine = { config, ... }: {
|
|
|
|
imports = [
|
|
|
|
testConfig
|
|
|
|
commonVmConfig
|
|
|
|
];
|
2022-09-05 16:12:09 +00:00
|
|
|
|
2022-10-22 17:37:44 +00:00
|
|
|
test.shellcheckServices.enable = true;
|
2020-09-27 10:43:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
testScript = nodes: let
|
|
|
|
cfg = nodes.nodes.machine.config;
|
|
|
|
data = {
|
|
|
|
data = cfg.test.data;
|
|
|
|
tests = cfg.tests;
|
|
|
|
};
|
2020-11-19 02:01:44 +00:00
|
|
|
dataFile = pkgs.writeText "test-data" (builtins.toJSON data);
|
2020-09-27 10:43:20 +00:00
|
|
|
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)
|
2021-10-01 09:52:05 +00:00
|
|
|
cfg.test.extraTestScript
|
2020-09-27 10:43:20 +00:00
|
|
|
# Don't run tests in interactive mode.
|
2022-10-22 17:37:58 +00:00
|
|
|
# is_interactive is set in ./run-vm.sh
|
2020-09-27 10:43:20 +00:00
|
|
|
''
|
|
|
|
if not "is_interactive" in vars():
|
2022-12-18 12:13:44 +00:00
|
|
|
nb_run_tests()
|
2020-09-27 10:43:20 +00:00
|
|
|
''
|
|
|
|
];
|
2021-03-22 12:19:47 +00:00
|
|
|
};
|
2020-09-27 10:43:28 +00:00
|
|
|
|
2022-10-22 17:37:58 +00:00
|
|
|
mkContainer = legacyInstallDirs:
|
|
|
|
extra-container.lib.buildContainers {
|
|
|
|
inherit system legacyInstallDirs;
|
|
|
|
config = {
|
|
|
|
# The container name has a 11 char length limit
|
|
|
|
containers.nb-test = { config, ... }: {
|
|
|
|
imports = [
|
|
|
|
{
|
|
|
|
config = {
|
|
|
|
extra = config.config.test.container;
|
|
|
|
config = testConfig;
|
|
|
|
};
|
2022-01-06 12:40:52 +00:00
|
|
|
}
|
2022-10-22 17:37:58 +00:00
|
|
|
|
|
|
|
# Enable FUSE inside the container when clightning replication
|
|
|
|
# is enabled.
|
|
|
|
# TODO-EXTERNAL: Remove this when
|
|
|
|
# https://github.com/systemd/systemd/issues/17607
|
|
|
|
# has been resolved. This will also improve security.
|
|
|
|
(
|
|
|
|
let
|
|
|
|
s = config.config.services;
|
|
|
|
in
|
|
|
|
lib.mkIf (s ? clightning && s.clightning.enable && s.clightning.replication.enable) {
|
|
|
|
bindMounts."/dev/fuse" = { hostPath = "/dev/fuse"; };
|
|
|
|
allowedDevices = [ { node = "/dev/fuse"; modifier = "rw"; } ];
|
|
|
|
}
|
|
|
|
)
|
|
|
|
];
|
|
|
|
};
|
|
|
|
};
|
2020-09-27 10:43:28 +00:00
|
|
|
};
|
2022-10-22 17:37:58 +00:00
|
|
|
|
|
|
|
container = mkContainer false;
|
|
|
|
containerLegacy = mkContainer true;
|
2020-12-11 12:26:05 +00:00
|
|
|
|
2021-03-26 22:23:22 +00:00
|
|
|
# This allows running a test scenario in a regular NixOS VM.
|
|
|
|
# No tests are executed.
|
2022-10-22 17:37:58 +00:00
|
|
|
vm = (pkgs.nixos ({ config, ... }: {
|
2021-03-26 22:23:22 +00:00
|
|
|
imports = [
|
|
|
|
testConfig
|
2022-10-22 17:37:58 +00:00
|
|
|
commonVmConfig
|
2022-10-22 17:37:47 +00:00
|
|
|
(pkgs.path + "/nixos/modules/virtualisation/qemu-vm.nix")
|
2021-03-26 22:23:22 +00:00
|
|
|
];
|
|
|
|
virtualisation.graphics = false;
|
2021-08-04 22:48:58 +00:00
|
|
|
services.getty.autologinUser = "root";
|
2021-03-26 22:23:22 +00:00
|
|
|
|
2022-10-22 17:37:45 +00:00
|
|
|
# Avoid lengthy build of the nixos manual
|
|
|
|
documentation.nixos.enable = false;
|
|
|
|
|
2022-10-22 17:37:46 +00:00
|
|
|
# Power off VM when the user exits the shell
|
|
|
|
systemd.services."serial-getty@".preStop = ''
|
|
|
|
echo o >/proc/sysrq-trigger
|
|
|
|
'';
|
2022-07-05 08:12:54 +00:00
|
|
|
|
2022-07-05 10:19:01 +00:00
|
|
|
system.stateVersion = lib.mkDefault config.system.nixos.release;
|
2022-10-22 17:37:58 +00:00
|
|
|
})).config.system.build.vm.overrideAttrs (old: {
|
|
|
|
meta = old.meta // { mainProgram = "run-vm-in-tmpdir"; };
|
|
|
|
buildCommand = old.buildCommand + "\n" + ''
|
|
|
|
install -m 700 ${./run-vm-without-tests.sh} $out/bin/run-vm-in-tmpdir
|
|
|
|
patchShebangs $out/bin/run-vm-in-tmpdir
|
|
|
|
'';
|
|
|
|
});
|
|
|
|
|
|
|
|
commonVmConfig = {
|
|
|
|
virtualisation = {
|
|
|
|
# Needed because duplicity requires 270 MB of free temp space, regardless of backup size
|
|
|
|
diskSize = 1024;
|
|
|
|
|
|
|
|
# Min. 800 MiB needed to avoid 'out of memory' errors
|
|
|
|
memorySize = lib.mkDefault 2048;
|
|
|
|
|
|
|
|
# There are no perf gains beyond 3 cores.
|
|
|
|
# Benchmark: Ryzen 7 2700 (8 cores), VM test `default` as of 34f6eb90.
|
|
|
|
# Num. Cores | 1 | 2 | 3 | 4 | 6
|
|
|
|
# Runtime (sec) | 125 | 95 | 89 | 89 | 90
|
|
|
|
cores = lib.mkDefault 3;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
in
|
|
|
|
test // {
|
|
|
|
inherit
|
|
|
|
vm
|
|
|
|
container
|
|
|
|
# For NixOS with `system.stateVersion` <22.05
|
|
|
|
containerLegacy;
|
2021-03-26 22:23:22 +00:00
|
|
|
|
2020-12-11 12:26:05 +00:00
|
|
|
config = testConfig;
|
2020-09-27 10:43:20 +00:00
|
|
|
}
|