1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2016, 2017, 2018 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-assert "shell and user commands"
127 ;; Is everything in $PATH?
128 (zero? (marionette-eval '(system "
137 (test-equal "special files"
141 (use-modules (ice-9 match))
145 (list file (readlink file))))
149 (test-assert "accounts"
150 (let ((users (marionette-eval '(begin
151 (use-modules (ice-9 match))
152 (let loop ((result '()))
154 (#f (reverse result))
155 (x (loop (cons x result))))))
158 (map passwd:name users)
160 #$@(map user-account-name
161 (operating-system-user-accounts os))))))
163 (test-assert "shepherd services"
164 (let ((services (marionette-eval
166 (use-modules (gnu services herd))
168 (map (compose car live-service-provision)
172 (pk 'services services)
173 '(root #$@(operating-system-shepherd-service-names os)))))
177 '#$(map user-account-home-directory
178 (filter user-account-create-home-directory?
179 (operating-system-user-accounts os)))))
182 (use-modules (gnu services herd) (srfi srfi-1))
184 ;; Home directories are supposed to exist once 'user-homes'
186 (start-service 'user-homes)
188 (every (lambda (home)
189 (and (file-exists? home)
190 (file-is-directory? home)))
194 (test-assert "skeletons in home directories"
196 '#$(filter-map (lambda (account)
197 (and (user-account-create-home-directory?
199 (not (user-account-system? account))
200 (list (user-account-name account)
201 (user-account-home-directory
203 (operating-system-user-accounts os))))
206 (use-modules (srfi srfi-1) (ice-9 ftw)
211 ;; Make sure HOME has all the skeletons...
212 (and (null? (lset-difference string=?
213 (scandir "/etc/skel/")
216 ;; ... and that everything is user-owned.
217 (let* ((pw (getpwnam user))
218 (uid (passwd:uid pw))
219 (gid (passwd:gid pw))
221 (define (user-owned? file)
222 (= uid (stat:uid (lstat file))))
224 (and (= uid (stat:uid st))
225 (eq? 'directory (stat:type st))
228 #:directories? #t)))))))
232 (test-equal "permissions on /root"
234 (let ((root-home #$(any (lambda (account)
235 (and (zero? (user-account-uid account))
236 (user-account-home-directory
238 (operating-system-user-accounts os))))
239 (stat:perms (marionette-eval `(stat ,root-home) marionette))))
241 (test-equal "no extra home directories"
244 ;; Make sure the home directories that are not supposed to be
245 ;; created are indeed not created.
247 '#$(filter-map (lambda (user)
249 (user-account-create-home-directory?
251 (user-account-home-directory user)))
252 (operating-system-user-accounts os))))
255 (use-modules (srfi srfi-1))
257 ;; Note: Do not flag "/var/empty".
259 ',(remove (cut string-prefix? "/var/" <>)
263 (test-equal "login on tty1"
266 (marionette-control "sendkey ctrl-alt-f1" marionette)
267 ;; Wait for the 'term-tty1' service to be running (using
268 ;; 'start-service' is the simplest and most reliable way to do
272 (use-modules (gnu services herd))
273 (start-service 'term-tty1))
277 (marionette-type "root\n\nid -un > logged-in\n" marionette)
279 ;; It can take a while before the shell commands are executed.
280 (marionette-eval '(use-modules (rnrs io ports)) marionette)
281 (wait-for-file "/root/logged-in" marionette
282 #:read 'get-string-all)))
284 ;; There should be one utmpx entry for the user logged in on tty1.
285 (test-equal "utmpx entry"
286 '(("root" "tty1" #f))
289 (use-modules (guix build syscalls)
292 (filter-map (lambda (entry)
293 (and (equal? (login-type USER_PROCESS)
294 (utmpx-login-type entry))
295 (list (utmpx-user entry) (utmpx-line entry)
296 (utmpx-host entry))))
300 ;; Likewise for /var/log/wtmp (used by 'last').
301 (test-assert "wtmp entry"
302 (match (marionette-eval
304 (use-modules (guix build syscalls)
307 (define (entry->list entry)
308 (list (utmpx-user entry) (utmpx-line entry)
309 (utmpx-host entry) (utmpx-login-type entry)))
311 (call-with-input-file "/var/log/wtmp"
313 (let loop ((result '()))
314 (if (eof-object? (peek-char port))
315 (map entry->list (reverse result))
316 (loop (cons (read-utmpx port) result)))))))
318 (((users lines hosts types) ..1)
319 (every (lambda (type)
320 (eqv? type (login-type LOGIN_PROCESS)))
323 (test-assert "host name resolution"
324 (match (marionette-eval
326 ;; Wait for nscd or our requests go through it.
327 (use-modules (gnu services herd))
328 (start-service 'nscd)
330 (list (getaddrinfo "localhost")
331 (getaddrinfo #$(operating-system-host-name os))))
333 ((((? vector?) ..1) ((? vector?) ..1))
336 (pk 'failure x #f))))
338 (test-equal "host not found"
341 '(false-if-exception (getaddrinfo "does-not-exist"))
346 (marionette-eval '(let ((before (setlocale LC_ALL "en_US.utf8")))
347 (setlocale LC_ALL before))
350 (test-eq "/run/current-system is a GC root"
352 (marionette-eval '(begin
353 ;; Make sure the (guix …) modules are found.
354 (eval-when (expand load eval)
356 (append (map (lambda (package)
357 (string-append package
359 (effective-version)))
363 (use-modules (srfi srfi-34) (guix store))
365 (let ((system (readlink "/run/current-system")))
366 (guard (c ((nix-protocol-error? c)
367 (and (file-exists? system)
370 (delete-paths store (list system))
374 ;; This symlink is currently unused, but better have it point to the
376 ;; <https://lists.gnu.org/archive/html/guix-devel/2016-08/msg01641.html>.
377 (test-equal "/var/guix/gcroots/profiles is a valid symlink"
379 (marionette-eval '(readlink "/var/guix/gcroots/profiles")
383 (test-assert "screendump"
385 (marionette-control (string-append "screendump " #$output
388 (file-exists? "tty1.ppm")))
390 (test-assert "screen text"
391 (let ((text (marionette-screen-text marionette
395 ;; Check whether the welcome message and shell prompt are
396 ;; displayed. Note: OCR confuses "y" and "V" for instance, so
397 ;; we cannot reliably match the whole text.
398 (and (string-contains text "This is the GNU")
399 (string-contains text
402 #$(operating-system-host-name os))))))
405 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
407 (gexp->derivation name test))
409 (define %test-basic-os
413 "Instrument %SIMPLE-OS, run it in a VM, and run a series of basic
414 functionality tests.")
416 (let* ((os (marionette-operating-system
418 #:imported-modules '((gnu services herd)
419 (guix combinators))))
420 (vm (virtual-machine os)))
421 ;; XXX: Add call to 'virtualized-operating-system' to get the exact same
422 ;; set of services as the OS produced by
423 ;; 'system-qemu-image/shared-store-script'.
424 (run-basic-test (virtualized-operating-system os '())
432 (define (run-halt-test vm)
433 ;; As reported in <http://bugs.gnu.org/26931>, running tmux would previously
434 ;; lead the 'stop' method of 'user-processes' to an infinite loop, with the
435 ;; tmux server process as a zombie that remains in the list of processes.
436 ;; This test reproduces this scenario.
438 (with-imported-modules '((gnu build marionette))
440 (use-modules (gnu build marionette))
443 (make-marionette '(#$vm)))
446 #$(file-append ocrad "/bin/ocrad"))
448 ;; Wait for tty1 and log in.
449 (marionette-eval '(begin
450 (use-modules (gnu services herd))
451 (start-service 'term-tty1))
453 (marionette-type "root\n" marionette)
454 (wait-for-screen-text marionette
456 (string-contains text "root@komputilo"))
459 ;; Start tmux and wait for it to be ready.
460 (marionette-type "tmux new-session 'echo 1 > /ready; bash'\n"
462 (wait-for-file "/ready" marionette)
464 ;; Make sure to stop the test after a while.
465 (sigaction SIGALRM (lambda _
466 (format (current-error-port)
467 "FAIL: Time is up, but VM still running.\n")
471 ;; Get debugging info.
472 (marionette-eval '(current-output-port
473 (open-file "/dev/console" "w0"))
475 (marionette-eval '(system* #$(file-append procps "/bin/ps")
476 "-eo" "pid,ppid,stat,comm")
479 ;; See if 'halt' actually works.
480 (marionette-eval '(system* "/run/current-system/profile/sbin/halt")
483 ;; If we reach this line, that means the VM was properly stopped in
486 (call-with-output-file #$output
488 (display "success!" port))))))
490 (gexp->derivation "halt" test))
496 "Use the 'halt' command and make sure it succeeds and does not get stuck
497 in a loop. See <http://bugs.gnu.org/26931>.")
499 (let ((os (marionette-operating-system
502 (packages (cons tmux %base-packages)))
503 #:imported-modules '((gnu services herd)
504 (guix combinators)))))
505 (run-halt-test (virtual-machine os))))))
509 ;;; Cleanup of /tmp, /var/run, etc.
513 (simple-operating-system
514 (simple-service 'dirty-things
516 (let ((script (plain-file
517 "create-utf8-file.sh"
519 "echo $0: dirtying /tmp...\n"
522 "exec touch /tmp/λαμβδα"))))
523 (with-imported-modules '((guix build utils))
526 #$(file-append coreutils "/bin"))
527 (invoke #$(file-append bash "/bin/sh")
530 (define (run-cleanup-test name)
532 (marionette-operating-system %cleanup-os
533 #:imported-modules '((gnu services herd)
534 (guix combinators))))
536 (with-imported-modules '((gnu build marionette))
538 (use-modules (gnu build marionette)
543 (make-marionette (list #$(virtual-machine os))))
548 (test-begin "cleanup")
550 (test-assert "dirty service worked"
551 (marionette-eval '(file-exists? "/witness") marionette))
553 (test-equal "/tmp cleaned up"
555 (marionette-eval '(begin
556 (use-modules (ice-9 ftw))
561 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
563 (gexp->derivation "cleanup" test))
565 (define %test-cleanup
566 ;; See <https://bugs.gnu.org/26353>.
569 (description "Make sure the 'cleanup' service can remove files with
570 non-ASCII names from /tmp.")
571 (value (run-cleanup-test name))))
579 ;; System with an mcron service, with one mcron job for "root" and one mcron
580 ;; job for an unprivileged user.
581 (let ((job1 #~(job '(next-second '(0 5 10 15 20 25 30 35 40 45 50 55))
583 (unless (file-exists? "witness")
584 (call-with-output-file "witness"
586 (display (list (getuid) (getgid)) port)))))))
587 (job2 #~(job next-second-from
589 (call-with-output-file "witness"
591 (display (list (getuid) (getgid)) port))))
593 (job3 #~(job next-second-from ;to test $PATH
594 "touch witness-touch")))
595 (simple-operating-system
596 (mcron-service (list job1 job2 job3)))))
598 (define (run-mcron-test name)
600 (marionette-operating-system
602 #:imported-modules '((gnu services herd)
603 (guix combinators))))
606 (with-imported-modules '((gnu build marionette))
608 (use-modules (gnu build marionette)
613 (make-marionette (list #$(virtual-machine os))))
620 (test-assert "service running"
623 (use-modules (gnu services herd))
624 (start-service 'mcron))
627 ;; Make sure root's mcron job runs, has its cwd set to "/root", and
628 ;; runs with the right UID/GID.
629 (test-equal "root's job"
631 (wait-for-file "/root/witness" marionette))
633 ;; Likewise for Alice's job. We cannot know what its GID is since
634 ;; it's chosen by 'groupadd', but it's strictly positive.
635 (test-assert "alice's job"
636 (match (wait-for-file "/home/alice/witness" marionette)
640 ;; Last, the job that uses a command; allows us to test whether
642 (test-equal "root's job with command"
644 (wait-for-file "/root/witness-touch" marionette
645 #:read '(@ (ice-9 rdelim) read-string)))
647 ;; Make sure the 'schedule' action is accepted.
648 (test-equal "schedule action"
650 (marionette-eval '(with-shepherd-action 'mcron ('schedule) result
655 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
657 (gexp->derivation name test))
662 (description "Make sure the mcron service works as advertised.")
663 (value (run-mcron-test name))))
667 ;;; Avahi and NSS-mDNS.
673 (name-service-switch %mdns-host-lookup-nss)
674 (services (cons* (avahi-service #:debug? #t)
676 (service dhcp-client-service-type) ;needed for multicast
678 ;; Enable heavyweight debugging output.
679 (modify-services (operating-system-user-services
681 (nscd-service-type config
682 => (nscd-configuration
685 (log-file "/dev/console")))
686 (syslog-service-type config
688 (syslog-configuration
693 "*.* /dev/console\n")))))))))
695 (define (run-nss-mdns-test)
696 ;; Test resolution of '.local' names via libc. Start the marionette service
697 ;; *after* nscd. Failing to do that, libc will try to connect to nscd,
698 ;; fail, then never try again (see '__nss_not_use_nscd_hosts' in libc),
699 ;; leading to '.local' resolution failures.
701 (marionette-operating-system
703 #:requirements '(nscd)
704 #:imported-modules '((gnu services herd)
705 (guix combinators))))
707 (define mdns-host-name
708 (string-append (operating-system-host-name os)
712 (with-imported-modules '((gnu build marionette))
714 (use-modules (gnu build marionette)
720 (make-marionette (list #$(virtual-machine os))))
727 (test-assert "nscd PID file is created"
730 (use-modules (gnu services herd))
731 (start-service 'nscd))
734 (test-assert "nscd is listening on its socket"
736 ;; XXX: Work around a race condition in nscd: nscd creates its
737 ;; PID file before it is listening on its socket.
738 '(let ((sock (socket PF_UNIX SOCK_STREAM 0)))
742 (connect sock AF_UNIX "/var/run/nscd/socket")
744 (format #t "nscd is ready~%")
747 (format #t "waiting for nscd...~%")
752 (test-assert "avahi is running"
755 (use-modules (gnu services herd))
756 (start-service 'avahi-daemon))
759 (test-assert "network is up"
762 (use-modules (gnu services herd))
763 (start-service 'networking))
766 (test-equal "avahi-resolve-host-name"
770 "/run/current-system/profile/bin/avahi-resolve-host-name"
771 "-v" #$mdns-host-name)
774 (test-equal "avahi-browse"
777 '(system* "avahi-browse" "-avt")
780 (test-assert "getaddrinfo .local"
781 ;; Wait for the 'avahi-daemon' service and perform a resolution.
782 (match (marionette-eval
783 '(getaddrinfo #$mdns-host-name)
785 (((? vector? addrinfos) ..1)
786 (pk 'getaddrinfo addrinfos)
787 (and (any (lambda (ai)
788 (= AF_INET (addrinfo:fam ai)))
791 (= AF_INET6 (addrinfo:fam ai)))
794 (test-assert "gethostbyname .local"
795 (match (pk 'gethostbyname
796 (marionette-eval '(gethostbyname #$mdns-host-name)
799 (and (string=? (hostent:name result) #$mdns-host-name)
800 (= (hostent:addrtype result) AF_INET)))))
804 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
806 (gexp->derivation "nss-mdns" test))
808 (define %test-nss-mdns
812 "Test Avahi's multicast-DNS implementation, and in particular, test its
813 glibc name service switch (NSS) module.")
814 (value (run-nss-mdns-test))))