Skip to content

Commit 0239894

Browse files
committed
nix: initial testing framework
1 parent abf1465 commit 0239894

File tree

3 files changed

+157
-28
lines changed

3 files changed

+157
-28
lines changed

checks/nixos/test.nix

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
{
2+
testers,
3+
writeText,
4+
...
5+
}:
6+
testers.runNixOSTest {
7+
name = "nh-nixos-test";
8+
nodes.machine = {
9+
lib,
10+
pkgs,
11+
...
12+
}: {
13+
imports = [
14+
../vm.nix
15+
];
16+
17+
nix.settings = {
18+
substituters = lib.mkForce [];
19+
hashed-mirrors = null;
20+
connect-timeout = 1;
21+
};
22+
23+
# Indicate parent config
24+
environment.systemPackages = [
25+
(pkgs.writeShellScriptBin "parent" "")
26+
];
27+
28+
programs.nh = {
29+
enable = true;
30+
flake = "/etc/nixos";
31+
};
32+
};
33+
34+
testScript = {nodes, ...}: let
35+
newConfig =
36+
writeText "configuration.nix" # nix
37+
38+
''
39+
{ lib, pkgs, ... }: {
40+
imports = [
41+
./hardware-configuration.nix
42+
<nixpkgs/nixos/modules/testing/test-instrumentation.nix>
43+
];
44+
45+
boot.loader.grub = {
46+
enable = true;
47+
device = "/dev/vda";
48+
forceInstall = true;
49+
};
50+
51+
documentation.enable = false;
52+
53+
environment.systemPackages = [
54+
(pkgs.writeShellScriptBin "parent" "")
55+
];
56+
57+
58+
specialisation.foo = {
59+
inheritParentConfig = true;
60+
61+
configuration = {...}: {
62+
environment.etc."specialisation".text = "foo";
63+
};
64+
};
65+
66+
specialisation.bar = {
67+
inheritParentConfig = true;
68+
69+
configuration = {...}: {
70+
environment.etc."specialisation".text = "bar";
71+
};
72+
};
73+
}
74+
'';
75+
in
76+
# python
77+
''
78+
machine.start()
79+
machine.succeed("udevadm settle")
80+
machine.wait_for_unit("multi-user.target")
81+
82+
machine.succeed("nixos-generate-config --flake")
83+
machine.copy_from_host("${newConfig}", "/etc/nixos/configuration.nix")
84+
85+
with subtest("Switch to the base system"):
86+
machine.succeed("nh os switch --no-nom")
87+
machine.succeed("parent")
88+
machine.fail("cat /etc/specialisation/text | grep 'foo'")
89+
machine.fail("cat /etc/specialisation/text | grep 'bar'")
90+
91+
with subtest("Switch to the foo system"):
92+
machine.succeed("nh os switch --no-nom --specialisation foo")
93+
machine.succeed("parent")
94+
machine.succeed("cat /etc/specialisation/text | grep 'foo'")
95+
machine.fail("cat /etc/specialisation/text | grep 'bar'")
96+
97+
with subtest("Switch to the bar system"):
98+
machine.succeed("nh os switch --no-nom --specialisation bar")
99+
machine.succeed("parent")
100+
machine.fail("cat /etc/specialisation/text | grep 'foo'")
101+
machine.succeed("cat /etc/specialisation/text | grep 'bar'")
102+
103+
with subtest("Switch into specialization using `nh os test`"):
104+
machine.succeed("nh os test --specialisation foo")
105+
machine.succeed("parent")
106+
machine.succeed("foo")
107+
machine.fail("bar")
108+
'';
109+
}

checks/vm.nix

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
pkgs,
3+
lib,
4+
...
5+
}: {
6+
nix.settings = {
7+
substituters = lib.mkForce [];
8+
hashed-mirrors = null;
9+
connect-timeout = 1;
10+
};
11+
12+
environment.systemPackages = [pkgs.hello];
13+
14+
system.includeBuildDependencies = true;
15+
16+
virtualisation = {
17+
cores = 2;
18+
memorySize = 4096;
19+
};
20+
}

flake.nix

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,36 @@
33
nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.11";
44
};
55

6-
outputs =
7-
{
8-
self,
9-
nixpkgs,
10-
}:
11-
let
12-
forAllSystems =
13-
function:
14-
nixpkgs.lib.genAttrs [
15-
"x86_64-linux"
16-
"aarch64-linux"
17-
# experimental
18-
"x86_64-darwin"
19-
"aarch64-darwin"
20-
] (system: function nixpkgs.legacyPackages.${system});
6+
outputs = {
7+
self,
8+
nixpkgs,
9+
}: let
10+
forAllSystems = function:
11+
nixpkgs.lib.genAttrs [
12+
"x86_64-linux"
13+
"aarch64-linux"
14+
# experimental
15+
"x86_64-darwin"
16+
"aarch64-darwin"
17+
] (system: function nixpkgs.legacyPackages.${system});
2118

22-
rev = self.shortRev or self.dirtyShortRev or "dirty";
23-
in
24-
{
25-
overlays.default = final: prev: { nh = final.callPackage ./package.nix { inherit rev; }; };
19+
rev = self.shortRev or self.dirtyShortRev or "dirty";
20+
in {
21+
overlays.default = final: prev: {nh = final.callPackage ./package.nix {inherit rev;};};
2622

27-
packages = forAllSystems (pkgs: rec {
28-
nh = pkgs.callPackage ./package.nix { inherit rev; };
29-
default = nh;
30-
});
23+
packages = forAllSystems (pkgs: rec {
24+
nh = pkgs.callPackage ./package.nix {inherit rev;};
25+
default = nh;
26+
});
3127

32-
devShells = forAllSystems (pkgs: {
33-
default = import ./shell.nix { inherit pkgs; };
34-
});
28+
checks = forAllSystems (pkgs: {
29+
nixos = pkgs.callPackage ./checks/nixos/test.nix {};
30+
});
3531

36-
formatter = forAllSystems (pkgs: pkgs.nixfmt-rfc-style);
37-
};
32+
devShells = forAllSystems (pkgs: {
33+
default = import ./shell.nix {inherit pkgs;};
34+
});
35+
36+
formatter = forAllSystems (pkgs: pkgs.nixfmt-rfc-style);
37+
};
3838
}

0 commit comments

Comments
 (0)