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