records: Improve reporting of "invalid field specifier" errors.
[jackhill/guix/guix.git] / tests / guix-system.sh
1 # GNU Guix --- Functional package management for GNU
2 # Copyright © 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
3 # Copyright © 2017 Tobias Geerinckx-Rice <me@tobias.gr>
4 # Copyright © 2018 Chris Marusich <cmmarusich@gmail.com>
5 #
6 # This file is part of GNU Guix.
7 #
8 # GNU Guix is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or (at
11 # your option) any later version.
12 #
13 # GNU Guix is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20
21 #
22 # Test 'guix system', mostly error reporting.
23 #
24
25 set -e
26
27 guix system --version
28
29 tmpfile="t-guix-system-$$"
30 errorfile="t-guix-system-error-$$"
31
32 # Note: This directory is chosen outside $builddir so that relative file name
33 # canonicalization doesn't mess up with 'current-source-directory', used by
34 # 'local-file' ('load' forces 'relative' for
35 # %FILE-PORT-NAME-CANONICALIZATION.)
36 tmpdir="${TMPDIR:-/tmp}/t-guix-system-$$"
37 mkdir "$tmpdir"
38
39 trap 'rm -f "$tmpfile" "$errorfile" "$tmpdir"/*; rmdir "$tmpdir"' EXIT
40
41 # Reporting of syntax errors.
42
43 cat > "$tmpfile"<<EOF
44 ;; This is line 1, and the next one is line 2.
45 (operating-system)
46 ;; The 'T' is at column 3.
47 EOF
48
49 if guix system vm "$tmpfile" 2> "$errorfile"
50 then
51 # This must not succeed.
52 exit 1
53 else
54 grep "$tmpfile:2:3:.*missing.* initializers" "$errorfile"
55 fi
56
57
58 cat > "$tmpfile"<<EOF
59 ;; This is line 1, and the next one is line 2.
60 (operating-system
61 ;; This is line 3, and there is no closing paren!
62 EOF
63
64 if guix system vm "$tmpfile" 2> "$errorfile"
65 then
66 # This must not succeed.
67 exit 1
68 else
69 grep "$tmpfile:4:1: missing closing paren" "$errorfile"
70 fi
71
72
73 # Reporting of module not found errors.
74
75 cat > "$tmpfile" <<EOF
76 ;; Line 1.
77 (use-modules (gnu))
78 (use-service-modules openssh)
79 EOF
80
81 if guix system build "$tmpfile" -n 2> "$errorfile"
82 then false
83 else
84 grep "$tmpfile:3:2: .*module .*openssh.*not found" "$errorfile"
85 grep "Try.*use-service-modules ssh" "$errorfile"
86 fi
87
88 cat > "$tmpfile" <<EOF
89 ;; Line 1.
90 (use-modules (gnu))
91 (use-package-modules qemu)
92 EOF
93
94 if guix system build "$tmpfile" -n 2> "$errorfile"
95 then false
96 else
97 grep "$tmpfile:3:2: .*module .*qemu.*not found" "$errorfile"
98 grep "Try.*use-package-modules virtualization" "$errorfile"
99 fi
100
101 # Reporting of unbound variables.
102
103 cat > "$tmpfile" <<EOF
104 (use-modules (gnu)) ; 1
105 (use-service-modules networking) ; 2
106
107 (operating-system ; 4
108 (host-name "antelope") ; 5
109 (timezone "Europe/Paris") ; 6
110 (locale "en_US.UTF-8") ; 7
111
112 (bootloader (GRUB-config (target "/dev/sdX"))) ; 9
113 (file-systems (cons (file-system
114 (device (file-system-label "root"))
115 (mount-point "/")
116 (type "ext4"))
117 %base-file-systems)))
118 EOF
119
120 if guix system build "$tmpfile" -n 2> "$errorfile"
121 then false
122 else
123 if test "`guile -c '(display (effective-version))'`" = 2.2
124 then
125 # FIXME: With Guile 2.2.0 the error is reported on line 4.
126 # See <http://bugs.gnu.org/26107>.
127 grep "$tmpfile:[49]:[0-9]\+:.*GRUB-config.*[Uu]nbound variable" "$errorfile"
128 else
129 grep "$tmpfile:9:[0-9]\+:.*GRUB-config.*[Uu]nbound variable" "$errorfile"
130 fi
131 fi
132
133 cat > "$tmpfile" <<EOF
134 (use-modules (gnu)) ; 1
135
136 (operating-system ; 3
137 (file-systems (cons (file-system ; 4
138 (device (file-system-label "root"))
139 (mount-point "/") ; 6
140 (type "ext4")))) ; 7 (!!)
141 %base-file-systems)
142 EOF
143
144 if guix system build "$tmpfile" -n 2> "$errorfile"
145 then false
146 else
147 # Here '%base-file-systems' appears as if it were a field specified of the
148 # enclosing 'operating-system' form due to parenthesis mismatch.
149 grep "$tmpfile:3:[0-9]\+:.*%base-file-system.*invalid field specifier" \
150 "$errorfile"
151 fi
152
153 OS_BASE='
154 (host-name "antelope")
155 (timezone "Europe/Paris")
156 (locale "en_US.UTF-8")
157
158 (bootloader (bootloader-configuration
159 (bootloader grub-bootloader)
160 (target "/dev/sdX")))
161 (file-systems (cons (file-system
162 (device (file-system-label "root"))
163 (mount-point "/")
164 (type "ext4"))
165 %base-file-systems))
166 '
167
168 # Reporting of duplicate service identifiers.
169
170 cat > "$tmpfile" <<EOF
171 (use-modules (gnu))
172 (use-service-modules networking)
173
174 (operating-system
175 $OS_BASE
176 (services (cons* (service dhcp-client-service-type)
177 (service dhcp-client-service-type) ;twice!
178 %base-services)))
179 EOF
180
181 if guix system vm "$tmpfile" 2> "$errorfile"
182 then
183 # This must not succeed.
184 exit 1
185 else
186 grep "service 'networking'.*more than once" "$errorfile"
187 fi
188
189 # Reporting unmet shepherd requirements.
190
191 cat > "$tmpfile" <<EOF
192 (use-modules (gnu) (gnu services shepherd))
193 (use-service-modules networking)
194
195 (define buggy-service-type
196 (shepherd-service-type
197 'buggy
198 (lambda _
199 (shepherd-service
200 (provision '(buggy!))
201 (requirement '(does-not-exist))
202 (start #t)))))
203
204 (operating-system
205 $OS_BASE
206 (services (cons (service buggy-service-type #t)
207 %base-services)))
208 EOF
209
210 if guix system build "$tmpfile" 2> "$errorfile"
211 then
212 exit 1
213 else
214 grep "service 'buggy!'.*'does-not-exist'.*not provided" "$errorfile"
215 fi
216
217 # Reporting inconsistent user accounts.
218
219 make_user_config ()
220 {
221 cat > "$tmpfile" <<EOF
222 (use-modules (gnu))
223 (use-service-modules networking)
224
225 (operating-system
226 (host-name "antelope")
227 (timezone "Europe/Paris")
228 (locale "en_US.UTF-8")
229
230 (bootloader (bootloader-configuration
231 (bootloader grub-bootloader)
232 (target "/dev/sdX")))
233 (file-systems (cons (file-system
234 (device (file-system-label "root"))
235 (mount-point "/")
236 (type "ext4"))
237 %base-file-systems))
238 (users (list (user-account
239 (name "dave")
240 (home-directory "/home/dave")
241 (group "$1")
242 (supplementary-groups '("$2"))))))
243 EOF
244 }
245
246 make_user_config "users" "wheel"
247 guix system build "$tmpfile" -n # succeeds
248
249 guix system build "$tmpfile" -d # succeeds
250 guix system build "$tmpfile" -d | grep '\.drv$'
251
252 guix system vm "$tmpfile" -d # succeeds
253 guix system vm "$tmpfile" -d | grep '\.drv$'
254
255 # Make sure the behavior is deterministic (<https://bugs.gnu.org/32652>).
256 drv1="`guix system vm "$tmpfile" -d`"
257 drv2="`guix system vm "$tmpfile" -d`"
258 test "$drv1" = "$drv2"
259 drv1="`guix system disk-image --file-system-type=iso9660 "$tmpfile" -d`"
260 drv2="`guix system disk-image --file-system-type=iso9660 "$tmpfile" -d`"
261 test "$drv1" = "$drv2"
262
263 make_user_config "group-that-does-not-exist" "users"
264 if guix system build "$tmpfile" -n 2> "$errorfile"
265 then false
266 else grep "primary group.*group-that-does-not-exist.*undeclared" "$errorfile"; fi
267
268 make_user_config "users" "group-that-does-not-exist"
269 if guix system build "$tmpfile" -n 2> "$errorfile"
270 then false
271 else grep "supplementary group.*group-that-does-not-exist.*undeclared" "$errorfile"; fi
272
273 # Try 'local-file' and relative file name resolution.
274
275 cat > "$tmpdir/config.scm"<<EOF
276 (use-modules (gnu))
277 (use-service-modules networking)
278
279 (operating-system
280 $OS_BASE
281 (services (cons (service tor-service-type
282 (tor-configuration
283 (config-file (local-file "my-torrc"))))
284 %base-services)))
285 EOF
286
287 cat > "$tmpdir/my-torrc"<<EOF
288 # This is an example file.
289 EOF
290
291 # In both cases 'my-torrc' should be properly resolved.
292 guix system build "$tmpdir/config.scm" -n
293 (cd "$tmpdir"; guix system build "config.scm" -n)
294
295 # Searching.
296 guix system search tor | grep "^name: tor"
297 guix system search tor | grep "^shepherdnames: tor"
298 guix system search anonym network | grep "^name: tor"
299
300 # Below, use -n (--dry-run) for the tests because if we actually tried to
301 # build these images, the commands would take hours to run in the worst case.
302
303 # Verify that the examples can be built.
304 for example in gnu/system/examples/*.tmpl; do
305 guix system -n disk-image "$example"
306 done
307
308 # Verify that the disk image types can be built.
309 guix system -n vm gnu/system/examples/vm-image.tmpl
310 guix system -n vm-image gnu/system/examples/vm-image.tmpl
311 # This invocation was taken care of in the loop above:
312 # guix system -n disk-image gnu/system/examples/bare-bones.tmpl
313 guix system -n disk-image --file-system-type=iso9660 gnu/system/examples/bare-bones.tmpl
314 guix system -n docker-image gnu/system/examples/docker-image.tmpl