2021-11-22 10:52:35 +01:00
|
|
|
# Adapted from the NixOps test in Nixpkgs.
|
|
|
|
#
|
2021-12-05 10:14:12 +01:00
|
|
|
# By default, we have four nodes: deployer, alpha, beta, gamma.
|
2021-11-22 10:52:35 +01:00
|
|
|
# deployer is where colmena will run.
|
|
|
|
#
|
2022-12-01 09:57:56 +01:00
|
|
|
# TODO: Modularize most of this
|
2021-11-22 10:52:35 +01:00
|
|
|
|
2021-12-05 10:14:12 +01:00
|
|
|
{ insideVm ? false
|
|
|
|
, deployers ? [ "deployer" ] # Nodes configured as deployers (with Colmena and pre-built system closure)
|
|
|
|
, targets ? [ "alpha" "beta" "gamma" ] # Nodes configured as targets (minimal config)
|
2022-06-10 20:29:25 +02:00
|
|
|
, extraDeployerConfig ? {} # Extra config on the deployer
|
2021-12-05 10:14:12 +01:00
|
|
|
, prebuiltTarget ? "alpha" # Target node to prebuild system closure for, or null
|
2021-12-08 08:13:31 +01:00
|
|
|
|
|
|
|
, pkgs ? if insideVm then import <nixpkgs> {} else throw "Must specify pkgs"
|
|
|
|
, colmena ? if !insideVm then pkgs.colmena else throw "Cannot eval inside VM"
|
2021-12-05 10:14:12 +01:00
|
|
|
}:
|
|
|
|
|
|
|
|
with builtins;
|
|
|
|
|
|
|
|
assert elem "deployer" deployers;
|
2021-11-22 10:52:35 +01:00
|
|
|
|
|
|
|
let
|
2021-12-05 10:14:12 +01:00
|
|
|
inherit (pkgs) lib;
|
2021-11-22 10:52:35 +01:00
|
|
|
|
|
|
|
colmenaExec = "${colmena}/bin/colmena";
|
|
|
|
|
2022-12-01 09:57:56 +01:00
|
|
|
## Utilities
|
2021-11-22 10:52:35 +01:00
|
|
|
sshKeys = import (pkgs.path + "/nixos/tests/ssh-keys.nix") pkgs;
|
2022-12-01 09:57:56 +01:00
|
|
|
nixosLib = import (pkgs.path + "/nixos/lib") { };
|
2021-11-22 10:52:35 +01:00
|
|
|
|
2022-12-01 09:57:56 +01:00
|
|
|
inputClosureOf = pkg: pkgs.runCommand "full-closure" {
|
|
|
|
refs = pkgs.writeReferencesToFile pkg.drvPath;
|
|
|
|
} ''
|
|
|
|
touch $out
|
2022-06-10 20:29:25 +02:00
|
|
|
|
2022-12-01 09:57:56 +01:00
|
|
|
while read ref; do
|
|
|
|
case $ref in
|
|
|
|
*.drv)
|
|
|
|
cat $ref >>$out
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done <$refs
|
|
|
|
'';
|
2022-08-17 09:09:43 +02:00
|
|
|
|
2022-12-01 09:57:56 +01:00
|
|
|
## The modular NixOS test framework with Colmena additions
|
|
|
|
colmenaTestModule = { lib, config, ... }: let
|
|
|
|
cfg = config.colmena.test;
|
2021-11-22 10:52:35 +01:00
|
|
|
|
2022-12-01 09:57:56 +01:00
|
|
|
targetList = "[${concatStringsSep ", " targets}]";
|
|
|
|
bundle = pkgs.stdenv.mkDerivation {
|
|
|
|
name = "${config.name}-bundle";
|
|
|
|
dontUnpack = true;
|
|
|
|
dontInstall = true;
|
|
|
|
buildPhase = ''
|
|
|
|
cp -r ${cfg.bundle} $out
|
|
|
|
chmod u+w $out
|
|
|
|
cp ${./tools.nix} $out/tools.nix
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
in {
|
|
|
|
options = {
|
|
|
|
colmena.test = {
|
|
|
|
bundle = lib.mkOption {
|
|
|
|
description = ''
|
|
|
|
Path to a directory to copy into the deployer as /tmp/bundle.
|
|
|
|
'';
|
|
|
|
type = lib.types.path;
|
|
|
|
};
|
|
|
|
|
|
|
|
testScript = lib.mkOption {
|
|
|
|
description = ''
|
|
|
|
The test script.
|
|
|
|
|
|
|
|
The Colmena test framework will prepend initialization
|
|
|
|
statements to the actual test script.
|
|
|
|
'';
|
|
|
|
type = lib.types.str;
|
|
|
|
};
|
2021-11-22 10:52:35 +01:00
|
|
|
};
|
2022-12-01 09:57:56 +01:00
|
|
|
};
|
|
|
|
config = {
|
|
|
|
testScript = ''
|
|
|
|
start_all()
|
|
|
|
'' + lib.optionalString (prebuiltTarget != null) ''
|
|
|
|
deployer.succeed("nix-store -qR ${prebuiltSystem}")
|
|
|
|
'' + ''
|
|
|
|
deployer.succeed("nix-store -qR ${pkgs.path}")
|
|
|
|
deployer.succeed("ln -sf ${pkgs.path} /nixpkgs")
|
|
|
|
deployer.succeed("mkdir -p /root/.ssh && touch /root/.ssh/id_rsa && chmod 600 /root/.ssh/id_rsa && cat ${sshKeys.snakeOilPrivateKey} > /root/.ssh/id_rsa")
|
|
|
|
|
|
|
|
${lib.optionalString (length targets != 0) ''
|
|
|
|
for node in ${targetList}:
|
|
|
|
node.wait_for_unit("sshd.service")
|
2023-01-28 07:44:41 +01:00
|
|
|
deployer.wait_until_succeeds(f"ssh -o StrictHostKeyChecking=accept-new {node.name} true", timeout=30)
|
2022-12-01 09:57:56 +01:00
|
|
|
''}
|
|
|
|
|
|
|
|
deployer.succeed("cp --no-preserve=mode -r ${bundle} /tmp/bundle && chmod u+w /tmp/bundle")
|
|
|
|
|
|
|
|
orig_store_paths = set(deployer.succeed("ls /nix/store").strip().split("\n"))
|
|
|
|
def get_new_store_paths():
|
|
|
|
cur_store_paths = set(deployer.succeed("ls /nix/store").strip().split("\n"))
|
|
|
|
new_store_paths = cur_store_paths.difference(orig_store_paths)
|
|
|
|
deployer.log(f"{len(new_store_paths)} store paths were created")
|
|
|
|
|
|
|
|
l = list(map(lambda n: f"/nix/store/{n}", new_store_paths))
|
|
|
|
return l
|
|
|
|
|
|
|
|
${cfg.testScript}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
evalTest = module: nixosLib.evalTest {
|
|
|
|
imports = [
|
|
|
|
module
|
|
|
|
colmenaTestModule
|
|
|
|
{ hostPkgs = pkgs; }
|
|
|
|
];
|
|
|
|
};
|
2021-12-04 10:03:26 +01:00
|
|
|
|
2022-12-01 09:57:56 +01:00
|
|
|
## Common setup
|
2022-01-02 01:41:35 +01:00
|
|
|
|
2022-12-01 09:57:56 +01:00
|
|
|
# Setup for deployer nodes
|
|
|
|
#
|
|
|
|
# We include the input closure of a prebuilt system profile
|
|
|
|
# so it can build system profiles for the targets without
|
|
|
|
# network access.
|
|
|
|
deployerConfig = { pkgs, lib, config, ... }: {
|
|
|
|
imports = [
|
|
|
|
extraDeployerConfig
|
|
|
|
];
|
2021-12-05 10:14:12 +01:00
|
|
|
|
2022-12-01 09:57:56 +01:00
|
|
|
nix.registry = lib.mkIf (pkgs ? _inputs) {
|
|
|
|
nixpkgs.flake = pkgs._inputs.nixpkgs;
|
2021-11-22 10:52:35 +01:00
|
|
|
};
|
2021-12-05 10:14:12 +01:00
|
|
|
|
2022-12-01 09:57:56 +01:00
|
|
|
nix.nixPath = [
|
|
|
|
"nixpkgs=${pkgs.path}"
|
|
|
|
];
|
2021-11-22 22:18:28 +01:00
|
|
|
|
2022-12-01 09:57:56 +01:00
|
|
|
nix.settings.substituters = lib.mkForce [];
|
2022-01-26 00:03:58 +01:00
|
|
|
|
2022-12-01 09:57:56 +01:00
|
|
|
virtualisation = {
|
2023-01-28 07:44:41 +01:00
|
|
|
memorySize = 4096;
|
2022-12-01 09:57:56 +01:00
|
|
|
writableStore = true;
|
|
|
|
additionalPaths = [
|
|
|
|
"${pkgs.path}"
|
|
|
|
] ++ lib.optionals (prebuiltTarget != null) [
|
|
|
|
prebuiltSystem
|
|
|
|
(inputClosureOf prebuiltSystem)
|
2021-11-22 10:52:35 +01:00
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2022-12-01 09:57:56 +01:00
|
|
|
services.openssh.enable = true;
|
|
|
|
users.users.root.openssh.authorizedKeys.keys = [
|
|
|
|
sshKeys.snakeOilPublicKey
|
|
|
|
];
|
|
|
|
|
|
|
|
environment.systemPackages = with pkgs; [
|
|
|
|
git # for git flake tests
|
|
|
|
inotify-tools # for key services build
|
|
|
|
|
|
|
|
# HACK: copy stderr to both stdout and stderr
|
|
|
|
# (the test framework only captures stdout, and only stderr appears on screen during the build)
|
|
|
|
(writeShellScriptBin "run-copy-stderr" ''
|
|
|
|
exec "$@" 2> >(tee /dev/stderr)
|
|
|
|
'')
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
# Setup for target nodes
|
|
|
|
#
|
|
|
|
# Kept as minimal as possible.
|
|
|
|
targetConfig = { lib, ... }: {
|
2022-12-01 09:57:56 +01:00
|
|
|
nix.settings.substituters = lib.mkForce [];
|
2022-12-01 09:57:56 +01:00
|
|
|
|
|
|
|
documentation.nixos.enable = lib.mkOverride 60 true;
|
|
|
|
|
|
|
|
services.openssh.enable = true;
|
|
|
|
users.users.root.openssh.authorizedKeys.keys = [
|
|
|
|
sshKeys.snakeOilPublicKey
|
|
|
|
];
|
|
|
|
virtualisation.writableStore = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
nodes = let
|
2021-12-05 10:14:12 +01:00
|
|
|
deployerNodes = map (name: lib.nameValuePair name deployerConfig) deployers;
|
|
|
|
targetNodes = map (name: lib.nameValuePair name targetConfig) targets;
|
|
|
|
in listToAttrs (deployerNodes ++ targetNodes);
|
|
|
|
|
2022-12-01 09:57:56 +01:00
|
|
|
# A "shallow" re-evaluation of the test for use from Colmena
|
|
|
|
standaloneTest = evalTest ({ ... }: {
|
|
|
|
inherit nodes;
|
|
|
|
});
|
|
|
|
|
|
|
|
prebuiltSystem = standaloneTest.config.nodes.${prebuiltTarget}.system.build.toplevel;
|
|
|
|
|
|
|
|
getStandaloneConfigFor = node: { lib, config, ... }: {
|
|
|
|
imports = [
|
|
|
|
(pkgs.path + "/nixos/lib/testing/nixos-test-base.nix")
|
|
|
|
(if elem node deployers then deployerConfig else targetConfig)
|
|
|
|
standaloneTest.config.nodes.${node}.system.build.networkConfig
|
2021-11-22 10:52:35 +01:00
|
|
|
];
|
|
|
|
|
2022-01-28 03:48:25 +01:00
|
|
|
documentation.nixos.enable = lib.mkOverride 55 false;
|
2021-11-22 10:52:35 +01:00
|
|
|
boot.loader.grub.enable = false;
|
|
|
|
system.nixos.revision = lib.mkForce "constant-nixos-revision";
|
|
|
|
|
|
|
|
nix.nixPath = lib.mkForce [ "nixpkgs=/nixpkgs" ];
|
|
|
|
|
|
|
|
deployment.tags = lib.optional (config.networking.hostName != "deployer") "target";
|
|
|
|
};
|
|
|
|
in {
|
2021-12-05 10:14:12 +01:00
|
|
|
inherit pkgs nodes colmena colmenaExec
|
2022-12-01 18:55:27 +01:00
|
|
|
getStandaloneConfigFor inputClosureOf;
|
2022-12-01 09:57:56 +01:00
|
|
|
|
|
|
|
runTest = module: (evalTest ({ config, ... }: {
|
|
|
|
imports = [ module { inherit nodes; } ];
|
|
|
|
result = config.test;
|
|
|
|
})).config.result;
|
2021-11-22 10:52:35 +01:00
|
|
|
}
|