services: user-homes: Do not create home directories marked as no-create.
[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 "no extra home directories"
203 '()
204
205 ;; Make sure the home directories that are not supposed to be
206 ;; created are indeed not created.
207 (let ((nonexistent
208 '#$(filter-map (lambda (user)
209 (and (not
210 (user-account-create-home-directory?
211 user))
212 (user-account-home-directory user)))
213 (operating-system-user-accounts os))))
214 (marionette-eval
215 `(begin
216 (use-modules (srfi srfi-1))
217
218 ;; Note: Do not flag "/var/empty".
219 (filter file-exists?
220 ',(remove (cut string-prefix? "/var/" <>)
221 nonexistent)))
222 marionette)))
223
224 (test-equal "login on tty1"
225 "root\n"
226 (begin
227 (marionette-control "sendkey ctrl-alt-f1" marionette)
228 ;; Wait for the 'term-tty1' service to be running (using
229 ;; 'start-service' is the simplest and most reliable way to do
230 ;; that.)
231 (marionette-eval
232 '(begin
233 (use-modules (gnu services herd))
234 (start-service 'term-tty1))
235 marionette)
236
237 ;; Now we can type.
238 (marionette-type "root\n\nid -un > logged-in\n" marionette)
239
240 ;; It can take a while before the shell commands are executed.
241 (marionette-eval '(use-modules (rnrs io ports)) marionette)
242 (marionette-eval
243 '(let loop ((i 0))
244 (catch 'system-error
245 (lambda ()
246 (call-with-input-file "/root/logged-in"
247 get-string-all))
248 (lambda args
249 (if (and (< i 15) (= ENOENT (system-error-errno args)))
250 (begin
251 (sleep 1)
252 (loop (+ i 1)))
253 (apply throw args)))))
254 marionette)))
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 (mlet* %store-monad ((os -> (marionette-operating-system
388 %simple-os
389 #:imported-modules '((gnu services herd)
390 (guix combinators))))
391 (run (system-qemu-image/shared-store-script
392 os #:graphic? #f)))
393 ;; XXX: Add call to 'virtualized-operating-system' to get the exact same
394 ;; set of services as the OS produced by
395 ;; 'system-qemu-image/shared-store-script'.
396 (run-basic-test (virtualized-operating-system os '())
397 #~(list #$run))))))
398
399 \f
400 ;;;
401 ;;; Mcron.
402 ;;;
403
404 (define %mcron-os
405 ;; System with an mcron service, with one mcron job for "root" and one mcron
406 ;; job for an unprivileged user (note: #:user is an 'mcron2' thing.)
407 (let ((job1 #~(job next-second-from
408 (lambda ()
409 (call-with-output-file "witness"
410 (lambda (port)
411 (display (list (getuid) (getgid)) port))))))
412 (job2 #~(job next-second-from
413 (lambda ()
414 (call-with-output-file "witness"
415 (lambda (port)
416 (display (list (getuid) (getgid)) port))))
417 #:user "alice"))
418 (job3 #~(job next-second-from ;to test $PATH
419 "touch witness-touch")))
420 (simple-operating-system
421 (mcron-service (list job1 job2 job3)))))
422
423 (define (run-mcron-test name)
424 (mlet* %store-monad ((os -> (marionette-operating-system
425 %mcron-os
426 #:imported-modules '((gnu services herd)
427 (guix combinators))))
428 (command (system-qemu-image/shared-store-script
429 os #:graphic? #f)))
430 (define test
431 (with-imported-modules '((gnu build marionette))
432 #~(begin
433 (use-modules (gnu build marionette)
434 (srfi srfi-64)
435 (ice-9 match))
436
437 (define marionette
438 (make-marionette (list #$command)))
439
440 (define (wait-for-file file)
441 ;; Wait until FILE exists in the guest; 'read' its content and
442 ;; return it.
443 (marionette-eval
444 `(let loop ((i 10))
445 (cond ((file-exists? ,file)
446 (call-with-input-file ,file read))
447 ((> i 0)
448 (sleep 1)
449 (loop (- i 1)))
450 (else
451 (error "file didn't show up" ,file))))
452 marionette))
453
454 (mkdir #$output)
455 (chdir #$output)
456
457 (test-begin "mcron")
458
459 (test-eq "service running"
460 'running!
461 (marionette-eval
462 '(begin
463 (use-modules (gnu services herd))
464 (start-service 'mcron)
465 'running!)
466 marionette))
467
468 ;; Make sure root's mcron job runs, has its cwd set to "/root", and
469 ;; runs with the right UID/GID.
470 (test-equal "root's job"
471 '(0 0)
472 (wait-for-file "/root/witness"))
473
474 ;; Likewise for Alice's job. We cannot know what its GID is since
475 ;; it's chosen by 'groupadd', but it's strictly positive.
476 (test-assert "alice's job"
477 (match (wait-for-file "/home/alice/witness")
478 ((1000 gid)
479 (>= gid 100))))
480
481 ;; Last, the job that uses a command; allows us to test whether
482 ;; $PATH is sane. (Note that 'marionette-eval' stringifies objects
483 ;; that don't have a read syntax, hence the string.)
484 (test-equal "root's job with command"
485 "#<eof>"
486 (wait-for-file "/root/witness-touch"))
487
488 (test-end)
489 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
490
491 (gexp->derivation name test)))
492
493 (define %test-mcron
494 (system-test
495 (name "mcron")
496 (description "Make sure the mcron service works as advertised.")
497 (value (run-mcron-test name))))
498
499 \f
500 ;;;
501 ;;; Avahi and NSS-mDNS.
502 ;;;
503
504 (define %avahi-os
505 (operating-system
506 (inherit %simple-os)
507 (name-service-switch %mdns-host-lookup-nss)
508 (services (cons* (avahi-service #:debug? #t)
509 (dbus-service)
510 (dhcp-client-service) ;needed for multicast
511
512 ;; Enable heavyweight debugging output.
513 (modify-services (operating-system-user-services
514 %simple-os)
515 (nscd-service-type config
516 => (nscd-configuration
517 (inherit config)
518 (debug-level 3)
519 (log-file "/dev/console")))
520 (syslog-service-type config
521 =>
522 (syslog-configuration
523 (inherit config)
524 (config-file
525 (plain-file
526 "syslog.conf"
527 "*.* /dev/console\n")))))))))
528
529 (define (run-nss-mdns-test)
530 ;; Test resolution of '.local' names via libc. Start the marionette service
531 ;; *after* nscd. Failing to do that, libc will try to connect to nscd,
532 ;; fail, then never try again (see '__nss_not_use_nscd_hosts' in libc),
533 ;; leading to '.local' resolution failures.
534 (mlet* %store-monad ((os -> (marionette-operating-system
535 %avahi-os
536 #:requirements '(nscd)
537 #:imported-modules '((gnu services herd)
538 (guix combinators))))
539 (run (system-qemu-image/shared-store-script
540 os #:graphic? #f)))
541 (define mdns-host-name
542 (string-append (operating-system-host-name os)
543 ".local"))
544
545 (define test
546 (with-imported-modules '((gnu build marionette))
547 #~(begin
548 (use-modules (gnu build marionette)
549 (srfi srfi-1)
550 (srfi srfi-64)
551 (ice-9 match))
552
553 (define marionette
554 (make-marionette (list #$run)))
555
556 (mkdir #$output)
557 (chdir #$output)
558
559 (test-begin "avahi")
560
561 (test-assert "wait for services"
562 (marionette-eval
563 '(begin
564 (use-modules (gnu services herd))
565
566 (start-service 'nscd)
567
568 ;; XXX: Work around a race condition in nscd: nscd creates its
569 ;; PID file before it is listening on its socket.
570 (let ((sock (socket PF_UNIX SOCK_STREAM 0)))
571 (let try ()
572 (catch 'system-error
573 (lambda ()
574 (connect sock AF_UNIX "/var/run/nscd/socket")
575 (close-port sock)
576 (format #t "nscd is ready~%"))
577 (lambda args
578 (format #t "waiting for nscd...~%")
579 (usleep 500000)
580 (try)))))
581
582 ;; Wait for the other useful things.
583 (start-service 'avahi-daemon)
584 (start-service 'networking)
585
586 #t)
587 marionette))
588
589 (test-equal "avahi-resolve-host-name"
590 0
591 (marionette-eval
592 '(system*
593 "/run/current-system/profile/bin/avahi-resolve-host-name"
594 "-v" #$mdns-host-name)
595 marionette))
596
597 (test-equal "avahi-browse"
598 0
599 (marionette-eval
600 '(system* "avahi-browse" "-avt")
601 marionette))
602
603 (test-assert "getaddrinfo .local"
604 ;; Wait for the 'avahi-daemon' service and perform a resolution.
605 (match (marionette-eval
606 '(getaddrinfo #$mdns-host-name)
607 marionette)
608 (((? vector? addrinfos) ..1)
609 (pk 'getaddrinfo addrinfos)
610 (and (any (lambda (ai)
611 (= AF_INET (addrinfo:fam ai)))
612 addrinfos)
613 (any (lambda (ai)
614 (= AF_INET6 (addrinfo:fam ai)))
615 addrinfos)))))
616
617 (test-assert "gethostbyname .local"
618 (match (pk 'gethostbyname
619 (marionette-eval '(gethostbyname #$mdns-host-name)
620 marionette))
621 ((? vector? result)
622 (and (string=? (hostent:name result) #$mdns-host-name)
623 (= (hostent:addrtype result) AF_INET)))))
624
625
626 (test-end)
627 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
628
629 (gexp->derivation "nss-mdns" test)))
630
631 (define %test-nss-mdns
632 (system-test
633 (name "nss-mdns")
634 (description
635 "Test Avahi's multicast-DNS implementation, and in particular, test its
636 glibc name service switch (NSS) module.")
637 (value (run-nss-mdns-test))))