Merge pull request #18 from MatthewCroughan/mc/fix-tests

tests/vmTest: fix and deduplicate code
This commit is contained in:
MatthewCroughan 2022-05-24 04:53:42 +01:00 committed by GitHub
commit 88b928100b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 89 additions and 75 deletions

View file

@ -14,6 +14,7 @@
nixpkgsFor = forAllSystems (system: pkgs: import nixpkgs { inherit system; overlays = [ self.overlay ]; }); nixpkgsFor = forAllSystems (system: pkgs: import nixpkgs { inherit system; overlays = [ self.overlay ]; });
in rec in rec
{ {
herculesCI.ciSystems = [ "x86_64-linux" ];
overlay = final: prev: { overlay = final: prev: {
generateApps = flake: generateApps = flake:
let let

14
tests/vmTest/common.nix Normal file
View file

@ -0,0 +1,14 @@
# Configuration that will be added to both the nixinatee node and the nixinator
# node.
{ inputs }:
{
nix = {
extraOptions =
let empty_registry = builtins.toFile "empty-flake-registry.json" ''{"flakes":[],"version":2}''; in
''
experimental-features = nix-command flakes
flake-registry = ${empty_registry}
'';
registry.nixpkgs.flake = inputs.nixpkgs;
};
}

View file

@ -22,86 +22,85 @@ let
((import (path + "/flake.nix")).outputs (inputs // {self = r;})); ((import (path + "/flake.nix")).outputs (inputs // {self = r;}));
in in
r; r;
exampleFlake = pkgs.writeTextFile { mkNixinateTest = buildOn:
name = "nixinate-example-flake"; let
destination = "/flake.nix"; exampleFlake = pkgs.writeTextFile {
text = '' name = "nixinate-example-flake";
{ destination = "/flake.nix";
outputs = { self, nixpkgs }: text = ''
let
makeTest = (import (nixpkgs + "/nixos/lib/testing-python.nix") { system = "${pkgs.hostPlatform.system}"; }).makeTest;
baseConfig = ((makeTest { nodes.baseConfig = { ... }: {}; testScript = "";}).nodes {}).baseConfig.extendModules {
modules = [
${builtins.readFile ./nixinateeBase.nix}
${builtins.readFile ./nixinateeAdditional.nix}
{
_module.args.nixinate = {
host = "nixinatee";
sshUser = "nixinator";
buildOn = "local"; # valid args are "local" or "remote"
};
}
];
};
in
{ {
nixosConfigurations = { outputs = { self, nixpkgs }:
nixinatee = baseConfig; let
}; makeTest = (import (nixpkgs + "/nixos/lib/testing-python.nix") { system = "${pkgs.hostPlatform.system}"; }).makeTest;
baseConfig = ((makeTest { nodes.baseConfig = { ... }: {}; testScript = "";}).nodes {}).baseConfig.extendModules {
modules = [
${builtins.readFile ./nixinateeBase.nix}
${builtins.readFile ./nixinateeAdditional.nix}
{
_module.args.nixinate = {
host = "nixinatee";
sshUser = "nixinator";
buildOn = "${buildOn}"; # valid args are "local" or "remote"
};
}
];
};
in
{
nixosConfigurations = {
nixinatee = baseConfig;
};
};
}
'';
};
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;
additionalPaths = [] ++ pkgs.lib.optional (buildOn == "remote") (allDrvOutputs exampleSystem);
};
};
nixinator = { ... }: {
imports = [
(import ./common.nix { inherit inputs; })
];
virtualisation = {
additionalPaths = [
(allDrvOutputs exampleSystem)
]
++ pkgs.lib.optional (buildOn == "remote") exampleFlake;
}; };
}
'';
};
deployScript = inputs.self.nixinate.${pkgs.hostPlatform.system} (callLocklessFlake "${exampleFlake}" { nixpkgs = inputs.nixpkgs; });
exampleSystem = (callLocklessFlake "${exampleFlake}" { nixpkgs = inputs.nixpkgs; }).nixosConfigurations.nixinatee.config.system.build.toplevel;
mkNixinateTest = buildOn: makeTest {
nodes = {
nixinatee = { ... }: {
imports = [
./nixinateeBase.nix
];
virtualisation = {
writableStore = true;
};
};
nixinator = { ... }: {
virtualisation = {
additionalPaths = [
(allDrvOutputs exampleSystem)
]
++ pkgs.lib.optional (buildOn == "remote") exampleFlake;
};
nix = {
extraOptions =
let empty_registry = builtins.toFile "empty-flake-registry.json" ''{"flakes":[],"version":2}''; in
''
experimental-features = nix-command flakes
flake-registry = ${empty_registry}
'';
registry.nixpkgs.flake = inputs.nixpkgs;
}; };
}; };
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")
nixinator.succeed("exec ${deployScript.nixinate.nixinatee.program} >&2")
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"
)
'';
}; };
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")
nixinator.succeed("exec ${deployScript.nixinate.nixinatee.program} >&2")
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"
)
'';
};
in in
{ {
local = (mkNixinateTest "local"); local = (mkNixinateTest "local");