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 (gnu packages linux)
36 #:use-module (gnu packages tmux)
37 #:use-module (guix gexp)
38 #:use-module (guix store)
39 #:use-module (guix packages)
40 #:use-module (srfi srfi-1)
41 #:export (run-basic-test
48 (simple-operating-system))
51 (define* (run-basic-test os command #:optional (name "basic")
53 "Return a derivation called NAME that tests basic features of the OS started
54 using COMMAND, a gexp that evaluates to a list of strings. Compare some
55 properties of running system to what's declared in OS, an <operating-system>.
57 When INITIALIZATION is true, it must be a one-argument procedure that is
58 passed a gexp denoting the marionette, and it must return gexp that is
59 inserted before the first test. This is used to introduce an extra
60 initialization step, such as entering a LUKS passphrase."
63 (fold-services (operating-system-services os)
64 #:target-type special-files-service-type)))
67 (with-imported-modules '((gnu build marionette)
68 (guix build syscalls))
70 (use-modules (gnu build marionette)
78 (make-marionette #$command))
86 (initialization #~marionette))
89 (match (marionette-eval '(uname) marionette)
90 (#("Linux" host-name version _ architecture)
91 (and (string=? host-name
92 #$(operating-system-host-name os))
93 (string-prefix? #$(package-version
94 (operating-system-kernel os))
96 (string-prefix? architecture %host-type)))))
98 (test-assert "shell and user commands"
99 ;; Is everything in $PATH?
100 (zero? (marionette-eval '(system "
109 (test-equal "special files"
113 (use-modules (ice-9 match))
117 (list file (readlink file))))
121 (test-assert "accounts"
122 (let ((users (marionette-eval '(begin
123 (use-modules (ice-9 match))
124 (let loop ((result '()))
126 (#f (reverse result))
127 (x (loop (cons x result))))))
130 (map passwd:name users)
132 #$@(map user-account-name
133 (operating-system-user-accounts os))))))
135 (test-assert "shepherd services"
136 (let ((services (marionette-eval
138 (use-modules (gnu services herd))
140 (map (compose car live-service-provision)
144 (pk 'services services)
145 '(root #$@(operating-system-shepherd-service-names os)))))
149 '#$(map user-account-home-directory
150 (filter user-account-create-home-directory?
151 (operating-system-user-accounts os)))))
154 (use-modules (gnu services herd) (srfi srfi-1))
156 ;; Home directories are supposed to exist once 'user-homes'
158 (start-service 'user-homes)
160 (every (lambda (home)
161 (and (file-exists? home)
162 (file-is-directory? home)))
166 (test-assert "skeletons in home directories"
168 '#$(filter-map (lambda (account)
169 (and (user-account-create-home-directory?
171 (not (user-account-system? account))
172 (list (user-account-name account)
173 (user-account-home-directory
175 (operating-system-user-accounts os))))
178 (use-modules (srfi srfi-1) (ice-9 ftw)
183 ;; Make sure HOME has all the skeletons...
184 (and (null? (lset-difference string=?
185 (scandir "/etc/skel/")
188 ;; ... and that everything is user-owned.
189 (let* ((pw (getpwnam user))
190 (uid (passwd:uid pw))
191 (gid (passwd:gid pw))
193 (define (user-owned? file)
194 (= uid (stat:uid (lstat file))))
196 (and (= uid (stat:uid st))
197 (eq? 'directory (stat:type st))
200 #:directories? #t)))))))
204 (test-equal "permissions on /root"
206 (let ((root-home #$(any (lambda (account)
207 (and (zero? (user-account-uid account))
208 (user-account-home-directory
210 (operating-system-user-accounts os))))
211 (stat:perms (marionette-eval `(stat ,root-home) marionette))))
213 (test-equal "no extra home directories"
216 ;; Make sure the home directories that are not supposed to be
217 ;; created are indeed not created.
219 '#$(filter-map (lambda (user)
221 (user-account-create-home-directory?
223 (user-account-home-directory user)))
224 (operating-system-user-accounts os))))
227 (use-modules (srfi srfi-1))
229 ;; Note: Do not flag "/var/empty".
231 ',(remove (cut string-prefix? "/var/" <>)
235 (test-equal "login on tty1"
238 (marionette-control "sendkey ctrl-alt-f1" marionette)
239 ;; Wait for the 'term-tty1' service to be running (using
240 ;; 'start-service' is the simplest and most reliable way to do
244 (use-modules (gnu services herd))
245 (start-service 'term-tty1))
249 (marionette-type "root\n\nid -un > logged-in\n" marionette)
251 ;; It can take a while before the shell commands are executed.
252 (marionette-eval '(use-modules (rnrs io ports)) marionette)
253 (wait-for-file "/root/logged-in" marionette
254 #:read 'get-string-all)))
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 (let* ((os (marionette-operating-system
389 #:imported-modules '((gnu services herd)
390 (guix combinators))))
391 (vm (virtual-machine os)))
392 ;; XXX: Add call to 'virtualized-operating-system' to get the exact same
393 ;; set of services as the OS produced by
394 ;; 'system-qemu-image/shared-store-script'.
395 (run-basic-test (virtualized-operating-system os '())
403 (define (run-halt-test vm)
404 ;; As reported in <http://bugs.gnu.org/26931>, running tmux would previously
405 ;; lead the 'stop' method of 'user-processes' to an infinite loop, with the
406 ;; tmux server process as a zombie that remains in the list of processes.
407 ;; This test reproduces this scenario.
409 (with-imported-modules '((gnu build marionette))
411 (use-modules (gnu build marionette))
414 (make-marionette '(#$vm)))
417 #$(file-append ocrad "/bin/ocrad"))
419 ;; Wait for tty1 and log in.
420 (marionette-eval '(begin
421 (use-modules (gnu services herd))
422 (start-service 'term-tty1))
424 (marionette-type "root\n" marionette)
425 (wait-for-screen-text marionette
427 (string-contains text "root@komputilo"))
430 ;; Start tmux and wait for it to be ready.
431 (marionette-type "tmux new-session 'echo 1 > /ready; bash'\n"
433 (wait-for-file "/ready" marionette)
435 ;; Make sure to stop the test after a while.
436 (sigaction SIGALRM (lambda _
437 (format (current-error-port)
438 "FAIL: Time is up, but VM still running.\n")
442 ;; Get debugging info.
443 (marionette-eval '(current-output-port
444 (open-file "/dev/console" "w0"))
446 (marionette-eval '(system* #$(file-append procps "/bin/ps")
447 "-eo" "pid,ppid,stat,comm")
450 ;; See if 'halt' actually works.
451 (marionette-eval '(system* "/run/current-system/profile/sbin/halt")
454 ;; If we reach this line, that means the VM was properly stopped in
457 (call-with-output-file #$output
459 (display "success!" port))))))
461 (gexp->derivation "halt" test))
467 "Use the 'halt' command and make sure it succeeds and does not get stuck
468 in a loop. See <http://bugs.gnu.org/26931>.")
470 (let ((os (marionette-operating-system
473 (packages (cons tmux %base-packages)))
474 #:imported-modules '((gnu services herd)
475 (guix combinators)))))
476 (run-halt-test (virtual-machine os))))))
484 ;; System with an mcron service, with one mcron job for "root" and one mcron
485 ;; job for an unprivileged user (note: #:user is an 'mcron2' thing.)
486 (let ((job1 #~(job next-second-from
488 (call-with-output-file "witness"
490 (display (list (getuid) (getgid)) port))))))
491 (job2 #~(job next-second-from
493 (call-with-output-file "witness"
495 (display (list (getuid) (getgid)) port))))
497 (job3 #~(job next-second-from ;to test $PATH
498 "touch witness-touch")))
499 (simple-operating-system
500 (mcron-service (list job1 job2 job3)))))
502 (define (run-mcron-test name)
504 (marionette-operating-system
506 #:imported-modules '((gnu services herd)
507 (guix combinators))))
510 (with-imported-modules '((gnu build marionette))
512 (use-modules (gnu build marionette)
517 (make-marionette (list #$(virtual-machine os))))
524 (test-eq "service running"
528 (use-modules (gnu services herd))
529 (start-service 'mcron)
533 ;; Make sure root's mcron job runs, has its cwd set to "/root", and
534 ;; runs with the right UID/GID.
535 (test-equal "root's job"
537 (wait-for-file "/root/witness" marionette))
539 ;; Likewise for Alice's job. We cannot know what its GID is since
540 ;; it's chosen by 'groupadd', but it's strictly positive.
541 (test-assert "alice's job"
542 (match (wait-for-file "/home/alice/witness" marionette)
546 ;; Last, the job that uses a command; allows us to test whether
548 (test-equal "root's job with command"
550 (wait-for-file "/root/witness-touch" marionette
551 #:read '(@ (ice-9 rdelim) read-string)))
554 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
556 (gexp->derivation name test))
561 (description "Make sure the mcron service works as advertised.")
562 (value (run-mcron-test name))))
566 ;;; Avahi and NSS-mDNS.
572 (name-service-switch %mdns-host-lookup-nss)
573 (services (cons* (avahi-service #:debug? #t)
575 (dhcp-client-service) ;needed for multicast
577 ;; Enable heavyweight debugging output.
578 (modify-services (operating-system-user-services
580 (nscd-service-type config
581 => (nscd-configuration
584 (log-file "/dev/console")))
585 (syslog-service-type config
587 (syslog-configuration
592 "*.* /dev/console\n")))))))))
594 (define (run-nss-mdns-test)
595 ;; Test resolution of '.local' names via libc. Start the marionette service
596 ;; *after* nscd. Failing to do that, libc will try to connect to nscd,
597 ;; fail, then never try again (see '__nss_not_use_nscd_hosts' in libc),
598 ;; leading to '.local' resolution failures.
600 (marionette-operating-system
602 #:requirements '(nscd)
603 #:imported-modules '((gnu services herd)
604 (guix combinators))))
606 (define mdns-host-name
607 (string-append (operating-system-host-name os)
611 (with-imported-modules '((gnu build marionette))
613 (use-modules (gnu build marionette)
619 (make-marionette (list #$(virtual-machine os))))
626 (test-assert "wait for services"
629 (use-modules (gnu services herd))
631 (start-service 'nscd)
633 ;; XXX: Work around a race condition in nscd: nscd creates its
634 ;; PID file before it is listening on its socket.
635 (let ((sock (socket PF_UNIX SOCK_STREAM 0)))
639 (connect sock AF_UNIX "/var/run/nscd/socket")
641 (format #t "nscd is ready~%"))
643 (format #t "waiting for nscd...~%")
647 ;; Wait for the other useful things.
648 (start-service 'avahi-daemon)
649 (start-service 'networking)
654 (test-equal "avahi-resolve-host-name"
658 "/run/current-system/profile/bin/avahi-resolve-host-name"
659 "-v" #$mdns-host-name)
662 (test-equal "avahi-browse"
665 '(system* "avahi-browse" "-avt")
668 (test-assert "getaddrinfo .local"
669 ;; Wait for the 'avahi-daemon' service and perform a resolution.
670 (match (marionette-eval
671 '(getaddrinfo #$mdns-host-name)
673 (((? vector? addrinfos) ..1)
674 (pk 'getaddrinfo addrinfos)
675 (and (any (lambda (ai)
676 (= AF_INET (addrinfo:fam ai)))
679 (= AF_INET6 (addrinfo:fam ai)))
682 (test-assert "gethostbyname .local"
683 (match (pk 'gethostbyname
684 (marionette-eval '(gethostbyname #$mdns-host-name)
687 (and (string=? (hostent:name result) #$mdns-host-name)
688 (= (hostent:addrtype result) AF_INET)))))
692 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
694 (gexp->derivation "nss-mdns" test))
696 (define %test-nss-mdns
700 "Test Avahi's multicast-DNS implementation, and in particular, test its
701 glibc name service switch (NSS) module.")
702 (value (run-nss-mdns-test))))