gnu: diamond: Update to 0.8.23.
[jackhill/guix/guix.git] / gnu / tests / base.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2016 Ludovic Courtès <ludo@gnu.org>
3 ;;;
4 ;;; This file is part of GNU Guix.
5 ;;;
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
10 ;;;
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19 (define-module (gnu tests base)
20 #:use-module (gnu tests)
21 #:use-module (gnu system)
22 #:use-module (gnu system grub)
23 #:use-module (gnu system file-systems)
24 #:use-module (gnu system shadow)
25 #:use-module (gnu system nss)
26 #:use-module (gnu system vm)
27 #:use-module (gnu services)
28 #:use-module (gnu services base)
29 #:use-module (gnu services dbus)
30 #:use-module (gnu services avahi)
31 #:use-module (gnu services mcron)
32 #:use-module (gnu services shepherd)
33 #:use-module (gnu services networking)
34 #:use-module (guix gexp)
35 #:use-module (guix store)
36 #:use-module (guix monads)
37 #:use-module (guix packages)
38 #:use-module (srfi srfi-1)
39 #:export (run-basic-test
40 %test-basic-os
41 %test-mcron
42 %test-nss-mdns))
43
44 (define %simple-os
45 (operating-system
46 (host-name "komputilo")
47 (timezone "Europe/Berlin")
48 (locale "en_US.UTF-8")
49
50 (bootloader (grub-configuration (device "/dev/sdX")))
51 (file-systems (cons (file-system
52 (device "my-root")
53 (title 'label)
54 (mount-point "/")
55 (type "ext4"))
56 %base-file-systems))
57 (firmware '())
58
59 (users (cons (user-account
60 (name "alice")
61 (comment "Bob's sister")
62 (group "users")
63 (supplementary-groups '("wheel" "audio" "video"))
64 (home-directory "/home/alice"))
65 %base-user-accounts))))
66
67 \f
68 (define* (run-basic-test os command #:optional (name "basic"))
69 "Return a derivation called NAME that tests basic features of the OS started
70 using COMMAND, a gexp that evaluates to a list of strings. Compare some
71 properties of running system to what's declared in OS, an <operating-system>."
72 (define test
73 (with-imported-modules '((gnu build marionette))
74 #~(begin
75 (use-modules (gnu build marionette)
76 (srfi srfi-1)
77 (srfi srfi-26)
78 (srfi srfi-64)
79 (ice-9 match))
80
81 (define marionette
82 (make-marionette #$command))
83
84 (mkdir #$output)
85 (chdir #$output)
86
87 (test-begin "basic")
88
89 (test-assert "uname"
90 (match (marionette-eval '(uname) marionette)
91 (#("Linux" host-name version _ architecture)
92 (and (string=? host-name
93 #$(operating-system-host-name os))
94 (string-prefix? #$(package-version
95 (operating-system-kernel os))
96 version)
97 (string-prefix? architecture %host-type)))))
98
99 (test-assert "shell and user commands"
100 ;; Is everything in $PATH?
101 (zero? (marionette-eval '(system "
102 . /etc/profile
103 set -e -x
104 guix --version
105 ls --version
106 grep --version
107 info --version")
108 marionette)))
109
110 (test-assert "accounts"
111 (let ((users (marionette-eval '(begin
112 (use-modules (ice-9 match))
113 (let loop ((result '()))
114 (match (getpw)
115 (#f (reverse result))
116 (x (loop (cons x result))))))
117 marionette)))
118 (lset= string=?
119 (map passwd:name users)
120 (list
121 #$@(map user-account-name
122 (operating-system-user-accounts os))))))
123
124 (test-assert "shepherd services"
125 (let ((services (marionette-eval
126 '(begin
127 (use-modules (gnu services herd))
128
129 (map (compose car live-service-provision)
130 (current-services)))
131 marionette)))
132 (lset= eq?
133 (pk 'services services)
134 '(root #$@(operating-system-shepherd-service-names os)))))
135
136 (test-equal "login on tty1"
137 "root\n"
138 (begin
139 (marionette-control "sendkey ctrl-alt-f1" marionette)
140 ;; Wait for the 'term-tty1' service to be running (using
141 ;; 'start-service' is the simplest and most reliable way to do
142 ;; that.)
143 (marionette-eval
144 '(begin
145 (use-modules (gnu services herd))
146 (start-service 'term-tty1))
147 marionette)
148
149 ;; Now we can type.
150 (marionette-type "root\n\nid -un > logged-in\n" marionette)
151
152 ;; It can take a while before the shell commands are executed.
153 (let loop ((i 0))
154 (unless (or (file-exists? "/root/logged-in") (> i 15))
155 (sleep 1)
156 (loop (+ i 1))))
157 (marionette-eval '(use-modules (rnrs io ports)) marionette)
158 (marionette-eval '(call-with-input-file "/root/logged-in"
159 get-string-all)
160 marionette)))
161
162 (test-assert "host name resolution"
163 (match (marionette-eval
164 '(begin
165 ;; Wait for nscd or our requests go through it.
166 (use-modules (gnu services herd))
167 (start-service 'nscd)
168
169 (list (getaddrinfo "localhost")
170 (getaddrinfo #$(operating-system-host-name os))))
171 marionette)
172 ((((? vector?) ..1) ((? vector?) ..1))
173 #t)
174 (x
175 (pk 'failure x #f))))
176
177 (test-equal "host not found"
178 #f
179 (marionette-eval
180 '(false-if-exception (getaddrinfo "does-not-exist"))
181 marionette))
182
183 (test-equal "locale"
184 "en_US.utf8"
185 (marionette-eval '(begin
186 ;; XXX: This 'setenv' call wouldn't be needed
187 ;; but our glibc@2.23 currently ignores
188 ;; /run/current-system/locale.
189 (setenv "GUIX_LOCPATH"
190 "/run/current-system/locale")
191 (let ((before (setlocale LC_ALL "en_US.utf8")))
192 (setlocale LC_ALL before)))
193 marionette))
194
195 (test-assert "/run/current-system is a GC root"
196 (marionette-eval '(begin
197 ;; Make sure the (guix …) modules are found.
198 (eval-when (expand load eval)
199 (set! %load-path
200 (cons
201 (string-append
202 "/run/current-system/profile/share/guile/site/"
203 (effective-version))
204 %load-path))
205 (set! %load-compiled-path
206 (cons
207 (string-append
208 "/run/current-system/profile/share/guile/site/"
209 (effective-version))
210 %load-compiled-path)))
211
212 (use-modules (srfi srfi-34) (guix store))
213
214 (let ((system (readlink "/run/current-system")))
215 (guard (c ((nix-protocol-error? c)
216 (file-exists? system)))
217 (with-store store
218 (delete-paths store (list system))
219 #f))))
220 marionette))
221
222 ;; This symlink is currently unused, but better have it point to the
223 ;; right place. See
224 ;; <https://lists.gnu.org/archive/html/guix-devel/2016-08/msg01641.html>.
225 (test-equal "/var/guix/gcroots/profiles is a valid symlink"
226 "/var/guix/profiles"
227 (marionette-eval '(readlink "/var/guix/gcroots/profiles")
228 marionette))
229
230
231 (test-assert "screendump"
232 (begin
233 (marionette-control (string-append "screendump " #$output
234 "/tty1.ppm")
235 marionette)
236 (file-exists? "tty1.ppm")))
237
238 (test-end)
239 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
240
241 (gexp->derivation name test))
242
243 (define %test-basic-os
244 (system-test
245 (name "basic")
246 (description
247 "Instrument %SIMPLE-OS, run it in a VM, and run a series of basic
248 functionality tests.")
249 (value
250 (mlet* %store-monad ((os -> (marionette-operating-system
251 %simple-os
252 #:imported-modules '((gnu services herd)
253 (guix combinators))))
254 (run (system-qemu-image/shared-store-script
255 os #:graphic? #f)))
256 ;; XXX: Add call to 'virtualized-operating-system' to get the exact same
257 ;; set of services as the OS produced by
258 ;; 'system-qemu-image/shared-store-script'.
259 (run-basic-test (virtualized-operating-system os '())
260 #~(list #$run))))))
261
262 \f
263 ;;;
264 ;;; Mcron.
265 ;;;
266
267 (define %mcron-os
268 ;; System with an mcron service, with one mcron job for "root" and one mcron
269 ;; job for an unprivileged user (note: #:user is an 'mcron2' thing.)
270 (let ((job1 #~(job next-second-from
271 (lambda ()
272 (call-with-output-file "witness"
273 (lambda (port)
274 (display (list (getuid) (getgid)) port))))))
275 (job2 #~(job next-second-from
276 (lambda ()
277 (call-with-output-file "witness"
278 (lambda (port)
279 (display (list (getuid) (getgid)) port))))
280 #:user "alice"))
281 (job3 #~(job next-second-from ;to test $PATH
282 "touch witness-touch")))
283 (operating-system
284 (inherit %simple-os)
285 (services (cons (mcron-service (list job1 job2 job3))
286 (operating-system-user-services %simple-os))))))
287
288 (define (run-mcron-test name)
289 (mlet* %store-monad ((os -> (marionette-operating-system
290 %mcron-os
291 #:imported-modules '((gnu services herd)
292 (guix combinators))))
293 (command (system-qemu-image/shared-store-script
294 os #:graphic? #f)))
295 (define test
296 (with-imported-modules '((gnu build marionette))
297 #~(begin
298 (use-modules (gnu build marionette)
299 (srfi srfi-64)
300 (ice-9 match))
301
302 (define marionette
303 (make-marionette (list #$command)))
304
305 (define (wait-for-file file)
306 ;; Wait until FILE exists in the guest; 'read' its content and
307 ;; return it.
308 (marionette-eval
309 `(let loop ((i 10))
310 (cond ((file-exists? ,file)
311 (call-with-input-file ,file read))
312 ((> i 0)
313 (sleep 1)
314 (loop (- i 1)))
315 (else
316 (error "file didn't show up" ,file))))
317 marionette))
318
319 (mkdir #$output)
320 (chdir #$output)
321
322 (test-begin "mcron")
323
324 (test-eq "service running"
325 'running!
326 (marionette-eval
327 '(begin
328 (use-modules (gnu services herd))
329 (start-service 'mcron)
330 'running!)
331 marionette))
332
333 ;; Make sure root's mcron job runs, has its cwd set to "/root", and
334 ;; runs with the right UID/GID.
335 (test-equal "root's job"
336 '(0 0)
337 (wait-for-file "/root/witness"))
338
339 ;; Likewise for Alice's job. We cannot know what its GID is since
340 ;; it's chosen by 'groupadd', but it's strictly positive.
341 (test-assert "alice's job"
342 (match (wait-for-file "/home/alice/witness")
343 ((1000 gid)
344 (>= gid 100))))
345
346 ;; Last, the job that uses a command; allows us to test whether
347 ;; $PATH is sane. (Note that 'marionette-eval' stringifies objects
348 ;; that don't have a read syntax, hence the string.)
349 (test-equal "root's job with command"
350 "#<eof>"
351 (wait-for-file "/root/witness-touch"))
352
353 (test-end)
354 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
355
356 (gexp->derivation name test)))
357
358 (define %test-mcron
359 (system-test
360 (name "mcron")
361 (description "Make sure the mcron service works as advertised.")
362 (value (run-mcron-test name))))
363
364 \f
365 ;;;
366 ;;; Avahi and NSS-mDNS.
367 ;;;
368
369 (define %avahi-os
370 (operating-system
371 (inherit %simple-os)
372 (name-service-switch %mdns-host-lookup-nss)
373 (services (cons* (avahi-service #:debug? #t)
374 (dbus-service)
375 (dhcp-client-service) ;needed for multicast
376
377 ;; Enable heavyweight debugging output.
378 (modify-services (operating-system-user-services
379 %simple-os)
380 (nscd-service-type config
381 => (nscd-configuration
382 (inherit config)
383 (debug-level 3)
384 (log-file "/dev/console")))
385 (syslog-service-type config
386 =>
387 (syslog-configuration
388 (inherit config)
389 (config-file
390 (plain-file
391 "syslog.conf"
392 "*.* /dev/console\n")))))))))
393
394 (define (run-nss-mdns-test)
395 ;; Test resolution of '.local' names via libc. Start the marionette service
396 ;; *after* nscd. Failing to do that, libc will try to connect to nscd,
397 ;; fail, then never try again (see '__nss_not_use_nscd_hosts' in libc),
398 ;; leading to '.local' resolution failures.
399 (mlet* %store-monad ((os -> (marionette-operating-system
400 %avahi-os
401 #:requirements '(nscd)
402 #:imported-modules '((gnu services herd)
403 (guix combinators))))
404 (run (system-qemu-image/shared-store-script
405 os #:graphic? #f)))
406 (define mdns-host-name
407 (string-append (operating-system-host-name os)
408 ".local"))
409
410 (define test
411 (with-imported-modules '((gnu build marionette))
412 #~(begin
413 (use-modules (gnu build marionette)
414 (srfi srfi-1)
415 (srfi srfi-64)
416 (ice-9 match))
417
418 (define marionette
419 (make-marionette (list #$run)))
420
421 (mkdir #$output)
422 (chdir #$output)
423
424 (test-begin "avahi")
425
426 (test-assert "wait for services"
427 (marionette-eval
428 '(begin
429 (use-modules (gnu services herd))
430
431 (start-service 'nscd)
432
433 ;; XXX: Work around a race condition in nscd: nscd creates its
434 ;; PID file before it is listening on its socket.
435 (let ((sock (socket PF_UNIX SOCK_STREAM 0)))
436 (let try ()
437 (catch 'system-error
438 (lambda ()
439 (connect sock AF_UNIX "/var/run/nscd/socket")
440 (close-port sock)
441 (format #t "nscd is ready~%"))
442 (lambda args
443 (format #t "waiting for nscd...~%")
444 (usleep 500000)
445 (try)))))
446
447 ;; Wait for the other useful things.
448 (start-service 'avahi-daemon)
449 (start-service 'networking)
450
451 #t)
452 marionette))
453
454 (test-equal "avahi-resolve-host-name"
455 0
456 (marionette-eval
457 '(system*
458 "/run/current-system/profile/bin/avahi-resolve-host-name"
459 "-v" #$mdns-host-name)
460 marionette))
461
462 (test-equal "avahi-browse"
463 0
464 (marionette-eval
465 '(system* "avahi-browse" "-avt")
466 marionette))
467
468 (test-assert "getaddrinfo .local"
469 ;; Wait for the 'avahi-daemon' service and perform a resolution.
470 (match (marionette-eval
471 '(getaddrinfo #$mdns-host-name)
472 marionette)
473 (((? vector? addrinfos) ..1)
474 (pk 'getaddrinfo addrinfos)
475 (and (any (lambda (ai)
476 (= AF_INET (addrinfo:fam ai)))
477 addrinfos)
478 (any (lambda (ai)
479 (= AF_INET6 (addrinfo:fam ai)))
480 addrinfos)))))
481
482 (test-assert "gethostbyname .local"
483 (match (pk 'gethostbyname
484 (marionette-eval '(gethostbyname #$mdns-host-name)
485 marionette))
486 ((? vector? result)
487 (and (string=? (hostent:name result) #$mdns-host-name)
488 (= (hostent:addrtype result) AF_INET)))))
489
490
491 (test-end)
492 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
493
494 (gexp->derivation "nss-mdns" test)))
495
496 (define %test-nss-mdns
497 (system-test
498 (name "nss-mdns")
499 (description
500 "Test Avahi's multicast-DNS implementation, and in particular, test its
501 glibc name service switch (NSS) module.")
502 (value (run-nss-mdns-test))))