1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2018 Clément Lassieur <clement@lassieur.org>
5 ;;; This file is part of GNU Guix.
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20 (define-module (gnu tests base)
21 #:use-module (gnu tests)
22 #:use-module (gnu system)
23 #:use-module (gnu system shadow)
24 #:use-module (gnu system nss)
25 #:use-module (gnu system vm)
26 #:use-module (gnu services)
27 #:use-module (gnu services base)
28 #:use-module (gnu services dbus)
29 #:use-module (gnu services avahi)
30 #:use-module (gnu services mcron)
31 #:use-module (gnu services shepherd)
32 #:use-module (gnu services networking)
33 #:use-module (gnu packages base)
34 #:use-module (gnu packages bash)
35 #:use-module (gnu packages imagemagick)
36 #:use-module (gnu packages ocr)
37 #:use-module (gnu packages package-management)
38 #:use-module (gnu packages linux)
39 #:use-module (gnu packages tmux)
40 #:use-module (guix gexp)
41 #:use-module (guix store)
42 #:use-module (guix monads)
43 #:use-module (guix packages)
44 #:use-module (srfi srfi-1)
45 #:use-module (ice-9 match)
46 #:export (run-basic-test
54 (simple-operating-system))
57 (define* (run-basic-test os command #:optional (name "basic")
59 "Return a derivation called NAME that tests basic features of the OS started
60 using COMMAND, a gexp that evaluates to a list of strings. Compare some
61 properties of running system to what's declared in OS, an <operating-system>.
63 When INITIALIZATION is true, it must be a one-argument procedure that is
64 passed a gexp denoting the marionette, and it must return gexp that is
65 inserted before the first test. This is used to introduce an extra
66 initialization step, such as entering a LUKS passphrase."
69 (fold-services (operating-system-services os)
70 #:target-type special-files-service-type)))
73 (match (package-transitive-propagated-inputs guix)
74 (((labels packages) ...)
75 (cons guix packages))))
78 (with-imported-modules '((gnu build marionette)
79 (guix build syscalls))
81 (use-modules (gnu build marionette)
89 (make-marionette #$command))
97 (initialization #~marionette))
100 (match (marionette-eval '(uname) marionette)
101 (#("Linux" host-name version _ architecture)
102 (and (string=? host-name
103 #$(operating-system-host-name os))
104 (string-prefix? #$(package-version
105 (operating-system-kernel os))
107 (string-prefix? architecture %host-type)))))
109 ;; Shepherd reads the config file *before* binding its control
110 ;; socket, so /var/run/shepherd/socket might not exist yet when the
111 ;; 'marionette' service is started.
112 (test-assert "shepherd socket ready"
115 (use-modules (gnu services herd))
117 (cond ((file-exists? (%shepherd-socket-file))
126 (test-eq "stdin is /dev/null"
128 ;; Make sure services can no longer read from stdin once the
129 ;; system has booted.
132 (use-modules (gnu services herd))
133 (start 'user-processes)
134 ((@@ (gnu services herd) eval-there)
135 '(let ((result (read (current-input-port))))
136 (if (eof-object? result)
141 (test-assert "shell and user commands"
142 ;; Is everything in $PATH?
143 (zero? (marionette-eval '(system "
152 (test-equal "special files"
156 (use-modules (ice-9 match))
160 (list file (readlink file))))
164 (test-assert "accounts"
165 (let ((users (marionette-eval '(begin
166 (use-modules (ice-9 match))
167 (let loop ((result '()))
169 (#f (reverse result))
170 (x (loop (cons x result))))))
174 (list (passwd:name user)
178 #$@(map (lambda (account)
179 `(list ,(user-account-name account)
180 ,(user-account-home-directory account)))
181 (operating-system-user-accounts os))))))
183 (test-assert "shepherd services"
184 (let ((services (marionette-eval
186 (use-modules (gnu services herd))
188 (map (compose car live-service-provision)
192 (pk 'services services)
193 '(root #$@(operating-system-shepherd-service-names os)))))
197 '#$(map user-account-home-directory
198 (filter user-account-create-home-directory?
199 (operating-system-user-accounts os)))))
202 (use-modules (gnu services herd) (srfi srfi-1))
204 ;; Home directories are supposed to exist once 'user-homes'
206 (start-service 'user-homes)
208 (every (lambda (home)
209 (and (file-exists? home)
210 (file-is-directory? home)))
214 (test-assert "skeletons in home directories"
216 '#$(filter-map (lambda (account)
217 (and (user-account-create-home-directory?
219 (not (user-account-system? account))
220 (list (user-account-name account)
221 (user-account-home-directory
223 (operating-system-user-accounts os))))
226 (use-modules (srfi srfi-1) (ice-9 ftw)
231 ;; Make sure HOME has all the skeletons...
232 (and (null? (lset-difference string=?
233 (scandir "/etc/skel/")
236 ;; ... and that everything is user-owned.
237 (let* ((pw (getpwnam user))
238 (uid (passwd:uid pw))
239 (gid (passwd:gid pw))
241 (define (user-owned? file)
242 (= uid (stat:uid (lstat file))))
244 (and (= uid (stat:uid st))
245 (eq? 'directory (stat:type st))
248 #:directories? #t)))))))
252 (test-equal "permissions on /root"
254 (let ((root-home #$(any (lambda (account)
255 (and (zero? (user-account-uid account))
256 (user-account-home-directory
258 (operating-system-user-accounts os))))
259 (stat:perms (marionette-eval `(stat ,root-home) marionette))))
261 (test-equal "ownership and permissions of /var/empty"
263 (let ((st (marionette-eval `(stat "/var/empty") marionette)))
264 (list (stat:uid st) (stat:gid st)
267 (test-equal "no extra home directories"
270 ;; Make sure the home directories that are not supposed to be
271 ;; created are indeed not created.
273 '#$(filter-map (lambda (user)
275 (user-account-create-home-directory?
277 (user-account-home-directory user)))
278 (operating-system-user-accounts os))))
281 (use-modules (srfi srfi-1))
283 ;; Note: Do not flag "/var/empty".
285 ',(remove (cut string-prefix? "/var/" <>)
289 (test-equal "login on tty1"
292 (marionette-control "sendkey ctrl-alt-f1" marionette)
293 ;; Wait for the 'term-tty1' service to be running (using
294 ;; 'start-service' is the simplest and most reliable way to do
298 (use-modules (gnu services herd))
299 (start-service 'term-tty1))
303 (marionette-type "root\n\nid -un > logged-in\n" marionette)
305 ;; It can take a while before the shell commands are executed.
306 (marionette-eval '(use-modules (rnrs io ports)) marionette)
307 (wait-for-file "/root/logged-in" marionette
308 #:read 'get-string-all)))
310 (test-equal "getlogin on tty1"
313 ;; Assume we logged in in the previous test and type.
314 (marionette-type "guile -c '(write (getlogin))' > /root/login-id.tmp\n"
316 (marionette-type "mv /root/login-id{.tmp,}\n"
319 ;; It can take a while before the shell commands are executed.
320 (marionette-eval '(use-modules (rnrs io ports)) marionette)
321 (wait-for-file "/root/login-id" marionette
322 #:read 'get-string-all)))
324 ;; There should be one utmpx entry for the user logged in on tty1.
325 (test-equal "utmpx entry"
326 '(("root" "tty1" #f))
329 (use-modules (guix build syscalls)
332 (filter-map (lambda (entry)
333 (and (equal? (login-type USER_PROCESS)
334 (utmpx-login-type entry))
335 (list (utmpx-user entry) (utmpx-line entry)
336 (utmpx-host entry))))
340 ;; Likewise for /var/log/wtmp (used by 'last').
341 (test-assert "wtmp entry"
342 (match (marionette-eval
344 (use-modules (guix build syscalls)
347 (define (entry->list entry)
348 (list (utmpx-user entry) (utmpx-line entry)
349 (utmpx-host entry) (utmpx-login-type entry)))
351 (call-with-input-file "/var/log/wtmp"
353 (let loop ((result '()))
354 (if (eof-object? (peek-char port))
355 (map entry->list (reverse result))
356 (loop (cons (read-utmpx port) result)))))))
358 (((users lines hosts types) ..1)
359 (every (lambda (type)
360 (eqv? type (login-type LOGIN_PROCESS)))
363 (test-assert "host name resolution"
364 (match (marionette-eval
366 ;; Wait for nscd or our requests go through it.
367 (use-modules (gnu services herd))
368 (start-service 'nscd)
370 (list (getaddrinfo "localhost")
371 (getaddrinfo #$(operating-system-host-name os))))
373 ((((? vector?) ..1) ((? vector?) ..1))
376 (pk 'failure x #f))))
378 (test-equal "nscd invalidate action"
380 (marionette-eval '(with-shepherd-action 'nscd ('invalidate "hosts")
385 ;; FIXME: The 'invalidate' action can't reliably obtain the exit
386 ;; code of 'nscd' so skip this test.
388 (test-equal "nscd invalidate action, wrong table"
390 (marionette-eval '(with-shepherd-action 'nscd ('invalidate "xyz")
395 (test-equal "host not found"
398 '(false-if-exception (getaddrinfo "does-not-exist"))
403 (marionette-eval '(let ((before (setlocale LC_ALL "en_US.utf8")))
404 (setlocale LC_ALL before))
407 (test-eq "/run/current-system is a GC root"
409 (marionette-eval '(begin
410 ;; Make sure the (guix …) modules are found.
411 (eval-when (expand load eval)
413 (append (map (lambda (package)
414 (string-append package
416 (effective-version)))
420 (use-modules (srfi srfi-34) (guix store))
422 (let ((system (readlink "/run/current-system")))
423 (guard (c ((store-protocol-error? c)
424 (and (file-exists? system)
427 (delete-paths store (list system))
431 ;; This symlink is currently unused, but better have it point to the
433 ;; <https://lists.gnu.org/archive/html/guix-devel/2016-08/msg01641.html>.
434 (test-equal "/var/guix/gcroots/profiles is a valid symlink"
436 (marionette-eval '(readlink "/var/guix/gcroots/profiles")
440 (test-assert "screendump"
442 (marionette-control (string-append "screendump " #$output
445 (file-exists? "tty1.ppm")))
447 (test-assert "screen text"
448 (let ((text (marionette-screen-text marionette
452 ;; Check whether the welcome message and shell prompt are
453 ;; displayed. Note: OCR confuses "y" and "V" for instance, so
454 ;; we cannot reliably match the whole text.
455 (and (string-contains text "This is the GNU")
456 (string-contains text
459 #$(operating-system-host-name os))))))
462 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
464 (gexp->derivation name test))
466 (define %test-basic-os
470 "Instrument %SIMPLE-OS, run it in a VM, and run a series of basic
471 functionality tests.")
473 (let* ((os (marionette-operating-system
475 #:imported-modules '((gnu services herd)
476 (guix combinators))))
477 (vm (virtual-machine os)))
478 ;; XXX: Add call to 'virtualized-operating-system' to get the exact same
479 ;; set of services as the OS produced by
480 ;; 'system-qemu-image/shared-store-script'.
481 (run-basic-test (virtualized-operating-system os '())
489 (define (run-halt-test vm)
490 ;; As reported in <http://bugs.gnu.org/26931>, running tmux would previously
491 ;; lead the 'stop' method of 'user-processes' to an infinite loop, with the
492 ;; tmux server process as a zombie that remains in the list of processes.
493 ;; This test reproduces this scenario.
495 (with-imported-modules '((gnu build marionette))
497 (use-modules (gnu build marionette))
500 (make-marionette '(#$vm)))
503 #$(file-append ocrad "/bin/ocrad"))
505 ;; Wait for tty1 and log in.
506 (marionette-eval '(begin
507 (use-modules (gnu services herd))
508 (start-service 'term-tty1))
510 (marionette-type "root\n" marionette)
511 (wait-for-screen-text marionette
513 (string-contains text "root@komputilo"))
516 ;; Start tmux and wait for it to be ready.
517 (marionette-type "tmux new-session 'echo 1 > /ready; bash'\n"
519 (wait-for-file "/ready" marionette)
521 ;; Make sure to stop the test after a while.
522 (sigaction SIGALRM (lambda _
523 (format (current-error-port)
524 "FAIL: Time is up, but VM still running.\n")
528 ;; Get debugging info.
529 (marionette-eval '(current-output-port
530 (open-file "/dev/console" "w0"))
532 (marionette-eval '(system* #$(file-append procps "/bin/ps")
533 "-eo" "pid,ppid,stat,comm")
536 ;; See if 'halt' actually works.
537 (marionette-eval '(system* "/run/current-system/profile/sbin/halt")
540 ;; If we reach this line, that means the VM was properly stopped in
543 (call-with-output-file #$output
545 (display "success!" port))))))
547 (gexp->derivation "halt" test))
553 "Use the 'halt' command and make sure it succeeds and does not get stuck
554 in a loop. See <http://bugs.gnu.org/26931>.")
556 (let ((os (marionette-operating-system
559 (packages (cons tmux %base-packages)))
560 #:imported-modules '((gnu services herd)
561 (guix combinators)))))
562 (run-halt-test (virtual-machine os))))))
566 ;;; Cleanup of /tmp, /var/run, etc.
570 (simple-operating-system
571 (simple-service 'dirty-things
573 (let ((script (plain-file
574 "create-utf8-file.sh"
576 "echo $0: dirtying /tmp...\n"
579 "exec touch /tmp/λαμβδα"))))
580 (with-imported-modules '((guix build utils))
583 #$(file-append coreutils "/bin"))
584 (invoke #$(file-append bash "/bin/sh")
587 (define (run-cleanup-test name)
589 (marionette-operating-system %cleanup-os
590 #:imported-modules '((gnu services herd)
591 (guix combinators))))
593 (with-imported-modules '((gnu build marionette))
595 (use-modules (gnu build marionette)
600 (make-marionette (list #$(virtual-machine os))))
605 (test-begin "cleanup")
607 (test-assert "dirty service worked"
608 (marionette-eval '(file-exists? "/witness") marionette))
610 (test-equal "/tmp cleaned up"
612 (marionette-eval '(begin
613 (use-modules (ice-9 ftw))
618 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
620 (gexp->derivation "cleanup" test))
622 (define %test-cleanup
623 ;; See <https://bugs.gnu.org/26353>.
626 (description "Make sure the 'cleanup' service can remove files with
627 non-ASCII names from /tmp.")
628 (value (run-cleanup-test name))))
636 ;; System with an mcron service, with one mcron job for "root" and one mcron
637 ;; job for an unprivileged user.
638 (let ((job1 #~(job '(next-second '(0 5 10 15 20 25 30 35 40 45 50 55))
640 (unless (file-exists? "witness")
641 (call-with-output-file "witness"
643 (display (list (getuid) (getgid)) port)))))))
644 (job2 #~(job next-second-from
646 (call-with-output-file "witness"
648 (display (list (getuid) (getgid)) port))))
650 (job3 #~(job next-second-from ;to test $PATH
651 "touch witness-touch")))
652 (simple-operating-system
653 (service mcron-service-type
654 (mcron-configuration (jobs (list job1 job2 job3)))))))
656 (define (run-mcron-test name)
658 (marionette-operating-system
660 #:imported-modules '((gnu services herd)
661 (guix combinators))))
664 (with-imported-modules '((gnu build marionette))
666 (use-modules (gnu build marionette)
671 (make-marionette (list #$(virtual-machine os))))
678 (test-assert "service running"
681 (use-modules (gnu services herd))
682 (start-service 'mcron))
685 ;; Make sure root's mcron job runs, has its cwd set to "/root", and
686 ;; runs with the right UID/GID.
687 (test-equal "root's job"
689 (wait-for-file "/root/witness" marionette))
691 ;; Likewise for Alice's job. We cannot know what its GID is since
692 ;; it's chosen by 'groupadd', but it's strictly positive.
693 (test-assert "alice's job"
694 (match (wait-for-file "/home/alice/witness" marionette)
698 ;; Last, the job that uses a command; allows us to test whether
700 (test-equal "root's job with command"
702 (wait-for-file "/root/witness-touch" marionette
703 #:read '(@ (ice-9 rdelim) read-string)))
705 ;; Make sure the 'schedule' action is accepted.
706 (test-equal "schedule action"
708 (marionette-eval '(with-shepherd-action 'mcron ('schedule) result
713 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
715 (gexp->derivation name test))
720 (description "Make sure the mcron service works as advertised.")
721 (value (run-mcron-test name))))
725 ;;; Avahi and NSS-mDNS.
731 (name-service-switch %mdns-host-lookup-nss)
732 (services (cons* (service avahi-service-type
733 (avahi-configuration (debug? #t)))
735 (service dhcp-client-service-type) ;needed for multicast
737 ;; Enable heavyweight debugging output.
738 (modify-services (operating-system-user-services
740 (nscd-service-type config
741 => (nscd-configuration
744 (log-file "/dev/console")))
745 (syslog-service-type config
747 (syslog-configuration
752 "*.* /dev/console\n")))))))))
754 (define (run-nss-mdns-test)
755 ;; Test resolution of '.local' names via libc. Start the marionette service
756 ;; *after* nscd. Failing to do that, libc will try to connect to nscd,
757 ;; fail, then never try again (see '__nss_not_use_nscd_hosts' in libc),
758 ;; leading to '.local' resolution failures.
760 (marionette-operating-system
762 #:requirements '(nscd)
763 #:imported-modules '((gnu services herd)
764 (guix combinators))))
766 (define mdns-host-name
767 (string-append (operating-system-host-name os)
771 (with-imported-modules '((gnu build marionette))
773 (use-modules (gnu build marionette)
779 (make-marionette (list #$(virtual-machine os))))
786 (test-assert "nscd PID file is created"
789 (use-modules (gnu services herd))
790 (start-service 'nscd))
793 (test-assert "nscd is listening on its socket"
795 ;; XXX: Work around a race condition in nscd: nscd creates its
796 ;; PID file before it is listening on its socket.
797 '(let ((sock (socket PF_UNIX SOCK_STREAM 0)))
801 (connect sock AF_UNIX "/var/run/nscd/socket")
803 (format #t "nscd is ready~%")
806 (format #t "waiting for nscd...~%")
811 (test-assert "avahi is running"
814 (use-modules (gnu services herd))
815 (start-service 'avahi-daemon))
818 (test-assert "network is up"
821 (use-modules (gnu services herd))
822 (start-service 'networking))
825 (test-equal "avahi-resolve-host-name"
829 "/run/current-system/profile/bin/avahi-resolve-host-name"
830 "-v" #$mdns-host-name)
833 (test-equal "avahi-browse"
836 '(system* "avahi-browse" "-avt")
839 (test-assert "getaddrinfo .local"
840 ;; Wait for the 'avahi-daemon' service and perform a resolution.
841 (match (marionette-eval
842 '(getaddrinfo #$mdns-host-name)
844 (((? vector? addrinfos) ..1)
845 (pk 'getaddrinfo addrinfos)
846 (and (any (lambda (ai)
847 (= AF_INET (addrinfo:fam ai)))
850 (= AF_INET6 (addrinfo:fam ai)))
853 (test-assert "gethostbyname .local"
854 (match (pk 'gethostbyname
855 (marionette-eval '(gethostbyname #$mdns-host-name)
858 (and (string=? (hostent:name result) #$mdns-host-name)
859 (= (hostent:addrtype result) AF_INET)))))
863 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
865 (gexp->derivation "nss-mdns" test))
867 (define %test-nss-mdns
871 "Test Avahi's multicast-DNS implementation, and in particular, test its
872 glibc name service switch (NSS) module.")
873 (value (run-nss-mdns-test))))