services: Add Shepherd 'configuration' action to various services.
[jackhill/guix/guix.git] / gnu / tests / base.scm
CommitLineData
e9f693d0 1;;; GNU Guix --- Functional package management for GNU
0483c71c 2;;; Copyright © 2016, 2017, 2018, 2019, 2020, 2022 Ludovic Courtès <ludo@gnu.org>
c24b1547 3;;; Copyright © 2018 Clément Lassieur <clement@lassieur.org>
21f641e9 4;;; Copyright © 2022 Maxim Cournoyer <maxim.cournoyer@gmail.com>
e9f693d0
LC
5;;;
6;;; This file is part of GNU Guix.
7;;;
8;;; GNU Guix is free software; you can redistribute it and/or modify it
9;;; under the terms of the GNU General Public License as published by
10;;; the Free Software Foundation; either version 3 of the License, or (at
11;;; your option) any later version.
12;;;
13;;; GNU Guix is distributed in the hope that it will be useful, but
14;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16;;; GNU General Public License for more details.
17;;;
18;;; You should have received a copy of the GNU General Public License
19;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20
21(define-module (gnu tests base)
22 #:use-module (gnu tests)
0483c71c 23 #:use-module (gnu image)
e9f693d0 24 #:use-module (gnu system)
0483c71c 25 #:autoload (gnu system image) (system-image)
e9f693d0 26 #:use-module (gnu system shadow)
d2fa61bc 27 #:use-module (gnu system nss)
e9f693d0
LC
28 #:use-module (gnu system vm)
29 #:use-module (gnu services)
d2fa61bc
LC
30 #:use-module (gnu services base)
31 #:use-module (gnu services dbus)
32 #:use-module (gnu services avahi)
c311089b 33 #:use-module (gnu services mcron)
e9f693d0 34 #:use-module (gnu services shepherd)
d2fa61bc 35 #:use-module (gnu services networking)
76c321d8
LC
36 #:use-module (gnu packages base)
37 #:use-module (gnu packages bash)
fe933833 38 #:use-module (gnu packages imagemagick)
0483c71c 39 #:use-module (gnu packages linux)
fe933833 40 #:use-module (gnu packages ocr)
e2f9832f 41 #:use-module (gnu packages package-management)
7f090203 42 #:use-module (gnu packages tmux)
0483c71c 43 #:use-module (gnu packages virtualization)
e9f693d0
LC
44 #:use-module (guix gexp)
45 #:use-module (guix store)
76c321d8 46 #:use-module (guix monads)
0483c71c 47 #:use-module (guix modules)
e9f693d0 48 #:use-module (guix packages)
0483c71c 49 #:use-module ((srfi srfi-1) #:hide (partition))
4ab6a2f2 50 #:use-module (ice-9 match)
e3de272a 51 #:export (run-basic-test
c311089b 52 %test-basic-os
7f090203 53 %test-halt
0483c71c 54 %test-root-unmount
76c321d8 55 %test-cleanup
d2fa61bc
LC
56 %test-mcron
57 %test-nss-mdns))
e9f693d0
LC
58
59(define %simple-os
892d9089 60 (simple-operating-system))
e9f693d0
LC
61
62\f
f7f292d3 63(define* (run-basic-test os command #:optional (name "basic")
6e71514a
MO
64 #:key
65 initialization
66 root-password
67 desktop?)
e3de272a
LC
68 "Return a derivation called NAME that tests basic features of the OS started
69using COMMAND, a gexp that evaluates to a list of strings. Compare some
f7f292d3
LC
70properties of running system to what's declared in OS, an <operating-system>.
71
72When INITIALIZATION is true, it must be a one-argument procedure that is
73passed a gexp denoting the marionette, and it must return gexp that is
74inserted before the first test. This is used to introduce an extra
91ba90c1
LC
75initialization step, such as entering a LUKS passphrase.
76
77When ROOT-PASSWORD is true, enter it as the root password when logging in.
78Otherwise assume that there is no password for root."
387e1754 79 (define special-files
efe7d19a 80 (service-value
387e1754
LC
81 (fold-services (operating-system-services os)
82 #:target-type special-files-service-type)))
83
4ab6a2f2
LC
84 (define guix&co
85 (match (package-transitive-propagated-inputs guix)
86 (((labels packages) ...)
87 (cons guix packages))))
88
e3de272a 89 (define test
caa78166
LC
90 (with-imported-modules '((gnu build marionette)
91 (guix build syscalls))
4ee96a79
LC
92 #~(begin
93 (use-modules (gnu build marionette)
caa78166 94 (guix build syscalls)
4ee96a79 95 (srfi srfi-1)
8c801194 96 (srfi srfi-19)
4ee96a79
LC
97 (srfi srfi-26)
98 (srfi srfi-64)
99 (ice-9 match))
100
101 (define marionette
102 (make-marionette #$command))
103
89b05442 104 (test-runner-current (system-test-runner #$output))
4ee96a79
LC
105 (test-begin "basic")
106
f7f292d3
LC
107 #$(and initialization
108 (initialization #~marionette))
109
4ee96a79
LC
110 (test-assert "uname"
111 (match (marionette-eval '(uname) marionette)
112 (#("Linux" host-name version _ architecture)
113 (and (string=? host-name
114 #$(operating-system-host-name os))
115 (string-prefix? #$(package-version
116 (operating-system-kernel os))
117 version)
118 (string-prefix? architecture %host-type)))))
119
3c78f5b5
LC
120 ;; Shepherd reads the config file *before* binding its control
121 ;; socket, so /var/run/shepherd/socket might not exist yet when the
122 ;; 'marionette' service is started.
123 (test-assert "shepherd socket ready"
124 (marionette-eval
125 `(begin
126 (use-modules (gnu services herd))
127 (let loop ((i 10))
128 (cond ((file-exists? (%shepherd-socket-file))
129 #t)
130 ((> i 0)
131 (sleep 1)
132 (loop (- i 1)))
133 (else
134 #f))))
135 marionette))
136
6ea6e147
LC
137 (test-eq "stdin is /dev/null"
138 'eof
139 ;; Make sure services can no longer read from stdin once the
140 ;; system has booted.
141 (marionette-eval
142 `(begin
143 (use-modules (gnu services herd))
144 (start 'user-processes)
145 ((@@ (gnu services herd) eval-there)
146 '(let ((result (read (current-input-port))))
147 (if (eof-object? result)
148 'eof
149 result))))
150 marionette))
151
4ee96a79
LC
152 (test-assert "shell and user commands"
153 ;; Is everything in $PATH?
154 (zero? (marionette-eval '(system "
e3de272a
LC
155. /etc/profile
156set -e -x
157guix --version
158ls --version
159grep --version
160info --version")
4ee96a79
LC
161 marionette)))
162
387e1754
LC
163 (test-equal "special files"
164 '#$special-files
165 (marionette-eval
166 '(begin
167 (use-modules (ice-9 match))
168
169 (map (match-lambda
170 ((file target)
171 (list file (readlink file))))
172 '#$special-files))
173 marionette))
174
4ee96a79
LC
175 (test-assert "accounts"
176 (let ((users (marionette-eval '(begin
177 (use-modules (ice-9 match))
178 (let loop ((result '()))
179 (match (getpw)
180 (#f (reverse result))
181 (x (loop (cons x result))))))
182 marionette)))
b2979344
LC
183 (lset= equal?
184 (map (lambda (user)
185 (list (passwd:name user)
186 (passwd:dir user)))
187 users)
4ee96a79 188 (list
b2979344
LC
189 #$@(map (lambda (account)
190 `(list ,(user-account-name account)
191 ,(user-account-home-directory account)))
4ee96a79
LC
192 (operating-system-user-accounts os))))))
193
194 (test-assert "shepherd services"
183605c8
LC
195 (let ((services (marionette-eval
196 '(begin
197 (use-modules (gnu services herd))
198
199 (map (compose car live-service-provision)
200 (current-services)))
201 marionette)))
4ee96a79
LC
202 (lset= eq?
203 (pk 'services services)
204 '(root #$@(operating-system-shepherd-service-names os)))))
205
8c801194
LC
206 (test-equal "libc honors /etc/localtime"
207 -7200 ;CEST = GMT+2
208 ;; Assume OS is configured to have a CEST timezone.
209 (let* ((sept-2021 (time-second
210 (date->time-utc
211 (make-date 0 0 00 12 01 09 2021 7200)))))
212 (marionette-eval
213 `(tm:gmtoff (localtime ,sept-2021))
214 marionette)))
215
d7113bb6
LC
216 (test-equal "/var/log/messages is not world-readable"
217 #o640 ;<https://bugs.gnu.org/40405>
218 (begin
219 (wait-for-file "/var/log/messages" marionette
220 #:read 'get-u8)
221 (marionette-eval '(stat:perms (lstat "/var/log/messages"))
222 marionette)))
223
ae763b5b
LC
224 (test-assert "homes"
225 (let ((homes
226 '#$(map user-account-home-directory
227 (filter user-account-create-home-directory?
228 (operating-system-user-accounts os)))))
229 (marionette-eval
230 `(begin
231 (use-modules (gnu services herd) (srfi srfi-1))
232
233 ;; Home directories are supposed to exist once 'user-homes'
234 ;; has been started.
235 (start-service 'user-homes)
236
237 (every (lambda (home)
238 (and (file-exists? home)
239 (file-is-directory? home)))
240 ',homes))
241 marionette)))
242
243 (test-assert "skeletons in home directories"
cf98d342 244 (let ((users+homes
ae763b5b
LC
245 '#$(filter-map (lambda (account)
246 (and (user-account-create-home-directory?
247 account)
248 (not (user-account-system? account))
cf98d342
LC
249 (list (user-account-name account)
250 (user-account-home-directory
251 account))))
ae763b5b
LC
252 (operating-system-user-accounts os))))
253 (marionette-eval
254 `(begin
f9d55c49
LC
255 (use-modules (guix build utils) (srfi srfi-1)
256 (ice-9 ftw) (ice-9 match))
cf98d342
LC
257
258 (every (match-lambda
259 ((user home)
260 ;; Make sure HOME has all the skeletons...
261 (and (null? (lset-difference string=?
262 (scandir "/etc/skel/")
263 (scandir home)))
264
265 ;; ... and that everything is user-owned.
266 (let* ((pw (getpwnam user))
267 (uid (passwd:uid pw))
268 (gid (passwd:gid pw))
269 (st (lstat home)))
270 (define (user-owned? file)
271 (= uid (stat:uid (lstat file))))
272
273 (and (= uid (stat:uid st))
274 (eq? 'directory (stat:type st))
275 (every user-owned?
276 (find-files home
277 #:directories? #t)))))))
278 ',users+homes))
ae763b5b
LC
279 marionette)))
280
41db5a75
LC
281 (test-equal "permissions on /root"
282 #o700
283 (let ((root-home #$(any (lambda (account)
284 (and (zero? (user-account-uid account))
285 (user-account-home-directory
286 account)))
287 (operating-system-user-accounts os))))
288 (stat:perms (marionette-eval `(stat ,root-home) marionette))))
289
d429878d
LC
290 (test-equal "ownership and permissions of /var/empty"
291 '(0 0 #o555)
292 (let ((st (marionette-eval `(stat "/var/empty") marionette)))
293 (list (stat:uid st) (stat:gid st)
294 (stat:perms st))))
295
41f76ae0
LC
296 (test-equal "no extra home directories"
297 '()
298
299 ;; Make sure the home directories that are not supposed to be
300 ;; created are indeed not created.
301 (let ((nonexistent
302 '#$(filter-map (lambda (user)
303 (and (not
304 (user-account-create-home-directory?
305 user))
306 (user-account-home-directory user)))
307 (operating-system-user-accounts os))))
308 (marionette-eval
309 `(begin
310 (use-modules (srfi srfi-1))
311
312 ;; Note: Do not flag "/var/empty".
313 (filter file-exists?
314 ',(remove (cut string-prefix? "/var/" <>)
315 nonexistent)))
316 marionette)))
317
4ee96a79
LC
318 (test-equal "login on tty1"
319 "root\n"
320 (begin
6e71514a
MO
321 ;; XXX: On desktop, GDM3 will switch to TTY7. If this happens
322 ;; after we switched to TTY1, we won't be able to login. Make
323 ;; sure to wait long enough before switching to TTY1.
324 (when #$desktop?
325 (sleep 30))
326
4ee96a79
LC
327 (marionette-control "sendkey ctrl-alt-f1" marionette)
328 ;; Wait for the 'term-tty1' service to be running (using
329 ;; 'start-service' is the simplest and most reliable way to do
330 ;; that.)
331 (marionette-eval
332 '(begin
333 (use-modules (gnu services herd))
334 (start-service 'term-tty1))
335 marionette)
336
337 ;; Now we can type.
91ba90c1
LC
338 (let ((password #$root-password))
339 (if password
340 (begin
341 (marionette-type "root\n" marionette)
342 (wait-for-screen-text marionette
343 (lambda (text)
344 (string-contains text "Password"))
42fee6d0 345 #:ocr
91ba90c1
LC
346 #$(file-append ocrad "/bin/ocrad"))
347 (marionette-type (string-append password "\n\n")
348 marionette))
349 (marionette-type "root\n\n" marionette)))
350 (marionette-type "id -un > logged-in\n" marionette)
4ee96a79
LC
351
352 ;; It can take a while before the shell commands are executed.
4ee96a79 353 (marionette-eval '(use-modules (rnrs io ports)) marionette)
13877c34 354 (wait-for-file "/root/logged-in" marionette
947b8f99
MC
355 #:read 'get-string-all
356 #:timeout 30)))
4ee96a79 357
e6b1a224
LC
358 (test-equal "getlogin on tty1"
359 "\"root\""
360 (begin
361 ;; Assume we logged in in the previous test and type.
8b310793
LC
362 (marionette-type "guile -c '(write (getlogin))' > /root/login-id.tmp\n"
363 marionette)
364 (marionette-type "mv /root/login-id{.tmp,}\n"
e6b1a224
LC
365 marionette)
366
367 ;; It can take a while before the shell commands are executed.
368 (marionette-eval '(use-modules (rnrs io ports)) marionette)
369 (wait-for-file "/root/login-id" marionette
947b8f99
MC
370 #:read 'get-string-all
371 #:timeout 30)))
e6b1a224 372
caa78166
LC
373 ;; There should be one utmpx entry for the user logged in on tty1.
374 (test-equal "utmpx entry"
375 '(("root" "tty1" #f))
376 (marionette-eval
377 '(begin
378 (use-modules (guix build syscalls)
379 (srfi srfi-1))
380
381 (filter-map (lambda (entry)
382 (and (equal? (login-type USER_PROCESS)
383 (utmpx-login-type entry))
384 (list (utmpx-user entry) (utmpx-line entry)
385 (utmpx-host entry))))
386 (utmpx-entries)))
387 marionette))
388
2986995b
LC
389 ;; Likewise for /var/log/wtmp (used by 'last').
390 (test-assert "wtmp entry"
391 (match (marionette-eval
392 '(begin
393 (use-modules (guix build syscalls)
394 (srfi srfi-1))
395
396 (define (entry->list entry)
397 (list (utmpx-user entry) (utmpx-line entry)
398 (utmpx-host entry) (utmpx-login-type entry)))
399
400 (call-with-input-file "/var/log/wtmp"
401 (lambda (port)
402 (let loop ((result '()))
403 (if (eof-object? (peek-char port))
404 (map entry->list (reverse result))
405 (loop (cons (read-utmpx port) result)))))))
406 marionette)
407 (((users lines hosts types) ..1)
408 (every (lambda (type)
409 (eqv? type (login-type LOGIN_PROCESS)))
410 types))))
411
4ee96a79
LC
412 (test-assert "host name resolution"
413 (match (marionette-eval
414 '(begin
415 ;; Wait for nscd or our requests go through it.
416 (use-modules (gnu services herd))
417 (start-service 'nscd)
418
419 (list (getaddrinfo "localhost")
420 (getaddrinfo #$(operating-system-host-name os))))
421 marionette)
422 ((((? vector?) ..1) ((? vector?) ..1))
423 #t)
424 (x
425 (pk 'failure x #f))))
426
8d9647d8
LC
427 (test-assert "nscd configuration action"
428 (marionette-eval '(with-shepherd-action 'nscd ('configuration)
429 results
430 (file-exists? (car results)))
431 marionette))
432
d3f75179
LC
433 (test-equal "nscd invalidate action"
434 '(#t) ;one value, #t
435 (marionette-eval '(with-shepherd-action 'nscd ('invalidate "hosts")
436 result
437 result)
438 marionette))
439
33572a36
LC
440 ;; FIXME: The 'invalidate' action can't reliably obtain the exit
441 ;; code of 'nscd' so skip this test.
442 (test-skip 1)
d3f75179
LC
443 (test-equal "nscd invalidate action, wrong table"
444 '(#f) ;one value, #f
445 (marionette-eval '(with-shepherd-action 'nscd ('invalidate "xyz")
446 result
447 result)
448 marionette))
449
4ee96a79
LC
450 (test-equal "host not found"
451 #f
e3de272a 452 (marionette-eval
4ee96a79
LC
453 '(false-if-exception (getaddrinfo "does-not-exist"))
454 marionette))
455
ab3a6450
LC
456 (test-equal "locale"
457 "en_US.utf8"
cc73339b
LC
458 (marionette-eval '(let ((before (setlocale LC_ALL "en_US.utf8")))
459 (setlocale LC_ALL before))
ab3a6450
LC
460 marionette))
461
d5094c81
LC
462 (test-eq "/run/current-system is a GC root"
463 'success!
40d28609
LC
464 (marionette-eval '(begin
465 ;; Make sure the (guix …) modules are found.
4ab6a2f2
LC
466 (eval-when (expand load eval)
467 (set! %load-path
468 (append (map (lambda (package)
469 (string-append package
470 "/share/guile/site/"
471 (effective-version)))
472 '#$guix&co)
473 %load-path)))
40d28609
LC
474
475 (use-modules (srfi srfi-34) (guix store))
476
477 (let ((system (readlink "/run/current-system")))
ba926e35 478 (guard (c ((store-protocol-error? c)
d5094c81
LC
479 (and (file-exists? system)
480 'success!)))
40d28609
LC
481 (with-store store
482 (delete-paths store (list system))
483 #f))))
484 marionette))
485
334bda9a
LC
486 ;; This symlink is currently unused, but better have it point to the
487 ;; right place. See
488 ;; <https://lists.gnu.org/archive/html/guix-devel/2016-08/msg01641.html>.
489 (test-equal "/var/guix/gcroots/profiles is a valid symlink"
490 "/var/guix/profiles"
491 (marionette-eval '(readlink "/var/guix/gcroots/profiles")
492 marionette))
493
3302e03b
LC
494 (test-equal "guix-daemon set-http-proxy action"
495 '(#t) ;one value, #t
496 (marionette-eval '(with-shepherd-action 'guix-daemon
497 ('set-http-proxy "http://localhost:8118")
498 result
499 result)
500 marionette))
501
502 (test-equal "guix-daemon set-http-proxy action, clear"
503 '(#t) ;one value, #t
504 (marionette-eval '(with-shepherd-action 'guix-daemon
505 ('set-http-proxy)
506 result
507 result)
508 marionette))
334bda9a 509
4ee96a79
LC
510 (test-assert "screendump"
511 (begin
d27e871e
MO
512 (let ((capture
513 (string-append #$output "/tty1.ppm")))
514 (marionette-control
515 (string-append "screendump " capture) marionette)
516 (file-exists? capture))))
4ee96a79 517
fe933833 518 (test-assert "screen text"
21f641e9
MC
519 (wait-for-screen-text
520 marionette
521 (lambda (text)
522 ;; Check whether the welcome message and shell prompt are
523 ;; displayed. Note: OCR confuses "y" and "V" for instance, so
524 ;; we cannot reliably match the whole text.
525 (and (string-contains text "This is the GNU")
526 (string-contains text
527 (string-append
528 "root@"
529 #$(operating-system-host-name os)))))
530 #:ocr #$(file-append ocrad "/bin/ocrad")))
fe933833 531
1fb75128 532 (test-end))))
4ee96a79
LC
533
534 (gexp->derivation name test))
e3de272a 535
e9f693d0 536(define %test-basic-os
98b65b5f
LC
537 (system-test
538 (name "basic")
539 (description
125af57e 540 "Instrument %SIMPLE-OS, run it in a VM, and run a series of basic
98b65b5f
LC
541functionality tests.")
542 (value
8b113790
LC
543 (let* ((os (marionette-operating-system
544 %simple-os
545 #:imported-modules '((gnu services herd)
546 (guix combinators))))
547 (vm (virtual-machine os)))
98b65b5f
LC
548 ;; XXX: Add call to 'virtualized-operating-system' to get the exact same
549 ;; set of services as the OS produced by
550 ;; 'system-qemu-image/shared-store-script'.
551 (run-basic-test (virtualized-operating-system os '())
8b113790 552 #~(list #$vm))))))
c311089b
LC
553
554\f
7f090203
LC
555;;;
556;;; Halt.
557;;;
558
559(define (run-halt-test vm)
560 ;; As reported in <http://bugs.gnu.org/26931>, running tmux would previously
561 ;; lead the 'stop' method of 'user-processes' to an infinite loop, with the
562 ;; tmux server process as a zombie that remains in the list of processes.
563 ;; This test reproduces this scenario.
564 (define test
565 (with-imported-modules '((gnu build marionette))
566 #~(begin
567 (use-modules (gnu build marionette))
568
569 (define marionette
570 (make-marionette '(#$vm)))
571
572 (define ocrad
573 #$(file-append ocrad "/bin/ocrad"))
574
575 ;; Wait for tty1 and log in.
576 (marionette-eval '(begin
577 (use-modules (gnu services herd))
578 (start-service 'term-tty1))
579 marionette)
580 (marionette-type "root\n" marionette)
7f090203
LC
581
582 ;; Start tmux and wait for it to be ready.
583 (marionette-type "tmux new-session 'echo 1 > /ready; bash'\n"
584 marionette)
585 (wait-for-file "/ready" marionette)
586
587 ;; Make sure to stop the test after a while.
588 (sigaction SIGALRM (lambda _
589 (format (current-error-port)
590 "FAIL: Time is up, but VM still running.\n")
591 (primitive-exit 1)))
592 (alarm 10)
593
594 ;; Get debugging info.
595 (marionette-eval '(current-output-port
596 (open-file "/dev/console" "w0"))
597 marionette)
598 (marionette-eval '(system* #$(file-append procps "/bin/ps")
599 "-eo" "pid,ppid,stat,comm")
600 marionette)
601
602 ;; See if 'halt' actually works.
603 (marionette-eval '(system* "/run/current-system/profile/sbin/halt")
604 marionette)
605
606 ;; If we reach this line, that means the VM was properly stopped in
607 ;; a timely fashion.
608 (alarm 0)
609 (call-with-output-file #$output
610 (lambda (port)
611 (display "success!" port))))))
612
613 (gexp->derivation "halt" test))
614
615(define %test-halt
616 (system-test
617 (name "halt")
618 (description
619 "Use the 'halt' command and make sure it succeeds and does not get stuck
620in a loop. See <http://bugs.gnu.org/26931>.")
621 (value
622 (let ((os (marionette-operating-system
623 (operating-system
624 (inherit %simple-os)
625 (packages (cons tmux %base-packages)))
626 #:imported-modules '((gnu services herd)
627 (guix combinators)))))
628 (run-halt-test (virtual-machine os))))))
629
630\f
0483c71c
LC
631;;;
632;;; Root cleanly unmounted.
633;;;
634
635(define (run-root-unmount-test os)
636 (define test-image
637 (image (operating-system os)
638 (format 'compressed-qcow2)
639 (volatile-root? #f)
640 (shared-store? #f)
641 (partition-table-type 'mbr)
642 (partitions
643 (list (partition
644 (size 'guess)
645 (offset (* 512 2048)) ;leave room for GRUB
646 (flags '(boot))
0483c71c
LC
647 (label "root-under-test")))))) ;max 16 characters!
648
649 (define observer-os
650 (marionette-operating-system
651 %simple-os
652 #:imported-modules
653 (source-module-closure '((guix build syscalls)
654 (gnu build file-systems)))))
655
656 (define test
657 (with-imported-modules (source-module-closure
658 '((gnu build marionette)
659 (guix build utils)))
660 #~(begin
661 (use-modules (gnu build marionette)
662 (guix build utils)
663 (srfi srfi-64)
664 (ice-9 ftw))
665
666 (define image
667 "/tmp/writable-image.qcow2")
668
669 (define (test-system-marionette)
670 ;; Return a marionette on a system where we'll run 'halt'.
671 (invoke #$(file-append qemu-minimal "/bin/qemu-img")
672 "create" "-f" "qcow2" image "3G"
673 "-b" #$(system-image test-image) "-F" "qcow2")
674 (make-marionette
675 `(,(string-append #$qemu-minimal "/bin/" (qemu-command))
676 ,@(if (file-exists? "/dev/kvm")
677 '("-enable-kvm")
678 '())
679 "-no-reboot"
680 "-m" "1024" ;memory size, in MiB
681 "-drive" ,(format #f "file=~a,if=virtio" image))))
682
683 (define witness-size
684 ;; Size of the /witness file.
685 (* 20 (expt 2 20)))
686
687 (test-runner-current (system-test-runner #$output))
688 (test-begin "root-unmount")
689
690 (let ((marionette (test-system-marionette)))
691 (test-assert "file created"
692 (marionette-eval `(begin
693 (use-modules (guix build utils))
694 (call-with-output-file "/witness"
695 (lambda (port)
696 (call-with-input-file "/dev/random"
697 (lambda (input)
698 (dump-port input port
699 ,witness-size))))))
700 marionette))
701
702 ;; Halt the system.
703 (marionette-eval '(system* "/run/current-system/profile/sbin/halt")
00b9ab0a
LC
704 marionette)
705
706 (display "waiting for marionette to complete...")
707 (force-output)
708 (false-if-exception (waitpid (marionette-pid marionette)))
709 (display " done\n")
710 (force-output))
0483c71c
LC
711
712 ;; Remove the sockets used by the marionette above to avoid
713 ;; EADDRINUSE.
714 (for-each delete-file
715 (find-files "/tmp" (lambda (file stat)
716 (eq? (stat:type stat) 'socket))))
717
718 ;; Now boot another system and check whether the root file system of
719 ;; the first one was cleanly unmounted.
720
721 (let ((observer
722 (make-marionette (list #$(virtual-machine observer-os)
723 "-drive"
724 (format #f "file=~a,if=virtio" image)))))
725 (test-assert "partitions"
726 (marionette-eval '(begin
727 (use-modules (gnu build file-systems))
728 (disk-partitions))
729 observer))
730
731 (test-assert "partition found"
732 (marionette-eval '(find-partition-by-label "root-under-test")
733 observer))
734
735 (test-assert "root file system is clean"
736 (marionette-eval '(cleanly-unmounted-ext2?
737 (find-partition-by-label "root-under-test"))
738 observer))
739
740 (test-equal "root file system contains /witness"
741 witness-size
742 (let ((files (marionette-eval
743 '(begin
744 (use-modules (guix build syscalls)
745 (ice-9 ftw))
746 (mount (find-partition-by-label "root-under-test")
747 "/mnt" "ext4" MS_RDONLY)
748 (scandir "/mnt"))
749 observer)))
750 (if (member "witness" files)
751 (marionette-eval '(stat:size (stat "/mnt/witness"))
752 observer)
753 files))))
754
755 (test-end))))
756
757 (gexp->derivation "root-unmount" test))
758
759(define %test-root-unmount
760 (system-test
761 (name "root-unmount")
762 (description
763 "Make sure the root file system is cleanly unmounted when the system is
764halted.")
765 (value
766 (let ((os (marionette-operating-system %simple-os)))
767 (run-root-unmount-test os)))))
768
769\f
76c321d8
LC
770;;;
771;;; Cleanup of /tmp, /var/run, etc.
772;;;
773
774(define %cleanup-os
775 (simple-operating-system
776 (simple-service 'dirty-things
777 boot-service-type
378daa8c
LC
778 (let ((script (plain-file
779 "create-utf8-file.sh"
780 (string-append
781 "echo $0: dirtying /tmp...\n"
782 "set -e; set -x\n"
783 "touch /witness\n"
784 "exec touch /tmp/λαμβδα"))))
785 (with-imported-modules '((guix build utils))
786 #~(begin
787 (setenv "PATH"
788 #$(file-append coreutils "/bin"))
789 (invoke #$(file-append bash "/bin/sh")
790 #$script)))))))
76c321d8
LC
791
792(define (run-cleanup-test name)
793 (define os
794 (marionette-operating-system %cleanup-os
795 #:imported-modules '((gnu services herd)
796 (guix combinators))))
797 (define test
798 (with-imported-modules '((gnu build marionette))
799 #~(begin
800 (use-modules (gnu build marionette)
801 (srfi srfi-64)
802 (ice-9 match))
803
804 (define marionette
805 (make-marionette (list #$(virtual-machine os))))
806
89b05442 807 (test-runner-current (system-test-runner #$output))
76c321d8
LC
808 (test-begin "cleanup")
809
810 (test-assert "dirty service worked"
811 (marionette-eval '(file-exists? "/witness") marionette))
812
813 (test-equal "/tmp cleaned up"
814 '("." "..")
815 (marionette-eval '(begin
816 (use-modules (ice-9 ftw))
817 (scandir "/tmp"))
818 marionette))
819
1fb75128 820 (test-end))))
76c321d8
LC
821
822 (gexp->derivation "cleanup" test))
823
824(define %test-cleanup
825 ;; See <https://bugs.gnu.org/26353>.
826 (system-test
827 (name "cleanup")
828 (description "Make sure the 'cleanup' service can remove files with
829non-ASCII names from /tmp.")
830 (value (run-cleanup-test name))))
831
832\f
c311089b
LC
833;;;
834;;; Mcron.
835;;;
836
837(define %mcron-os
838 ;; System with an mcron service, with one mcron job for "root" and one mcron
cfbf6de1 839 ;; job for an unprivileged user.
67a51b67 840 (let ((job1 #~(job '(next-second '(0 5 10 15 20 25 30 35 40 45 50 55))
c311089b 841 (lambda ()
67a51b67
LC
842 (unless (file-exists? "witness")
843 (call-with-output-file "witness"
844 (lambda (port)
845 (display (list (getuid) (getgid)) port)))))))
c311089b
LC
846 (job2 #~(job next-second-from
847 (lambda ()
848 (call-with-output-file "witness"
849 (lambda (port)
850 (display (list (getuid) (getgid)) port))))
851 #:user "alice"))
852 (job3 #~(job next-second-from ;to test $PATH
853 "touch witness-touch")))
892d9089 854 (simple-operating-system
84a2de36
LC
855 (service mcron-service-type
856 (mcron-configuration (jobs (list job1 job2 job3)))))))
c311089b
LC
857
858(define (run-mcron-test name)
8b113790
LC
859 (define os
860 (marionette-operating-system
861 %mcron-os
862 #:imported-modules '((gnu services herd)
863 (guix combinators))))
864
865 (define test
866 (with-imported-modules '((gnu build marionette))
867 #~(begin
868 (use-modules (gnu build marionette)
869 (srfi srfi-64)
870 (ice-9 match))
871
872 (define marionette
873 (make-marionette (list #$(virtual-machine os))))
874
89b05442 875 (test-runner-current (system-test-runner #$output))
8b113790
LC
876 (test-begin "mcron")
877
c24b1547 878 (test-assert "service running"
8b113790
LC
879 (marionette-eval
880 '(begin
881 (use-modules (gnu services herd))
c24b1547 882 (start-service 'mcron))
8b113790
LC
883 marionette))
884
885 ;; Make sure root's mcron job runs, has its cwd set to "/root", and
886 ;; runs with the right UID/GID.
887 (test-equal "root's job"
888 '(0 0)
889 (wait-for-file "/root/witness" marionette))
890
891 ;; Likewise for Alice's job. We cannot know what its GID is since
892 ;; it's chosen by 'groupadd', but it's strictly positive.
893 (test-assert "alice's job"
894 (match (wait-for-file "/home/alice/witness" marionette)
895 ((1000 gid)
896 (>= gid 100))))
897
898 ;; Last, the job that uses a command; allows us to test whether
077f1e63 899 ;; $PATH is sane.
8b113790 900 (test-equal "root's job with command"
077f1e63
LC
901 ""
902 (wait-for-file "/root/witness-touch" marionette
903 #:read '(@ (ice-9 rdelim) read-string)))
8b113790 904
147c5aa5
LC
905 ;; Make sure the 'schedule' action is accepted.
906 (test-equal "schedule action"
907 '(#t) ;one value, #t
908 (marionette-eval '(with-shepherd-action 'mcron ('schedule) result
909 result)
910 marionette))
911
1fb75128 912 (test-end))))
8b113790
LC
913
914 (gexp->derivation name test))
c311089b
LC
915
916(define %test-mcron
917 (system-test
918 (name "mcron")
919 (description "Make sure the mcron service works as advertised.")
920 (value (run-mcron-test name))))
d2fa61bc
LC
921
922\f
923;;;
924;;; Avahi and NSS-mDNS.
925;;;
926
927(define %avahi-os
928 (operating-system
929 (inherit %simple-os)
930 (name-service-switch %mdns-host-lookup-nss)
2e04ab71
LC
931 (services (cons* (service avahi-service-type
932 (avahi-configuration (debug? #t)))
d2fa61bc 933 (dbus-service)
39d7fdce 934 (service dhcp-client-service-type) ;needed for multicast
d2fa61bc
LC
935
936 ;; Enable heavyweight debugging output.
937 (modify-services (operating-system-user-services
938 %simple-os)
939 (nscd-service-type config
940 => (nscd-configuration
941 (inherit config)
942 (debug-level 3)
943 (log-file "/dev/console")))
944 (syslog-service-type config
945 =>
ec2e2f6c
DC
946 (syslog-configuration
947 (inherit config)
948 (config-file
949 (plain-file
950 "syslog.conf"
951 "*.* /dev/console\n")))))))))
d2fa61bc
LC
952
953(define (run-nss-mdns-test)
954 ;; Test resolution of '.local' names via libc. Start the marionette service
955 ;; *after* nscd. Failing to do that, libc will try to connect to nscd,
956 ;; fail, then never try again (see '__nss_not_use_nscd_hosts' in libc),
957 ;; leading to '.local' resolution failures.
8b113790
LC
958 (define os
959 (marionette-operating-system
960 %avahi-os
961 #:requirements '(nscd)
962 #:imported-modules '((gnu services herd)
963 (guix combinators))))
4ee96a79 964
8b113790
LC
965 (define mdns-host-name
966 (string-append (operating-system-host-name os)
967 ".local"))
4ee96a79 968
8b113790
LC
969 (define test
970 (with-imported-modules '((gnu build marionette))
971 #~(begin
972 (use-modules (gnu build marionette)
973 (srfi srfi-1)
974 (srfi srfi-64)
975 (ice-9 match))
976
977 (define marionette
978 (make-marionette (list #$(virtual-machine os))))
979
980 (mkdir #$output)
981 (chdir #$output)
982
1fb75128 983 (test-runner-current (system-test-runner))
8b113790
LC
984 (test-begin "avahi")
985
c24b1547 986 (test-assert "nscd PID file is created"
8b113790
LC
987 (marionette-eval
988 '(begin
989 (use-modules (gnu services herd))
c24b1547
CL
990 (start-service 'nscd))
991 marionette))
992
993 (test-assert "nscd is listening on its socket"
994 (marionette-eval
995 ;; XXX: Work around a race condition in nscd: nscd creates its
996 ;; PID file before it is listening on its socket.
997 '(let ((sock (socket PF_UNIX SOCK_STREAM 0)))
998 (let try ()
999 (catch 'system-error
1000 (lambda ()
1001 (connect sock AF_UNIX "/var/run/nscd/socket")
1002 (close-port sock)
1003 (format #t "nscd is ready~%")
1004 #t)
1005 (lambda args
1006 (format #t "waiting for nscd...~%")
1007 (usleep 500000)
1008 (try)))))
1009 marionette))
1010
1011 (test-assert "avahi is running"
1012 (marionette-eval
1013 '(begin
1014 (use-modules (gnu services herd))
1015 (start-service 'avahi-daemon))
1016 marionette))
8b113790 1017
c24b1547
CL
1018 (test-assert "network is up"
1019 (marionette-eval
1020 '(begin
1021 (use-modules (gnu services herd))
1022 (start-service 'networking))
8b113790
LC
1023 marionette))
1024
1025 (test-equal "avahi-resolve-host-name"
1026 0
1027 (marionette-eval
1028 '(system*
1029 "/run/current-system/profile/bin/avahi-resolve-host-name"
1030 "-v" #$mdns-host-name)
1031 marionette))
1032
1033 (test-equal "avahi-browse"
1034 0
1035 (marionette-eval
572c59a7 1036 '(system* "/run/current-system/profile/bin/avahi-browse" "-avt")
8b113790
LC
1037 marionette))
1038
1039 (test-assert "getaddrinfo .local"
1040 ;; Wait for the 'avahi-daemon' service and perform a resolution.
1041 (match (marionette-eval
1042 '(getaddrinfo #$mdns-host-name)
1043 marionette)
1044 (((? vector? addrinfos) ..1)
1045 (pk 'getaddrinfo addrinfos)
1046 (and (any (lambda (ai)
1047 (= AF_INET (addrinfo:fam ai)))
1048 addrinfos)
1049 (any (lambda (ai)
1050 (= AF_INET6 (addrinfo:fam ai)))
1051 addrinfos)))))
1052
1053 (test-assert "gethostbyname .local"
1054 (match (pk 'gethostbyname
1055 (marionette-eval '(gethostbyname #$mdns-host-name)
1056 marionette))
1057 ((? vector? result)
1058 (and (string=? (hostent:name result) #$mdns-host-name)
1059 (= (hostent:addrtype result) AF_INET)))))
1060
1061
1fb75128 1062 (test-end))))
8b113790
LC
1063
1064 (gexp->derivation "nss-mdns" test))
d2fa61bc
LC
1065
1066(define %test-nss-mdns
1067 (system-test
1068 (name "nss-mdns")
1069 (description
1070 "Test Avahi's multicast-DNS implementation, and in particular, test its
1071glibc name service switch (NSS) module.")
1072 (value (run-nss-mdns-test))))