tests: mcron: Adjust use of 'wait-for-file'.
[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 (wait-for-file "/root/logged-in" marionette
254 #:read 'get-string-all)))
255
256 ;; There should be one utmpx entry for the user logged in on tty1.
257 (test-equal "utmpx entry"
258 '(("root" "tty1" #f))
259 (marionette-eval
260 '(begin
261 (use-modules (guix build syscalls)
262 (srfi srfi-1))
263
264 (filter-map (lambda (entry)
265 (and (equal? (login-type USER_PROCESS)
266 (utmpx-login-type entry))
267 (list (utmpx-user entry) (utmpx-line entry)
268 (utmpx-host entry))))
269 (utmpx-entries)))
270 marionette))
271
272 ;; Likewise for /var/log/wtmp (used by 'last').
273 (test-assert "wtmp entry"
274 (match (marionette-eval
275 '(begin
276 (use-modules (guix build syscalls)
277 (srfi srfi-1))
278
279 (define (entry->list entry)
280 (list (utmpx-user entry) (utmpx-line entry)
281 (utmpx-host entry) (utmpx-login-type entry)))
282
283 (call-with-input-file "/var/log/wtmp"
284 (lambda (port)
285 (let loop ((result '()))
286 (if (eof-object? (peek-char port))
287 (map entry->list (reverse result))
288 (loop (cons (read-utmpx port) result)))))))
289 marionette)
290 (((users lines hosts types) ..1)
291 (every (lambda (type)
292 (eqv? type (login-type LOGIN_PROCESS)))
293 types))))
294
295 (test-assert "host name resolution"
296 (match (marionette-eval
297 '(begin
298 ;; Wait for nscd or our requests go through it.
299 (use-modules (gnu services herd))
300 (start-service 'nscd)
301
302 (list (getaddrinfo "localhost")
303 (getaddrinfo #$(operating-system-host-name os))))
304 marionette)
305 ((((? vector?) ..1) ((? vector?) ..1))
306 #t)
307 (x
308 (pk 'failure x #f))))
309
310 (test-equal "host not found"
311 #f
312 (marionette-eval
313 '(false-if-exception (getaddrinfo "does-not-exist"))
314 marionette))
315
316 (test-equal "locale"
317 "en_US.utf8"
318 (marionette-eval '(let ((before (setlocale LC_ALL "en_US.utf8")))
319 (setlocale LC_ALL before))
320 marionette))
321
322 (test-eq "/run/current-system is a GC root"
323 'success!
324 (marionette-eval '(begin
325 ;; Make sure the (guix …) modules are found.
326 ;;
327 ;; XXX: Currently shepherd and marionette run
328 ;; on Guile 2.0 whereas Guix is on 2.2. Yet
329 ;; we should be able to load the 2.0 Scheme
330 ;; files since it's pure Scheme.
331 (add-to-load-path
332 #+(file-append guix "/share/guile/site/2.2"))
333
334 (use-modules (srfi srfi-34) (guix store))
335
336 (let ((system (readlink "/run/current-system")))
337 (guard (c ((nix-protocol-error? c)
338 (and (file-exists? system)
339 'success!)))
340 (with-store store
341 (delete-paths store (list system))
342 #f))))
343 marionette))
344
345 ;; This symlink is currently unused, but better have it point to the
346 ;; right place. See
347 ;; <https://lists.gnu.org/archive/html/guix-devel/2016-08/msg01641.html>.
348 (test-equal "/var/guix/gcroots/profiles is a valid symlink"
349 "/var/guix/profiles"
350 (marionette-eval '(readlink "/var/guix/gcroots/profiles")
351 marionette))
352
353
354 (test-assert "screendump"
355 (begin
356 (marionette-control (string-append "screendump " #$output
357 "/tty1.ppm")
358 marionette)
359 (file-exists? "tty1.ppm")))
360
361 (test-assert "screen text"
362 (let ((text (marionette-screen-text marionette
363 #:ocrad
364 #$(file-append ocrad
365 "/bin/ocrad"))))
366 ;; Check whether the welcome message and shell prompt are
367 ;; displayed. Note: OCR confuses "y" and "V" for instance, so
368 ;; we cannot reliably match the whole text.
369 (and (string-contains text "This is the GNU")
370 (string-contains text
371 (string-append
372 "root@"
373 #$(operating-system-host-name os))))))
374
375 (test-end)
376 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
377
378 (gexp->derivation name test))
379
380 (define %test-basic-os
381 (system-test
382 (name "basic")
383 (description
384 "Instrument %SIMPLE-OS, run it in a VM, and run a series of basic
385 functionality tests.")
386 (value
387 (let* ((os (marionette-operating-system
388 %simple-os
389 #:imported-modules '((gnu services herd)
390 (guix combinators))))
391 (vm (virtual-machine os)))
392 ;; XXX: Add call to 'virtualized-operating-system' to get the exact same
393 ;; set of services as the OS produced by
394 ;; 'system-qemu-image/shared-store-script'.
395 (run-basic-test (virtualized-operating-system os '())
396 #~(list #$vm))))))
397
398 \f
399 ;;;
400 ;;; Halt.
401 ;;;
402
403 (define (run-halt-test vm)
404 ;; As reported in <http://bugs.gnu.org/26931>, running tmux would previously
405 ;; lead the 'stop' method of 'user-processes' to an infinite loop, with the
406 ;; tmux server process as a zombie that remains in the list of processes.
407 ;; This test reproduces this scenario.
408 (define test
409 (with-imported-modules '((gnu build marionette))
410 #~(begin
411 (use-modules (gnu build marionette))
412
413 (define marionette
414 (make-marionette '(#$vm)))
415
416 (define ocrad
417 #$(file-append ocrad "/bin/ocrad"))
418
419 ;; Wait for tty1 and log in.
420 (marionette-eval '(begin
421 (use-modules (gnu services herd))
422 (start-service 'term-tty1))
423 marionette)
424 (marionette-type "root\n" marionette)
425 (wait-for-screen-text marionette
426 (lambda (text)
427 (string-contains text "root@komputilo"))
428 #:ocrad ocrad)
429
430 ;; Start tmux and wait for it to be ready.
431 (marionette-type "tmux new-session 'echo 1 > /ready; bash'\n"
432 marionette)
433 (wait-for-file "/ready" marionette)
434
435 ;; Make sure to stop the test after a while.
436 (sigaction SIGALRM (lambda _
437 (format (current-error-port)
438 "FAIL: Time is up, but VM still running.\n")
439 (primitive-exit 1)))
440 (alarm 10)
441
442 ;; Get debugging info.
443 (marionette-eval '(current-output-port
444 (open-file "/dev/console" "w0"))
445 marionette)
446 (marionette-eval '(system* #$(file-append procps "/bin/ps")
447 "-eo" "pid,ppid,stat,comm")
448 marionette)
449
450 ;; See if 'halt' actually works.
451 (marionette-eval '(system* "/run/current-system/profile/sbin/halt")
452 marionette)
453
454 ;; If we reach this line, that means the VM was properly stopped in
455 ;; a timely fashion.
456 (alarm 0)
457 (call-with-output-file #$output
458 (lambda (port)
459 (display "success!" port))))))
460
461 (gexp->derivation "halt" test))
462
463 (define %test-halt
464 (system-test
465 (name "halt")
466 (description
467 "Use the 'halt' command and make sure it succeeds and does not get stuck
468 in a loop. See <http://bugs.gnu.org/26931>.")
469 (value
470 (let ((os (marionette-operating-system
471 (operating-system
472 (inherit %simple-os)
473 (packages (cons tmux %base-packages)))
474 #:imported-modules '((gnu services herd)
475 (guix combinators)))))
476 (run-halt-test (virtual-machine os))))))
477
478 \f
479 ;;;
480 ;;; Mcron.
481 ;;;
482
483 (define %mcron-os
484 ;; System with an mcron service, with one mcron job for "root" and one mcron
485 ;; job for an unprivileged user (note: #:user is an 'mcron2' thing.)
486 (let ((job1 #~(job next-second-from
487 (lambda ()
488 (call-with-output-file "witness"
489 (lambda (port)
490 (display (list (getuid) (getgid)) port))))))
491 (job2 #~(job next-second-from
492 (lambda ()
493 (call-with-output-file "witness"
494 (lambda (port)
495 (display (list (getuid) (getgid)) port))))
496 #:user "alice"))
497 (job3 #~(job next-second-from ;to test $PATH
498 "touch witness-touch")))
499 (simple-operating-system
500 (mcron-service (list job1 job2 job3)))))
501
502 (define (run-mcron-test name)
503 (define os
504 (marionette-operating-system
505 %mcron-os
506 #:imported-modules '((gnu services herd)
507 (guix combinators))))
508
509 (define test
510 (with-imported-modules '((gnu build marionette))
511 #~(begin
512 (use-modules (gnu build marionette)
513 (srfi srfi-64)
514 (ice-9 match))
515
516 (define marionette
517 (make-marionette (list #$(virtual-machine os))))
518
519 (mkdir #$output)
520 (chdir #$output)
521
522 (test-begin "mcron")
523
524 (test-eq "service running"
525 'running!
526 (marionette-eval
527 '(begin
528 (use-modules (gnu services herd))
529 (start-service 'mcron)
530 'running!)
531 marionette))
532
533 ;; Make sure root's mcron job runs, has its cwd set to "/root", and
534 ;; runs with the right UID/GID.
535 (test-equal "root's job"
536 '(0 0)
537 (wait-for-file "/root/witness" marionette))
538
539 ;; Likewise for Alice's job. We cannot know what its GID is since
540 ;; it's chosen by 'groupadd', but it's strictly positive.
541 (test-assert "alice's job"
542 (match (wait-for-file "/home/alice/witness" marionette)
543 ((1000 gid)
544 (>= gid 100))))
545
546 ;; Last, the job that uses a command; allows us to test whether
547 ;; $PATH is sane.
548 (test-equal "root's job with command"
549 ""
550 (wait-for-file "/root/witness-touch" marionette
551 #:read '(@ (ice-9 rdelim) read-string)))
552
553 (test-end)
554 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
555
556 (gexp->derivation name test))
557
558 (define %test-mcron
559 (system-test
560 (name "mcron")
561 (description "Make sure the mcron service works as advertised.")
562 (value (run-mcron-test name))))
563
564 \f
565 ;;;
566 ;;; Avahi and NSS-mDNS.
567 ;;;
568
569 (define %avahi-os
570 (operating-system
571 (inherit %simple-os)
572 (name-service-switch %mdns-host-lookup-nss)
573 (services (cons* (avahi-service #:debug? #t)
574 (dbus-service)
575 (dhcp-client-service) ;needed for multicast
576
577 ;; Enable heavyweight debugging output.
578 (modify-services (operating-system-user-services
579 %simple-os)
580 (nscd-service-type config
581 => (nscd-configuration
582 (inherit config)
583 (debug-level 3)
584 (log-file "/dev/console")))
585 (syslog-service-type config
586 =>
587 (syslog-configuration
588 (inherit config)
589 (config-file
590 (plain-file
591 "syslog.conf"
592 "*.* /dev/console\n")))))))))
593
594 (define (run-nss-mdns-test)
595 ;; Test resolution of '.local' names via libc. Start the marionette service
596 ;; *after* nscd. Failing to do that, libc will try to connect to nscd,
597 ;; fail, then never try again (see '__nss_not_use_nscd_hosts' in libc),
598 ;; leading to '.local' resolution failures.
599 (define os
600 (marionette-operating-system
601 %avahi-os
602 #:requirements '(nscd)
603 #:imported-modules '((gnu services herd)
604 (guix combinators))))
605
606 (define mdns-host-name
607 (string-append (operating-system-host-name os)
608 ".local"))
609
610 (define test
611 (with-imported-modules '((gnu build marionette))
612 #~(begin
613 (use-modules (gnu build marionette)
614 (srfi srfi-1)
615 (srfi srfi-64)
616 (ice-9 match))
617
618 (define marionette
619 (make-marionette (list #$(virtual-machine os))))
620
621 (mkdir #$output)
622 (chdir #$output)
623
624 (test-begin "avahi")
625
626 (test-assert "wait for services"
627 (marionette-eval
628 '(begin
629 (use-modules (gnu services herd))
630
631 (start-service 'nscd)
632
633 ;; XXX: Work around a race condition in nscd: nscd creates its
634 ;; PID file before it is listening on its socket.
635 (let ((sock (socket PF_UNIX SOCK_STREAM 0)))
636 (let try ()
637 (catch 'system-error
638 (lambda ()
639 (connect sock AF_UNIX "/var/run/nscd/socket")
640 (close-port sock)
641 (format #t "nscd is ready~%"))
642 (lambda args
643 (format #t "waiting for nscd...~%")
644 (usleep 500000)
645 (try)))))
646
647 ;; Wait for the other useful things.
648 (start-service 'avahi-daemon)
649 (start-service 'networking)
650
651 #t)
652 marionette))
653
654 (test-equal "avahi-resolve-host-name"
655 0
656 (marionette-eval
657 '(system*
658 "/run/current-system/profile/bin/avahi-resolve-host-name"
659 "-v" #$mdns-host-name)
660 marionette))
661
662 (test-equal "avahi-browse"
663 0
664 (marionette-eval
665 '(system* "avahi-browse" "-avt")
666 marionette))
667
668 (test-assert "getaddrinfo .local"
669 ;; Wait for the 'avahi-daemon' service and perform a resolution.
670 (match (marionette-eval
671 '(getaddrinfo #$mdns-host-name)
672 marionette)
673 (((? vector? addrinfos) ..1)
674 (pk 'getaddrinfo addrinfos)
675 (and (any (lambda (ai)
676 (= AF_INET (addrinfo:fam ai)))
677 addrinfos)
678 (any (lambda (ai)
679 (= AF_INET6 (addrinfo:fam ai)))
680 addrinfos)))))
681
682 (test-assert "gethostbyname .local"
683 (match (pk 'gethostbyname
684 (marionette-eval '(gethostbyname #$mdns-host-name)
685 marionette))
686 ((? vector? result)
687 (and (string=? (hostent:name result) #$mdns-host-name)
688 (= (hostent:addrtype result) AF_INET)))))
689
690
691 (test-end)
692 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
693
694 (gexp->derivation "nss-mdns" test))
695
696 (define %test-nss-mdns
697 (system-test
698 (name "nss-mdns")
699 (description
700 "Test Avahi's multicast-DNS implementation, and in particular, test its
701 glibc name service switch (NSS) module.")
702 (value (run-nss-mdns-test))))