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