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