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 (mkdir #$output)
450 (chdir #$output)
451
452 (test-begin "mcron")
453
454 (test-eq "service running"
455 'running!
456 (marionette-eval
457 '(begin
458 (use-modules (gnu services herd))
459 (start-service 'mcron)
460 'running!)
461 marionette))
462
463 ;; Make sure root's mcron job runs, has its cwd set to "/root", and
464 ;; runs with the right UID/GID.
465 (test-equal "root's job"
466 '(0 0)
467 (wait-for-file "/root/witness" marionette))
468
469 ;; Likewise for Alice's job. We cannot know what its GID is since
470 ;; it's chosen by 'groupadd', but it's strictly positive.
471 (test-assert "alice's job"
472 (match (wait-for-file "/home/alice/witness" marionette)
473 ((1000 gid)
474 (>= gid 100))))
475
476 ;; Last, the job that uses a command; allows us to test whether
477 ;; $PATH is sane. (Note that 'marionette-eval' stringifies objects
478 ;; that don't have a read syntax, hence the string.)
479 (test-equal "root's job with command"
480 "#<eof>"
481 (wait-for-file "/root/witness-touch" marionette))
482
483 (test-end)
484 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
485
486 (gexp->derivation name test)))
487
488 (define %test-mcron
489 (system-test
490 (name "mcron")
491 (description "Make sure the mcron service works as advertised.")
492 (value (run-mcron-test name))))
493
494 \f
495 ;;;
496 ;;; Avahi and NSS-mDNS.
497 ;;;
498
499 (define %avahi-os
500 (operating-system
501 (inherit %simple-os)
502 (name-service-switch %mdns-host-lookup-nss)
503 (services (cons* (avahi-service #:debug? #t)
504 (dbus-service)
505 (dhcp-client-service) ;needed for multicast
506
507 ;; Enable heavyweight debugging output.
508 (modify-services (operating-system-user-services
509 %simple-os)
510 (nscd-service-type config
511 => (nscd-configuration
512 (inherit config)
513 (debug-level 3)
514 (log-file "/dev/console")))
515 (syslog-service-type config
516 =>
517 (syslog-configuration
518 (inherit config)
519 (config-file
520 (plain-file
521 "syslog.conf"
522 "*.* /dev/console\n")))))))))
523
524 (define (run-nss-mdns-test)
525 ;; Test resolution of '.local' names via libc. Start the marionette service
526 ;; *after* nscd. Failing to do that, libc will try to connect to nscd,
527 ;; fail, then never try again (see '__nss_not_use_nscd_hosts' in libc),
528 ;; leading to '.local' resolution failures.
529 (mlet* %store-monad ((os -> (marionette-operating-system
530 %avahi-os
531 #:requirements '(nscd)
532 #:imported-modules '((gnu services herd)
533 (guix combinators))))
534 (run (system-qemu-image/shared-store-script
535 os #:graphic? #f)))
536 (define mdns-host-name
537 (string-append (operating-system-host-name os)
538 ".local"))
539
540 (define test
541 (with-imported-modules '((gnu build marionette))
542 #~(begin
543 (use-modules (gnu build marionette)
544 (srfi srfi-1)
545 (srfi srfi-64)
546 (ice-9 match))
547
548 (define marionette
549 (make-marionette (list #$run)))
550
551 (mkdir #$output)
552 (chdir #$output)
553
554 (test-begin "avahi")
555
556 (test-assert "wait for services"
557 (marionette-eval
558 '(begin
559 (use-modules (gnu services herd))
560
561 (start-service 'nscd)
562
563 ;; XXX: Work around a race condition in nscd: nscd creates its
564 ;; PID file before it is listening on its socket.
565 (let ((sock (socket PF_UNIX SOCK_STREAM 0)))
566 (let try ()
567 (catch 'system-error
568 (lambda ()
569 (connect sock AF_UNIX "/var/run/nscd/socket")
570 (close-port sock)
571 (format #t "nscd is ready~%"))
572 (lambda args
573 (format #t "waiting for nscd...~%")
574 (usleep 500000)
575 (try)))))
576
577 ;; Wait for the other useful things.
578 (start-service 'avahi-daemon)
579 (start-service 'networking)
580
581 #t)
582 marionette))
583
584 (test-equal "avahi-resolve-host-name"
585 0
586 (marionette-eval
587 '(system*
588 "/run/current-system/profile/bin/avahi-resolve-host-name"
589 "-v" #$mdns-host-name)
590 marionette))
591
592 (test-equal "avahi-browse"
593 0
594 (marionette-eval
595 '(system* "avahi-browse" "-avt")
596 marionette))
597
598 (test-assert "getaddrinfo .local"
599 ;; Wait for the 'avahi-daemon' service and perform a resolution.
600 (match (marionette-eval
601 '(getaddrinfo #$mdns-host-name)
602 marionette)
603 (((? vector? addrinfos) ..1)
604 (pk 'getaddrinfo addrinfos)
605 (and (any (lambda (ai)
606 (= AF_INET (addrinfo:fam ai)))
607 addrinfos)
608 (any (lambda (ai)
609 (= AF_INET6 (addrinfo:fam ai)))
610 addrinfos)))))
611
612 (test-assert "gethostbyname .local"
613 (match (pk 'gethostbyname
614 (marionette-eval '(gethostbyname #$mdns-host-name)
615 marionette))
616 ((? vector? result)
617 (and (string=? (hostent:name result) #$mdns-host-name)
618 (= (hostent:addrtype result) AF_INET)))))
619
620
621 (test-end)
622 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
623
624 (gexp->derivation "nss-mdns" test)))
625
626 (define %test-nss-mdns
627 (system-test
628 (name "nss-mdns")
629 (description
630 "Test Avahi's multicast-DNS implementation, and in particular, test its
631 glibc name service switch (NSS) module.")
632 (value (run-nss-mdns-test))))