1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2016, 2017 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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
46 (simple-operating-system))
49 (define* (run-basic-test os command #:optional (name "basic")
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>.
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."
61 (fold-services (operating-system-services os)
62 #:target-type special-files-service-type)))
65 (with-imported-modules '((gnu build marionette)
66 (guix build syscalls))
68 (use-modules (gnu build marionette)
76 (make-marionette #$command))
84 (initialization #~marionette))
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))
94 (string-prefix? architecture %host-type)))))
96 (test-assert "shell and user commands"
97 ;; Is everything in $PATH?
98 (zero? (marionette-eval '(system "
107 (test-equal "special files"
111 (use-modules (ice-9 match))
115 (list file (readlink file))))
119 (test-assert "accounts"
120 (let ((users (marionette-eval '(begin
121 (use-modules (ice-9 match))
122 (let loop ((result '()))
124 (#f (reverse result))
125 (x (loop (cons x result))))))
128 (map passwd:name users)
130 #$@(map user-account-name
131 (operating-system-user-accounts os))))))
133 (test-assert "shepherd services"
134 (let ((services (marionette-eval
136 (use-modules (gnu services herd))
138 (map (compose car live-service-provision)
142 (pk 'services services)
143 '(root #$@(operating-system-shepherd-service-names os)))))
147 '#$(map user-account-home-directory
148 (filter user-account-create-home-directory?
149 (operating-system-user-accounts os)))))
152 (use-modules (gnu services herd) (srfi srfi-1))
154 ;; Home directories are supposed to exist once 'user-homes'
156 (start-service 'user-homes)
158 (every (lambda (home)
159 (and (file-exists? home)
160 (file-is-directory? home)))
164 (test-assert "skeletons in home directories"
166 '#$(filter-map (lambda (account)
167 (and (user-account-create-home-directory?
169 (not (user-account-system? account))
170 (list (user-account-name account)
171 (user-account-home-directory
173 (operating-system-user-accounts os))))
176 (use-modules (srfi srfi-1) (ice-9 ftw)
181 ;; Make sure HOME has all the skeletons...
182 (and (null? (lset-difference string=?
183 (scandir "/etc/skel/")
186 ;; ... and that everything is user-owned.
187 (let* ((pw (getpwnam user))
188 (uid (passwd:uid pw))
189 (gid (passwd:gid pw))
191 (define (user-owned? file)
192 (= uid (stat:uid (lstat file))))
194 (and (= uid (stat:uid st))
195 (eq? 'directory (stat:type st))
198 #:directories? #t)))))))
202 (test-equal "permissions on /root"
204 (let ((root-home #$(any (lambda (account)
205 (and (zero? (user-account-uid account))
206 (user-account-home-directory
208 (operating-system-user-accounts os))))
209 (stat:perms (marionette-eval `(stat ,root-home) marionette))))
211 (test-equal "no extra home directories"
214 ;; Make sure the home directories that are not supposed to be
215 ;; created are indeed not created.
217 '#$(filter-map (lambda (user)
219 (user-account-create-home-directory?
221 (user-account-home-directory user)))
222 (operating-system-user-accounts os))))
225 (use-modules (srfi srfi-1))
227 ;; Note: Do not flag "/var/empty".
229 ',(remove (cut string-prefix? "/var/" <>)
233 (test-equal "login on tty1"
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
242 (use-modules (gnu services herd))
243 (start-service 'term-tty1))
247 (marionette-type "root\n\nid -un > logged-in\n" marionette)
249 ;; It can take a while before the shell commands are executed.
250 (marionette-eval '(use-modules (rnrs io ports)) marionette)
255 (call-with-input-file "/root/logged-in"
258 (if (and (< i 15) (= ENOENT (system-error-errno args)))
262 (apply throw args)))))
265 ;; There should be one utmpx entry for the user logged in on tty1.
266 (test-equal "utmpx entry"
267 '(("root" "tty1" #f))
270 (use-modules (guix build syscalls)
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))))
281 ;; Likewise for /var/log/wtmp (used by 'last').
282 (test-assert "wtmp entry"
283 (match (marionette-eval
285 (use-modules (guix build syscalls)
288 (define (entry->list entry)
289 (list (utmpx-user entry) (utmpx-line entry)
290 (utmpx-host entry) (utmpx-login-type entry)))
292 (call-with-input-file "/var/log/wtmp"
294 (let loop ((result '()))
295 (if (eof-object? (peek-char port))
296 (map entry->list (reverse result))
297 (loop (cons (read-utmpx port) result)))))))
299 (((users lines hosts types) ..1)
300 (every (lambda (type)
301 (eqv? type (login-type LOGIN_PROCESS)))
304 (test-assert "host name resolution"
305 (match (marionette-eval
307 ;; Wait for nscd or our requests go through it.
308 (use-modules (gnu services herd))
309 (start-service 'nscd)
311 (list (getaddrinfo "localhost")
312 (getaddrinfo #$(operating-system-host-name os))))
314 ((((? vector?) ..1) ((? vector?) ..1))
317 (pk 'failure x #f))))
319 (test-equal "host not found"
322 '(false-if-exception (getaddrinfo "does-not-exist"))
327 (marionette-eval '(let ((before (setlocale LC_ALL "en_US.utf8")))
328 (setlocale LC_ALL before))
331 (test-eq "/run/current-system is a GC root"
333 (marionette-eval '(begin
334 ;; Make sure the (guix …) modules are found.
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.
341 #+(file-append guix "/share/guile/site/2.2"))
343 (use-modules (srfi srfi-34) (guix store))
345 (let ((system (readlink "/run/current-system")))
346 (guard (c ((nix-protocol-error? c)
347 (and (file-exists? system)
350 (delete-paths store (list system))
354 ;; This symlink is currently unused, but better have it point to the
356 ;; <https://lists.gnu.org/archive/html/guix-devel/2016-08/msg01641.html>.
357 (test-equal "/var/guix/gcroots/profiles is a valid symlink"
359 (marionette-eval '(readlink "/var/guix/gcroots/profiles")
363 (test-assert "screendump"
365 (marionette-control (string-append "screendump " #$output
368 (file-exists? "tty1.ppm")))
370 (test-assert "screen text"
371 (let ((text (marionette-screen-text marionette
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
382 #$(operating-system-host-name os))))))
385 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
387 (gexp->derivation name test))
389 (define %test-basic-os
393 "Instrument %SIMPLE-OS, run it in a VM, and run a series of basic
394 functionality tests.")
396 (mlet* %store-monad ((os -> (marionette-operating-system
398 #:imported-modules '((gnu services herd)
399 (guix combinators))))
400 (run (system-qemu-image/shared-store-script
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 '())
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
418 (call-with-output-file "witness"
420 (display (list (getuid) (getgid)) port))))))
421 (job2 #~(job next-second-from
423 (call-with-output-file "witness"
425 (display (list (getuid) (getgid)) port))))
427 (job3 #~(job next-second-from ;to test $PATH
428 "touch witness-touch")))
429 (simple-operating-system
430 (mcron-service (list job1 job2 job3)))))
432 (define (run-mcron-test name)
433 (mlet* %store-monad ((os -> (marionette-operating-system
435 #:imported-modules '((gnu services herd)
436 (guix combinators))))
437 (command (system-qemu-image/shared-store-script
440 (with-imported-modules '((gnu build marionette))
442 (use-modules (gnu build marionette)
447 (make-marionette (list #$command)))
449 (define (wait-for-file file)
450 ;; Wait until FILE exists in the guest; 'read' its content and
454 (cond ((file-exists? ,file)
455 (call-with-input-file ,file read))
460 (error "file didn't show up" ,file))))
468 (test-eq "service running"
472 (use-modules (gnu services herd))
473 (start-service 'mcron)
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"
481 (wait-for-file "/root/witness"))
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")
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"
495 (wait-for-file "/root/witness-touch"))
498 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
500 (gexp->derivation name test)))
505 (description "Make sure the mcron service works as advertised.")
506 (value (run-mcron-test name))))
510 ;;; Avahi and NSS-mDNS.
516 (name-service-switch %mdns-host-lookup-nss)
517 (services (cons* (avahi-service #:debug? #t)
519 (dhcp-client-service) ;needed for multicast
521 ;; Enable heavyweight debugging output.
522 (modify-services (operating-system-user-services
524 (nscd-service-type config
525 => (nscd-configuration
528 (log-file "/dev/console")))
529 (syslog-service-type config
531 (syslog-configuration
536 "*.* /dev/console\n")))))))))
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
545 #:requirements '(nscd)
546 #:imported-modules '((gnu services herd)
547 (guix combinators))))
548 (run (system-qemu-image/shared-store-script
550 (define mdns-host-name
551 (string-append (operating-system-host-name os)
555 (with-imported-modules '((gnu build marionette))
557 (use-modules (gnu build marionette)
563 (make-marionette (list #$run)))
570 (test-assert "wait for services"
573 (use-modules (gnu services herd))
575 (start-service 'nscd)
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)))
583 (connect sock AF_UNIX "/var/run/nscd/socket")
585 (format #t "nscd is ready~%"))
587 (format #t "waiting for nscd...~%")
591 ;; Wait for the other useful things.
592 (start-service 'avahi-daemon)
593 (start-service 'networking)
598 (test-equal "avahi-resolve-host-name"
602 "/run/current-system/profile/bin/avahi-resolve-host-name"
603 "-v" #$mdns-host-name)
606 (test-equal "avahi-browse"
609 '(system* "avahi-browse" "-avt")
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)
617 (((? vector? addrinfos) ..1)
618 (pk 'getaddrinfo addrinfos)
619 (and (any (lambda (ai)
620 (= AF_INET (addrinfo:fam ai)))
623 (= AF_INET6 (addrinfo:fam ai)))
626 (test-assert "gethostbyname .local"
627 (match (pk 'gethostbyname
628 (marionette-eval '(gethostbyname #$mdns-host-name)
631 (and (string=? (hostent:name result) #$mdns-host-name)
632 (= (hostent:addrtype result) AF_INET)))))
636 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
638 (gexp->derivation "nss-mdns" test)))
640 (define %test-nss-mdns
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))))