2023-12-11 20:37:06 +01:00
|
|
|
{ pkgs
|
|
|
|
, makeTest
|
|
|
|
, inputs
|
|
|
|
,
|
|
|
|
}:
|
2022-03-31 20:36:59 +01:00
|
|
|
let
|
2022-05-23 18:57:50 +01:00
|
|
|
inherit (pkgs) lib;
|
2022-03-31 20:36:59 +01:00
|
|
|
# Return a store path with a closure containing everything including
|
|
|
|
# derivations and all build dependency outputs, all the way down.
|
|
|
|
allDrvOutputs = pkg:
|
2023-12-11 20:37:06 +01:00
|
|
|
let
|
|
|
|
name = "allDrvOutputs-${pkg.pname or pkg.name or "unknown"}";
|
2022-03-31 20:36:59 +01:00
|
|
|
in
|
|
|
|
pkgs.runCommand name { refs = pkgs.writeReferencesToFile pkg.drvPath; } ''
|
|
|
|
touch $out
|
|
|
|
while read ref; do
|
|
|
|
case $ref in
|
|
|
|
*.drv)
|
|
|
|
cat $ref >>$out
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done <$refs
|
|
|
|
'';
|
|
|
|
# Imports a flake with inputs passed in by hand, rather than
|
|
|
|
# builtins.getFlake, which cannot be used in this way.
|
2023-12-11 20:37:06 +01:00
|
|
|
callLocklessFlake = path: inputs:
|
|
|
|
let
|
|
|
|
r =
|
|
|
|
{ outPath = path; }
|
|
|
|
// ((import (path + "/flake.nix")).outputs (inputs // { self = r; }));
|
|
|
|
in
|
2022-03-31 20:36:59 +01:00
|
|
|
r;
|
2023-12-11 20:37:06 +01:00
|
|
|
mkNixinateTest =
|
|
|
|
{ buildOn
|
|
|
|
, hermetic ? false
|
|
|
|
, ...
|
|
|
|
}:
|
2022-05-24 02:39:45 +01:00
|
|
|
let
|
|
|
|
exampleFlake = pkgs.writeTextFile {
|
|
|
|
name = "nixinate-example-flake";
|
|
|
|
destination = "/flake.nix";
|
|
|
|
text = ''
|
2022-03-31 20:36:59 +01:00
|
|
|
{
|
2022-05-24 02:39:45 +01:00
|
|
|
outputs = { self, nixpkgs }:
|
|
|
|
let
|
|
|
|
makeTest = (import (nixpkgs + "/nixos/lib/testing-python.nix") { system = "${pkgs.hostPlatform.system}"; }).makeTest;
|
2022-05-24 05:06:57 +01:00
|
|
|
baseConfig = ((makeTest { nodes.baseConfig = { ... }: {}; testScript = "";}).nodes).baseConfig.extendModules {
|
2022-05-24 02:39:45 +01:00
|
|
|
modules = [
|
|
|
|
${builtins.readFile ./nixinateeBase.nix}
|
|
|
|
${builtins.readFile ./nixinateeAdditional.nix}
|
|
|
|
{
|
|
|
|
_module.args.nixinate = {
|
|
|
|
host = "nixinatee";
|
|
|
|
sshUser = "nixinator";
|
|
|
|
buildOn = "${buildOn}"; # valid args are "local" or "remote"
|
2022-05-23 18:57:50 +01:00
|
|
|
hermetic = ${lib.boolToString hermetic}; # valid args are true or false
|
2022-05-24 02:39:45 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
|
|
|
in
|
|
|
|
{
|
|
|
|
nixosConfigurations = {
|
|
|
|
nixinatee = baseConfig;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
'';
|
2022-03-31 20:36:59 +01:00
|
|
|
};
|
2022-05-24 02:39:45 +01:00
|
|
|
deployScript = inputs.self.nixinate.${pkgs.hostPlatform.system} (callLocklessFlake "${exampleFlake}" { nixpkgs = inputs.nixpkgs; });
|
|
|
|
exampleSystem = (callLocklessFlake "${exampleFlake}" { nixpkgs = inputs.nixpkgs; }).nixosConfigurations.nixinatee.config.system.build.toplevel;
|
|
|
|
in
|
|
|
|
makeTest {
|
|
|
|
nodes = {
|
|
|
|
nixinatee = { ... }: {
|
|
|
|
imports = [
|
|
|
|
./nixinateeBase.nix
|
|
|
|
(import ./common.nix { inherit inputs; })
|
|
|
|
];
|
|
|
|
virtualisation = {
|
|
|
|
writableStore = true;
|
2023-12-11 20:37:06 +01:00
|
|
|
additionalPaths =
|
|
|
|
[ ]
|
2022-05-23 18:57:50 +01:00
|
|
|
++ lib.optional (buildOn == "remote") (allDrvOutputs exampleSystem)
|
2022-10-28 01:31:08 +01:00
|
|
|
++ lib.optional (hermetic == true) (pkgs.nixinate.nixos-rebuild.drvPath)
|
|
|
|
++ lib.optional (hermetic == true) (pkgs.flock.drvPath);
|
2022-05-24 02:39:45 +01:00
|
|
|
};
|
2022-04-21 02:08:02 +01:00
|
|
|
};
|
2022-05-24 02:39:45 +01:00
|
|
|
nixinator = { ... }: {
|
|
|
|
imports = [
|
|
|
|
(import ./common.nix { inherit inputs; })
|
|
|
|
];
|
|
|
|
virtualisation = {
|
2023-12-11 20:37:06 +01:00
|
|
|
additionalPaths =
|
|
|
|
[
|
|
|
|
(allDrvOutputs exampleSystem)
|
|
|
|
]
|
2022-10-28 01:31:08 +01:00
|
|
|
++ lib.optional (buildOn == "remote") exampleFlake
|
|
|
|
++ lib.optional (hermetic == true) pkgs.flock.drvPath;
|
2022-05-24 02:39:45 +01:00
|
|
|
};
|
2022-04-21 02:08:02 +01:00
|
|
|
};
|
2022-03-31 20:36:59 +01:00
|
|
|
};
|
2023-12-11 20:37:06 +01:00
|
|
|
testScript = ''
|
|
|
|
start_all()
|
|
|
|
nixinatee.wait_for_unit("sshd.service")
|
|
|
|
nixinator.wait_for_unit("multi-user.target")
|
|
|
|
nixinator.succeed("mkdir ~/.ssh/")
|
|
|
|
nixinator.succeed("ssh-keyscan -H nixinatee >> ~/.ssh/known_hosts")
|
2023-12-11 21:13:29 +01:00
|
|
|
nixinator.succeed("exec ${deployScript.nixinate.nixinatee.program} >&2")
|
2023-12-11 20:37:06 +01:00
|
|
|
nixinatee.wait_for_unit("nginx.service")
|
|
|
|
nixinatee.wait_for_open_port("80")
|
|
|
|
with subtest("Check that Nginx webserver can be reached by deployer after deployment"):
|
|
|
|
assert "<title>Welcome to nginx!</title>" in nixinator.succeed(
|
|
|
|
"curl -sSf http:/nixinatee/ | grep title"
|
|
|
|
)
|
|
|
|
with subtest("Check that Nginx webserver can be reached by deployee after deployment"):
|
|
|
|
assert "<title>Welcome to nginx!</title>" in nixinatee.succeed(
|
|
|
|
"curl -sSf http:/127.0.0.1/ | grep title"
|
|
|
|
)
|
|
|
|
'';
|
2022-03-31 20:36:59 +01:00
|
|
|
};
|
2022-04-21 02:08:02 +01:00
|
|
|
in
|
|
|
|
{
|
2023-12-11 20:37:06 +01:00
|
|
|
local = mkNixinateTest { buildOn = "local"; };
|
|
|
|
remote = mkNixinateTest { buildOn = "remote"; };
|
|
|
|
localHermetic = mkNixinateTest {
|
|
|
|
buildOn = "local";
|
|
|
|
hermetic = true;
|
|
|
|
};
|
|
|
|
remoteHermetic = mkNixinateTest {
|
|
|
|
buildOn = "remote";
|
|
|
|
hermetic = true;
|
|
|
|
};
|
2022-03-31 20:36:59 +01:00
|
|
|
}
|