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