1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
{
pkgs,
myPkgs,
lib,
nixos-lib,
extraModules,
...
}: {
name,
configuration,
description,
hash,
testData,
}:
nixos-lib.runTest {
hostPkgs = pkgs; # the Nixpkgs package set used outside the VMs
inherit name;
node = {
specialArgs = {
inherit myPkgs;
};
# Use the nixpkgs as constructed by the `nixpkgs.*` options
pkgs = null;
};
nodes = {
machine = {config, ...}: {
imports =
[
extraModules.home-manager
../../modules/by-name/us/users/module.nix
]
++ configuration.imports;
config = lib.modules.mkMerge [
{
soispha = {
users = {
enable = true;
# The password is 'test'.
hashedPassword = "$y$j9T$yYYUpE9OaxmEO8MaPI2jr0$WGpYYWaLySakI.Mwqz4sljGSOetAp4s5CIUa1VUU1l2";
};
};
home-manager.users.soispha.home.stateVersion = "24.11";
}
configuration.config
];
};
};
testScript = {nodes, ...}: let
testDir = "${nodes.machine.home-manager.users.soispha.home.homeDirectory}/test";
goldenFile = "${testDir}/__test_golden";
logFile = "${testDir}/__test_log";
in
/*
python
*/
''
start_all()
machine.succeed("sudo -u soispha ${pkgs.writeShellScript "mkTestEnvironment" ''
set -e
mkdir --parents "${testDir}"
cd "${testDir}"
${
lib.strings.concatStringsSep "\n\n" (
builtins.attrValues
(builtins.mapAttrs
(name: value: ''
mkdir --parents "$(dirname '${name}')"
cp --recursive '${value}' '${name}'
'')
testData)
)
}
''}")
machine.succeed("sudo -u soispha ${pkgs.writeShellScript "mkGoldenRecord" ''
set -ex
# HACK: Prevent zsh from complaining about missing configuration.
# TODO: These tests should probably just run under bash. <2024-11-22>
touch ~soispha/.zshrc
cd "${testDir}"
__TEST_TMUX="${lib.getExe pkgs.tmux}"
__TEST_TMUX_PANE="__TEST_TMUX_PANE"
__TEST_EVAL_AWK_CLEAN_FILE="${./clean.awk}"
__TEST_EVAL_LOG_FILE="${logFile}"
__TEST_EVAL_GOLDEN_FILE="$(mktemp)"
printf "%s" "${goldenFile}" >"$__TEST_EVAL_GOLDEN_FILE"
. ${./driver.sh}
"$__TEST_TMUX" new-session -d -s "$__TEST_TMUX_PANE"
"$__TEST_TMUX" pipe-pane -t "$__TEST_TMUX_PANE" -o 'cat >>${goldenFile}'
__test_eval "${description}"
# Clear the pipe again
"$__TEST_TMUX" pipe-pane -t "$__TEST_TMUX_PANE"
# Check if the golden file was changed.
if [ "$(cat "$__TEST_EVAL_GOLDEN_FILE")" != "${goldenFile}" ]; then
mv "$(cat "$__TEST_EVAL_GOLDEN_FILE")" "${goldenFile}"
fi
rm "$__TEST_EVAL_GOLDEN_FILE"
''}")
machine.succeed("sudo -u soispha ${pkgs.writeShellScript "testHashOfGolden" (''
set -e
cd "${testDir}"
''
+ lib.optionalString (hash != null)
/*
bash
*/
''
golden_hash="$(sha256sum ${goldenFile} | awk '{print $1}')"
if [ "$golden_hash" != "${hash}" ]; then
echo "Hash mismatch."
echo "Expected '${hash}',"
echo "but got '$golden_hash'"
exit 1
else
echo "Hash was successfully checked."
fi
'')}")
machine.copy_from_vm("${goldenFile}", "golden")
machine.copy_from_vm("${logFile}", "log")
'';
}
|