gnu: tint2: Add source file-name.
[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
d3f75179
LC
427 (test-equal "nscd invalidate action"
428 '(#t) ;one value, #t
429 (marionette-eval '(with-shepherd-action 'nscd ('invalidate "hosts")
430 result
431 result)
432 marionette))
433
33572a36
LC
434 ;; FIXME: The 'invalidate' action can't reliably obtain the exit
435 ;; code of 'nscd' so skip this test.
436 (test-skip 1)
d3f75179
LC
437 (test-equal "nscd invalidate action, wrong table"
438 '(#f) ;one value, #f
439 (marionette-eval '(with-shepherd-action 'nscd ('invalidate "xyz")
440 result
441 result)
442 marionette))
443
4ee96a79
LC
444 (test-equal "host not found"
445 #f
e3de272a 446 (marionette-eval
4ee96a79
LC
447 '(false-if-exception (getaddrinfo "does-not-exist"))
448 marionette))
449
ab3a6450
LC
450 (test-equal "locale"
451 "en_US.utf8"
cc73339b
LC
452 (marionette-eval '(let ((before (setlocale LC_ALL "en_US.utf8")))
453 (setlocale LC_ALL before))
ab3a6450
LC
454 marionette))
455
d5094c81
LC
456 (test-eq "/run/current-system is a GC root"
457 'success!
40d28609
LC
458 (marionette-eval '(begin
459 ;; Make sure the (guix …) modules are found.
4ab6a2f2
LC
460 (eval-when (expand load eval)
461 (set! %load-path
462 (append (map (lambda (package)
463 (string-append package
464 "/share/guile/site/"
465 (effective-version)))
466 '#$guix&co)
467 %load-path)))
40d28609
LC
468
469 (use-modules (srfi srfi-34) (guix store))
470
471 (let ((system (readlink "/run/current-system")))
ba926e35 472 (guard (c ((store-protocol-error? c)
d5094c81
LC
473 (and (file-exists? system)
474 'success!)))
40d28609
LC
475 (with-store store
476 (delete-paths store (list system))
477 #f))))
478 marionette))
479
334bda9a
LC
480 ;; This symlink is currently unused, but better have it point to the
481 ;; right place. See
482 ;; <https://lists.gnu.org/archive/html/guix-devel/2016-08/msg01641.html>.
483 (test-equal "/var/guix/gcroots/profiles is a valid symlink"
484 "/var/guix/profiles"
485 (marionette-eval '(readlink "/var/guix/gcroots/profiles")
486 marionette))
487
3302e03b
LC
488 (test-equal "guix-daemon set-http-proxy action"
489 '(#t) ;one value, #t
490 (marionette-eval '(with-shepherd-action 'guix-daemon
491 ('set-http-proxy "http://localhost:8118")
492 result
493 result)
494 marionette))
495
496 (test-equal "guix-daemon set-http-proxy action, clear"
497 '(#t) ;one value, #t
498 (marionette-eval '(with-shepherd-action 'guix-daemon
499 ('set-http-proxy)
500 result
501 result)
502 marionette))
334bda9a 503
4ee96a79
LC
504 (test-assert "screendump"
505 (begin
d27e871e
MO
506 (let ((capture
507 (string-append #$output "/tty1.ppm")))
508 (marionette-control
509 (string-append "screendump " capture) marionette)
510 (file-exists? capture))))
4ee96a79 511
fe933833 512 (test-assert "screen text"
21f641e9
MC
513 (wait-for-screen-text
514 marionette
515 (lambda (text)
516 ;; Check whether the welcome message and shell prompt are
517 ;; displayed. Note: OCR confuses "y" and "V" for instance, so
518 ;; we cannot reliably match the whole text.
519 (and (string-contains text "This is the GNU")
520 (string-contains text
521 (string-append
522 "root@"
523 #$(operating-system-host-name os)))))
524 #:ocr #$(file-append ocrad "/bin/ocrad")))
fe933833 525
1fb75128 526 (test-end))))
4ee96a79
LC
527
528 (gexp->derivation name test))
e3de272a 529
e9f693d0 530(define %test-basic-os
98b65b5f
LC
531 (system-test
532 (name "basic")
533 (description
125af57e 534 "Instrument %SIMPLE-OS, run it in a VM, and run a series of basic
98b65b5f
LC
535functionality tests.")
536 (value
8b113790
LC
537 (let* ((os (marionette-operating-system
538 %simple-os
539 #:imported-modules '((gnu services herd)
540 (guix combinators))))
541 (vm (virtual-machine os)))
98b65b5f
LC
542 ;; XXX: Add call to 'virtualized-operating-system' to get the exact same
543 ;; set of services as the OS produced by
544 ;; 'system-qemu-image/shared-store-script'.
545 (run-basic-test (virtualized-operating-system os '())
8b113790 546 #~(list #$vm))))))
c311089b
LC
547
548\f
7f090203
LC
549;;;
550;;; Halt.
551;;;
552
553(define (run-halt-test vm)
554 ;; As reported in <http://bugs.gnu.org/26931>, running tmux would previously
555 ;; lead the 'stop' method of 'user-processes' to an infinite loop, with the
556 ;; tmux server process as a zombie that remains in the list of processes.
557 ;; This test reproduces this scenario.
558 (define test
559 (with-imported-modules '((gnu build marionette))
560 #~(begin
561 (use-modules (gnu build marionette))
562
563 (define marionette
564 (make-marionette '(#$vm)))
565
566 (define ocrad
567 #$(file-append ocrad "/bin/ocrad"))
568
569 ;; Wait for tty1 and log in.
570 (marionette-eval '(begin
571 (use-modules (gnu services herd))
572 (start-service 'term-tty1))
573 marionette)
574 (marionette-type "root\n" marionette)
7f090203
LC
575
576 ;; Start tmux and wait for it to be ready.
577 (marionette-type "tmux new-session 'echo 1 > /ready; bash'\n"
578 marionette)
579 (wait-for-file "/ready" marionette)
580
581 ;; Make sure to stop the test after a while.
582 (sigaction SIGALRM (lambda _
583 (format (current-error-port)
584 "FAIL: Time is up, but VM still running.\n")
585 (primitive-exit 1)))
586 (alarm 10)
587
588 ;; Get debugging info.
589 (marionette-eval '(current-output-port
590 (open-file "/dev/console" "w0"))
591 marionette)
592 (marionette-eval '(system* #$(file-append procps "/bin/ps")
593 "-eo" "pid,ppid,stat,comm")
594 marionette)
595
596 ;; See if 'halt' actually works.
597 (marionette-eval '(system* "/run/current-system/profile/sbin/halt")
598 marionette)
599
600 ;; If we reach this line, that means the VM was properly stopped in
601 ;; a timely fashion.
602 (alarm 0)
603 (call-with-output-file #$output
604 (lambda (port)
605 (display "success!" port))))))
606
607 (gexp->derivation "halt" test))
608
609(define %test-halt
610 (system-test
611 (name "halt")
612 (description
613 "Use the 'halt' command and make sure it succeeds and does not get stuck
614in a loop. See <http://bugs.gnu.org/26931>.")
615 (value
616 (let ((os (marionette-operating-system
617 (operating-system
618 (inherit %simple-os)
619 (packages (cons tmux %base-packages)))
620 #:imported-modules '((gnu services herd)
621 (guix combinators)))))
622 (run-halt-test (virtual-machine os))))))
623
624\f
0483c71c
LC
625;;;
626;;; Root cleanly unmounted.
627;;;
628
629(define (run-root-unmount-test os)
630 (define test-image
631 (image (operating-system os)
632 (format 'compressed-qcow2)
633 (volatile-root? #f)
634 (shared-store? #f)
635 (partition-table-type 'mbr)
636 (partitions
637 (list (partition
638 (size 'guess)
639 (offset (* 512 2048)) ;leave room for GRUB
640 (flags '(boot))
0483c71c
LC
641 (label "root-under-test")))))) ;max 16 characters!
642
643 (define observer-os
644 (marionette-operating-system
645 %simple-os
646 #:imported-modules
647 (source-module-closure '((guix build syscalls)
648 (gnu build file-systems)))))
649
650 (define test
651 (with-imported-modules (source-module-closure
652 '((gnu build marionette)
653 (guix build utils)))
654 #~(begin
655 (use-modules (gnu build marionette)
656 (guix build utils)
657 (srfi srfi-64)
658 (ice-9 ftw))
659
660 (define image
661 "/tmp/writable-image.qcow2")
662
663 (define (test-system-marionette)
664 ;; Return a marionette on a system where we'll run 'halt'.
665 (invoke #$(file-append qemu-minimal "/bin/qemu-img")
666 "create" "-f" "qcow2" image "3G"
667 "-b" #$(system-image test-image) "-F" "qcow2")
668 (make-marionette
669 `(,(string-append #$qemu-minimal "/bin/" (qemu-command))
670 ,@(if (file-exists? "/dev/kvm")
671 '("-enable-kvm")
672 '())
673 "-no-reboot"
674 "-m" "1024" ;memory size, in MiB
675 "-drive" ,(format #f "file=~a,if=virtio" image))))
676
677 (define witness-size
678 ;; Size of the /witness file.
679 (* 20 (expt 2 20)))
680
681 (test-runner-current (system-test-runner #$output))
682 (test-begin "root-unmount")
683
684 (let ((marionette (test-system-marionette)))
685 (test-assert "file created"
686 (marionette-eval `(begin
687 (use-modules (guix build utils))
688 (call-with-output-file "/witness"
689 (lambda (port)
690 (call-with-input-file "/dev/random"
691 (lambda (input)
692 (dump-port input port
693 ,witness-size))))))
694 marionette))
695
696 ;; Halt the system.
697 (marionette-eval '(system* "/run/current-system/profile/sbin/halt")
698 marionette))
699
700 ;; Remove the sockets used by the marionette above to avoid
701 ;; EADDRINUSE.
702 (for-each delete-file
703 (find-files "/tmp" (lambda (file stat)
704 (eq? (stat:type stat) 'socket))))
705
706 ;; Now boot another system and check whether the root file system of
707 ;; the first one was cleanly unmounted.
708
709 (let ((observer
710 (make-marionette (list #$(virtual-machine observer-os)
711 "-drive"
712 (format #f "file=~a,if=virtio" image)))))
713 (test-assert "partitions"
714 (marionette-eval '(begin
715 (use-modules (gnu build file-systems))
716 (disk-partitions))
717 observer))
718
719 (test-assert "partition found"
720 (marionette-eval '(find-partition-by-label "root-under-test")
721 observer))
722
723 (test-assert "root file system is clean"
724 (marionette-eval '(cleanly-unmounted-ext2?
725 (find-partition-by-label "root-under-test"))
726 observer))
727
728 (test-equal "root file system contains /witness"
729 witness-size
730 (let ((files (marionette-eval
731 '(begin
732 (use-modules (guix build syscalls)
733 (ice-9 ftw))
734 (mount (find-partition-by-label "root-under-test")
735 "/mnt" "ext4" MS_RDONLY)
736 (scandir "/mnt"))
737 observer)))
738 (if (member "witness" files)
739 (marionette-eval '(stat:size (stat "/mnt/witness"))
740 observer)
741 files))))
742
743 (test-end))))
744
745 (gexp->derivation "root-unmount" test))
746
747(define %test-root-unmount
748 (system-test
749 (name "root-unmount")
750 (description
751 "Make sure the root file system is cleanly unmounted when the system is
752halted.")
753 (value
754 (let ((os (marionette-operating-system %simple-os)))
755 (run-root-unmount-test os)))))
756
757\f
76c321d8
LC
758;;;
759;;; Cleanup of /tmp, /var/run, etc.
760;;;
761
762(define %cleanup-os
763 (simple-operating-system
764 (simple-service 'dirty-things
765 boot-service-type
378daa8c
LC
766 (let ((script (plain-file
767 "create-utf8-file.sh"
768 (string-append
769 "echo $0: dirtying /tmp...\n"
770 "set -e; set -x\n"
771 "touch /witness\n"
772 "exec touch /tmp/λαμβδα"))))
773 (with-imported-modules '((guix build utils))
774 #~(begin
775 (setenv "PATH"
776 #$(file-append coreutils "/bin"))
777 (invoke #$(file-append bash "/bin/sh")
778 #$script)))))))
76c321d8
LC
779
780(define (run-cleanup-test name)
781 (define os
782 (marionette-operating-system %cleanup-os
783 #:imported-modules '((gnu services herd)
784 (guix combinators))))
785 (define test
786 (with-imported-modules '((gnu build marionette))
787 #~(begin
788 (use-modules (gnu build marionette)
789 (srfi srfi-64)
790 (ice-9 match))
791
792 (define marionette
793 (make-marionette (list #$(virtual-machine os))))
794
89b05442 795 (test-runner-current (system-test-runner #$output))
76c321d8
LC
796 (test-begin "cleanup")
797
798 (test-assert "dirty service worked"
799 (marionette-eval '(file-exists? "/witness") marionette))
800
801 (test-equal "/tmp cleaned up"
802 '("." "..")
803 (marionette-eval '(begin
804 (use-modules (ice-9 ftw))
805 (scandir "/tmp"))
806 marionette))
807
1fb75128 808 (test-end))))
76c321d8
LC
809
810 (gexp->derivation "cleanup" test))
811
812(define %test-cleanup
813 ;; See <https://bugs.gnu.org/26353>.
814 (system-test
815 (name "cleanup")
816 (description "Make sure the 'cleanup' service can remove files with
817non-ASCII names from /tmp.")
818 (value (run-cleanup-test name))))
819
820\f
c311089b
LC
821;;;
822;;; Mcron.
823;;;
824
825(define %mcron-os
826 ;; System with an mcron service, with one mcron job for "root" and one mcron
cfbf6de1 827 ;; job for an unprivileged user.
67a51b67 828 (let ((job1 #~(job '(next-second '(0 5 10 15 20 25 30 35 40 45 50 55))
c311089b 829 (lambda ()
67a51b67
LC
830 (unless (file-exists? "witness")
831 (call-with-output-file "witness"
832 (lambda (port)
833 (display (list (getuid) (getgid)) port)))))))
c311089b
LC
834 (job2 #~(job next-second-from
835 (lambda ()
836 (call-with-output-file "witness"
837 (lambda (port)
838 (display (list (getuid) (getgid)) port))))
839 #:user "alice"))
840 (job3 #~(job next-second-from ;to test $PATH
841 "touch witness-touch")))
892d9089 842 (simple-operating-system
84a2de36
LC
843 (service mcron-service-type
844 (mcron-configuration (jobs (list job1 job2 job3)))))))
c311089b
LC
845
846(define (run-mcron-test name)
8b113790
LC
847 (define os
848 (marionette-operating-system
849 %mcron-os
850 #:imported-modules '((gnu services herd)
851 (guix combinators))))
852
853 (define test
854 (with-imported-modules '((gnu build marionette))
855 #~(begin
856 (use-modules (gnu build marionette)
857 (srfi srfi-64)
858 (ice-9 match))
859
860 (define marionette
861 (make-marionette (list #$(virtual-machine os))))
862
89b05442 863 (test-runner-current (system-test-runner #$output))
8b113790
LC
864 (test-begin "mcron")
865
c24b1547 866 (test-assert "service running"
8b113790
LC
867 (marionette-eval
868 '(begin
869 (use-modules (gnu services herd))
c24b1547 870 (start-service 'mcron))
8b113790
LC
871 marionette))
872
873 ;; Make sure root's mcron job runs, has its cwd set to "/root", and
874 ;; runs with the right UID/GID.
875 (test-equal "root's job"
876 '(0 0)
877 (wait-for-file "/root/witness" marionette))
878
879 ;; Likewise for Alice's job. We cannot know what its GID is since
880 ;; it's chosen by 'groupadd', but it's strictly positive.
881 (test-assert "alice's job"
882 (match (wait-for-file "/home/alice/witness" marionette)
883 ((1000 gid)
884 (>= gid 100))))
885
886 ;; Last, the job that uses a command; allows us to test whether
077f1e63 887 ;; $PATH is sane.
8b113790 888 (test-equal "root's job with command"
077f1e63
LC
889 ""
890 (wait-for-file "/root/witness-touch" marionette
891 #:read '(@ (ice-9 rdelim) read-string)))
8b113790 892
147c5aa5
LC
893 ;; Make sure the 'schedule' action is accepted.
894 (test-equal "schedule action"
895 '(#t) ;one value, #t
896 (marionette-eval '(with-shepherd-action 'mcron ('schedule) result
897 result)
898 marionette))
899
1fb75128 900 (test-end))))
8b113790
LC
901
902 (gexp->derivation name test))
c311089b
LC
903
904(define %test-mcron
905 (system-test
906 (name "mcron")
907 (description "Make sure the mcron service works as advertised.")
908 (value (run-mcron-test name))))
d2fa61bc
LC
909
910\f
911;;;
912;;; Avahi and NSS-mDNS.
913;;;
914
915(define %avahi-os
916 (operating-system
917 (inherit %simple-os)
918 (name-service-switch %mdns-host-lookup-nss)
2e04ab71
LC
919 (services (cons* (service avahi-service-type
920 (avahi-configuration (debug? #t)))
d2fa61bc 921 (dbus-service)
39d7fdce 922 (service dhcp-client-service-type) ;needed for multicast
d2fa61bc
LC
923
924 ;; Enable heavyweight debugging output.
925 (modify-services (operating-system-user-services
926 %simple-os)
927 (nscd-service-type config
928 => (nscd-configuration
929 (inherit config)
930 (debug-level 3)
931 (log-file "/dev/console")))
932 (syslog-service-type config
933 =>
ec2e2f6c
DC
934 (syslog-configuration
935 (inherit config)
936 (config-file
937 (plain-file
938 "syslog.conf"
939 "*.* /dev/console\n")))))))))
d2fa61bc
LC
940
941(define (run-nss-mdns-test)
942 ;; Test resolution of '.local' names via libc. Start the marionette service
943 ;; *after* nscd. Failing to do that, libc will try to connect to nscd,
944 ;; fail, then never try again (see '__nss_not_use_nscd_hosts' in libc),
945 ;; leading to '.local' resolution failures.
8b113790
LC
946 (define os
947 (marionette-operating-system
948 %avahi-os
949 #:requirements '(nscd)
950 #:imported-modules '((gnu services herd)
951 (guix combinators))))
4ee96a79 952
8b113790
LC
953 (define mdns-host-name
954 (string-append (operating-system-host-name os)
955 ".local"))
4ee96a79 956
8b113790
LC
957 (define test
958 (with-imported-modules '((gnu build marionette))
959 #~(begin
960 (use-modules (gnu build marionette)
961 (srfi srfi-1)
962 (srfi srfi-64)
963 (ice-9 match))
964
965 (define marionette
966 (make-marionette (list #$(virtual-machine os))))
967
968 (mkdir #$output)
969 (chdir #$output)
970
1fb75128 971 (test-runner-current (system-test-runner))
8b113790
LC
972 (test-begin "avahi")
973
c24b1547 974 (test-assert "nscd PID file is created"
8b113790
LC
975 (marionette-eval
976 '(begin
977 (use-modules (gnu services herd))
c24b1547
CL
978 (start-service 'nscd))
979 marionette))
980
981 (test-assert "nscd is listening on its socket"
982 (marionette-eval
983 ;; XXX: Work around a race condition in nscd: nscd creates its
984 ;; PID file before it is listening on its socket.
985 '(let ((sock (socket PF_UNIX SOCK_STREAM 0)))
986 (let try ()
987 (catch 'system-error
988 (lambda ()
989 (connect sock AF_UNIX "/var/run/nscd/socket")
990 (close-port sock)
991 (format #t "nscd is ready~%")
992 #t)
993 (lambda args
994 (format #t "waiting for nscd...~%")
995 (usleep 500000)
996 (try)))))
997 marionette))
998
999 (test-assert "avahi is running"
1000 (marionette-eval
1001 '(begin
1002 (use-modules (gnu services herd))
1003 (start-service 'avahi-daemon))
1004 marionette))
8b113790 1005
c24b1547
CL
1006 (test-assert "network is up"
1007 (marionette-eval
1008 '(begin
1009 (use-modules (gnu services herd))
1010 (start-service 'networking))
8b113790
LC
1011 marionette))
1012
1013 (test-equal "avahi-resolve-host-name"
1014 0
1015 (marionette-eval
1016 '(system*
1017 "/run/current-system/profile/bin/avahi-resolve-host-name"
1018 "-v" #$mdns-host-name)
1019 marionette))
1020
1021 (test-equal "avahi-browse"
1022 0
1023 (marionette-eval
572c59a7 1024 '(system* "/run/current-system/profile/bin/avahi-browse" "-avt")
8b113790
LC
1025 marionette))
1026
1027 (test-assert "getaddrinfo .local"
1028 ;; Wait for the 'avahi-daemon' service and perform a resolution.
1029 (match (marionette-eval
1030 '(getaddrinfo #$mdns-host-name)
1031 marionette)
1032 (((? vector? addrinfos) ..1)
1033 (pk 'getaddrinfo addrinfos)
1034 (and (any (lambda (ai)
1035 (= AF_INET (addrinfo:fam ai)))
1036 addrinfos)
1037 (any (lambda (ai)
1038 (= AF_INET6 (addrinfo:fam ai)))
1039 addrinfos)))))
1040
1041 (test-assert "gethostbyname .local"
1042 (match (pk 'gethostbyname
1043 (marionette-eval '(gethostbyname #$mdns-host-name)
1044 marionette))
1045 ((? vector? result)
1046 (and (string=? (hostent:name result) #$mdns-host-name)
1047 (= (hostent:addrtype result) AF_INET)))))
1048
1049
1fb75128 1050 (test-end))))
8b113790
LC
1051
1052 (gexp->derivation "nss-mdns" test))
d2fa61bc
LC
1053
1054(define %test-nss-mdns
1055 (system-test
1056 (name "nss-mdns")
1057 (description
1058 "Test Avahi's multicast-DNS implementation, and in particular, test its
1059glibc name service switch (NSS) module.")
1060 (value (run-nss-mdns-test))))