services: guix: Add 'set-http-proxy' action.
[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-equal "/var/log/messages is not world-readable"
199 #o640 ;<https://bugs.gnu.org/40405>
200 (begin
201 (wait-for-file "/var/log/messages" marionette
202 #:read 'get-u8)
203 (marionette-eval '(stat:perms (lstat "/var/log/messages"))
204 marionette)))
205
206 (test-assert "homes"
207 (let ((homes
208 '#$(map user-account-home-directory
209 (filter user-account-create-home-directory?
210 (operating-system-user-accounts os)))))
211 (marionette-eval
212 `(begin
213 (use-modules (gnu services herd) (srfi srfi-1))
214
215 ;; Home directories are supposed to exist once 'user-homes'
216 ;; has been started.
217 (start-service 'user-homes)
218
219 (every (lambda (home)
220 (and (file-exists? home)
221 (file-is-directory? home)))
222 ',homes))
223 marionette)))
224
225 (test-assert "skeletons in home directories"
226 (let ((users+homes
227 '#$(filter-map (lambda (account)
228 (and (user-account-create-home-directory?
229 account)
230 (not (user-account-system? account))
231 (list (user-account-name account)
232 (user-account-home-directory
233 account))))
234 (operating-system-user-accounts os))))
235 (marionette-eval
236 `(begin
237 (use-modules (guix build utils) (srfi srfi-1)
238 (ice-9 ftw) (ice-9 match))
239
240 (every (match-lambda
241 ((user home)
242 ;; Make sure HOME has all the skeletons...
243 (and (null? (lset-difference string=?
244 (scandir "/etc/skel/")
245 (scandir home)))
246
247 ;; ... and that everything is user-owned.
248 (let* ((pw (getpwnam user))
249 (uid (passwd:uid pw))
250 (gid (passwd:gid pw))
251 (st (lstat home)))
252 (define (user-owned? file)
253 (= uid (stat:uid (lstat file))))
254
255 (and (= uid (stat:uid st))
256 (eq? 'directory (stat:type st))
257 (every user-owned?
258 (find-files home
259 #:directories? #t)))))))
260 ',users+homes))
261 marionette)))
262
263 (test-equal "permissions on /root"
264 #o700
265 (let ((root-home #$(any (lambda (account)
266 (and (zero? (user-account-uid account))
267 (user-account-home-directory
268 account)))
269 (operating-system-user-accounts os))))
270 (stat:perms (marionette-eval `(stat ,root-home) marionette))))
271
272 (test-equal "ownership and permissions of /var/empty"
273 '(0 0 #o555)
274 (let ((st (marionette-eval `(stat "/var/empty") marionette)))
275 (list (stat:uid st) (stat:gid st)
276 (stat:perms st))))
277
278 (test-equal "no extra home directories"
279 '()
280
281 ;; Make sure the home directories that are not supposed to be
282 ;; created are indeed not created.
283 (let ((nonexistent
284 '#$(filter-map (lambda (user)
285 (and (not
286 (user-account-create-home-directory?
287 user))
288 (user-account-home-directory user)))
289 (operating-system-user-accounts os))))
290 (marionette-eval
291 `(begin
292 (use-modules (srfi srfi-1))
293
294 ;; Note: Do not flag "/var/empty".
295 (filter file-exists?
296 ',(remove (cut string-prefix? "/var/" <>)
297 nonexistent)))
298 marionette)))
299
300 (test-equal "login on tty1"
301 "root\n"
302 (begin
303 (marionette-control "sendkey ctrl-alt-f1" marionette)
304 ;; Wait for the 'term-tty1' service to be running (using
305 ;; 'start-service' is the simplest and most reliable way to do
306 ;; that.)
307 (marionette-eval
308 '(begin
309 (use-modules (gnu services herd))
310 (start-service 'term-tty1))
311 marionette)
312
313 ;; Now we can type.
314 (let ((password #$root-password))
315 (if password
316 (begin
317 (marionette-type "root\n" marionette)
318 (wait-for-screen-text marionette
319 (lambda (text)
320 (string-contains text "Password"))
321 #:ocrad
322 #$(file-append ocrad "/bin/ocrad"))
323 (marionette-type (string-append password "\n\n")
324 marionette))
325 (marionette-type "root\n\n" marionette)))
326 (marionette-type "id -un > logged-in\n" marionette)
327
328 ;; It can take a while before the shell commands are executed.
329 (marionette-eval '(use-modules (rnrs io ports)) marionette)
330 (wait-for-file "/root/logged-in" marionette
331 #:read 'get-string-all)))
332
333 (test-equal "getlogin on tty1"
334 "\"root\""
335 (begin
336 ;; Assume we logged in in the previous test and type.
337 (marionette-type "guile -c '(write (getlogin))' > /root/login-id.tmp\n"
338 marionette)
339 (marionette-type "mv /root/login-id{.tmp,}\n"
340 marionette)
341
342 ;; It can take a while before the shell commands are executed.
343 (marionette-eval '(use-modules (rnrs io ports)) marionette)
344 (wait-for-file "/root/login-id" marionette
345 #:read 'get-string-all)))
346
347 ;; There should be one utmpx entry for the user logged in on tty1.
348 (test-equal "utmpx entry"
349 '(("root" "tty1" #f))
350 (marionette-eval
351 '(begin
352 (use-modules (guix build syscalls)
353 (srfi srfi-1))
354
355 (filter-map (lambda (entry)
356 (and (equal? (login-type USER_PROCESS)
357 (utmpx-login-type entry))
358 (list (utmpx-user entry) (utmpx-line entry)
359 (utmpx-host entry))))
360 (utmpx-entries)))
361 marionette))
362
363 ;; Likewise for /var/log/wtmp (used by 'last').
364 (test-assert "wtmp entry"
365 (match (marionette-eval
366 '(begin
367 (use-modules (guix build syscalls)
368 (srfi srfi-1))
369
370 (define (entry->list entry)
371 (list (utmpx-user entry) (utmpx-line entry)
372 (utmpx-host entry) (utmpx-login-type entry)))
373
374 (call-with-input-file "/var/log/wtmp"
375 (lambda (port)
376 (let loop ((result '()))
377 (if (eof-object? (peek-char port))
378 (map entry->list (reverse result))
379 (loop (cons (read-utmpx port) result)))))))
380 marionette)
381 (((users lines hosts types) ..1)
382 (every (lambda (type)
383 (eqv? type (login-type LOGIN_PROCESS)))
384 types))))
385
386 (test-assert "host name resolution"
387 (match (marionette-eval
388 '(begin
389 ;; Wait for nscd or our requests go through it.
390 (use-modules (gnu services herd))
391 (start-service 'nscd)
392
393 (list (getaddrinfo "localhost")
394 (getaddrinfo #$(operating-system-host-name os))))
395 marionette)
396 ((((? vector?) ..1) ((? vector?) ..1))
397 #t)
398 (x
399 (pk 'failure x #f))))
400
401 (test-equal "nscd invalidate action"
402 '(#t) ;one value, #t
403 (marionette-eval '(with-shepherd-action 'nscd ('invalidate "hosts")
404 result
405 result)
406 marionette))
407
408 ;; FIXME: The 'invalidate' action can't reliably obtain the exit
409 ;; code of 'nscd' so skip this test.
410 (test-skip 1)
411 (test-equal "nscd invalidate action, wrong table"
412 '(#f) ;one value, #f
413 (marionette-eval '(with-shepherd-action 'nscd ('invalidate "xyz")
414 result
415 result)
416 marionette))
417
418 (test-equal "host not found"
419 #f
420 (marionette-eval
421 '(false-if-exception (getaddrinfo "does-not-exist"))
422 marionette))
423
424 (test-equal "locale"
425 "en_US.utf8"
426 (marionette-eval '(let ((before (setlocale LC_ALL "en_US.utf8")))
427 (setlocale LC_ALL before))
428 marionette))
429
430 (test-eq "/run/current-system is a GC root"
431 'success!
432 (marionette-eval '(begin
433 ;; Make sure the (guix …) modules are found.
434 (eval-when (expand load eval)
435 (set! %load-path
436 (append (map (lambda (package)
437 (string-append package
438 "/share/guile/site/"
439 (effective-version)))
440 '#$guix&co)
441 %load-path)))
442
443 (use-modules (srfi srfi-34) (guix store))
444
445 (let ((system (readlink "/run/current-system")))
446 (guard (c ((store-protocol-error? c)
447 (and (file-exists? system)
448 'success!)))
449 (with-store store
450 (delete-paths store (list system))
451 #f))))
452 marionette))
453
454 ;; This symlink is currently unused, but better have it point to the
455 ;; right place. See
456 ;; <https://lists.gnu.org/archive/html/guix-devel/2016-08/msg01641.html>.
457 (test-equal "/var/guix/gcroots/profiles is a valid symlink"
458 "/var/guix/profiles"
459 (marionette-eval '(readlink "/var/guix/gcroots/profiles")
460 marionette))
461
462 (test-equal "guix-daemon set-http-proxy action"
463 '(#t) ;one value, #t
464 (marionette-eval '(with-shepherd-action 'guix-daemon
465 ('set-http-proxy "http://localhost:8118")
466 result
467 result)
468 marionette))
469
470 (test-equal "guix-daemon set-http-proxy action, clear"
471 '(#t) ;one value, #t
472 (marionette-eval '(with-shepherd-action 'guix-daemon
473 ('set-http-proxy)
474 result
475 result)
476 marionette))
477
478 (test-assert "screendump"
479 (begin
480 (marionette-control (string-append "screendump " #$output
481 "/tty1.ppm")
482 marionette)
483 (file-exists? "tty1.ppm")))
484
485 (test-assert "screen text"
486 (let ((text (marionette-screen-text marionette
487 #:ocrad
488 #$(file-append ocrad
489 "/bin/ocrad"))))
490 ;; Check whether the welcome message and shell prompt are
491 ;; displayed. Note: OCR confuses "y" and "V" for instance, so
492 ;; we cannot reliably match the whole text.
493 (and (string-contains text "This is the GNU")
494 (string-contains text
495 (string-append
496 "root@"
497 #$(operating-system-host-name os))))))
498
499 (test-end)
500 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
501
502 (gexp->derivation name test))
503
504 (define %test-basic-os
505 (system-test
506 (name "basic")
507 (description
508 "Instrument %SIMPLE-OS, run it in a VM, and run a series of basic
509 functionality tests.")
510 (value
511 (let* ((os (marionette-operating-system
512 %simple-os
513 #:imported-modules '((gnu services herd)
514 (guix combinators))))
515 (vm (virtual-machine os)))
516 ;; XXX: Add call to 'virtualized-operating-system' to get the exact same
517 ;; set of services as the OS produced by
518 ;; 'system-qemu-image/shared-store-script'.
519 (run-basic-test (virtualized-operating-system os '())
520 #~(list #$vm))))))
521
522 \f
523 ;;;
524 ;;; Halt.
525 ;;;
526
527 (define (run-halt-test vm)
528 ;; As reported in <http://bugs.gnu.org/26931>, running tmux would previously
529 ;; lead the 'stop' method of 'user-processes' to an infinite loop, with the
530 ;; tmux server process as a zombie that remains in the list of processes.
531 ;; This test reproduces this scenario.
532 (define test
533 (with-imported-modules '((gnu build marionette))
534 #~(begin
535 (use-modules (gnu build marionette))
536
537 (define marionette
538 (make-marionette '(#$vm)))
539
540 (define ocrad
541 #$(file-append ocrad "/bin/ocrad"))
542
543 ;; Wait for tty1 and log in.
544 (marionette-eval '(begin
545 (use-modules (gnu services herd))
546 (start-service 'term-tty1))
547 marionette)
548 (marionette-type "root\n" marionette)
549 (wait-for-screen-text marionette
550 (lambda (text)
551 (string-contains text "root@komputilo"))
552 #:ocrad ocrad)
553
554 ;; Start tmux and wait for it to be ready.
555 (marionette-type "tmux new-session 'echo 1 > /ready; bash'\n"
556 marionette)
557 (wait-for-file "/ready" marionette)
558
559 ;; Make sure to stop the test after a while.
560 (sigaction SIGALRM (lambda _
561 (format (current-error-port)
562 "FAIL: Time is up, but VM still running.\n")
563 (primitive-exit 1)))
564 (alarm 10)
565
566 ;; Get debugging info.
567 (marionette-eval '(current-output-port
568 (open-file "/dev/console" "w0"))
569 marionette)
570 (marionette-eval '(system* #$(file-append procps "/bin/ps")
571 "-eo" "pid,ppid,stat,comm")
572 marionette)
573
574 ;; See if 'halt' actually works.
575 (marionette-eval '(system* "/run/current-system/profile/sbin/halt")
576 marionette)
577
578 ;; If we reach this line, that means the VM was properly stopped in
579 ;; a timely fashion.
580 (alarm 0)
581 (call-with-output-file #$output
582 (lambda (port)
583 (display "success!" port))))))
584
585 (gexp->derivation "halt" test))
586
587 (define %test-halt
588 (system-test
589 (name "halt")
590 (description
591 "Use the 'halt' command and make sure it succeeds and does not get stuck
592 in a loop. See <http://bugs.gnu.org/26931>.")
593 (value
594 (let ((os (marionette-operating-system
595 (operating-system
596 (inherit %simple-os)
597 (packages (cons tmux %base-packages)))
598 #:imported-modules '((gnu services herd)
599 (guix combinators)))))
600 (run-halt-test (virtual-machine os))))))
601
602 \f
603 ;;;
604 ;;; Cleanup of /tmp, /var/run, etc.
605 ;;;
606
607 (define %cleanup-os
608 (simple-operating-system
609 (simple-service 'dirty-things
610 boot-service-type
611 (let ((script (plain-file
612 "create-utf8-file.sh"
613 (string-append
614 "echo $0: dirtying /tmp...\n"
615 "set -e; set -x\n"
616 "touch /witness\n"
617 "exec touch /tmp/λαμβδα"))))
618 (with-imported-modules '((guix build utils))
619 #~(begin
620 (setenv "PATH"
621 #$(file-append coreutils "/bin"))
622 (invoke #$(file-append bash "/bin/sh")
623 #$script)))))))
624
625 (define (run-cleanup-test name)
626 (define os
627 (marionette-operating-system %cleanup-os
628 #:imported-modules '((gnu services herd)
629 (guix combinators))))
630 (define test
631 (with-imported-modules '((gnu build marionette))
632 #~(begin
633 (use-modules (gnu build marionette)
634 (srfi srfi-64)
635 (ice-9 match))
636
637 (define marionette
638 (make-marionette (list #$(virtual-machine os))))
639
640 (mkdir #$output)
641 (chdir #$output)
642
643 (test-begin "cleanup")
644
645 (test-assert "dirty service worked"
646 (marionette-eval '(file-exists? "/witness") marionette))
647
648 (test-equal "/tmp cleaned up"
649 '("." "..")
650 (marionette-eval '(begin
651 (use-modules (ice-9 ftw))
652 (scandir "/tmp"))
653 marionette))
654
655 (test-end)
656 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
657
658 (gexp->derivation "cleanup" test))
659
660 (define %test-cleanup
661 ;; See <https://bugs.gnu.org/26353>.
662 (system-test
663 (name "cleanup")
664 (description "Make sure the 'cleanup' service can remove files with
665 non-ASCII names from /tmp.")
666 (value (run-cleanup-test name))))
667
668 \f
669 ;;;
670 ;;; Mcron.
671 ;;;
672
673 (define %mcron-os
674 ;; System with an mcron service, with one mcron job for "root" and one mcron
675 ;; job for an unprivileged user.
676 (let ((job1 #~(job '(next-second '(0 5 10 15 20 25 30 35 40 45 50 55))
677 (lambda ()
678 (unless (file-exists? "witness")
679 (call-with-output-file "witness"
680 (lambda (port)
681 (display (list (getuid) (getgid)) port)))))))
682 (job2 #~(job next-second-from
683 (lambda ()
684 (call-with-output-file "witness"
685 (lambda (port)
686 (display (list (getuid) (getgid)) port))))
687 #:user "alice"))
688 (job3 #~(job next-second-from ;to test $PATH
689 "touch witness-touch")))
690 (simple-operating-system
691 (service mcron-service-type
692 (mcron-configuration (jobs (list job1 job2 job3)))))))
693
694 (define (run-mcron-test name)
695 (define os
696 (marionette-operating-system
697 %mcron-os
698 #:imported-modules '((gnu services herd)
699 (guix combinators))))
700
701 (define test
702 (with-imported-modules '((gnu build marionette))
703 #~(begin
704 (use-modules (gnu build marionette)
705 (srfi srfi-64)
706 (ice-9 match))
707
708 (define marionette
709 (make-marionette (list #$(virtual-machine os))))
710
711 (mkdir #$output)
712 (chdir #$output)
713
714 (test-begin "mcron")
715
716 (test-assert "service running"
717 (marionette-eval
718 '(begin
719 (use-modules (gnu services herd))
720 (start-service 'mcron))
721 marionette))
722
723 ;; Make sure root's mcron job runs, has its cwd set to "/root", and
724 ;; runs with the right UID/GID.
725 (test-equal "root's job"
726 '(0 0)
727 (wait-for-file "/root/witness" marionette))
728
729 ;; Likewise for Alice's job. We cannot know what its GID is since
730 ;; it's chosen by 'groupadd', but it's strictly positive.
731 (test-assert "alice's job"
732 (match (wait-for-file "/home/alice/witness" marionette)
733 ((1000 gid)
734 (>= gid 100))))
735
736 ;; Last, the job that uses a command; allows us to test whether
737 ;; $PATH is sane.
738 (test-equal "root's job with command"
739 ""
740 (wait-for-file "/root/witness-touch" marionette
741 #:read '(@ (ice-9 rdelim) read-string)))
742
743 ;; Make sure the 'schedule' action is accepted.
744 (test-equal "schedule action"
745 '(#t) ;one value, #t
746 (marionette-eval '(with-shepherd-action 'mcron ('schedule) result
747 result)
748 marionette))
749
750 (test-end)
751 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
752
753 (gexp->derivation name test))
754
755 (define %test-mcron
756 (system-test
757 (name "mcron")
758 (description "Make sure the mcron service works as advertised.")
759 (value (run-mcron-test name))))
760
761 \f
762 ;;;
763 ;;; Avahi and NSS-mDNS.
764 ;;;
765
766 (define %avahi-os
767 (operating-system
768 (inherit %simple-os)
769 (name-service-switch %mdns-host-lookup-nss)
770 (services (cons* (service avahi-service-type
771 (avahi-configuration (debug? #t)))
772 (dbus-service)
773 (service dhcp-client-service-type) ;needed for multicast
774
775 ;; Enable heavyweight debugging output.
776 (modify-services (operating-system-user-services
777 %simple-os)
778 (nscd-service-type config
779 => (nscd-configuration
780 (inherit config)
781 (debug-level 3)
782 (log-file "/dev/console")))
783 (syslog-service-type config
784 =>
785 (syslog-configuration
786 (inherit config)
787 (config-file
788 (plain-file
789 "syslog.conf"
790 "*.* /dev/console\n")))))))))
791
792 (define (run-nss-mdns-test)
793 ;; Test resolution of '.local' names via libc. Start the marionette service
794 ;; *after* nscd. Failing to do that, libc will try to connect to nscd,
795 ;; fail, then never try again (see '__nss_not_use_nscd_hosts' in libc),
796 ;; leading to '.local' resolution failures.
797 (define os
798 (marionette-operating-system
799 %avahi-os
800 #:requirements '(nscd)
801 #:imported-modules '((gnu services herd)
802 (guix combinators))))
803
804 (define mdns-host-name
805 (string-append (operating-system-host-name os)
806 ".local"))
807
808 (define test
809 (with-imported-modules '((gnu build marionette))
810 #~(begin
811 (use-modules (gnu build marionette)
812 (srfi srfi-1)
813 (srfi srfi-64)
814 (ice-9 match))
815
816 (define marionette
817 (make-marionette (list #$(virtual-machine os))))
818
819 (mkdir #$output)
820 (chdir #$output)
821
822 (test-begin "avahi")
823
824 (test-assert "nscd PID file is created"
825 (marionette-eval
826 '(begin
827 (use-modules (gnu services herd))
828 (start-service 'nscd))
829 marionette))
830
831 (test-assert "nscd is listening on its socket"
832 (marionette-eval
833 ;; XXX: Work around a race condition in nscd: nscd creates its
834 ;; PID file before it is listening on its socket.
835 '(let ((sock (socket PF_UNIX SOCK_STREAM 0)))
836 (let try ()
837 (catch 'system-error
838 (lambda ()
839 (connect sock AF_UNIX "/var/run/nscd/socket")
840 (close-port sock)
841 (format #t "nscd is ready~%")
842 #t)
843 (lambda args
844 (format #t "waiting for nscd...~%")
845 (usleep 500000)
846 (try)))))
847 marionette))
848
849 (test-assert "avahi is running"
850 (marionette-eval
851 '(begin
852 (use-modules (gnu services herd))
853 (start-service 'avahi-daemon))
854 marionette))
855
856 (test-assert "network is up"
857 (marionette-eval
858 '(begin
859 (use-modules (gnu services herd))
860 (start-service 'networking))
861 marionette))
862
863 (test-equal "avahi-resolve-host-name"
864 0
865 (marionette-eval
866 '(system*
867 "/run/current-system/profile/bin/avahi-resolve-host-name"
868 "-v" #$mdns-host-name)
869 marionette))
870
871 (test-equal "avahi-browse"
872 0
873 (marionette-eval
874 '(system* "avahi-browse" "-avt")
875 marionette))
876
877 (test-assert "getaddrinfo .local"
878 ;; Wait for the 'avahi-daemon' service and perform a resolution.
879 (match (marionette-eval
880 '(getaddrinfo #$mdns-host-name)
881 marionette)
882 (((? vector? addrinfos) ..1)
883 (pk 'getaddrinfo addrinfos)
884 (and (any (lambda (ai)
885 (= AF_INET (addrinfo:fam ai)))
886 addrinfos)
887 (any (lambda (ai)
888 (= AF_INET6 (addrinfo:fam ai)))
889 addrinfos)))))
890
891 (test-assert "gethostbyname .local"
892 (match (pk 'gethostbyname
893 (marionette-eval '(gethostbyname #$mdns-host-name)
894 marionette))
895 ((? vector? result)
896 (and (string=? (hostent:name result) #$mdns-host-name)
897 (= (hostent:addrtype result) AF_INET)))))
898
899
900 (test-end)
901 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
902
903 (gexp->derivation "nss-mdns" test))
904
905 (define %test-nss-mdns
906 (system-test
907 (name "nss-mdns")
908 (description
909 "Test Avahi's multicast-DNS implementation, and in particular, test its
910 glibc name service switch (NSS) module.")
911 (value (run-nss-mdns-test))))