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