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 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
48 (host-name "komputilo")
49 (timezone "Europe/Berlin")
50 (locale "en_US.UTF-8")
52 (bootloader (grub-configuration (device "/dev/sdX")))
53 (file-systems (cons (file-system
61 (users (cons (user-account
63 (comment "Bob's sister")
65 (supplementary-groups '("wheel" "audio" "video"))
66 (home-directory "/home/alice"))
67 %base-user-accounts))))
70 (define* (run-basic-test os command #:optional (name "basic")
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>.
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."
81 (with-imported-modules '((gnu build marionette)
82 (guix build syscalls))
84 (use-modules (gnu build marionette)
92 (make-marionette #$command))
100 (initialization #~marionette))
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))
110 (string-prefix? architecture %host-type)))))
112 (test-assert "shell and user commands"
113 ;; Is everything in $PATH?
114 (zero? (marionette-eval '(system "
123 (test-assert "accounts"
124 (let ((users (marionette-eval '(begin
125 (use-modules (ice-9 match))
126 (let loop ((result '()))
128 (#f (reverse result))
129 (x (loop (cons x result))))))
132 (map passwd:name users)
134 #$@(map user-account-name
135 (operating-system-user-accounts os))))))
137 (test-assert "shepherd services"
138 (let ((services (marionette-eval
140 (use-modules (gnu services herd))
142 (map (compose car live-service-provision)
146 (pk 'services services)
147 '(root #$@(operating-system-shepherd-service-names os)))))
151 '#$(map user-account-home-directory
152 (filter user-account-create-home-directory?
153 (operating-system-user-accounts os)))))
156 (use-modules (gnu services herd) (srfi srfi-1))
158 ;; Home directories are supposed to exist once 'user-homes'
160 (start-service 'user-homes)
162 (every (lambda (home)
163 (and (file-exists? home)
164 (file-is-directory? home)))
168 (test-assert "skeletons in home directories"
170 '#$(filter-map (lambda (account)
171 (and (user-account-create-home-directory?
173 (not (user-account-system? account))
174 (list (user-account-name account)
175 (user-account-home-directory
177 (operating-system-user-accounts os))))
180 (use-modules (srfi srfi-1) (ice-9 ftw)
185 ;; Make sure HOME has all the skeletons...
186 (and (null? (lset-difference string=?
187 (scandir "/etc/skel/")
190 ;; ... and that everything is user-owned.
191 (let* ((pw (getpwnam user))
192 (uid (passwd:uid pw))
193 (gid (passwd:gid pw))
195 (define (user-owned? file)
196 (= uid (stat:uid (lstat file))))
198 (and (= uid (stat:uid st))
199 (eq? 'directory (stat:type st))
202 #:directories? #t)))))))
206 (test-equal "login on tty1"
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
215 (use-modules (gnu services herd))
216 (start-service 'term-tty1))
220 (marionette-type "root\n\nid -un > logged-in\n" marionette)
222 ;; It can take a while before the shell commands are executed.
223 (marionette-eval '(use-modules (rnrs io ports)) marionette)
228 (call-with-input-file "/root/logged-in"
231 (if (and (< i 15) (= ENOENT (system-error-errno args)))
235 (apply throw args)))))
238 ;; There should be one utmpx entry for the user logged in on tty1.
239 (test-equal "utmpx entry"
240 '(("root" "tty1" #f))
243 (use-modules (guix build syscalls)
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))))
254 ;; Likewise for /var/log/wtmp (used by 'last').
255 (test-assert "wtmp entry"
256 (match (marionette-eval
258 (use-modules (guix build syscalls)
261 (define (entry->list entry)
262 (list (utmpx-user entry) (utmpx-line entry)
263 (utmpx-host entry) (utmpx-login-type entry)))
265 (call-with-input-file "/var/log/wtmp"
267 (let loop ((result '()))
268 (if (eof-object? (peek-char port))
269 (map entry->list (reverse result))
270 (loop (cons (read-utmpx port) result)))))))
272 (((users lines hosts types) ..1)
273 (every (lambda (type)
274 (eqv? type (login-type LOGIN_PROCESS)))
277 (test-assert "host name resolution"
278 (match (marionette-eval
280 ;; Wait for nscd or our requests go through it.
281 (use-modules (gnu services herd))
282 (start-service 'nscd)
284 (list (getaddrinfo "localhost")
285 (getaddrinfo #$(operating-system-host-name os))))
287 ((((? vector?) ..1) ((? vector?) ..1))
290 (pk 'failure x #f))))
292 (test-equal "host not found"
295 '(false-if-exception (getaddrinfo "does-not-exist"))
300 (marionette-eval '(let ((before (setlocale LC_ALL "en_US.utf8")))
301 (setlocale LC_ALL before))
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)
311 "/run/current-system/profile/share/guile/site/"
314 (set! %load-compiled-path
317 "/run/current-system/profile/share/guile/site/"
319 %load-compiled-path)))
321 (use-modules (srfi srfi-34) (guix store))
323 (let ((system (readlink "/run/current-system")))
324 (guard (c ((nix-protocol-error? c)
325 (file-exists? system)))
327 (delete-paths store (list system))
331 ;; This symlink is currently unused, but better have it point to the
333 ;; <https://lists.gnu.org/archive/html/guix-devel/2016-08/msg01641.html>.
334 (test-equal "/var/guix/gcroots/profiles is a valid symlink"
336 (marionette-eval '(readlink "/var/guix/gcroots/profiles")
340 (test-assert "screendump"
342 (marionette-control (string-append "screendump " #$output
345 (file-exists? "tty1.ppm")))
347 (test-assert "screen text"
348 (let ((text (marionette-screen-text marionette
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
359 #$(operating-system-host-name os))))))
362 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
364 (gexp->derivation name test))
366 (define %test-basic-os
370 "Instrument %SIMPLE-OS, run it in a VM, and run a series of basic
371 functionality tests.")
373 (mlet* %store-monad ((os -> (marionette-operating-system
375 #:imported-modules '((gnu services herd)
376 (guix combinators))))
377 (run (system-qemu-image/shared-store-script
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 '())
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
395 (call-with-output-file "witness"
397 (display (list (getuid) (getgid)) port))))))
398 (job2 #~(job next-second-from
400 (call-with-output-file "witness"
402 (display (list (getuid) (getgid)) port))))
404 (job3 #~(job next-second-from ;to test $PATH
405 "touch witness-touch")))
408 (services (cons (mcron-service (list job1 job2 job3))
409 (operating-system-user-services %simple-os))))))
411 (define (run-mcron-test name)
412 (mlet* %store-monad ((os -> (marionette-operating-system
414 #:imported-modules '((gnu services herd)
415 (guix combinators))))
416 (command (system-qemu-image/shared-store-script
419 (with-imported-modules '((gnu build marionette))
421 (use-modules (gnu build marionette)
426 (make-marionette (list #$command)))
428 (define (wait-for-file file)
429 ;; Wait until FILE exists in the guest; 'read' its content and
433 (cond ((file-exists? ,file)
434 (call-with-input-file ,file read))
439 (error "file didn't show up" ,file))))
447 (test-eq "service running"
451 (use-modules (gnu services herd))
452 (start-service 'mcron)
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"
460 (wait-for-file "/root/witness"))
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")
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"
474 (wait-for-file "/root/witness-touch"))
477 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
479 (gexp->derivation name test)))
484 (description "Make sure the mcron service works as advertised.")
485 (value (run-mcron-test name))))
489 ;;; Avahi and NSS-mDNS.
495 (name-service-switch %mdns-host-lookup-nss)
496 (services (cons* (avahi-service #:debug? #t)
498 (dhcp-client-service) ;needed for multicast
500 ;; Enable heavyweight debugging output.
501 (modify-services (operating-system-user-services
503 (nscd-service-type config
504 => (nscd-configuration
507 (log-file "/dev/console")))
508 (syslog-service-type config
510 (syslog-configuration
515 "*.* /dev/console\n")))))))))
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
524 #:requirements '(nscd)
525 #:imported-modules '((gnu services herd)
526 (guix combinators))))
527 (run (system-qemu-image/shared-store-script
529 (define mdns-host-name
530 (string-append (operating-system-host-name os)
534 (with-imported-modules '((gnu build marionette))
536 (use-modules (gnu build marionette)
542 (make-marionette (list #$run)))
549 (test-assert "wait for services"
552 (use-modules (gnu services herd))
554 (start-service 'nscd)
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)))
562 (connect sock AF_UNIX "/var/run/nscd/socket")
564 (format #t "nscd is ready~%"))
566 (format #t "waiting for nscd...~%")
570 ;; Wait for the other useful things.
571 (start-service 'avahi-daemon)
572 (start-service 'networking)
577 (test-equal "avahi-resolve-host-name"
581 "/run/current-system/profile/bin/avahi-resolve-host-name"
582 "-v" #$mdns-host-name)
585 (test-equal "avahi-browse"
588 '(system* "avahi-browse" "-avt")
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)
596 (((? vector? addrinfos) ..1)
597 (pk 'getaddrinfo addrinfos)
598 (and (any (lambda (ai)
599 (= AF_INET (addrinfo:fam ai)))
602 (= AF_INET6 (addrinfo:fam ai)))
605 (test-assert "gethostbyname .local"
606 (match (pk 'gethostbyname
607 (marionette-eval '(gethostbyname #$mdns-host-name)
610 (and (string=? (hostent:name result) #$mdns-host-name)
611 (= (hostent:addrtype result) AF_INET)))))
615 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
617 (gexp->derivation "nss-mdns" test)))
619 (define %test-nss-mdns
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))))