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