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