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 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 "permissions on /root"
203 (let ((root-home #$(any (lambda (account)
204 (and (zero? (user-account-uid account))
205 (user-account-home-directory
207 (operating-system-user-accounts os))))
208 (stat:perms (marionette-eval `(stat ,root-home) marionette))))
210 (test-equal "no extra home directories"
213 ;; Make sure the home directories that are not supposed to be
214 ;; created are indeed not created.
216 '#$(filter-map (lambda (user)
218 (user-account-create-home-directory?
220 (user-account-home-directory user)))
221 (operating-system-user-accounts os))))
224 (use-modules (srfi srfi-1))
226 ;; Note: Do not flag "/var/empty".
228 ',(remove (cut string-prefix? "/var/" <>)
232 (test-equal "login on tty1"
235 (marionette-control "sendkey ctrl-alt-f1" marionette)
236 ;; Wait for the 'term-tty1' service to be running (using
237 ;; 'start-service' is the simplest and most reliable way to do
241 (use-modules (gnu services herd))
242 (start-service 'term-tty1))
246 (marionette-type "root\n\nid -un > logged-in\n" marionette)
248 ;; It can take a while before the shell commands are executed.
249 (marionette-eval '(use-modules (rnrs io ports)) marionette)
254 (call-with-input-file "/root/logged-in"
257 (if (and (< i 15) (= ENOENT (system-error-errno args)))
261 (apply throw args)))))
264 ;; There should be one utmpx entry for the user logged in on tty1.
265 (test-equal "utmpx entry"
266 '(("root" "tty1" #f))
269 (use-modules (guix build syscalls)
272 (filter-map (lambda (entry)
273 (and (equal? (login-type USER_PROCESS)
274 (utmpx-login-type entry))
275 (list (utmpx-user entry) (utmpx-line entry)
276 (utmpx-host entry))))
280 ;; Likewise for /var/log/wtmp (used by 'last').
281 (test-assert "wtmp entry"
282 (match (marionette-eval
284 (use-modules (guix build syscalls)
287 (define (entry->list entry)
288 (list (utmpx-user entry) (utmpx-line entry)
289 (utmpx-host entry) (utmpx-login-type entry)))
291 (call-with-input-file "/var/log/wtmp"
293 (let loop ((result '()))
294 (if (eof-object? (peek-char port))
295 (map entry->list (reverse result))
296 (loop (cons (read-utmpx port) result)))))))
298 (((users lines hosts types) ..1)
299 (every (lambda (type)
300 (eqv? type (login-type LOGIN_PROCESS)))
303 (test-assert "host name resolution"
304 (match (marionette-eval
306 ;; Wait for nscd or our requests go through it.
307 (use-modules (gnu services herd))
308 (start-service 'nscd)
310 (list (getaddrinfo "localhost")
311 (getaddrinfo #$(operating-system-host-name os))))
313 ((((? vector?) ..1) ((? vector?) ..1))
316 (pk 'failure x #f))))
318 (test-equal "host not found"
321 '(false-if-exception (getaddrinfo "does-not-exist"))
326 (marionette-eval '(let ((before (setlocale LC_ALL "en_US.utf8")))
327 (setlocale LC_ALL before))
330 (test-eq "/run/current-system is a GC root"
332 (marionette-eval '(begin
333 ;; Make sure the (guix …) modules are found.
335 ;; XXX: Currently shepherd and marionette run
336 ;; on Guile 2.0 whereas Guix is on 2.2. Yet
337 ;; we should be able to load the 2.0 Scheme
338 ;; files since it's pure Scheme.
340 #+(file-append guix "/share/guile/site/2.2"))
342 (use-modules (srfi srfi-34) (guix store))
344 (let ((system (readlink "/run/current-system")))
345 (guard (c ((nix-protocol-error? c)
346 (and (file-exists? system)
349 (delete-paths store (list system))
353 ;; This symlink is currently unused, but better have it point to the
355 ;; <https://lists.gnu.org/archive/html/guix-devel/2016-08/msg01641.html>.
356 (test-equal "/var/guix/gcroots/profiles is a valid symlink"
358 (marionette-eval '(readlink "/var/guix/gcroots/profiles")
362 (test-assert "screendump"
364 (marionette-control (string-append "screendump " #$output
367 (file-exists? "tty1.ppm")))
369 (test-assert "screen text"
370 (let ((text (marionette-screen-text marionette
374 ;; Check whether the welcome message and shell prompt are
375 ;; displayed. Note: OCR confuses "y" and "V" for instance, so
376 ;; we cannot reliably match the whole text.
377 (and (string-contains text "This is the GNU")
378 (string-contains text
381 #$(operating-system-host-name os))))))
384 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
386 (gexp->derivation name test))
388 (define %test-basic-os
392 "Instrument %SIMPLE-OS, run it in a VM, and run a series of basic
393 functionality tests.")
395 (let* ((os (marionette-operating-system
397 #:imported-modules '((gnu services herd)
398 (guix combinators))))
399 (vm (virtual-machine os)))
400 ;; XXX: Add call to 'virtualized-operating-system' to get the exact same
401 ;; set of services as the OS produced by
402 ;; 'system-qemu-image/shared-store-script'.
403 (run-basic-test (virtualized-operating-system os '())
412 ;; System with an mcron service, with one mcron job for "root" and one mcron
413 ;; job for an unprivileged user (note: #:user is an 'mcron2' thing.)
414 (let ((job1 #~(job next-second-from
416 (call-with-output-file "witness"
418 (display (list (getuid) (getgid)) port))))))
419 (job2 #~(job next-second-from
421 (call-with-output-file "witness"
423 (display (list (getuid) (getgid)) port))))
425 (job3 #~(job next-second-from ;to test $PATH
426 "touch witness-touch")))
427 (simple-operating-system
428 (mcron-service (list job1 job2 job3)))))
430 (define (run-mcron-test name)
432 (marionette-operating-system
434 #:imported-modules '((gnu services herd)
435 (guix combinators))))
438 (with-imported-modules '((gnu build marionette))
440 (use-modules (gnu build marionette)
445 (make-marionette (list #$(virtual-machine os))))
452 (test-eq "service running"
456 (use-modules (gnu services herd))
457 (start-service 'mcron)
461 ;; Make sure root's mcron job runs, has its cwd set to "/root", and
462 ;; runs with the right UID/GID.
463 (test-equal "root's job"
465 (wait-for-file "/root/witness" marionette))
467 ;; Likewise for Alice's job. We cannot know what its GID is since
468 ;; it's chosen by 'groupadd', but it's strictly positive.
469 (test-assert "alice's job"
470 (match (wait-for-file "/home/alice/witness" marionette)
474 ;; Last, the job that uses a command; allows us to test whether
475 ;; $PATH is sane. (Note that 'marionette-eval' stringifies objects
476 ;; that don't have a read syntax, hence the string.)
477 (test-equal "root's job with command"
479 (wait-for-file "/root/witness-touch" marionette))
482 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
484 (gexp->derivation name test))
489 (description "Make sure the mcron service works as advertised.")
490 (value (run-mcron-test name))))
494 ;;; Avahi and NSS-mDNS.
500 (name-service-switch %mdns-host-lookup-nss)
501 (services (cons* (avahi-service #:debug? #t)
503 (dhcp-client-service) ;needed for multicast
505 ;; Enable heavyweight debugging output.
506 (modify-services (operating-system-user-services
508 (nscd-service-type config
509 => (nscd-configuration
512 (log-file "/dev/console")))
513 (syslog-service-type config
515 (syslog-configuration
520 "*.* /dev/console\n")))))))))
522 (define (run-nss-mdns-test)
523 ;; Test resolution of '.local' names via libc. Start the marionette service
524 ;; *after* nscd. Failing to do that, libc will try to connect to nscd,
525 ;; fail, then never try again (see '__nss_not_use_nscd_hosts' in libc),
526 ;; leading to '.local' resolution failures.
528 (marionette-operating-system
530 #:requirements '(nscd)
531 #:imported-modules '((gnu services herd)
532 (guix combinators))))
534 (define mdns-host-name
535 (string-append (operating-system-host-name os)
539 (with-imported-modules '((gnu build marionette))
541 (use-modules (gnu build marionette)
547 (make-marionette (list #$(virtual-machine os))))
554 (test-assert "wait for services"
557 (use-modules (gnu services herd))
559 (start-service 'nscd)
561 ;; XXX: Work around a race condition in nscd: nscd creates its
562 ;; PID file before it is listening on its socket.
563 (let ((sock (socket PF_UNIX SOCK_STREAM 0)))
567 (connect sock AF_UNIX "/var/run/nscd/socket")
569 (format #t "nscd is ready~%"))
571 (format #t "waiting for nscd...~%")
575 ;; Wait for the other useful things.
576 (start-service 'avahi-daemon)
577 (start-service 'networking)
582 (test-equal "avahi-resolve-host-name"
586 "/run/current-system/profile/bin/avahi-resolve-host-name"
587 "-v" #$mdns-host-name)
590 (test-equal "avahi-browse"
593 '(system* "avahi-browse" "-avt")
596 (test-assert "getaddrinfo .local"
597 ;; Wait for the 'avahi-daemon' service and perform a resolution.
598 (match (marionette-eval
599 '(getaddrinfo #$mdns-host-name)
601 (((? vector? addrinfos) ..1)
602 (pk 'getaddrinfo addrinfos)
603 (and (any (lambda (ai)
604 (= AF_INET (addrinfo:fam ai)))
607 (= AF_INET6 (addrinfo:fam ai)))
610 (test-assert "gethostbyname .local"
611 (match (pk 'gethostbyname
612 (marionette-eval '(gethostbyname #$mdns-host-name)
615 (and (string=? (hostent:name result) #$mdns-host-name)
616 (= (hostent:addrtype result) AF_INET)))))
620 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
622 (gexp->derivation "nss-mdns" test))
624 (define %test-nss-mdns
628 "Test Avahi's multicast-DNS implementation, and in particular, test its
629 glibc name service switch (NSS) module.")
630 (value (run-nss-mdns-test))))