1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2016, 2017, 2018, 2019, 2020 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")
58 #:key initialization root-password)
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.
68 When ROOT-PASSWORD is true, enter it as the root password when logging in.
69 Otherwise assume that there is no password for root."
72 (fold-services (operating-system-services os)
73 #:target-type special-files-service-type)))
76 (match (package-transitive-propagated-inputs guix)
77 (((labels packages) ...)
78 (cons guix packages))))
81 (with-imported-modules '((gnu build marionette)
82 (guix build syscalls))
84 (use-modules (gnu build marionette)
92 (make-marionette #$command))
100 (initialization #~marionette))
103 (match (marionette-eval '(uname) marionette)
104 (#("Linux" host-name version _ architecture)
105 (and (string=? host-name
106 #$(operating-system-host-name os))
107 (string-prefix? #$(package-version
108 (operating-system-kernel os))
110 (string-prefix? architecture %host-type)))))
112 ;; Shepherd reads the config file *before* binding its control
113 ;; socket, so /var/run/shepherd/socket might not exist yet when the
114 ;; 'marionette' service is started.
115 (test-assert "shepherd socket ready"
118 (use-modules (gnu services herd))
120 (cond ((file-exists? (%shepherd-socket-file))
129 (test-eq "stdin is /dev/null"
131 ;; Make sure services can no longer read from stdin once the
132 ;; system has booted.
135 (use-modules (gnu services herd))
136 (start 'user-processes)
137 ((@@ (gnu services herd) eval-there)
138 '(let ((result (read (current-input-port))))
139 (if (eof-object? result)
144 (test-assert "shell and user commands"
145 ;; Is everything in $PATH?
146 (zero? (marionette-eval '(system "
155 (test-equal "special files"
159 (use-modules (ice-9 match))
163 (list file (readlink file))))
167 (test-assert "accounts"
168 (let ((users (marionette-eval '(begin
169 (use-modules (ice-9 match))
170 (let loop ((result '()))
172 (#f (reverse result))
173 (x (loop (cons x result))))))
177 (list (passwd:name user)
181 #$@(map (lambda (account)
182 `(list ,(user-account-name account)
183 ,(user-account-home-directory account)))
184 (operating-system-user-accounts os))))))
186 (test-assert "shepherd services"
187 (let ((services (marionette-eval
189 (use-modules (gnu services herd))
191 (map (compose car live-service-provision)
195 (pk 'services services)
196 '(root #$@(operating-system-shepherd-service-names os)))))
200 '#$(map user-account-home-directory
201 (filter user-account-create-home-directory?
202 (operating-system-user-accounts os)))))
205 (use-modules (gnu services herd) (srfi srfi-1))
207 ;; Home directories are supposed to exist once 'user-homes'
209 (start-service 'user-homes)
211 (every (lambda (home)
212 (and (file-exists? home)
213 (file-is-directory? home)))
217 (test-assert "skeletons in home directories"
219 '#$(filter-map (lambda (account)
220 (and (user-account-create-home-directory?
222 (not (user-account-system? account))
223 (list (user-account-name account)
224 (user-account-home-directory
226 (operating-system-user-accounts os))))
229 (use-modules (guix build utils) (srfi srfi-1)
230 (ice-9 ftw) (ice-9 match))
234 ;; Make sure HOME has all the skeletons...
235 (and (null? (lset-difference string=?
236 (scandir "/etc/skel/")
239 ;; ... and that everything is user-owned.
240 (let* ((pw (getpwnam user))
241 (uid (passwd:uid pw))
242 (gid (passwd:gid pw))
244 (define (user-owned? file)
245 (= uid (stat:uid (lstat file))))
247 (and (= uid (stat:uid st))
248 (eq? 'directory (stat:type st))
251 #:directories? #t)))))))
255 (test-equal "permissions on /root"
257 (let ((root-home #$(any (lambda (account)
258 (and (zero? (user-account-uid account))
259 (user-account-home-directory
261 (operating-system-user-accounts os))))
262 (stat:perms (marionette-eval `(stat ,root-home) marionette))))
264 (test-equal "ownership and permissions of /var/empty"
266 (let ((st (marionette-eval `(stat "/var/empty") marionette)))
267 (list (stat:uid st) (stat:gid st)
270 (test-equal "no extra home directories"
273 ;; Make sure the home directories that are not supposed to be
274 ;; created are indeed not created.
276 '#$(filter-map (lambda (user)
278 (user-account-create-home-directory?
280 (user-account-home-directory user)))
281 (operating-system-user-accounts os))))
284 (use-modules (srfi srfi-1))
286 ;; Note: Do not flag "/var/empty".
288 ',(remove (cut string-prefix? "/var/" <>)
292 (test-equal "login on tty1"
295 (marionette-control "sendkey ctrl-alt-f1" marionette)
296 ;; Wait for the 'term-tty1' service to be running (using
297 ;; 'start-service' is the simplest and most reliable way to do
301 (use-modules (gnu services herd))
302 (start-service 'term-tty1))
306 (let ((password #$root-password))
309 (marionette-type "root\n" marionette)
310 (wait-for-screen-text marionette
312 (string-contains text "Password"))
314 #$(file-append ocrad "/bin/ocrad"))
315 (marionette-type (string-append password "\n\n")
317 (marionette-type "root\n\n" marionette)))
318 (marionette-type "id -un > logged-in\n" marionette)
320 ;; It can take a while before the shell commands are executed.
321 (marionette-eval '(use-modules (rnrs io ports)) marionette)
322 (wait-for-file "/root/logged-in" marionette
323 #:read 'get-string-all)))
325 (test-equal "getlogin on tty1"
328 ;; Assume we logged in in the previous test and type.
329 (marionette-type "guile -c '(write (getlogin))' > /root/login-id.tmp\n"
331 (marionette-type "mv /root/login-id{.tmp,}\n"
334 ;; It can take a while before the shell commands are executed.
335 (marionette-eval '(use-modules (rnrs io ports)) marionette)
336 (wait-for-file "/root/login-id" marionette
337 #:read 'get-string-all)))
339 ;; There should be one utmpx entry for the user logged in on tty1.
340 (test-equal "utmpx entry"
341 '(("root" "tty1" #f))
344 (use-modules (guix build syscalls)
347 (filter-map (lambda (entry)
348 (and (equal? (login-type USER_PROCESS)
349 (utmpx-login-type entry))
350 (list (utmpx-user entry) (utmpx-line entry)
351 (utmpx-host entry))))
355 ;; Likewise for /var/log/wtmp (used by 'last').
356 (test-assert "wtmp entry"
357 (match (marionette-eval
359 (use-modules (guix build syscalls)
362 (define (entry->list entry)
363 (list (utmpx-user entry) (utmpx-line entry)
364 (utmpx-host entry) (utmpx-login-type entry)))
366 (call-with-input-file "/var/log/wtmp"
368 (let loop ((result '()))
369 (if (eof-object? (peek-char port))
370 (map entry->list (reverse result))
371 (loop (cons (read-utmpx port) result)))))))
373 (((users lines hosts types) ..1)
374 (every (lambda (type)
375 (eqv? type (login-type LOGIN_PROCESS)))
378 (test-assert "host name resolution"
379 (match (marionette-eval
381 ;; Wait for nscd or our requests go through it.
382 (use-modules (gnu services herd))
383 (start-service 'nscd)
385 (list (getaddrinfo "localhost")
386 (getaddrinfo #$(operating-system-host-name os))))
388 ((((? vector?) ..1) ((? vector?) ..1))
391 (pk 'failure x #f))))
393 (test-equal "nscd invalidate action"
395 (marionette-eval '(with-shepherd-action 'nscd ('invalidate "hosts")
400 ;; FIXME: The 'invalidate' action can't reliably obtain the exit
401 ;; code of 'nscd' so skip this test.
403 (test-equal "nscd invalidate action, wrong table"
405 (marionette-eval '(with-shepherd-action 'nscd ('invalidate "xyz")
410 (test-equal "host not found"
413 '(false-if-exception (getaddrinfo "does-not-exist"))
418 (marionette-eval '(let ((before (setlocale LC_ALL "en_US.utf8")))
419 (setlocale LC_ALL before))
422 (test-eq "/run/current-system is a GC root"
424 (marionette-eval '(begin
425 ;; Make sure the (guix …) modules are found.
426 (eval-when (expand load eval)
428 (append (map (lambda (package)
429 (string-append package
431 (effective-version)))
435 (use-modules (srfi srfi-34) (guix store))
437 (let ((system (readlink "/run/current-system")))
438 (guard (c ((store-protocol-error? c)
439 (and (file-exists? system)
442 (delete-paths store (list system))
446 ;; This symlink is currently unused, but better have it point to the
448 ;; <https://lists.gnu.org/archive/html/guix-devel/2016-08/msg01641.html>.
449 (test-equal "/var/guix/gcroots/profiles is a valid symlink"
451 (marionette-eval '(readlink "/var/guix/gcroots/profiles")
455 (test-assert "screendump"
457 (marionette-control (string-append "screendump " #$output
460 (file-exists? "tty1.ppm")))
462 (test-assert "screen text"
463 (let ((text (marionette-screen-text marionette
467 ;; Check whether the welcome message and shell prompt are
468 ;; displayed. Note: OCR confuses "y" and "V" for instance, so
469 ;; we cannot reliably match the whole text.
470 (and (string-contains text "This is the GNU")
471 (string-contains text
474 #$(operating-system-host-name os))))))
477 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
479 (gexp->derivation name test))
481 (define %test-basic-os
485 "Instrument %SIMPLE-OS, run it in a VM, and run a series of basic
486 functionality tests.")
488 (let* ((os (marionette-operating-system
490 #:imported-modules '((gnu services herd)
491 (guix combinators))))
492 (vm (virtual-machine os)))
493 ;; XXX: Add call to 'virtualized-operating-system' to get the exact same
494 ;; set of services as the OS produced by
495 ;; 'system-qemu-image/shared-store-script'.
496 (run-basic-test (virtualized-operating-system os '())
504 (define (run-halt-test vm)
505 ;; As reported in <http://bugs.gnu.org/26931>, running tmux would previously
506 ;; lead the 'stop' method of 'user-processes' to an infinite loop, with the
507 ;; tmux server process as a zombie that remains in the list of processes.
508 ;; This test reproduces this scenario.
510 (with-imported-modules '((gnu build marionette))
512 (use-modules (gnu build marionette))
515 (make-marionette '(#$vm)))
518 #$(file-append ocrad "/bin/ocrad"))
520 ;; Wait for tty1 and log in.
521 (marionette-eval '(begin
522 (use-modules (gnu services herd))
523 (start-service 'term-tty1))
525 (marionette-type "root\n" marionette)
526 (wait-for-screen-text marionette
528 (string-contains text "root@komputilo"))
531 ;; Start tmux and wait for it to be ready.
532 (marionette-type "tmux new-session 'echo 1 > /ready; bash'\n"
534 (wait-for-file "/ready" marionette)
536 ;; Make sure to stop the test after a while.
537 (sigaction SIGALRM (lambda _
538 (format (current-error-port)
539 "FAIL: Time is up, but VM still running.\n")
543 ;; Get debugging info.
544 (marionette-eval '(current-output-port
545 (open-file "/dev/console" "w0"))
547 (marionette-eval '(system* #$(file-append procps "/bin/ps")
548 "-eo" "pid,ppid,stat,comm")
551 ;; See if 'halt' actually works.
552 (marionette-eval '(system* "/run/current-system/profile/sbin/halt")
555 ;; If we reach this line, that means the VM was properly stopped in
558 (call-with-output-file #$output
560 (display "success!" port))))))
562 (gexp->derivation "halt" test))
568 "Use the 'halt' command and make sure it succeeds and does not get stuck
569 in a loop. See <http://bugs.gnu.org/26931>.")
571 (let ((os (marionette-operating-system
574 (packages (cons tmux %base-packages)))
575 #:imported-modules '((gnu services herd)
576 (guix combinators)))))
577 (run-halt-test (virtual-machine os))))))
581 ;;; Cleanup of /tmp, /var/run, etc.
585 (simple-operating-system
586 (simple-service 'dirty-things
588 (let ((script (plain-file
589 "create-utf8-file.sh"
591 "echo $0: dirtying /tmp...\n"
594 "exec touch /tmp/λαμβδα"))))
595 (with-imported-modules '((guix build utils))
598 #$(file-append coreutils "/bin"))
599 (invoke #$(file-append bash "/bin/sh")
602 (define (run-cleanup-test name)
604 (marionette-operating-system %cleanup-os
605 #:imported-modules '((gnu services herd)
606 (guix combinators))))
608 (with-imported-modules '((gnu build marionette))
610 (use-modules (gnu build marionette)
615 (make-marionette (list #$(virtual-machine os))))
620 (test-begin "cleanup")
622 (test-assert "dirty service worked"
623 (marionette-eval '(file-exists? "/witness") marionette))
625 (test-equal "/tmp cleaned up"
627 (marionette-eval '(begin
628 (use-modules (ice-9 ftw))
633 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
635 (gexp->derivation "cleanup" test))
637 (define %test-cleanup
638 ;; See <https://bugs.gnu.org/26353>.
641 (description "Make sure the 'cleanup' service can remove files with
642 non-ASCII names from /tmp.")
643 (value (run-cleanup-test name))))
651 ;; System with an mcron service, with one mcron job for "root" and one mcron
652 ;; job for an unprivileged user.
653 (let ((job1 #~(job '(next-second '(0 5 10 15 20 25 30 35 40 45 50 55))
655 (unless (file-exists? "witness")
656 (call-with-output-file "witness"
658 (display (list (getuid) (getgid)) port)))))))
659 (job2 #~(job next-second-from
661 (call-with-output-file "witness"
663 (display (list (getuid) (getgid)) port))))
665 (job3 #~(job next-second-from ;to test $PATH
666 "touch witness-touch")))
667 (simple-operating-system
668 (service mcron-service-type
669 (mcron-configuration (jobs (list job1 job2 job3)))))))
671 (define (run-mcron-test name)
673 (marionette-operating-system
675 #:imported-modules '((gnu services herd)
676 (guix combinators))))
679 (with-imported-modules '((gnu build marionette))
681 (use-modules (gnu build marionette)
686 (make-marionette (list #$(virtual-machine os))))
693 (test-assert "service running"
696 (use-modules (gnu services herd))
697 (start-service 'mcron))
700 ;; Make sure root's mcron job runs, has its cwd set to "/root", and
701 ;; runs with the right UID/GID.
702 (test-equal "root's job"
704 (wait-for-file "/root/witness" marionette))
706 ;; Likewise for Alice's job. We cannot know what its GID is since
707 ;; it's chosen by 'groupadd', but it's strictly positive.
708 (test-assert "alice's job"
709 (match (wait-for-file "/home/alice/witness" marionette)
713 ;; Last, the job that uses a command; allows us to test whether
715 (test-equal "root's job with command"
717 (wait-for-file "/root/witness-touch" marionette
718 #:read '(@ (ice-9 rdelim) read-string)))
720 ;; Make sure the 'schedule' action is accepted.
721 (test-equal "schedule action"
723 (marionette-eval '(with-shepherd-action 'mcron ('schedule) result
728 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
730 (gexp->derivation name test))
735 (description "Make sure the mcron service works as advertised.")
736 (value (run-mcron-test name))))
740 ;;; Avahi and NSS-mDNS.
746 (name-service-switch %mdns-host-lookup-nss)
747 (services (cons* (service avahi-service-type
748 (avahi-configuration (debug? #t)))
750 (service dhcp-client-service-type) ;needed for multicast
752 ;; Enable heavyweight debugging output.
753 (modify-services (operating-system-user-services
755 (nscd-service-type config
756 => (nscd-configuration
759 (log-file "/dev/console")))
760 (syslog-service-type config
762 (syslog-configuration
767 "*.* /dev/console\n")))))))))
769 (define (run-nss-mdns-test)
770 ;; Test resolution of '.local' names via libc. Start the marionette service
771 ;; *after* nscd. Failing to do that, libc will try to connect to nscd,
772 ;; fail, then never try again (see '__nss_not_use_nscd_hosts' in libc),
773 ;; leading to '.local' resolution failures.
775 (marionette-operating-system
777 #:requirements '(nscd)
778 #:imported-modules '((gnu services herd)
779 (guix combinators))))
781 (define mdns-host-name
782 (string-append (operating-system-host-name os)
786 (with-imported-modules '((gnu build marionette))
788 (use-modules (gnu build marionette)
794 (make-marionette (list #$(virtual-machine os))))
801 (test-assert "nscd PID file is created"
804 (use-modules (gnu services herd))
805 (start-service 'nscd))
808 (test-assert "nscd is listening on its socket"
810 ;; XXX: Work around a race condition in nscd: nscd creates its
811 ;; PID file before it is listening on its socket.
812 '(let ((sock (socket PF_UNIX SOCK_STREAM 0)))
816 (connect sock AF_UNIX "/var/run/nscd/socket")
818 (format #t "nscd is ready~%")
821 (format #t "waiting for nscd...~%")
826 (test-assert "avahi is running"
829 (use-modules (gnu services herd))
830 (start-service 'avahi-daemon))
833 (test-assert "network is up"
836 (use-modules (gnu services herd))
837 (start-service 'networking))
840 (test-equal "avahi-resolve-host-name"
844 "/run/current-system/profile/bin/avahi-resolve-host-name"
845 "-v" #$mdns-host-name)
848 (test-equal "avahi-browse"
851 '(system* "avahi-browse" "-avt")
854 (test-assert "getaddrinfo .local"
855 ;; Wait for the 'avahi-daemon' service and perform a resolution.
856 (match (marionette-eval
857 '(getaddrinfo #$mdns-host-name)
859 (((? vector? addrinfos) ..1)
860 (pk 'getaddrinfo addrinfos)
861 (and (any (lambda (ai)
862 (= AF_INET (addrinfo:fam ai)))
865 (= AF_INET6 (addrinfo:fam ai)))
868 (test-assert "gethostbyname .local"
869 (match (pk 'gethostbyname
870 (marionette-eval '(gethostbyname #$mdns-host-name)
873 (and (string=? (hostent:name result) #$mdns-host-name)
874 (= (hostent:addrtype result) AF_INET)))))
878 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
880 (gexp->derivation "nss-mdns" test))
882 (define %test-nss-mdns
886 "Test Avahi's multicast-DNS implementation, and in particular, test its
887 glibc name service switch (NSS) module.")
888 (value (run-nss-mdns-test))))