tests: 'run-basic-test' can enter a root password.
[jackhill/guix/guix.git] / gnu / tests / base.scm
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>
4 ;;;
5 ;;; This file is part of GNU Guix.
6 ;;;
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.
11 ;;;
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.
16 ;;;
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/>.
19
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
47 %test-basic-os
48 %test-halt
49 %test-cleanup
50 %test-mcron
51 %test-nss-mdns))
52
53 (define %simple-os
54 (simple-operating-system))
55
56 \f
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>.
62
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.
67
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."
70 (define special-files
71 (service-value
72 (fold-services (operating-system-services os)
73 #:target-type special-files-service-type)))
74
75 (define guix&co
76 (match (package-transitive-propagated-inputs guix)
77 (((labels packages) ...)
78 (cons guix packages))))
79
80 (define test
81 (with-imported-modules '((gnu build marionette)
82 (guix build syscalls))
83 #~(begin
84 (use-modules (gnu build marionette)
85 (guix build syscalls)
86 (srfi srfi-1)
87 (srfi srfi-26)
88 (srfi srfi-64)
89 (ice-9 match))
90
91 (define marionette
92 (make-marionette #$command))
93
94 (mkdir #$output)
95 (chdir #$output)
96
97 (test-begin "basic")
98
99 #$(and initialization
100 (initialization #~marionette))
101
102 (test-assert "uname"
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))
109 version)
110 (string-prefix? architecture %host-type)))))
111
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"
116 (marionette-eval
117 `(begin
118 (use-modules (gnu services herd))
119 (let loop ((i 10))
120 (cond ((file-exists? (%shepherd-socket-file))
121 #t)
122 ((> i 0)
123 (sleep 1)
124 (loop (- i 1)))
125 (else
126 #f))))
127 marionette))
128
129 (test-eq "stdin is /dev/null"
130 'eof
131 ;; Make sure services can no longer read from stdin once the
132 ;; system has booted.
133 (marionette-eval
134 `(begin
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)
140 'eof
141 result))))
142 marionette))
143
144 (test-assert "shell and user commands"
145 ;; Is everything in $PATH?
146 (zero? (marionette-eval '(system "
147 . /etc/profile
148 set -e -x
149 guix --version
150 ls --version
151 grep --version
152 info --version")
153 marionette)))
154
155 (test-equal "special files"
156 '#$special-files
157 (marionette-eval
158 '(begin
159 (use-modules (ice-9 match))
160
161 (map (match-lambda
162 ((file target)
163 (list file (readlink file))))
164 '#$special-files))
165 marionette))
166
167 (test-assert "accounts"
168 (let ((users (marionette-eval '(begin
169 (use-modules (ice-9 match))
170 (let loop ((result '()))
171 (match (getpw)
172 (#f (reverse result))
173 (x (loop (cons x result))))))
174 marionette)))
175 (lset= equal?
176 (map (lambda (user)
177 (list (passwd:name user)
178 (passwd:dir user)))
179 users)
180 (list
181 #$@(map (lambda (account)
182 `(list ,(user-account-name account)
183 ,(user-account-home-directory account)))
184 (operating-system-user-accounts os))))))
185
186 (test-assert "shepherd services"
187 (let ((services (marionette-eval
188 '(begin
189 (use-modules (gnu services herd))
190
191 (map (compose car live-service-provision)
192 (current-services)))
193 marionette)))
194 (lset= eq?
195 (pk 'services services)
196 '(root #$@(operating-system-shepherd-service-names os)))))
197
198 (test-assert "homes"
199 (let ((homes
200 '#$(map user-account-home-directory
201 (filter user-account-create-home-directory?
202 (operating-system-user-accounts os)))))
203 (marionette-eval
204 `(begin
205 (use-modules (gnu services herd) (srfi srfi-1))
206
207 ;; Home directories are supposed to exist once 'user-homes'
208 ;; has been started.
209 (start-service 'user-homes)
210
211 (every (lambda (home)
212 (and (file-exists? home)
213 (file-is-directory? home)))
214 ',homes))
215 marionette)))
216
217 (test-assert "skeletons in home directories"
218 (let ((users+homes
219 '#$(filter-map (lambda (account)
220 (and (user-account-create-home-directory?
221 account)
222 (not (user-account-system? account))
223 (list (user-account-name account)
224 (user-account-home-directory
225 account))))
226 (operating-system-user-accounts os))))
227 (marionette-eval
228 `(begin
229 (use-modules (guix build utils) (srfi srfi-1)
230 (ice-9 ftw) (ice-9 match))
231
232 (every (match-lambda
233 ((user home)
234 ;; Make sure HOME has all the skeletons...
235 (and (null? (lset-difference string=?
236 (scandir "/etc/skel/")
237 (scandir home)))
238
239 ;; ... and that everything is user-owned.
240 (let* ((pw (getpwnam user))
241 (uid (passwd:uid pw))
242 (gid (passwd:gid pw))
243 (st (lstat home)))
244 (define (user-owned? file)
245 (= uid (stat:uid (lstat file))))
246
247 (and (= uid (stat:uid st))
248 (eq? 'directory (stat:type st))
249 (every user-owned?
250 (find-files home
251 #:directories? #t)))))))
252 ',users+homes))
253 marionette)))
254
255 (test-equal "permissions on /root"
256 #o700
257 (let ((root-home #$(any (lambda (account)
258 (and (zero? (user-account-uid account))
259 (user-account-home-directory
260 account)))
261 (operating-system-user-accounts os))))
262 (stat:perms (marionette-eval `(stat ,root-home) marionette))))
263
264 (test-equal "ownership and permissions of /var/empty"
265 '(0 0 #o555)
266 (let ((st (marionette-eval `(stat "/var/empty") marionette)))
267 (list (stat:uid st) (stat:gid st)
268 (stat:perms st))))
269
270 (test-equal "no extra home directories"
271 '()
272
273 ;; Make sure the home directories that are not supposed to be
274 ;; created are indeed not created.
275 (let ((nonexistent
276 '#$(filter-map (lambda (user)
277 (and (not
278 (user-account-create-home-directory?
279 user))
280 (user-account-home-directory user)))
281 (operating-system-user-accounts os))))
282 (marionette-eval
283 `(begin
284 (use-modules (srfi srfi-1))
285
286 ;; Note: Do not flag "/var/empty".
287 (filter file-exists?
288 ',(remove (cut string-prefix? "/var/" <>)
289 nonexistent)))
290 marionette)))
291
292 (test-equal "login on tty1"
293 "root\n"
294 (begin
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
298 ;; that.)
299 (marionette-eval
300 '(begin
301 (use-modules (gnu services herd))
302 (start-service 'term-tty1))
303 marionette)
304
305 ;; Now we can type.
306 (let ((password #$root-password))
307 (if password
308 (begin
309 (marionette-type "root\n" marionette)
310 (wait-for-screen-text marionette
311 (lambda (text)
312 (string-contains text "Password"))
313 #:ocrad
314 #$(file-append ocrad "/bin/ocrad"))
315 (marionette-type (string-append password "\n\n")
316 marionette))
317 (marionette-type "root\n\n" marionette)))
318 (marionette-type "id -un > logged-in\n" marionette)
319
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)))
324
325 (test-equal "getlogin on tty1"
326 "\"root\""
327 (begin
328 ;; Assume we logged in in the previous test and type.
329 (marionette-type "guile -c '(write (getlogin))' > /root/login-id.tmp\n"
330 marionette)
331 (marionette-type "mv /root/login-id{.tmp,}\n"
332 marionette)
333
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)))
338
339 ;; There should be one utmpx entry for the user logged in on tty1.
340 (test-equal "utmpx entry"
341 '(("root" "tty1" #f))
342 (marionette-eval
343 '(begin
344 (use-modules (guix build syscalls)
345 (srfi srfi-1))
346
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))))
352 (utmpx-entries)))
353 marionette))
354
355 ;; Likewise for /var/log/wtmp (used by 'last').
356 (test-assert "wtmp entry"
357 (match (marionette-eval
358 '(begin
359 (use-modules (guix build syscalls)
360 (srfi srfi-1))
361
362 (define (entry->list entry)
363 (list (utmpx-user entry) (utmpx-line entry)
364 (utmpx-host entry) (utmpx-login-type entry)))
365
366 (call-with-input-file "/var/log/wtmp"
367 (lambda (port)
368 (let loop ((result '()))
369 (if (eof-object? (peek-char port))
370 (map entry->list (reverse result))
371 (loop (cons (read-utmpx port) result)))))))
372 marionette)
373 (((users lines hosts types) ..1)
374 (every (lambda (type)
375 (eqv? type (login-type LOGIN_PROCESS)))
376 types))))
377
378 (test-assert "host name resolution"
379 (match (marionette-eval
380 '(begin
381 ;; Wait for nscd or our requests go through it.
382 (use-modules (gnu services herd))
383 (start-service 'nscd)
384
385 (list (getaddrinfo "localhost")
386 (getaddrinfo #$(operating-system-host-name os))))
387 marionette)
388 ((((? vector?) ..1) ((? vector?) ..1))
389 #t)
390 (x
391 (pk 'failure x #f))))
392
393 (test-equal "nscd invalidate action"
394 '(#t) ;one value, #t
395 (marionette-eval '(with-shepherd-action 'nscd ('invalidate "hosts")
396 result
397 result)
398 marionette))
399
400 ;; FIXME: The 'invalidate' action can't reliably obtain the exit
401 ;; code of 'nscd' so skip this test.
402 (test-skip 1)
403 (test-equal "nscd invalidate action, wrong table"
404 '(#f) ;one value, #f
405 (marionette-eval '(with-shepherd-action 'nscd ('invalidate "xyz")
406 result
407 result)
408 marionette))
409
410 (test-equal "host not found"
411 #f
412 (marionette-eval
413 '(false-if-exception (getaddrinfo "does-not-exist"))
414 marionette))
415
416 (test-equal "locale"
417 "en_US.utf8"
418 (marionette-eval '(let ((before (setlocale LC_ALL "en_US.utf8")))
419 (setlocale LC_ALL before))
420 marionette))
421
422 (test-eq "/run/current-system is a GC root"
423 'success!
424 (marionette-eval '(begin
425 ;; Make sure the (guix …) modules are found.
426 (eval-when (expand load eval)
427 (set! %load-path
428 (append (map (lambda (package)
429 (string-append package
430 "/share/guile/site/"
431 (effective-version)))
432 '#$guix&co)
433 %load-path)))
434
435 (use-modules (srfi srfi-34) (guix store))
436
437 (let ((system (readlink "/run/current-system")))
438 (guard (c ((store-protocol-error? c)
439 (and (file-exists? system)
440 'success!)))
441 (with-store store
442 (delete-paths store (list system))
443 #f))))
444 marionette))
445
446 ;; This symlink is currently unused, but better have it point to the
447 ;; right place. See
448 ;; <https://lists.gnu.org/archive/html/guix-devel/2016-08/msg01641.html>.
449 (test-equal "/var/guix/gcroots/profiles is a valid symlink"
450 "/var/guix/profiles"
451 (marionette-eval '(readlink "/var/guix/gcroots/profiles")
452 marionette))
453
454
455 (test-assert "screendump"
456 (begin
457 (marionette-control (string-append "screendump " #$output
458 "/tty1.ppm")
459 marionette)
460 (file-exists? "tty1.ppm")))
461
462 (test-assert "screen text"
463 (let ((text (marionette-screen-text marionette
464 #:ocrad
465 #$(file-append ocrad
466 "/bin/ocrad"))))
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
472 (string-append
473 "root@"
474 #$(operating-system-host-name os))))))
475
476 (test-end)
477 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
478
479 (gexp->derivation name test))
480
481 (define %test-basic-os
482 (system-test
483 (name "basic")
484 (description
485 "Instrument %SIMPLE-OS, run it in a VM, and run a series of basic
486 functionality tests.")
487 (value
488 (let* ((os (marionette-operating-system
489 %simple-os
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 '())
497 #~(list #$vm))))))
498
499 \f
500 ;;;
501 ;;; Halt.
502 ;;;
503
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.
509 (define test
510 (with-imported-modules '((gnu build marionette))
511 #~(begin
512 (use-modules (gnu build marionette))
513
514 (define marionette
515 (make-marionette '(#$vm)))
516
517 (define ocrad
518 #$(file-append ocrad "/bin/ocrad"))
519
520 ;; Wait for tty1 and log in.
521 (marionette-eval '(begin
522 (use-modules (gnu services herd))
523 (start-service 'term-tty1))
524 marionette)
525 (marionette-type "root\n" marionette)
526 (wait-for-screen-text marionette
527 (lambda (text)
528 (string-contains text "root@komputilo"))
529 #:ocrad ocrad)
530
531 ;; Start tmux and wait for it to be ready.
532 (marionette-type "tmux new-session 'echo 1 > /ready; bash'\n"
533 marionette)
534 (wait-for-file "/ready" marionette)
535
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")
540 (primitive-exit 1)))
541 (alarm 10)
542
543 ;; Get debugging info.
544 (marionette-eval '(current-output-port
545 (open-file "/dev/console" "w0"))
546 marionette)
547 (marionette-eval '(system* #$(file-append procps "/bin/ps")
548 "-eo" "pid,ppid,stat,comm")
549 marionette)
550
551 ;; See if 'halt' actually works.
552 (marionette-eval '(system* "/run/current-system/profile/sbin/halt")
553 marionette)
554
555 ;; If we reach this line, that means the VM was properly stopped in
556 ;; a timely fashion.
557 (alarm 0)
558 (call-with-output-file #$output
559 (lambda (port)
560 (display "success!" port))))))
561
562 (gexp->derivation "halt" test))
563
564 (define %test-halt
565 (system-test
566 (name "halt")
567 (description
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>.")
570 (value
571 (let ((os (marionette-operating-system
572 (operating-system
573 (inherit %simple-os)
574 (packages (cons tmux %base-packages)))
575 #:imported-modules '((gnu services herd)
576 (guix combinators)))))
577 (run-halt-test (virtual-machine os))))))
578
579 \f
580 ;;;
581 ;;; Cleanup of /tmp, /var/run, etc.
582 ;;;
583
584 (define %cleanup-os
585 (simple-operating-system
586 (simple-service 'dirty-things
587 boot-service-type
588 (let ((script (plain-file
589 "create-utf8-file.sh"
590 (string-append
591 "echo $0: dirtying /tmp...\n"
592 "set -e; set -x\n"
593 "touch /witness\n"
594 "exec touch /tmp/λαμβδα"))))
595 (with-imported-modules '((guix build utils))
596 #~(begin
597 (setenv "PATH"
598 #$(file-append coreutils "/bin"))
599 (invoke #$(file-append bash "/bin/sh")
600 #$script)))))))
601
602 (define (run-cleanup-test name)
603 (define os
604 (marionette-operating-system %cleanup-os
605 #:imported-modules '((gnu services herd)
606 (guix combinators))))
607 (define test
608 (with-imported-modules '((gnu build marionette))
609 #~(begin
610 (use-modules (gnu build marionette)
611 (srfi srfi-64)
612 (ice-9 match))
613
614 (define marionette
615 (make-marionette (list #$(virtual-machine os))))
616
617 (mkdir #$output)
618 (chdir #$output)
619
620 (test-begin "cleanup")
621
622 (test-assert "dirty service worked"
623 (marionette-eval '(file-exists? "/witness") marionette))
624
625 (test-equal "/tmp cleaned up"
626 '("." "..")
627 (marionette-eval '(begin
628 (use-modules (ice-9 ftw))
629 (scandir "/tmp"))
630 marionette))
631
632 (test-end)
633 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
634
635 (gexp->derivation "cleanup" test))
636
637 (define %test-cleanup
638 ;; See <https://bugs.gnu.org/26353>.
639 (system-test
640 (name "cleanup")
641 (description "Make sure the 'cleanup' service can remove files with
642 non-ASCII names from /tmp.")
643 (value (run-cleanup-test name))))
644
645 \f
646 ;;;
647 ;;; Mcron.
648 ;;;
649
650 (define %mcron-os
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))
654 (lambda ()
655 (unless (file-exists? "witness")
656 (call-with-output-file "witness"
657 (lambda (port)
658 (display (list (getuid) (getgid)) port)))))))
659 (job2 #~(job next-second-from
660 (lambda ()
661 (call-with-output-file "witness"
662 (lambda (port)
663 (display (list (getuid) (getgid)) port))))
664 #:user "alice"))
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)))))))
670
671 (define (run-mcron-test name)
672 (define os
673 (marionette-operating-system
674 %mcron-os
675 #:imported-modules '((gnu services herd)
676 (guix combinators))))
677
678 (define test
679 (with-imported-modules '((gnu build marionette))
680 #~(begin
681 (use-modules (gnu build marionette)
682 (srfi srfi-64)
683 (ice-9 match))
684
685 (define marionette
686 (make-marionette (list #$(virtual-machine os))))
687
688 (mkdir #$output)
689 (chdir #$output)
690
691 (test-begin "mcron")
692
693 (test-assert "service running"
694 (marionette-eval
695 '(begin
696 (use-modules (gnu services herd))
697 (start-service 'mcron))
698 marionette))
699
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"
703 '(0 0)
704 (wait-for-file "/root/witness" marionette))
705
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)
710 ((1000 gid)
711 (>= gid 100))))
712
713 ;; Last, the job that uses a command; allows us to test whether
714 ;; $PATH is sane.
715 (test-equal "root's job with command"
716 ""
717 (wait-for-file "/root/witness-touch" marionette
718 #:read '(@ (ice-9 rdelim) read-string)))
719
720 ;; Make sure the 'schedule' action is accepted.
721 (test-equal "schedule action"
722 '(#t) ;one value, #t
723 (marionette-eval '(with-shepherd-action 'mcron ('schedule) result
724 result)
725 marionette))
726
727 (test-end)
728 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
729
730 (gexp->derivation name test))
731
732 (define %test-mcron
733 (system-test
734 (name "mcron")
735 (description "Make sure the mcron service works as advertised.")
736 (value (run-mcron-test name))))
737
738 \f
739 ;;;
740 ;;; Avahi and NSS-mDNS.
741 ;;;
742
743 (define %avahi-os
744 (operating-system
745 (inherit %simple-os)
746 (name-service-switch %mdns-host-lookup-nss)
747 (services (cons* (service avahi-service-type
748 (avahi-configuration (debug? #t)))
749 (dbus-service)
750 (service dhcp-client-service-type) ;needed for multicast
751
752 ;; Enable heavyweight debugging output.
753 (modify-services (operating-system-user-services
754 %simple-os)
755 (nscd-service-type config
756 => (nscd-configuration
757 (inherit config)
758 (debug-level 3)
759 (log-file "/dev/console")))
760 (syslog-service-type config
761 =>
762 (syslog-configuration
763 (inherit config)
764 (config-file
765 (plain-file
766 "syslog.conf"
767 "*.* /dev/console\n")))))))))
768
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.
774 (define os
775 (marionette-operating-system
776 %avahi-os
777 #:requirements '(nscd)
778 #:imported-modules '((gnu services herd)
779 (guix combinators))))
780
781 (define mdns-host-name
782 (string-append (operating-system-host-name os)
783 ".local"))
784
785 (define test
786 (with-imported-modules '((gnu build marionette))
787 #~(begin
788 (use-modules (gnu build marionette)
789 (srfi srfi-1)
790 (srfi srfi-64)
791 (ice-9 match))
792
793 (define marionette
794 (make-marionette (list #$(virtual-machine os))))
795
796 (mkdir #$output)
797 (chdir #$output)
798
799 (test-begin "avahi")
800
801 (test-assert "nscd PID file is created"
802 (marionette-eval
803 '(begin
804 (use-modules (gnu services herd))
805 (start-service 'nscd))
806 marionette))
807
808 (test-assert "nscd is listening on its socket"
809 (marionette-eval
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)))
813 (let try ()
814 (catch 'system-error
815 (lambda ()
816 (connect sock AF_UNIX "/var/run/nscd/socket")
817 (close-port sock)
818 (format #t "nscd is ready~%")
819 #t)
820 (lambda args
821 (format #t "waiting for nscd...~%")
822 (usleep 500000)
823 (try)))))
824 marionette))
825
826 (test-assert "avahi is running"
827 (marionette-eval
828 '(begin
829 (use-modules (gnu services herd))
830 (start-service 'avahi-daemon))
831 marionette))
832
833 (test-assert "network is up"
834 (marionette-eval
835 '(begin
836 (use-modules (gnu services herd))
837 (start-service 'networking))
838 marionette))
839
840 (test-equal "avahi-resolve-host-name"
841 0
842 (marionette-eval
843 '(system*
844 "/run/current-system/profile/bin/avahi-resolve-host-name"
845 "-v" #$mdns-host-name)
846 marionette))
847
848 (test-equal "avahi-browse"
849 0
850 (marionette-eval
851 '(system* "avahi-browse" "-avt")
852 marionette))
853
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)
858 marionette)
859 (((? vector? addrinfos) ..1)
860 (pk 'getaddrinfo addrinfos)
861 (and (any (lambda (ai)
862 (= AF_INET (addrinfo:fam ai)))
863 addrinfos)
864 (any (lambda (ai)
865 (= AF_INET6 (addrinfo:fam ai)))
866 addrinfos)))))
867
868 (test-assert "gethostbyname .local"
869 (match (pk 'gethostbyname
870 (marionette-eval '(gethostbyname #$mdns-host-name)
871 marionette))
872 ((? vector? result)
873 (and (string=? (hostent:name result) #$mdns-host-name)
874 (= (hostent:addrtype result) AF_INET)))))
875
876
877 (test-end)
878 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
879
880 (gexp->derivation "nss-mdns" test))
881
882 (define %test-nss-mdns
883 (system-test
884 (name "nss-mdns")
885 (description
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))))