gnu: Add r-htscluster.
[jackhill/guix/guix.git] / gnu / tests / base.scm
CommitLineData
e9f693d0 1;;; GNU Guix --- Functional package management for GNU
84a2de36 2;;; Copyright © 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
c24b1547 3;;; Copyright © 2018 Clément Lassieur <clement@lassieur.org>
e9f693d0
LC
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)
e9f693d0 23 #:use-module (gnu system shadow)
d2fa61bc 24 #:use-module (gnu system nss)
e9f693d0
LC
25 #:use-module (gnu system vm)
26 #:use-module (gnu services)
d2fa61bc
LC
27 #:use-module (gnu services base)
28 #:use-module (gnu services dbus)
29 #:use-module (gnu services avahi)
c311089b 30 #:use-module (gnu services mcron)
e9f693d0 31 #:use-module (gnu services shepherd)
d2fa61bc 32 #:use-module (gnu services networking)
76c321d8
LC
33 #:use-module (gnu packages base)
34 #:use-module (gnu packages bash)
fe933833
LC
35 #:use-module (gnu packages imagemagick)
36 #:use-module (gnu packages ocr)
e2f9832f 37 #:use-module (gnu packages package-management)
7f090203
LC
38 #:use-module (gnu packages linux)
39 #:use-module (gnu packages tmux)
e9f693d0
LC
40 #:use-module (guix gexp)
41 #:use-module (guix store)
76c321d8 42 #:use-module (guix monads)
e9f693d0
LC
43 #:use-module (guix packages)
44 #:use-module (srfi srfi-1)
4ab6a2f2 45 #:use-module (ice-9 match)
e3de272a 46 #:export (run-basic-test
c311089b 47 %test-basic-os
7f090203 48 %test-halt
76c321d8 49 %test-cleanup
d2fa61bc
LC
50 %test-mcron
51 %test-nss-mdns))
e9f693d0
LC
52
53(define %simple-os
892d9089 54 (simple-operating-system))
e9f693d0
LC
55
56\f
f7f292d3
LC
57(define* (run-basic-test os command #:optional (name "basic")
58 #:key initialization)
e3de272a
LC
59 "Return a derivation called NAME that tests basic features of the OS started
60using COMMAND, a gexp that evaluates to a list of strings. Compare some
f7f292d3
LC
61properties of running system to what's declared in OS, an <operating-system>.
62
63When INITIALIZATION is true, it must be a one-argument procedure that is
64passed a gexp denoting the marionette, and it must return gexp that is
65inserted before the first test. This is used to introduce an extra
66initialization step, such as entering a LUKS passphrase."
387e1754 67 (define special-files
efe7d19a 68 (service-value
387e1754
LC
69 (fold-services (operating-system-services os)
70 #:target-type special-files-service-type)))
71
4ab6a2f2
LC
72 (define guix&co
73 (match (package-transitive-propagated-inputs guix)
74 (((labels packages) ...)
75 (cons guix packages))))
76
e3de272a 77 (define test
caa78166
LC
78 (with-imported-modules '((gnu build marionette)
79 (guix build syscalls))
4ee96a79
LC
80 #~(begin
81 (use-modules (gnu build marionette)
caa78166 82 (guix build syscalls)
4ee96a79
LC
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
f7f292d3
LC
96 #$(and initialization
97 (initialization #~marionette))
98
4ee96a79
LC
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
3c78f5b5
LC
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
6ea6e147
LC
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
4ee96a79
LC
141 (test-assert "shell and user commands"
142 ;; Is everything in $PATH?
143 (zero? (marionette-eval '(system "
e3de272a
LC
144. /etc/profile
145set -e -x
146guix --version
147ls --version
148grep --version
149info --version")
4ee96a79
LC
150 marionette)))
151
387e1754
LC
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
4ee96a79
LC
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)))
b2979344
LC
172 (lset= equal?
173 (map (lambda (user)
174 (list (passwd:name user)
175 (passwd:dir user)))
176 users)
4ee96a79 177 (list
b2979344
LC
178 #$@(map (lambda (account)
179 `(list ,(user-account-name account)
180 ,(user-account-home-directory account)))
4ee96a79
LC
181 (operating-system-user-accounts os))))))
182
183 (test-assert "shepherd services"
183605c8
LC
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)))
4ee96a79
LC
191 (lset= eq?
192 (pk 'services services)
193 '(root #$@(operating-system-shepherd-service-names os)))))
194
ae763b5b
LC
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"
cf98d342 215 (let ((users+homes
ae763b5b
LC
216 '#$(filter-map (lambda (account)
217 (and (user-account-create-home-directory?
218 account)
219 (not (user-account-system? account))
cf98d342
LC
220 (list (user-account-name account)
221 (user-account-home-directory
222 account))))
ae763b5b
LC
223 (operating-system-user-accounts os))))
224 (marionette-eval
225 `(begin
cf98d342
LC
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))
ae763b5b
LC
250 marionette)))
251
41db5a75
LC
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
41f76ae0
LC
261 (test-equal "no extra home directories"
262 '()
263
264 ;; Make sure the home directories that are not supposed to be
265 ;; created are indeed not created.
266 (let ((nonexistent
267 '#$(filter-map (lambda (user)
268 (and (not
269 (user-account-create-home-directory?
270 user))
271 (user-account-home-directory user)))
272 (operating-system-user-accounts os))))
273 (marionette-eval
274 `(begin
275 (use-modules (srfi srfi-1))
276
277 ;; Note: Do not flag "/var/empty".
278 (filter file-exists?
279 ',(remove (cut string-prefix? "/var/" <>)
280 nonexistent)))
281 marionette)))
282
4ee96a79
LC
283 (test-equal "login on tty1"
284 "root\n"
285 (begin
286 (marionette-control "sendkey ctrl-alt-f1" marionette)
287 ;; Wait for the 'term-tty1' service to be running (using
288 ;; 'start-service' is the simplest and most reliable way to do
289 ;; that.)
290 (marionette-eval
291 '(begin
292 (use-modules (gnu services herd))
293 (start-service 'term-tty1))
294 marionette)
295
296 ;; Now we can type.
297 (marionette-type "root\n\nid -un > logged-in\n" marionette)
298
299 ;; It can take a while before the shell commands are executed.
4ee96a79 300 (marionette-eval '(use-modules (rnrs io ports)) marionette)
13877c34
LC
301 (wait-for-file "/root/logged-in" marionette
302 #:read 'get-string-all)))
4ee96a79 303
caa78166
LC
304 ;; There should be one utmpx entry for the user logged in on tty1.
305 (test-equal "utmpx entry"
306 '(("root" "tty1" #f))
307 (marionette-eval
308 '(begin
309 (use-modules (guix build syscalls)
310 (srfi srfi-1))
311
312 (filter-map (lambda (entry)
313 (and (equal? (login-type USER_PROCESS)
314 (utmpx-login-type entry))
315 (list (utmpx-user entry) (utmpx-line entry)
316 (utmpx-host entry))))
317 (utmpx-entries)))
318 marionette))
319
2986995b
LC
320 ;; Likewise for /var/log/wtmp (used by 'last').
321 (test-assert "wtmp entry"
322 (match (marionette-eval
323 '(begin
324 (use-modules (guix build syscalls)
325 (srfi srfi-1))
326
327 (define (entry->list entry)
328 (list (utmpx-user entry) (utmpx-line entry)
329 (utmpx-host entry) (utmpx-login-type entry)))
330
331 (call-with-input-file "/var/log/wtmp"
332 (lambda (port)
333 (let loop ((result '()))
334 (if (eof-object? (peek-char port))
335 (map entry->list (reverse result))
336 (loop (cons (read-utmpx port) result)))))))
337 marionette)
338 (((users lines hosts types) ..1)
339 (every (lambda (type)
340 (eqv? type (login-type LOGIN_PROCESS)))
341 types))))
342
4ee96a79
LC
343 (test-assert "host name resolution"
344 (match (marionette-eval
345 '(begin
346 ;; Wait for nscd or our requests go through it.
347 (use-modules (gnu services herd))
348 (start-service 'nscd)
349
350 (list (getaddrinfo "localhost")
351 (getaddrinfo #$(operating-system-host-name os))))
352 marionette)
353 ((((? vector?) ..1) ((? vector?) ..1))
354 #t)
355 (x
356 (pk 'failure x #f))))
357
d3f75179
LC
358 (test-equal "nscd invalidate action"
359 '(#t) ;one value, #t
360 (marionette-eval '(with-shepherd-action 'nscd ('invalidate "hosts")
361 result
362 result)
363 marionette))
364
365 (test-equal "nscd invalidate action, wrong table"
366 '(#f) ;one value, #f
367 (marionette-eval '(with-shepherd-action 'nscd ('invalidate "xyz")
368 result
369 result)
370 marionette))
371
4ee96a79
LC
372 (test-equal "host not found"
373 #f
e3de272a 374 (marionette-eval
4ee96a79
LC
375 '(false-if-exception (getaddrinfo "does-not-exist"))
376 marionette))
377
ab3a6450
LC
378 (test-equal "locale"
379 "en_US.utf8"
cc73339b
LC
380 (marionette-eval '(let ((before (setlocale LC_ALL "en_US.utf8")))
381 (setlocale LC_ALL before))
ab3a6450
LC
382 marionette))
383
d5094c81
LC
384 (test-eq "/run/current-system is a GC root"
385 'success!
40d28609
LC
386 (marionette-eval '(begin
387 ;; Make sure the (guix …) modules are found.
4ab6a2f2
LC
388 (eval-when (expand load eval)
389 (set! %load-path
390 (append (map (lambda (package)
391 (string-append package
392 "/share/guile/site/"
393 (effective-version)))
394 '#$guix&co)
395 %load-path)))
40d28609
LC
396
397 (use-modules (srfi srfi-34) (guix store))
398
399 (let ((system (readlink "/run/current-system")))
400 (guard (c ((nix-protocol-error? c)
d5094c81
LC
401 (and (file-exists? system)
402 'success!)))
40d28609
LC
403 (with-store store
404 (delete-paths store (list system))
405 #f))))
406 marionette))
407
334bda9a
LC
408 ;; This symlink is currently unused, but better have it point to the
409 ;; right place. See
410 ;; <https://lists.gnu.org/archive/html/guix-devel/2016-08/msg01641.html>.
411 (test-equal "/var/guix/gcroots/profiles is a valid symlink"
412 "/var/guix/profiles"
413 (marionette-eval '(readlink "/var/guix/gcroots/profiles")
414 marionette))
415
416
4ee96a79
LC
417 (test-assert "screendump"
418 (begin
419 (marionette-control (string-append "screendump " #$output
420 "/tty1.ppm")
421 marionette)
422 (file-exists? "tty1.ppm")))
423
fe933833
LC
424 (test-assert "screen text"
425 (let ((text (marionette-screen-text marionette
426 #:ocrad
427 #$(file-append ocrad
428 "/bin/ocrad"))))
429 ;; Check whether the welcome message and shell prompt are
430 ;; displayed. Note: OCR confuses "y" and "V" for instance, so
431 ;; we cannot reliably match the whole text.
432 (and (string-contains text "This is the GNU")
433 (string-contains text
434 (string-append
435 "root@"
436 #$(operating-system-host-name os))))))
437
4ee96a79
LC
438 (test-end)
439 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
440
441 (gexp->derivation name test))
e3de272a 442
e9f693d0 443(define %test-basic-os
98b65b5f
LC
444 (system-test
445 (name "basic")
446 (description
125af57e 447 "Instrument %SIMPLE-OS, run it in a VM, and run a series of basic
98b65b5f
LC
448functionality tests.")
449 (value
8b113790
LC
450 (let* ((os (marionette-operating-system
451 %simple-os
452 #:imported-modules '((gnu services herd)
453 (guix combinators))))
454 (vm (virtual-machine os)))
98b65b5f
LC
455 ;; XXX: Add call to 'virtualized-operating-system' to get the exact same
456 ;; set of services as the OS produced by
457 ;; 'system-qemu-image/shared-store-script'.
458 (run-basic-test (virtualized-operating-system os '())
8b113790 459 #~(list #$vm))))))
c311089b
LC
460
461\f
7f090203
LC
462;;;
463;;; Halt.
464;;;
465
466(define (run-halt-test vm)
467 ;; As reported in <http://bugs.gnu.org/26931>, running tmux would previously
468 ;; lead the 'stop' method of 'user-processes' to an infinite loop, with the
469 ;; tmux server process as a zombie that remains in the list of processes.
470 ;; This test reproduces this scenario.
471 (define test
472 (with-imported-modules '((gnu build marionette))
473 #~(begin
474 (use-modules (gnu build marionette))
475
476 (define marionette
477 (make-marionette '(#$vm)))
478
479 (define ocrad
480 #$(file-append ocrad "/bin/ocrad"))
481
482 ;; Wait for tty1 and log in.
483 (marionette-eval '(begin
484 (use-modules (gnu services herd))
485 (start-service 'term-tty1))
486 marionette)
487 (marionette-type "root\n" marionette)
488 (wait-for-screen-text marionette
489 (lambda (text)
490 (string-contains text "root@komputilo"))
491 #:ocrad ocrad)
492
493 ;; Start tmux and wait for it to be ready.
494 (marionette-type "tmux new-session 'echo 1 > /ready; bash'\n"
495 marionette)
496 (wait-for-file "/ready" marionette)
497
498 ;; Make sure to stop the test after a while.
499 (sigaction SIGALRM (lambda _
500 (format (current-error-port)
501 "FAIL: Time is up, but VM still running.\n")
502 (primitive-exit 1)))
503 (alarm 10)
504
505 ;; Get debugging info.
506 (marionette-eval '(current-output-port
507 (open-file "/dev/console" "w0"))
508 marionette)
509 (marionette-eval '(system* #$(file-append procps "/bin/ps")
510 "-eo" "pid,ppid,stat,comm")
511 marionette)
512
513 ;; See if 'halt' actually works.
514 (marionette-eval '(system* "/run/current-system/profile/sbin/halt")
515 marionette)
516
517 ;; If we reach this line, that means the VM was properly stopped in
518 ;; a timely fashion.
519 (alarm 0)
520 (call-with-output-file #$output
521 (lambda (port)
522 (display "success!" port))))))
523
524 (gexp->derivation "halt" test))
525
526(define %test-halt
527 (system-test
528 (name "halt")
529 (description
530 "Use the 'halt' command and make sure it succeeds and does not get stuck
531in a loop. See <http://bugs.gnu.org/26931>.")
532 (value
533 (let ((os (marionette-operating-system
534 (operating-system
535 (inherit %simple-os)
536 (packages (cons tmux %base-packages)))
537 #:imported-modules '((gnu services herd)
538 (guix combinators)))))
539 (run-halt-test (virtual-machine os))))))
540
541\f
76c321d8
LC
542;;;
543;;; Cleanup of /tmp, /var/run, etc.
544;;;
545
546(define %cleanup-os
547 (simple-operating-system
548 (simple-service 'dirty-things
549 boot-service-type
378daa8c
LC
550 (let ((script (plain-file
551 "create-utf8-file.sh"
552 (string-append
553 "echo $0: dirtying /tmp...\n"
554 "set -e; set -x\n"
555 "touch /witness\n"
556 "exec touch /tmp/λαμβδα"))))
557 (with-imported-modules '((guix build utils))
558 #~(begin
559 (setenv "PATH"
560 #$(file-append coreutils "/bin"))
561 (invoke #$(file-append bash "/bin/sh")
562 #$script)))))))
76c321d8
LC
563
564(define (run-cleanup-test name)
565 (define os
566 (marionette-operating-system %cleanup-os
567 #:imported-modules '((gnu services herd)
568 (guix combinators))))
569 (define test
570 (with-imported-modules '((gnu build marionette))
571 #~(begin
572 (use-modules (gnu build marionette)
573 (srfi srfi-64)
574 (ice-9 match))
575
576 (define marionette
577 (make-marionette (list #$(virtual-machine os))))
578
579 (mkdir #$output)
580 (chdir #$output)
581
582 (test-begin "cleanup")
583
584 (test-assert "dirty service worked"
585 (marionette-eval '(file-exists? "/witness") marionette))
586
587 (test-equal "/tmp cleaned up"
588 '("." "..")
589 (marionette-eval '(begin
590 (use-modules (ice-9 ftw))
591 (scandir "/tmp"))
592 marionette))
593
594 (test-end)
595 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
596
597 (gexp->derivation "cleanup" test))
598
599(define %test-cleanup
600 ;; See <https://bugs.gnu.org/26353>.
601 (system-test
602 (name "cleanup")
603 (description "Make sure the 'cleanup' service can remove files with
604non-ASCII names from /tmp.")
605 (value (run-cleanup-test name))))
606
607\f
c311089b
LC
608;;;
609;;; Mcron.
610;;;
611
612(define %mcron-os
613 ;; System with an mcron service, with one mcron job for "root" and one mcron
cfbf6de1 614 ;; job for an unprivileged user.
67a51b67 615 (let ((job1 #~(job '(next-second '(0 5 10 15 20 25 30 35 40 45 50 55))
c311089b 616 (lambda ()
67a51b67
LC
617 (unless (file-exists? "witness")
618 (call-with-output-file "witness"
619 (lambda (port)
620 (display (list (getuid) (getgid)) port)))))))
c311089b
LC
621 (job2 #~(job next-second-from
622 (lambda ()
623 (call-with-output-file "witness"
624 (lambda (port)
625 (display (list (getuid) (getgid)) port))))
626 #:user "alice"))
627 (job3 #~(job next-second-from ;to test $PATH
628 "touch witness-touch")))
892d9089 629 (simple-operating-system
84a2de36
LC
630 (service mcron-service-type
631 (mcron-configuration (jobs (list job1 job2 job3)))))))
c311089b
LC
632
633(define (run-mcron-test name)
8b113790
LC
634 (define os
635 (marionette-operating-system
636 %mcron-os
637 #:imported-modules '((gnu services herd)
638 (guix combinators))))
639
640 (define test
641 (with-imported-modules '((gnu build marionette))
642 #~(begin
643 (use-modules (gnu build marionette)
644 (srfi srfi-64)
645 (ice-9 match))
646
647 (define marionette
648 (make-marionette (list #$(virtual-machine os))))
649
650 (mkdir #$output)
651 (chdir #$output)
652
653 (test-begin "mcron")
654
c24b1547 655 (test-assert "service running"
8b113790
LC
656 (marionette-eval
657 '(begin
658 (use-modules (gnu services herd))
c24b1547 659 (start-service 'mcron))
8b113790
LC
660 marionette))
661
662 ;; Make sure root's mcron job runs, has its cwd set to "/root", and
663 ;; runs with the right UID/GID.
664 (test-equal "root's job"
665 '(0 0)
666 (wait-for-file "/root/witness" marionette))
667
668 ;; Likewise for Alice's job. We cannot know what its GID is since
669 ;; it's chosen by 'groupadd', but it's strictly positive.
670 (test-assert "alice's job"
671 (match (wait-for-file "/home/alice/witness" marionette)
672 ((1000 gid)
673 (>= gid 100))))
674
675 ;; Last, the job that uses a command; allows us to test whether
077f1e63 676 ;; $PATH is sane.
8b113790 677 (test-equal "root's job with command"
077f1e63
LC
678 ""
679 (wait-for-file "/root/witness-touch" marionette
680 #:read '(@ (ice-9 rdelim) read-string)))
8b113790 681
147c5aa5
LC
682 ;; Make sure the 'schedule' action is accepted.
683 (test-equal "schedule action"
684 '(#t) ;one value, #t
685 (marionette-eval '(with-shepherd-action 'mcron ('schedule) result
686 result)
687 marionette))
688
8b113790
LC
689 (test-end)
690 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
691
692 (gexp->derivation name test))
c311089b
LC
693
694(define %test-mcron
695 (system-test
696 (name "mcron")
697 (description "Make sure the mcron service works as advertised.")
698 (value (run-mcron-test name))))
d2fa61bc
LC
699
700\f
701;;;
702;;; Avahi and NSS-mDNS.
703;;;
704
705(define %avahi-os
706 (operating-system
707 (inherit %simple-os)
708 (name-service-switch %mdns-host-lookup-nss)
2e04ab71
LC
709 (services (cons* (service avahi-service-type
710 (avahi-configuration (debug? #t)))
d2fa61bc 711 (dbus-service)
39d7fdce 712 (service dhcp-client-service-type) ;needed for multicast
d2fa61bc
LC
713
714 ;; Enable heavyweight debugging output.
715 (modify-services (operating-system-user-services
716 %simple-os)
717 (nscd-service-type config
718 => (nscd-configuration
719 (inherit config)
720 (debug-level 3)
721 (log-file "/dev/console")))
722 (syslog-service-type config
723 =>
ec2e2f6c
DC
724 (syslog-configuration
725 (inherit config)
726 (config-file
727 (plain-file
728 "syslog.conf"
729 "*.* /dev/console\n")))))))))
d2fa61bc
LC
730
731(define (run-nss-mdns-test)
732 ;; Test resolution of '.local' names via libc. Start the marionette service
733 ;; *after* nscd. Failing to do that, libc will try to connect to nscd,
734 ;; fail, then never try again (see '__nss_not_use_nscd_hosts' in libc),
735 ;; leading to '.local' resolution failures.
8b113790
LC
736 (define os
737 (marionette-operating-system
738 %avahi-os
739 #:requirements '(nscd)
740 #:imported-modules '((gnu services herd)
741 (guix combinators))))
4ee96a79 742
8b113790
LC
743 (define mdns-host-name
744 (string-append (operating-system-host-name os)
745 ".local"))
4ee96a79 746
8b113790
LC
747 (define test
748 (with-imported-modules '((gnu build marionette))
749 #~(begin
750 (use-modules (gnu build marionette)
751 (srfi srfi-1)
752 (srfi srfi-64)
753 (ice-9 match))
754
755 (define marionette
756 (make-marionette (list #$(virtual-machine os))))
757
758 (mkdir #$output)
759 (chdir #$output)
760
761 (test-begin "avahi")
762
c24b1547 763 (test-assert "nscd PID file is created"
8b113790
LC
764 (marionette-eval
765 '(begin
766 (use-modules (gnu services herd))
c24b1547
CL
767 (start-service 'nscd))
768 marionette))
769
770 (test-assert "nscd is listening on its socket"
771 (marionette-eval
772 ;; XXX: Work around a race condition in nscd: nscd creates its
773 ;; PID file before it is listening on its socket.
774 '(let ((sock (socket PF_UNIX SOCK_STREAM 0)))
775 (let try ()
776 (catch 'system-error
777 (lambda ()
778 (connect sock AF_UNIX "/var/run/nscd/socket")
779 (close-port sock)
780 (format #t "nscd is ready~%")
781 #t)
782 (lambda args
783 (format #t "waiting for nscd...~%")
784 (usleep 500000)
785 (try)))))
786 marionette))
787
788 (test-assert "avahi is running"
789 (marionette-eval
790 '(begin
791 (use-modules (gnu services herd))
792 (start-service 'avahi-daemon))
793 marionette))
8b113790 794
c24b1547
CL
795 (test-assert "network is up"
796 (marionette-eval
797 '(begin
798 (use-modules (gnu services herd))
799 (start-service 'networking))
8b113790
LC
800 marionette))
801
802 (test-equal "avahi-resolve-host-name"
803 0
804 (marionette-eval
805 '(system*
806 "/run/current-system/profile/bin/avahi-resolve-host-name"
807 "-v" #$mdns-host-name)
808 marionette))
809
810 (test-equal "avahi-browse"
811 0
812 (marionette-eval
813 '(system* "avahi-browse" "-avt")
814 marionette))
815
816 (test-assert "getaddrinfo .local"
817 ;; Wait for the 'avahi-daemon' service and perform a resolution.
818 (match (marionette-eval
819 '(getaddrinfo #$mdns-host-name)
820 marionette)
821 (((? vector? addrinfos) ..1)
822 (pk 'getaddrinfo addrinfos)
823 (and (any (lambda (ai)
824 (= AF_INET (addrinfo:fam ai)))
825 addrinfos)
826 (any (lambda (ai)
827 (= AF_INET6 (addrinfo:fam ai)))
828 addrinfos)))))
829
830 (test-assert "gethostbyname .local"
831 (match (pk 'gethostbyname
832 (marionette-eval '(gethostbyname #$mdns-host-name)
833 marionette))
834 ((? vector? result)
835 (and (string=? (hostent:name result) #$mdns-host-name)
836 (= (hostent:addrtype result) AF_INET)))))
837
838
839 (test-end)
840 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
841
842 (gexp->derivation "nss-mdns" test))
d2fa61bc
LC
843
844(define %test-nss-mdns
845 (system-test
846 (name "nss-mdns")
847 (description
848 "Test Avahi's multicast-DNS implementation, and in particular, test its
849glibc name service switch (NSS) module.")
850 (value (run-nss-mdns-test))))