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 (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
45 (simple-operating-system))
48 (define* (run-basic-test os command #:optional (name "basic")
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>.
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."
60 (fold-services (operating-system-services os)
61 #:target-type special-files-service-type)))
64 (with-imported-modules '((gnu build marionette)
65 (guix build syscalls))
67 (use-modules (gnu build marionette)
75 (make-marionette #$command))
83 (initialization #~marionette))
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))
93 (string-prefix? architecture %host-type)))))
95 (test-assert "shell and user commands"
96 ;; Is everything in $PATH?
97 (zero? (marionette-eval '(system "
106 (test-equal "special files"
110 (use-modules (ice-9 match))
114 (list file (readlink file))))
118 (test-assert "accounts"
119 (let ((users (marionette-eval '(begin
120 (use-modules (ice-9 match))
121 (let loop ((result '()))
123 (#f (reverse result))
124 (x (loop (cons x result))))))
127 (map passwd:name users)
129 #$@(map user-account-name
130 (operating-system-user-accounts os))))))
132 (test-assert "shepherd services"
133 (let ((services (marionette-eval
135 (use-modules (gnu services herd))
137 (map (compose car live-service-provision)
141 (pk 'services services)
142 '(root #$@(operating-system-shepherd-service-names os)))))
146 '#$(map user-account-home-directory
147 (filter user-account-create-home-directory?
148 (operating-system-user-accounts os)))))
151 (use-modules (gnu services herd) (srfi srfi-1))
153 ;; Home directories are supposed to exist once 'user-homes'
155 (start-service 'user-homes)
157 (every (lambda (home)
158 (and (file-exists? home)
159 (file-is-directory? home)))
163 (test-assert "skeletons in home directories"
165 '#$(filter-map (lambda (account)
166 (and (user-account-create-home-directory?
168 (not (user-account-system? account))
169 (list (user-account-name account)
170 (user-account-home-directory
172 (operating-system-user-accounts os))))
175 (use-modules (srfi srfi-1) (ice-9 ftw)
180 ;; Make sure HOME has all the skeletons...
181 (and (null? (lset-difference string=?
182 (scandir "/etc/skel/")
185 ;; ... and that everything is user-owned.
186 (let* ((pw (getpwnam user))
187 (uid (passwd:uid pw))
188 (gid (passwd:gid pw))
190 (define (user-owned? file)
191 (= uid (stat:uid (lstat file))))
193 (and (= uid (stat:uid st))
194 (eq? 'directory (stat:type st))
197 #:directories? #t)))))))
201 (test-equal "login on tty1"
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
210 (use-modules (gnu services herd))
211 (start-service 'term-tty1))
215 (marionette-type "root\n\nid -un > logged-in\n" marionette)
217 ;; It can take a while before the shell commands are executed.
218 (marionette-eval '(use-modules (rnrs io ports)) marionette)
223 (call-with-input-file "/root/logged-in"
226 (if (and (< i 15) (= ENOENT (system-error-errno args)))
230 (apply throw args)))))
233 ;; There should be one utmpx entry for the user logged in on tty1.
234 (test-equal "utmpx entry"
235 '(("root" "tty1" #f))
238 (use-modules (guix build syscalls)
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))))
249 ;; Likewise for /var/log/wtmp (used by 'last').
250 (test-assert "wtmp entry"
251 (match (marionette-eval
253 (use-modules (guix build syscalls)
256 (define (entry->list entry)
257 (list (utmpx-user entry) (utmpx-line entry)
258 (utmpx-host entry) (utmpx-login-type entry)))
260 (call-with-input-file "/var/log/wtmp"
262 (let loop ((result '()))
263 (if (eof-object? (peek-char port))
264 (map entry->list (reverse result))
265 (loop (cons (read-utmpx port) result)))))))
267 (((users lines hosts types) ..1)
268 (every (lambda (type)
269 (eqv? type (login-type LOGIN_PROCESS)))
272 (test-assert "host name resolution"
273 (match (marionette-eval
275 ;; Wait for nscd or our requests go through it.
276 (use-modules (gnu services herd))
277 (start-service 'nscd)
279 (list (getaddrinfo "localhost")
280 (getaddrinfo #$(operating-system-host-name os))))
282 ((((? vector?) ..1) ((? vector?) ..1))
285 (pk 'failure x #f))))
287 (test-equal "host not found"
290 '(false-if-exception (getaddrinfo "does-not-exist"))
295 (marionette-eval '(let ((before (setlocale LC_ALL "en_US.utf8")))
296 (setlocale LC_ALL before))
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)
306 "/run/current-system/profile/share/guile/site/"
309 (set! %load-compiled-path
312 "/run/current-system/profile/share/guile/site/"
314 %load-compiled-path)))
316 (use-modules (srfi srfi-34) (guix store))
318 (let ((system (readlink "/run/current-system")))
319 (guard (c ((nix-protocol-error? c)
320 (file-exists? system)))
322 (delete-paths store (list system))
326 ;; This symlink is currently unused, but better have it point to the
328 ;; <https://lists.gnu.org/archive/html/guix-devel/2016-08/msg01641.html>.
329 (test-equal "/var/guix/gcroots/profiles is a valid symlink"
331 (marionette-eval '(readlink "/var/guix/gcroots/profiles")
335 (test-assert "screendump"
337 (marionette-control (string-append "screendump " #$output
340 (file-exists? "tty1.ppm")))
342 (test-assert "screen text"
343 (let ((text (marionette-screen-text marionette
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
354 #$(operating-system-host-name os))))))
357 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
359 (gexp->derivation name test))
361 (define %test-basic-os
365 "Instrument %SIMPLE-OS, run it in a VM, and run a series of basic
366 functionality tests.")
368 (mlet* %store-monad ((os -> (marionette-operating-system
370 #:imported-modules '((gnu services herd)
371 (guix combinators))))
372 (run (system-qemu-image/shared-store-script
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 '())
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
390 (call-with-output-file "witness"
392 (display (list (getuid) (getgid)) port))))))
393 (job2 #~(job next-second-from
395 (call-with-output-file "witness"
397 (display (list (getuid) (getgid)) port))))
399 (job3 #~(job next-second-from ;to test $PATH
400 "touch witness-touch")))
401 (simple-operating-system
402 (mcron-service (list job1 job2 job3)))))
404 (define (run-mcron-test name)
405 (mlet* %store-monad ((os -> (marionette-operating-system
407 #:imported-modules '((gnu services herd)
408 (guix combinators))))
409 (command (system-qemu-image/shared-store-script
412 (with-imported-modules '((gnu build marionette))
414 (use-modules (gnu build marionette)
419 (make-marionette (list #$command)))
421 (define (wait-for-file file)
422 ;; Wait until FILE exists in the guest; 'read' its content and
426 (cond ((file-exists? ,file)
427 (call-with-input-file ,file read))
432 (error "file didn't show up" ,file))))
440 (test-eq "service running"
444 (use-modules (gnu services herd))
445 (start-service 'mcron)
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"
453 (wait-for-file "/root/witness"))
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")
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"
467 (wait-for-file "/root/witness-touch"))
470 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
472 (gexp->derivation name test)))
477 (description "Make sure the mcron service works as advertised.")
478 (value (run-mcron-test name))))
482 ;;; Avahi and NSS-mDNS.
488 (name-service-switch %mdns-host-lookup-nss)
489 (services (cons* (avahi-service #:debug? #t)
491 (dhcp-client-service) ;needed for multicast
493 ;; Enable heavyweight debugging output.
494 (modify-services (operating-system-user-services
496 (nscd-service-type config
497 => (nscd-configuration
500 (log-file "/dev/console")))
501 (syslog-service-type config
503 (syslog-configuration
508 "*.* /dev/console\n")))))))))
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
517 #:requirements '(nscd)
518 #:imported-modules '((gnu services herd)
519 (guix combinators))))
520 (run (system-qemu-image/shared-store-script
522 (define mdns-host-name
523 (string-append (operating-system-host-name os)
527 (with-imported-modules '((gnu build marionette))
529 (use-modules (gnu build marionette)
535 (make-marionette (list #$run)))
542 (test-assert "wait for services"
545 (use-modules (gnu services herd))
547 (start-service 'nscd)
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)))
555 (connect sock AF_UNIX "/var/run/nscd/socket")
557 (format #t "nscd is ready~%"))
559 (format #t "waiting for nscd...~%")
563 ;; Wait for the other useful things.
564 (start-service 'avahi-daemon)
565 (start-service 'networking)
570 (test-equal "avahi-resolve-host-name"
574 "/run/current-system/profile/bin/avahi-resolve-host-name"
575 "-v" #$mdns-host-name)
578 (test-equal "avahi-browse"
581 '(system* "avahi-browse" "-avt")
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)
589 (((? vector? addrinfos) ..1)
590 (pk 'getaddrinfo addrinfos)
591 (and (any (lambda (ai)
592 (= AF_INET (addrinfo:fam ai)))
595 (= AF_INET6 (addrinfo:fam ai)))
598 (test-assert "gethostbyname .local"
599 (match (pk 'gethostbyname
600 (marionette-eval '(gethostbyname #$mdns-host-name)
603 (and (string=? (hostent:name result) #$mdns-host-name)
604 (= (hostent:addrtype result) AF_INET)))))
608 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
610 (gexp->derivation "nss-mdns" test)))
612 (define %test-nss-mdns
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))))