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 "no extra home directories"
205 ;; Make sure the home directories that are not supposed to be
206 ;; created are indeed not created.
208 '#$(filter-map (lambda (user)
210 (user-account-create-home-directory?
212 (user-account-home-directory user)))
213 (operating-system-user-accounts os))))
216 (use-modules (srfi srfi-1))
218 ;; Note: Do not flag "/var/empty".
220 ',(remove (cut string-prefix? "/var/" <>)
224 (test-equal "login on tty1"
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
233 (use-modules (gnu services herd))
234 (start-service 'term-tty1))
238 (marionette-type "root\n\nid -un > logged-in\n" marionette)
240 ;; It can take a while before the shell commands are executed.
241 (marionette-eval '(use-modules (rnrs io ports)) marionette)
246 (call-with-input-file "/root/logged-in"
249 (if (and (< i 15) (= ENOENT (system-error-errno args)))
253 (apply throw args)))))
256 ;; There should be one utmpx entry for the user logged in on tty1.
257 (test-equal "utmpx entry"
258 '(("root" "tty1" #f))
261 (use-modules (guix build syscalls)
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))))
272 ;; Likewise for /var/log/wtmp (used by 'last').
273 (test-assert "wtmp entry"
274 (match (marionette-eval
276 (use-modules (guix build syscalls)
279 (define (entry->list entry)
280 (list (utmpx-user entry) (utmpx-line entry)
281 (utmpx-host entry) (utmpx-login-type entry)))
283 (call-with-input-file "/var/log/wtmp"
285 (let loop ((result '()))
286 (if (eof-object? (peek-char port))
287 (map entry->list (reverse result))
288 (loop (cons (read-utmpx port) result)))))))
290 (((users lines hosts types) ..1)
291 (every (lambda (type)
292 (eqv? type (login-type LOGIN_PROCESS)))
295 (test-assert "host name resolution"
296 (match (marionette-eval
298 ;; Wait for nscd or our requests go through it.
299 (use-modules (gnu services herd))
300 (start-service 'nscd)
302 (list (getaddrinfo "localhost")
303 (getaddrinfo #$(operating-system-host-name os))))
305 ((((? vector?) ..1) ((? vector?) ..1))
308 (pk 'failure x #f))))
310 (test-equal "host not found"
313 '(false-if-exception (getaddrinfo "does-not-exist"))
318 (marionette-eval '(let ((before (setlocale LC_ALL "en_US.utf8")))
319 (setlocale LC_ALL before))
322 (test-eq "/run/current-system is a GC root"
324 (marionette-eval '(begin
325 ;; Make sure the (guix …) modules are found.
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.
332 #+(file-append guix "/share/guile/site/2.2"))
334 (use-modules (srfi srfi-34) (guix store))
336 (let ((system (readlink "/run/current-system")))
337 (guard (c ((nix-protocol-error? c)
338 (and (file-exists? system)
341 (delete-paths store (list system))
345 ;; This symlink is currently unused, but better have it point to the
347 ;; <https://lists.gnu.org/archive/html/guix-devel/2016-08/msg01641.html>.
348 (test-equal "/var/guix/gcroots/profiles is a valid symlink"
350 (marionette-eval '(readlink "/var/guix/gcroots/profiles")
354 (test-assert "screendump"
356 (marionette-control (string-append "screendump " #$output
359 (file-exists? "tty1.ppm")))
361 (test-assert "screen text"
362 (let ((text (marionette-screen-text marionette
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
373 #$(operating-system-host-name os))))))
376 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
378 (gexp->derivation name test))
380 (define %test-basic-os
384 "Instrument %SIMPLE-OS, run it in a VM, and run a series of basic
385 functionality tests.")
387 (mlet* %store-monad ((os -> (marionette-operating-system
389 #:imported-modules '((gnu services herd)
390 (guix combinators))))
391 (run (system-qemu-image/shared-store-script
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 '())
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
409 (call-with-output-file "witness"
411 (display (list (getuid) (getgid)) port))))))
412 (job2 #~(job next-second-from
414 (call-with-output-file "witness"
416 (display (list (getuid) (getgid)) port))))
418 (job3 #~(job next-second-from ;to test $PATH
419 "touch witness-touch")))
420 (simple-operating-system
421 (mcron-service (list job1 job2 job3)))))
423 (define (run-mcron-test name)
424 (mlet* %store-monad ((os -> (marionette-operating-system
426 #:imported-modules '((gnu services herd)
427 (guix combinators))))
428 (command (system-qemu-image/shared-store-script
431 (with-imported-modules '((gnu build marionette))
433 (use-modules (gnu build marionette)
438 (make-marionette (list #$command)))
440 (define (wait-for-file file)
441 ;; Wait until FILE exists in the guest; 'read' its content and
445 (cond ((file-exists? ,file)
446 (call-with-input-file ,file read))
451 (error "file didn't show up" ,file))))
459 (test-eq "service running"
463 (use-modules (gnu services herd))
464 (start-service 'mcron)
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"
472 (wait-for-file "/root/witness"))
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")
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"
486 (wait-for-file "/root/witness-touch"))
489 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
491 (gexp->derivation name test)))
496 (description "Make sure the mcron service works as advertised.")
497 (value (run-mcron-test name))))
501 ;;; Avahi and NSS-mDNS.
507 (name-service-switch %mdns-host-lookup-nss)
508 (services (cons* (avahi-service #:debug? #t)
510 (dhcp-client-service) ;needed for multicast
512 ;; Enable heavyweight debugging output.
513 (modify-services (operating-system-user-services
515 (nscd-service-type config
516 => (nscd-configuration
519 (log-file "/dev/console")))
520 (syslog-service-type config
522 (syslog-configuration
527 "*.* /dev/console\n")))))))))
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
536 #:requirements '(nscd)
537 #:imported-modules '((gnu services herd)
538 (guix combinators))))
539 (run (system-qemu-image/shared-store-script
541 (define mdns-host-name
542 (string-append (operating-system-host-name os)
546 (with-imported-modules '((gnu build marionette))
548 (use-modules (gnu build marionette)
554 (make-marionette (list #$run)))
561 (test-assert "wait for services"
564 (use-modules (gnu services herd))
566 (start-service 'nscd)
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)))
574 (connect sock AF_UNIX "/var/run/nscd/socket")
576 (format #t "nscd is ready~%"))
578 (format #t "waiting for nscd...~%")
582 ;; Wait for the other useful things.
583 (start-service 'avahi-daemon)
584 (start-service 'networking)
589 (test-equal "avahi-resolve-host-name"
593 "/run/current-system/profile/bin/avahi-resolve-host-name"
594 "-v" #$mdns-host-name)
597 (test-equal "avahi-browse"
600 '(system* "avahi-browse" "-avt")
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)
608 (((? vector? addrinfos) ..1)
609 (pk 'getaddrinfo addrinfos)
610 (and (any (lambda (ai)
611 (= AF_INET (addrinfo:fam ai)))
614 (= AF_INET6 (addrinfo:fam ai)))
617 (test-assert "gethostbyname .local"
618 (match (pk 'gethostbyname
619 (marionette-eval '(gethostbyname #$mdns-host-name)
622 (and (string=? (hostent:name result) #$mdns-host-name)
623 (= (hostent:addrtype result) AF_INET)))))
627 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
629 (gexp->derivation "nss-mdns" test)))
631 (define %test-nss-mdns
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))))