system: Create home directories once 'file-systems' is up.
[jackhill/guix/guix.git] / gnu / tests / base.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2016, 2017 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 (gnu packages imagemagick)
35 #:use-module (gnu packages ocr)
36 #:use-module (guix gexp)
37 #:use-module (guix store)
38 #:use-module (guix monads)
39 #:use-module (guix packages)
40 #:use-module (srfi srfi-1)
41 #:export (run-basic-test
42 %test-basic-os
43 %test-mcron
44 %test-nss-mdns))
45
46 (define %simple-os
47 (operating-system
48 (host-name "komputilo")
49 (timezone "Europe/Berlin")
50 (locale "en_US.UTF-8")
51
52 (bootloader (grub-configuration (device "/dev/sdX")))
53 (file-systems (cons (file-system
54 (device "my-root")
55 (title 'label)
56 (mount-point "/")
57 (type "ext4"))
58 %base-file-systems))
59 (firmware '())
60
61 (users (cons (user-account
62 (name "alice")
63 (comment "Bob's sister")
64 (group "users")
65 (supplementary-groups '("wheel" "audio" "video"))
66 (home-directory "/home/alice"))
67 %base-user-accounts))))
68
69 \f
70 (define* (run-basic-test os command #:optional (name "basic")
71 #:key initialization)
72 "Return a derivation called NAME that tests basic features of the OS started
73 using COMMAND, a gexp that evaluates to a list of strings. Compare some
74 properties of running system to what's declared in OS, an <operating-system>.
75
76 When INITIALIZATION is true, it must be a one-argument procedure that is
77 passed a gexp denoting the marionette, and it must return gexp that is
78 inserted before the first test. This is used to introduce an extra
79 initialization step, such as entering a LUKS passphrase."
80 (define test
81 (with-imported-modules '((gnu build marionette)
82 (guix build syscalls))
83 #~(begin
84 (use-modules (gnu build marionette)
85 (guix build syscalls)
86 (srfi srfi-1)
87 (srfi srfi-26)
88 (srfi srfi-64)
89 (ice-9 match))
90
91 (define marionette
92 (make-marionette #$command))
93
94 (mkdir #$output)
95 (chdir #$output)
96
97 (test-begin "basic")
98
99 #$(and initialization
100 (initialization #~marionette))
101
102 (test-assert "uname"
103 (match (marionette-eval '(uname) marionette)
104 (#("Linux" host-name version _ architecture)
105 (and (string=? host-name
106 #$(operating-system-host-name os))
107 (string-prefix? #$(package-version
108 (operating-system-kernel os))
109 version)
110 (string-prefix? architecture %host-type)))))
111
112 (test-assert "shell and user commands"
113 ;; Is everything in $PATH?
114 (zero? (marionette-eval '(system "
115 . /etc/profile
116 set -e -x
117 guix --version
118 ls --version
119 grep --version
120 info --version")
121 marionette)))
122
123 (test-assert "accounts"
124 (let ((users (marionette-eval '(begin
125 (use-modules (ice-9 match))
126 (let loop ((result '()))
127 (match (getpw)
128 (#f (reverse result))
129 (x (loop (cons x result))))))
130 marionette)))
131 (lset= string=?
132 (map passwd:name users)
133 (list
134 #$@(map user-account-name
135 (operating-system-user-accounts os))))))
136
137 (test-assert "shepherd services"
138 (let ((services (marionette-eval
139 '(begin
140 (use-modules (gnu services herd))
141
142 (map (compose car live-service-provision)
143 (current-services)))
144 marionette)))
145 (lset= eq?
146 (pk 'services services)
147 '(root #$@(operating-system-shepherd-service-names os)))))
148
149 (test-assert "homes"
150 (let ((homes
151 '#$(map user-account-home-directory
152 (filter user-account-create-home-directory?
153 (operating-system-user-accounts os)))))
154 (marionette-eval
155 `(begin
156 (use-modules (gnu services herd) (srfi srfi-1))
157
158 ;; Home directories are supposed to exist once 'user-homes'
159 ;; has been started.
160 (start-service 'user-homes)
161
162 (every (lambda (home)
163 (and (file-exists? home)
164 (file-is-directory? home)))
165 ',homes))
166 marionette)))
167
168 (test-assert "skeletons in home directories"
169 (let ((homes
170 '#$(filter-map (lambda (account)
171 (and (user-account-create-home-directory?
172 account)
173 (not (user-account-system? account))
174 (user-account-home-directory account)))
175 (operating-system-user-accounts os))))
176 (marionette-eval
177 `(begin
178 (use-modules (srfi srfi-1) (ice-9 ftw))
179 (every (lambda (home)
180 (null? (lset-difference string=?
181 (scandir "/etc/skel/")
182 (scandir home))))
183 ',homes))
184 marionette)))
185
186 (test-equal "login on tty1"
187 "root\n"
188 (begin
189 (marionette-control "sendkey ctrl-alt-f1" marionette)
190 ;; Wait for the 'term-tty1' service to be running (using
191 ;; 'start-service' is the simplest and most reliable way to do
192 ;; that.)
193 (marionette-eval
194 '(begin
195 (use-modules (gnu services herd))
196 (start-service 'term-tty1))
197 marionette)
198
199 ;; Now we can type.
200 (marionette-type "root\n\nid -un > logged-in\n" marionette)
201
202 ;; It can take a while before the shell commands are executed.
203 (marionette-eval '(use-modules (rnrs io ports)) marionette)
204 (marionette-eval
205 '(let loop ((i 0))
206 (catch 'system-error
207 (lambda ()
208 (call-with-input-file "/root/logged-in"
209 get-string-all))
210 (lambda args
211 (if (and (< i 15) (= ENOENT (system-error-errno args)))
212 (begin
213 (sleep 1)
214 (loop (+ i 1)))
215 (apply throw args)))))
216 marionette)))
217
218 ;; There should be one utmpx entry for the user logged in on tty1.
219 (test-equal "utmpx entry"
220 '(("root" "tty1" #f))
221 (marionette-eval
222 '(begin
223 (use-modules (guix build syscalls)
224 (srfi srfi-1))
225
226 (filter-map (lambda (entry)
227 (and (equal? (login-type USER_PROCESS)
228 (utmpx-login-type entry))
229 (list (utmpx-user entry) (utmpx-line entry)
230 (utmpx-host entry))))
231 (utmpx-entries)))
232 marionette))
233
234 ;; Likewise for /var/log/wtmp (used by 'last').
235 (test-assert "wtmp entry"
236 (match (marionette-eval
237 '(begin
238 (use-modules (guix build syscalls)
239 (srfi srfi-1))
240
241 (define (entry->list entry)
242 (list (utmpx-user entry) (utmpx-line entry)
243 (utmpx-host entry) (utmpx-login-type entry)))
244
245 (call-with-input-file "/var/log/wtmp"
246 (lambda (port)
247 (let loop ((result '()))
248 (if (eof-object? (peek-char port))
249 (map entry->list (reverse result))
250 (loop (cons (read-utmpx port) result)))))))
251 marionette)
252 (((users lines hosts types) ..1)
253 (every (lambda (type)
254 (eqv? type (login-type LOGIN_PROCESS)))
255 types))))
256
257 (test-assert "host name resolution"
258 (match (marionette-eval
259 '(begin
260 ;; Wait for nscd or our requests go through it.
261 (use-modules (gnu services herd))
262 (start-service 'nscd)
263
264 (list (getaddrinfo "localhost")
265 (getaddrinfo #$(operating-system-host-name os))))
266 marionette)
267 ((((? vector?) ..1) ((? vector?) ..1))
268 #t)
269 (x
270 (pk 'failure x #f))))
271
272 (test-equal "host not found"
273 #f
274 (marionette-eval
275 '(false-if-exception (getaddrinfo "does-not-exist"))
276 marionette))
277
278 (test-equal "locale"
279 "en_US.utf8"
280 (marionette-eval '(let ((before (setlocale LC_ALL "en_US.utf8")))
281 (setlocale LC_ALL before))
282 marionette))
283
284 (test-assert "/run/current-system is a GC root"
285 (marionette-eval '(begin
286 ;; Make sure the (guix …) modules are found.
287 (eval-when (expand load eval)
288 (set! %load-path
289 (cons
290 (string-append
291 "/run/current-system/profile/share/guile/site/"
292 (effective-version))
293 %load-path))
294 (set! %load-compiled-path
295 (cons
296 (string-append
297 "/run/current-system/profile/share/guile/site/"
298 (effective-version))
299 %load-compiled-path)))
300
301 (use-modules (srfi srfi-34) (guix store))
302
303 (let ((system (readlink "/run/current-system")))
304 (guard (c ((nix-protocol-error? c)
305 (file-exists? system)))
306 (with-store store
307 (delete-paths store (list system))
308 #f))))
309 marionette))
310
311 ;; This symlink is currently unused, but better have it point to the
312 ;; right place. See
313 ;; <https://lists.gnu.org/archive/html/guix-devel/2016-08/msg01641.html>.
314 (test-equal "/var/guix/gcroots/profiles is a valid symlink"
315 "/var/guix/profiles"
316 (marionette-eval '(readlink "/var/guix/gcroots/profiles")
317 marionette))
318
319
320 (test-assert "screendump"
321 (begin
322 (marionette-control (string-append "screendump " #$output
323 "/tty1.ppm")
324 marionette)
325 (file-exists? "tty1.ppm")))
326
327 (test-assert "screen text"
328 (let ((text (marionette-screen-text marionette
329 #:ocrad
330 #$(file-append ocrad
331 "/bin/ocrad"))))
332 ;; Check whether the welcome message and shell prompt are
333 ;; displayed. Note: OCR confuses "y" and "V" for instance, so
334 ;; we cannot reliably match the whole text.
335 (and (string-contains text "This is the GNU")
336 (string-contains text
337 (string-append
338 "root@"
339 #$(operating-system-host-name os))))))
340
341 (test-end)
342 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
343
344 (gexp->derivation name test))
345
346 (define %test-basic-os
347 (system-test
348 (name "basic")
349 (description
350 "Instrument %SIMPLE-OS, run it in a VM, and run a series of basic
351 functionality tests.")
352 (value
353 (mlet* %store-monad ((os -> (marionette-operating-system
354 %simple-os
355 #:imported-modules '((gnu services herd)
356 (guix combinators))))
357 (run (system-qemu-image/shared-store-script
358 os #:graphic? #f)))
359 ;; XXX: Add call to 'virtualized-operating-system' to get the exact same
360 ;; set of services as the OS produced by
361 ;; 'system-qemu-image/shared-store-script'.
362 (run-basic-test (virtualized-operating-system os '())
363 #~(list #$run))))))
364
365 \f
366 ;;;
367 ;;; Mcron.
368 ;;;
369
370 (define %mcron-os
371 ;; System with an mcron service, with one mcron job for "root" and one mcron
372 ;; job for an unprivileged user (note: #:user is an 'mcron2' thing.)
373 (let ((job1 #~(job next-second-from
374 (lambda ()
375 (call-with-output-file "witness"
376 (lambda (port)
377 (display (list (getuid) (getgid)) port))))))
378 (job2 #~(job next-second-from
379 (lambda ()
380 (call-with-output-file "witness"
381 (lambda (port)
382 (display (list (getuid) (getgid)) port))))
383 #:user "alice"))
384 (job3 #~(job next-second-from ;to test $PATH
385 "touch witness-touch")))
386 (operating-system
387 (inherit %simple-os)
388 (services (cons (mcron-service (list job1 job2 job3))
389 (operating-system-user-services %simple-os))))))
390
391 (define (run-mcron-test name)
392 (mlet* %store-monad ((os -> (marionette-operating-system
393 %mcron-os
394 #:imported-modules '((gnu services herd)
395 (guix combinators))))
396 (command (system-qemu-image/shared-store-script
397 os #:graphic? #f)))
398 (define test
399 (with-imported-modules '((gnu build marionette))
400 #~(begin
401 (use-modules (gnu build marionette)
402 (srfi srfi-64)
403 (ice-9 match))
404
405 (define marionette
406 (make-marionette (list #$command)))
407
408 (define (wait-for-file file)
409 ;; Wait until FILE exists in the guest; 'read' its content and
410 ;; return it.
411 (marionette-eval
412 `(let loop ((i 10))
413 (cond ((file-exists? ,file)
414 (call-with-input-file ,file read))
415 ((> i 0)
416 (sleep 1)
417 (loop (- i 1)))
418 (else
419 (error "file didn't show up" ,file))))
420 marionette))
421
422 (mkdir #$output)
423 (chdir #$output)
424
425 (test-begin "mcron")
426
427 (test-eq "service running"
428 'running!
429 (marionette-eval
430 '(begin
431 (use-modules (gnu services herd))
432 (start-service 'mcron)
433 'running!)
434 marionette))
435
436 ;; Make sure root's mcron job runs, has its cwd set to "/root", and
437 ;; runs with the right UID/GID.
438 (test-equal "root's job"
439 '(0 0)
440 (wait-for-file "/root/witness"))
441
442 ;; Likewise for Alice's job. We cannot know what its GID is since
443 ;; it's chosen by 'groupadd', but it's strictly positive.
444 (test-assert "alice's job"
445 (match (wait-for-file "/home/alice/witness")
446 ((1000 gid)
447 (>= gid 100))))
448
449 ;; Last, the job that uses a command; allows us to test whether
450 ;; $PATH is sane. (Note that 'marionette-eval' stringifies objects
451 ;; that don't have a read syntax, hence the string.)
452 (test-equal "root's job with command"
453 "#<eof>"
454 (wait-for-file "/root/witness-touch"))
455
456 (test-end)
457 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
458
459 (gexp->derivation name test)))
460
461 (define %test-mcron
462 (system-test
463 (name "mcron")
464 (description "Make sure the mcron service works as advertised.")
465 (value (run-mcron-test name))))
466
467 \f
468 ;;;
469 ;;; Avahi and NSS-mDNS.
470 ;;;
471
472 (define %avahi-os
473 (operating-system
474 (inherit %simple-os)
475 (name-service-switch %mdns-host-lookup-nss)
476 (services (cons* (avahi-service #:debug? #t)
477 (dbus-service)
478 (dhcp-client-service) ;needed for multicast
479
480 ;; Enable heavyweight debugging output.
481 (modify-services (operating-system-user-services
482 %simple-os)
483 (nscd-service-type config
484 => (nscd-configuration
485 (inherit config)
486 (debug-level 3)
487 (log-file "/dev/console")))
488 (syslog-service-type config
489 =>
490 (syslog-configuration
491 (inherit config)
492 (config-file
493 (plain-file
494 "syslog.conf"
495 "*.* /dev/console\n")))))))))
496
497 (define (run-nss-mdns-test)
498 ;; Test resolution of '.local' names via libc. Start the marionette service
499 ;; *after* nscd. Failing to do that, libc will try to connect to nscd,
500 ;; fail, then never try again (see '__nss_not_use_nscd_hosts' in libc),
501 ;; leading to '.local' resolution failures.
502 (mlet* %store-monad ((os -> (marionette-operating-system
503 %avahi-os
504 #:requirements '(nscd)
505 #:imported-modules '((gnu services herd)
506 (guix combinators))))
507 (run (system-qemu-image/shared-store-script
508 os #:graphic? #f)))
509 (define mdns-host-name
510 (string-append (operating-system-host-name os)
511 ".local"))
512
513 (define test
514 (with-imported-modules '((gnu build marionette))
515 #~(begin
516 (use-modules (gnu build marionette)
517 (srfi srfi-1)
518 (srfi srfi-64)
519 (ice-9 match))
520
521 (define marionette
522 (make-marionette (list #$run)))
523
524 (mkdir #$output)
525 (chdir #$output)
526
527 (test-begin "avahi")
528
529 (test-assert "wait for services"
530 (marionette-eval
531 '(begin
532 (use-modules (gnu services herd))
533
534 (start-service 'nscd)
535
536 ;; XXX: Work around a race condition in nscd: nscd creates its
537 ;; PID file before it is listening on its socket.
538 (let ((sock (socket PF_UNIX SOCK_STREAM 0)))
539 (let try ()
540 (catch 'system-error
541 (lambda ()
542 (connect sock AF_UNIX "/var/run/nscd/socket")
543 (close-port sock)
544 (format #t "nscd is ready~%"))
545 (lambda args
546 (format #t "waiting for nscd...~%")
547 (usleep 500000)
548 (try)))))
549
550 ;; Wait for the other useful things.
551 (start-service 'avahi-daemon)
552 (start-service 'networking)
553
554 #t)
555 marionette))
556
557 (test-equal "avahi-resolve-host-name"
558 0
559 (marionette-eval
560 '(system*
561 "/run/current-system/profile/bin/avahi-resolve-host-name"
562 "-v" #$mdns-host-name)
563 marionette))
564
565 (test-equal "avahi-browse"
566 0
567 (marionette-eval
568 '(system* "avahi-browse" "-avt")
569 marionette))
570
571 (test-assert "getaddrinfo .local"
572 ;; Wait for the 'avahi-daemon' service and perform a resolution.
573 (match (marionette-eval
574 '(getaddrinfo #$mdns-host-name)
575 marionette)
576 (((? vector? addrinfos) ..1)
577 (pk 'getaddrinfo addrinfos)
578 (and (any (lambda (ai)
579 (= AF_INET (addrinfo:fam ai)))
580 addrinfos)
581 (any (lambda (ai)
582 (= AF_INET6 (addrinfo:fam ai)))
583 addrinfos)))))
584
585 (test-assert "gethostbyname .local"
586 (match (pk 'gethostbyname
587 (marionette-eval '(gethostbyname #$mdns-host-name)
588 marionette))
589 ((? vector? result)
590 (and (string=? (hostent:name result) #$mdns-host-name)
591 (= (hostent:addrtype result) AF_INET)))))
592
593
594 (test-end)
595 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
596
597 (gexp->derivation "nss-mdns" test)))
598
599 (define %test-nss-mdns
600 (system-test
601 (name "nss-mdns")
602 (description
603 "Test Avahi's multicast-DNS implementation, and in particular, test its
604 glibc name service switch (NSS) module.")
605 (value (run-nss-mdns-test))))