projects/guix·blob bootstrap-team · refs

1359 lines · 53.8 KiB

1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2016-2020, 2022, 2024-2025 Ludovic Courtès <[email protected]>
3;;; Copyright © 2018 Clément Lassieur <[email protected]>
4;;; Copyright © 2022, 2025 Maxim Cournoyer <[email protected]>
5;;; Copyright © 2022 Marius Bakke <[email protected]>
6;;; Copyright © 2024 Dariqq <[email protected]>
7;;;
8;;; This file is part of GNU Guix.
9;;;
10;;; GNU Guix is free software; you can redistribute it and/or modify it
11;;; under the terms of the GNU General Public License as published by
12;;; the Free Software Foundation; either version 3 of the License, or (at
13;;; your option) any later version.
14;;;
15;;; GNU Guix is distributed in the hope that it will be useful, but
16;;; WITHOUT ANY WARRANTY; without even the implied warranty of
17;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;;; GNU General Public License for more details.
19;;;
20;;; You should have received a copy of the GNU General Public License
21;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
22
23(define-module (gnu tests base)
24 #:use-module (gnu tests)
25 #:use-module (gnu image)
26 #:use-module (gnu system)
27 #:autoload (gnu system image) (system-image)
28 #:use-module (gnu system privilege)
29 #:use-module (gnu system shadow)
30 #:use-module (gnu system vm)
31 #:use-module (gnu services)
32 #:use-module (gnu services base)
33 #:use-module (gnu services shepherd)
34 #:use-module (gnu services networking)
35 #:use-module (gnu packages base)
36 #:use-module (gnu packages bash)
37 #:use-module (gnu packages bootstrap)
38 #:use-module (gnu packages guile)
39 #:use-module (gnu packages imagemagick)
40 #:use-module (gnu packages linux)
41 #:use-module (gnu packages ocr)
42 #:use-module (gnu packages package-management)
43 #:use-module (gnu packages tmux)
44 #:use-module (gnu packages virtualization)
45 #:use-module (guix gexp)
46 #:use-module (guix store)
47 #:use-module (guix monads)
48 #:use-module (guix modules)
49 #:use-module (guix packages)
50 #:use-module (guix profiles)
51 #:use-module (guix utils)
52 #:use-module ((srfi srfi-1) #:hide (partition))
53 #:use-module (ice-9 match)
54 #:export (run-basic-test
55 %test-basic-os
56 %test-linux-libre-5.15
57 %test-linux-libre-5.10
58 %test-linux-libre-5.4
59 %test-halt
60 %test-root-unmount
61 %test-cleanup
62 %test-activation
63
64 %hello-dependencies-manifest
65 guix-daemon-test-cases
66 %test-guix-daemon
67 %test-guix-daemon-unprivileged))
68
69(define %simple-os
70 (simple-operating-system))
71
72
73(define* (run-basic-test os command #:optional (name "basic")
74 #:key
75 initialization
76 root-password
77 desktop?
78 extra-tests)
79 "Return a derivation called NAME that tests basic features of the OS started
80using COMMAND, a gexp that evaluates to a list of strings. Compare some
81properties of running system to what's declared in OS, an <operating-system>.
82
83When INITIALIZATION is true, it must be a one-argument procedure that is
84passed a gexp denoting the marionette, and it must return gexp that is
85inserted before the first test. This is used to introduce an extra
86initialization step, such as entering a LUKS passphrase.
87
88When ROOT-PASSWORD is true, enter it as the root password when logging in.
89Otherwise assume that there is no password for root.
90
91When EXTRA-TESTS is true, it must be a one-argument procedure that is
92passed a gexp denoting the marionette. It must then return a gexp that is
93inserted after the last test. This is meant as a way of extending the basic
94tests that are defined within this procedure."
95 (define special-files
96 (service-value
97 (fold-services (operating-system-services os)
98 #:target-type special-files-service-type)))
99
100 (define guix&co
101 (match (package-transitive-propagated-inputs guix)
102 (((labels packages) ...)
103 (cons guix packages))))
104
105 (define test
106 (with-imported-modules '((gnu build marionette)
107 (guix build syscalls))
108 #~(begin
109 (use-modules (gnu build marionette)
110 (guix build syscalls)
111 (srfi srfi-1)
112 (srfi srfi-19)
113 (srfi srfi-26)
114 (srfi srfi-64)
115 (ice-9 match))
116
117 (define marionette
118 (make-marionette #$command))
119
120 (test-runner-current (system-test-runner #$output))
121 (test-begin "basic")
122
123 #$(and initialization
124 (initialization #~marionette))
125
126 (test-assert "uname"
127 (match (marionette-eval '(uname) marionette)
128 (#("Linux" host-name version _ architecture)
129 (and (string=? host-name
130 #$(operating-system-host-name os))
131 (string-prefix? #$(package-version
132 (operating-system-kernel os))
133 version)
134 (string-prefix? architecture %host-type)))))
135
136 ;; Shepherd reads the config file *before* binding its control
137 ;; socket, so /var/run/shepherd/socket might not exist yet when the
138 ;; 'marionette' service is started.
139 (test-assert "shepherd socket ready"
140 (marionette-eval
141 `(begin
142 (use-modules (gnu services herd))
143 (let loop ((i 10))
144 (cond ((file-exists? (%shepherd-socket-file))
145 #t)
146 ((> i 0)
147 (sleep 1)
148 (loop (- i 1)))
149 (else
150 #f))))
151 marionette))
152
153 (test-eq "stdin is /dev/null"
154 'eof
155 ;; Make sure services can no longer read from stdin once the
156 ;; system has booted.
157 (marionette-eval
158 `(begin
159 (use-modules (gnu services herd))
160 (start-service 'user-processes)
161 ((@@ (gnu services herd) eval-there)
162 '(let ((result (read (current-input-port))))
163 (if (eof-object? result)
164 'eof
165 result))))
166 marionette))
167
168 (test-assert "shell and user commands"
169 ;; Is everything in $PATH?
170 (zero? (marionette-eval '(system "
171. /etc/profile
172set -e -x
173guix --version
174ls --version
175grep --version
176info --version")
177 marionette)))
178
179 (test-equal "special files"
180 '#$special-files
181 (marionette-eval
182 '(begin
183 (use-modules (ice-9 match))
184
185 (map (match-lambda
186 ((file target)
187 (list file (readlink file))))
188 '#$special-files))
189 marionette))
190
191 (test-assert "accounts"
192 (let ((users (marionette-eval '(begin
193 (use-modules (ice-9 match))
194 (let loop ((result '()))
195 (match (getpw)
196 (#f (reverse result))
197 (x (loop (cons x result))))))
198 marionette)))
199 (lset= equal?
200 (map (lambda (user)
201 (list (passwd:name user)
202 (passwd:dir user)))
203 users)
204 (list
205 #$@(map (lambda (account)
206 `(list ,(user-account-name account)
207 ,(user-account-home-directory account)))
208 (operating-system-user-accounts os))))))
209
210 (test-assert "shepherd services"
211 (let ((services (marionette-eval
212 '(begin
213 (use-modules (gnu services herd)
214 (srfi srfi-1))
215
216 (append-map live-service-provision
217 (current-services)))
218 marionette)))
219 (lset= eq?
220 (pk 'services services)
221 '(root
222 shepherd
223 #$@(operating-system-shepherd-service-names os)))))
224
225 (test-equal "libc honors /etc/localtime"
226 -7200 ;CEST = GMT+2
227 ;; Assume OS is configured to have a CEST timezone.
228 (let* ((sept-2021 (time-second
229 (date->time-utc
230 (make-date 0 0 00 12 01 09 2021 7200)))))
231 (marionette-eval
232 `(tm:gmtoff (localtime ,sept-2021))
233 marionette)))
234
235 (test-equal "/etc/localtime is a symlink to the timezone data file"
236 ;; This is a systemd-ism that most distributions follow; we do to,
237 ;; for compatibility with software that assumes this is convention
238 ;; is followed (see:
239 ;; <https://www.freedesktop.org/software/systemd/man/latest/localtime.html#>)
240 #$(operating-system-timezone os)
241 (marionette-eval
242 `(string-join
243 (take-right (string-split (readlink "/etc/localtime") #\/) 2)
244 "/")
245 marionette))
246
247 (test-equal "/var/log/messages is not world-readable"
248 #o640 ;<https://bugs.gnu.org/40405>
249 (begin
250 (wait-for-file "/var/log/messages" marionette
251 #:read 'get-u8)
252 (marionette-eval '(stat:perms (lstat "/var/log/messages"))
253 marionette)))
254
255 (test-assert "homes"
256 (let ((homes
257 '#$(map user-account-home-directory
258 (filter user-account-create-home-directory?
259 (operating-system-user-accounts os)))))
260 (marionette-eval
261 `(begin
262 (use-modules (gnu services herd) (srfi srfi-1))
263
264 ;; Home directories are supposed to exist once 'user-homes'
265 ;; has been started.
266 (start-service 'user-homes)
267
268 (every (lambda (home)
269 (and (file-exists? home)
270 (file-is-directory? home)))
271 ',homes))
272 marionette)))
273
274 (test-assert "skeletons in home directories"
275 (let ((users+homes
276 '#$(filter-map (lambda (account)
277 (and (user-account-create-home-directory?
278 account)
279 (not (user-account-system? account))
280 (list (user-account-name account)
281 (user-account-home-directory
282 account))))
283 (operating-system-user-accounts os))))
284 (marionette-eval
285 `(begin
286 (use-modules (guix build utils) (srfi srfi-1)
287 (ice-9 ftw) (ice-9 match))
288
289 (every (match-lambda
290 ((user home)
291 ;; Make sure HOME has all the skeletons...
292 (and (null? (lset-difference string=?
293 (scandir "/etc/skel/")
294 (scandir home)))
295
296 ;; ... and that everything is user-owned.
297 (let* ((pw (getpwnam user))
298 (uid (passwd:uid pw))
299 (gid (passwd:gid pw))
300 (st (lstat home)))
301 (define (user-owned? file)
302 (= uid (stat:uid (lstat file))))
303
304 (and (= uid (stat:uid st))
305 (eq? 'directory (stat:type st))
306 (every user-owned?
307 (find-files home
308 #:directories? #t)))))))
309 ',users+homes))
310 marionette)))
311
312 (test-equal "permissions on /root"
313 #o700
314 (let ((root-home #$(any (lambda (account)
315 (and (zero? (user-account-uid account))
316 (user-account-home-directory
317 account)))
318 (operating-system-user-accounts os))))
319 (stat:perms (marionette-eval `(stat ,root-home) marionette))))
320
321 (test-equal "permissions on /tmp"
322 #o1777
323 (stat:perms (marionette-eval '(lstat "/tmp") marionette)))
324
325 (test-equal "ownership and permissions of /var/empty"
326 '(0 0 #o555)
327 (let ((st (marionette-eval `(stat "/var/empty") marionette)))
328 (list (stat:uid st) (stat:gid st)
329 (stat:perms st))))
330
331 (test-equal "no extra home directories"
332 '()
333
334 ;; Make sure the home directories that are not supposed to be
335 ;; created are indeed not created.
336 (let ((nonexistent
337 '#$(filter-map (lambda (user)
338 (and (not
339 (user-account-create-home-directory?
340 user))
341 (user-account-home-directory user)))
342 (operating-system-user-accounts os))))
343 (marionette-eval
344 `(begin
345 (use-modules (srfi srfi-1))
346
347 ;; Note: Do not flag "/var/empty".
348 (filter file-exists?
349 ',(remove (cut string-prefix? "/var/" <>)
350 nonexistent)))
351 marionette)))
352
353 (test-equal "login on tty1"
354 "root\n"
355 (begin
356 ;; XXX: On desktop, GDM3 will switch to TTY7. If this happens
357 ;; after we switched to TTY1, we won't be able to login. Make
358 ;; sure to wait long enough before switching to TTY1.
359 (when #$desktop?
360 (sleep 30))
361
362 (marionette-control "sendkey ctrl-alt-f1" marionette)
363 ;; Wait for the 'term-tty1' service to be running (using
364 ;; 'start-service' is the simplest and most reliable way to do
365 ;; that.)
366 (marionette-eval
367 '(begin
368 (use-modules (gnu services herd))
369 (start-service 'term-tty1))
370 marionette)
371
372 ;; Now we can type.
373 (let ((password #$root-password))
374 (if password
375 (begin
376 (marionette-type "root\n" marionette)
377 (wait-for-screen-text marionette
378 (lambda (text)
379 (string-contains text "Password"))
380 #:ocr
381 #$(file-append ocrad "/bin/ocrad"))
382 (marionette-type (string-append password "\n\n")
383 marionette))
384 (marionette-type "root\n\n" marionette)))
385 (marionette-type "id -un > logged-in\n" marionette)
386
387 ;; It can take a while before the shell commands are executed.
388 (marionette-eval '(use-modules (rnrs io ports)) marionette)
389 (wait-for-file "/root/logged-in" marionette
390 #:read 'get-string-all
391 #:timeout 30)))
392
393 (test-equal "getlogin on tty1"
394 "\"root\""
395 (begin
396 ;; Assume we logged in in the previous test and type.
397 (marionette-type "guile -c '(write (getlogin))' > /root/login-id.tmp\n"
398 marionette)
399 (marionette-type "mv /root/login-id{.tmp,}\n"
400 marionette)
401
402 ;; It can take a while before the shell commands are executed.
403 (marionette-eval '(use-modules (rnrs io ports)) marionette)
404 (wait-for-file "/root/login-id" marionette
405 #:read 'get-string-all
406 #:timeout 30)))
407
408 ;; There should be one utmpx entry for the user logged in on tty1.
409 (test-equal "utmpx entry"
410 '(("root" "tty1" #f))
411 (marionette-eval
412 '(begin
413 (use-modules (guix build syscalls)
414 (srfi srfi-1))
415
416 (filter-map (lambda (entry)
417 (and (equal? (login-type USER_PROCESS)
418 (utmpx-login-type entry))
419 (list (utmpx-user entry) (utmpx-line entry)
420 (utmpx-host entry))))
421 (utmpx-entries)))
422 marionette))
423
424 ;; Likewise for /var/log/wtmp (used by 'last').
425 (test-assert "wtmp entry"
426 (match (marionette-eval
427 '(begin
428 (use-modules (guix build syscalls)
429 (srfi srfi-1))
430
431 (define (entry->list entry)
432 (list (utmpx-user entry) (utmpx-line entry)
433 (utmpx-host entry) (utmpx-login-type entry)))
434
435 (call-with-input-file "/var/log/wtmp"
436 (lambda (port)
437 (let loop ((result '()))
438 (if (eof-object? (peek-char port))
439 (map entry->list (reverse result))
440 (loop (cons (read-utmpx port) result)))))))
441 marionette)
442 (((users lines hosts types) ..1)
443 (every (lambda (type)
444 (eqv? type (login-type LOGIN_PROCESS)))
445 types))))
446
447 (test-assert "host name resolution"
448 (match (marionette-eval
449 '(begin
450 ;; Wait for nscd or our requests go through it.
451 (use-modules (gnu services herd))
452 (start-service 'nscd)
453
454 (list (getaddrinfo "localhost")
455 (getaddrinfo #$(operating-system-host-name os))))
456 marionette)
457 ((((? vector?) ..1) ((? vector?) ..1))
458 #t)
459 (x
460 (pk 'failure x #f))))
461
462 (test-assert "nscd configuration action"
463 (marionette-eval '(with-shepherd-action 'nscd ('configuration)
464 results
465 (file-exists? (car results)))
466 marionette))
467
468 (test-equal "nscd invalidate action"
469 '(#t) ;one value, #t
470 (marionette-eval '(with-shepherd-action 'nscd ('invalidate "hosts")
471 result
472 result)
473 marionette))
474
475 ;; FIXME: The 'invalidate' action can't reliably obtain the exit
476 ;; code of 'nscd' so skip this test.
477 (test-skip 1)
478 (test-equal "nscd invalidate action, wrong table"
479 '(#f) ;one value, #f
480 (marionette-eval '(with-shepherd-action 'nscd ('invalidate "xyz")
481 result
482 result)
483 marionette))
484
485 (test-equal "host not found"
486 #f
487 (marionette-eval
488 '(false-if-exception (getaddrinfo "does-not-exist"))
489 marionette))
490
491 (test-equal "locale"
492 "en_US.utf8"
493 (marionette-eval '(let ((before (setlocale LC_ALL "en_US.utf8")))
494 (setlocale LC_ALL before))
495 marionette))
496
497 (test-eq "/run/current-system is a GC root"
498 'success!
499 (marionette-eval '(begin
500 ;; Make sure the (guix …) modules are found.
501 (eval-when (expand load eval)
502 (set! %load-path
503 (append (map (lambda (package)
504 (string-append package
505 "/share/guile/site/"
506 (effective-version)))
507 '#$guix&co)
508 %load-path)))
509
510 (use-modules (srfi srfi-34) (guix store))
511
512 (let ((system (readlink "/run/current-system")))
513 (guard (c ((store-protocol-error? c)
514 (and (file-exists? system)
515 'success!)))
516 (with-store store
517 (delete-paths store (list system))
518 #f))))
519 marionette))
520
521 ;; This symlink is currently unused, but better have it point to the
522 ;; right place. See
523 ;; <https://lists.gnu.org/archive/html/guix-devel/2016-08/msg01641.html>.
524 (test-equal "/var/guix/gcroots/profiles is a valid symlink"
525 "/var/guix/profiles"
526 (marionette-eval '(readlink "/var/guix/gcroots/profiles")
527 marionette))
528
529 (test-equal "guix-daemon set-http-proxy action"
530 '(#t) ;one value, #t
531 (marionette-eval '(with-shepherd-action 'guix-daemon
532 ('set-http-proxy "http://localhost:8118")
533 result
534 result)
535 marionette))
536
537 (test-equal "guix-daemon set-http-proxy action, clear"
538 '(#t) ;one value, #t
539 (marionette-eval '(with-shepherd-action 'guix-daemon
540 ('set-http-proxy)
541 result
542 result)
543 marionette))
544
545 (test-assert "screendump"
546 (begin
547 (let ((capture
548 (string-append #$output "/tty1.ppm")))
549 (marionette-control
550 (string-append "screendump " capture) marionette)
551 (file-exists? capture))))
552
553 (test-assert "screen text"
554 (wait-for-screen-text
555 marionette
556 (lambda (text)
557 ;; Check whether the welcome message and shell prompt are
558 ;; displayed. Note: OCR confuses "y" and "V" for instance, so
559 ;; we cannot reliably match the whole text.
560 (and (string-contains text "This is the GNU")
561 (string-contains text
562 (string-append
563 "root@"
564 #$(operating-system-host-name os)))))
565 #:ocr #$(file-append ocrad "/bin/ocrad")))
566
567 (test-equal "block devices have correct default polling value"
568 "2000"
569 ;; This tests that the 'udevadm trigger' correctly creates the
570 ;; subsystems nodes, by checking that the standard 60-block.rules
571 ;; udev rules was applied.
572 (marionette-eval
573 '(begin
574 (use-modules (ice-9 textual-ports))
575 (call-with-input-file
576 "/sys/module/block/parameters/events_dfl_poll_msecs"
577 (compose string-trim-right get-string-all)))
578 marionette))
579
580 #$(and extra-tests
581 (extra-tests #~marionette))
582
583 (test-end))))
584
585 (gexp->derivation name test))
586
587(define* (test-basic-os #:optional (kernel linux-libre))
588 (system-test
589 (name (if (eq? kernel linux-libre)
590 "basic"
591 (string-append (package-name kernel) "-"
592 (version-major+minor (package-version kernel)))))
593 (description
594 "Instrument %SIMPLE-OS, run it in a VM, and run a series of basic
595functionality tests, using the given KERNEL.")
596 (value
597 (let* ((os (marionette-operating-system
598 (operating-system
599 (inherit %simple-os)
600 (kernel kernel)
601 (services (cons* (service
602 etc-profile-d-service-type
603 (list (plain-file
604 "test_profile_d.sh"
605 "export PROFILE_D_OK=yes\n")
606 (plain-file
607 "invalid-name"
608 "not a POSIX script -- ignore me")))
609 (simple-service
610 'extra-bashrc-d-files
611 etc-bashrc-d-service-type
612 (list (plain-file
613 "test_bashrc_d.sh"
614 "export BASHRC_D_OK=yes\n")
615 (plain-file
616 "invalid-name"
617 "not a Bash script -- ignore me")))
618 %base-services)))
619 #:imported-modules '((gnu services herd)
620 (guix combinators))))
621 (vm (virtual-machine os)))
622 ;; XXX: Add call to 'virtualized-operating-system' to get the exact same
623 ;; set of services as the OS produced by
624 ;; 'system-qemu-image/shared-store-script'.
625 (run-basic-test (virtualized-operating-system os '())
626 #~(list #$vm)
627 name
628 ;; Add extra tests for the etc-profile-d-service-type
629 ;; and etc-bashrc-d-service-type services defined above.
630 ;; Those tests cannot directly be part of the
631 ;; run-basic-test procedure that is used in many other
632 ;; locations.
633 #:extra-tests
634 (lambda (marionette)
635 #~(begin
636 (test-assert "/etc/profile.d is sourced"
637 (zero?
638 (marionette-eval '(system "
639. /etc/profile
640set -e -x
641test -f /etc/profile.d/test_profile_d.sh
642test \"$PROFILE_D_OK\" = yes")
643 #$marionette)))
644
645 (test-assert "/etc/bashrc.d is sourced"
646 (zero?
647 (marionette-eval
648 '(system* "bash"
649 "-i" ;run interactively
650 #$(plain-file "test_bashrc_d.sh"
651 "\
652. /etc/bashrc
653set -e -x
654test -f /etc/bashrc.d/bash_completion.sh
655test -f /etc/bashrc.d/aliases.sh
656test -f /etc/bashrc.d/test_bashrc_d.sh
657test \"$BASHRC_D_OK\" = yes"))
658 #$marionette))))))))))
659
660(define %test-basic-os
661 (test-basic-os))
662
663;; Ensure the LTS kernels are up to snuff, too.
664(define %test-linux-libre-6.12
665 (test-basic-os linux-libre-6.12))
666
667(define %test-linux-libre-6.6
668 (test-basic-os linux-libre-6.6))
669
670(define %test-linux-libre-6.1
671 (test-basic-os linux-libre-6.1))
672
673(define %test-linux-libre-5.15
674 (test-basic-os linux-libre-5.15))
675
676(define %test-linux-libre-5.10
677 (test-basic-os linux-libre-5.10))
678
679(define %test-linux-libre-5.4
680 (test-basic-os linux-libre-5.4))
681
682
683;;;
684;;; Halt.
685;;;
686
687(define (run-halt-test vm)
688 ;; As reported in <http://bugs.gnu.org/26931>, running tmux would previously
689 ;; lead the 'stop' method of 'user-processes' to an infinite loop, with the
690 ;; tmux server process as a zombie that remains in the list of processes.
691 ;; This test reproduces this scenario.
692 (define test
693 (with-imported-modules '((gnu build marionette))
694 #~(begin
695 (use-modules (gnu build marionette))
696
697 (define marionette
698 (make-marionette '(#$vm)))
699
700 (define ocrad
701 #$(file-append ocrad "/bin/ocrad"))
702
703 ;; Wait for tty1 and log in.
704 (marionette-eval '(begin
705 (use-modules (gnu services herd))
706 (start-service 'term-tty1))
707 marionette)
708 (marionette-type "root\n" marionette)
709
710 ;; Start tmux and wait for it to be ready.
711 (marionette-type "tmux new-session 'echo 1 > /ready; bash'\n"
712 marionette)
713 (wait-for-file "/ready" marionette)
714
715 ;; Make sure to stop the test after a while.
716 (sigaction SIGALRM (lambda _
717 (format (current-error-port)
718 "FAIL: Time is up, but VM still running.\n")
719 (primitive-exit 1)))
720 (alarm 10)
721
722 ;; Get debugging info.
723 (marionette-eval '(current-output-port
724 (open-file "/dev/console" "w0"))
725 marionette)
726 (marionette-eval '(system* #$(file-append procps "/bin/ps")
727 "-eo" "pid,ppid,stat,comm")
728 marionette)
729
730 ;; See if 'halt' actually works.
731 (marionette-eval '(system* "/run/current-system/profile/sbin/halt")
732 marionette)
733
734 ;; If we reach this line, that means the VM was properly stopped in
735 ;; a timely fashion.
736 (alarm 0)
737 (call-with-output-file #$output
738 (lambda (port)
739 (display "success!" port))))))
740
741 (gexp->derivation "halt" test))
742
743(define %test-halt
744 (system-test
745 (name "halt")
746 (description
747 "Use the 'halt' command and make sure it succeeds and does not get stuck
748in a loop. See <http://bugs.gnu.org/26931>.")
749 (value
750 (let ((os (marionette-operating-system
751 (operating-system
752 (inherit %simple-os)
753 (packages (cons tmux %base-packages)))
754 #:imported-modules '((gnu services herd)
755 (guix combinators)))))
756 (run-halt-test (virtual-machine os))))))
757
758
759;;;
760;;; Root cleanly unmounted.
761;;;
762
763(define (run-root-unmount-test os)
764 (define test-image
765 (image (operating-system os)
766 (format 'compressed-qcow2)
767 (volatile-root? #f)
768 (shared-store? #f)
769 (partition-table-type 'mbr)
770 (partitions
771 (list (partition
772 (size 'guess)
773 (offset (* 512 2048)) ;leave room for GRUB
774 (flags '(boot))
775 (label "root-under-test")))))) ;max 16 characters!
776
777 (define observer-os
778 (marionette-operating-system
779 %simple-os
780 #:imported-modules
781 (source-module-closure '((guix build syscalls)
782 (gnu build file-systems)))))
783
784 (define test
785 (with-imported-modules (source-module-closure
786 '((gnu build marionette)
787 (guix build utils)))
788 #~(begin
789 (use-modules (gnu build marionette)
790 (guix build utils)
791 (srfi srfi-64)
792 (ice-9 ftw))
793
794 (define image
795 "/tmp/writable-image.qcow2")
796
797 (define (test-system-marionette)
798 ;; Return a marionette on a system where we'll run 'halt'.
799 (invoke #$(file-append qemu-minimal "/bin/qemu-img")
800 "create" "-f" "qcow2" image "3G"
801 "-b" #$(system-image test-image) "-F" "qcow2")
802 (make-marionette
803 `(,(string-append #$qemu-minimal "/bin/" (qemu-command))
804 ,@(if (file-exists? "/dev/kvm")
805 '("-enable-kvm")
806 '())
807 "-no-reboot"
808 "-m" "1024" ;memory size, in MiB
809 "-drive" ,(format #f "file=~a,if=virtio" image))))
810
811 (define witness-size
812 ;; Size of the /witness file.
813 (* 20 (expt 2 20)))
814
815 (test-runner-current (system-test-runner #$output))
816 (test-begin "root-unmount")
817
818 (let ((marionette (test-system-marionette)))
819 (test-assert "file created"
820 (marionette-eval `(begin
821 (use-modules (guix build utils))
822 (call-with-output-file "/witness"
823 (lambda (port)
824 (call-with-input-file "/dev/random"
825 (lambda (input)
826 (dump-port input port
827 ,witness-size))))))
828 marionette))
829
830 ;; Cause PID 1 to create a mapping to nscd's database files.
831 ;; Those mappings used to prevent 'root-file-system' to remount
832 ;; read-only on shutdown. See
833 ;; <https://codeberg.org/guix/guix/issues/4269>.
834 (test-equal "open libc NSS database"
835 "root"
836 (marionette-eval '(begin
837 (use-modules (gnu services herd))
838 (start-service 'nscd) ;wait for nscd
839 (eval-there '(passwd:name (getgr "root"))))
840 marionette))
841
842 ;; Halt the system.
843 (marionette-eval '(system* "/run/current-system/profile/sbin/halt")
844 marionette)
845
846 (display "waiting for marionette to complete...")
847 (force-output)
848 (false-if-exception (waitpid (marionette-pid marionette)))
849 (display " done\n")
850 (force-output))
851
852 ;; Remove the sockets used by the marionette above to avoid
853 ;; EADDRINUSE.
854 (for-each delete-file
855 (find-files "/tmp" (lambda (file stat)
856 (eq? (stat:type stat) 'socket))))
857
858 ;; Now boot another system and check whether the root file system of
859 ;; the first one was cleanly unmounted.
860
861 (let ((observer
862 (make-marionette (list #$(virtual-machine observer-os)
863 "-drive"
864 (format #f "file=~a,if=virtio" image)))))
865 (test-assert "partitions"
866 (marionette-eval '(begin
867 (use-modules (gnu build file-systems))
868 (disk-partitions))
869 observer))
870
871 (test-assert "partition found"
872 (marionette-eval '(find-partition-by-label "root-under-test")
873 observer))
874
875 (test-assert "root file system is clean"
876 (marionette-eval '(cleanly-unmounted-ext2?
877 (find-partition-by-label "root-under-test"))
878 observer))
879
880 (test-equal "root file system contains /witness"
881 witness-size
882 (let ((files (marionette-eval
883 '(begin
884 (use-modules (guix build syscalls)
885 (ice-9 ftw))
886 (mount (find-partition-by-label "root-under-test")
887 "/mnt" "ext4" MS_RDONLY)
888 (scandir "/mnt"))
889 observer)))
890 (if (member "witness" files)
891 (marionette-eval '(stat:size (stat "/mnt/witness"))
892 observer)
893 files))))
894
895 (test-end))))
896
897 (gexp->derivation "root-unmount" test))
898
899(define %test-root-unmount
900 (system-test
901 (name "root-unmount")
902 (description
903 "Make sure the root file system is cleanly unmounted when the system is
904halted.")
905 (value
906 (let ((os (marionette-operating-system
907 %simple-os
908 #:imported-modules '((gnu services herd)
909 (guix combinators)))))
910 (run-root-unmount-test os)))))
911
912
913;;;
914;;; Cleanup of /tmp, /var/run, etc.
915;;;
916
917(define %cleanup-os
918 (simple-operating-system
919 (simple-service 'dirty-things
920 boot-service-type
921 (let ((script (plain-file
922 "create-utf8-file.sh"
923 (string-append
924 "echo $0: dirtying /tmp...\n"
925 "set -e; set -x\n"
926 "touch /witness\n"
927 "exec touch /tmp/λαμβδα"))))
928 (with-imported-modules '((guix build utils))
929 #~(begin
930 (setenv "PATH"
931 #$(file-append coreutils "/bin"))
932 (invoke #$(file-append bash "/bin/sh")
933 #$script)))))))
934
935(define (run-cleanup-test name)
936 (define os
937 (marionette-operating-system %cleanup-os
938 #:imported-modules '((gnu services herd)
939 (guix combinators))))
940 (define test
941 (with-imported-modules '((gnu build marionette))
942 #~(begin
943 (use-modules (gnu build marionette)
944 (srfi srfi-64)
945 (ice-9 match))
946
947 (define marionette
948 (make-marionette (list #$(virtual-machine os))))
949
950 (test-runner-current (system-test-runner #$output))
951 (test-begin "cleanup")
952
953 (test-assert "dirty service worked"
954 (marionette-eval '(file-exists? "/witness") marionette))
955
956 (test-equal "/tmp cleaned up"
957 '("." "..")
958 (marionette-eval '(begin
959 (use-modules (ice-9 ftw))
960 (scandir "/tmp"))
961 marionette))
962
963 (test-end))))
964
965 (gexp->derivation "cleanup" test))
966
967(define %test-cleanup
968 ;; See <https://bugs.gnu.org/26353>.
969 (system-test
970 (name "cleanup")
971 (description "Make sure the 'cleanup' service can remove files with
972non-ASCII names from /tmp.")
973 (value (run-cleanup-test name))))
974
975
976;;;
977;;; Activation: Order of activation scripts
978;;; Create accounts before running scripts using them
979
980(define %activation-os
981 ;; System with a new user/group, a setuid/setgid binary and an activation script
982 (let* ((%hello-accounts
983 (list (user-group (name "hello") (system? #t))
984 (user-account
985 (name "hello")
986 (group "hello")
987 (system? #t)
988 (comment "")
989 (home-directory "/var/empty"))))
990 (%hello-privileged
991 (list
992 (privileged-program
993 (program (file-append hello "/bin/hello"))
994 (setuid? #t)
995 (setgid? #t)
996 (user "hello")
997 (group "hello"))))
998 (%hello-activation
999 (with-imported-modules (source-module-closure
1000 '((gnu build activation)))
1001 #~(begin
1002 (use-modules (gnu build activation))
1003
1004 (let ((user (getpwnam "hello")))
1005 (mkdir-p/perms "/run/hello" user #o755)))))
1006
1007 (hello-service-type
1008 (service-type
1009 (name 'hello)
1010 (extensions
1011 (list (service-extension account-service-type
1012 (const %hello-accounts))
1013 (service-extension activation-service-type
1014 (const %hello-activation))
1015 (service-extension privileged-program-service-type
1016 (const %hello-privileged))))
1017 (default-value #f)
1018 (description ""))))
1019
1020 (operating-system
1021 (inherit %simple-os)
1022 (services
1023 (cons* (service hello-service-type)
1024 (operating-system-user-services
1025 %simple-os))))))
1026
1027(define (run-activation-test name)
1028 (define os
1029 (marionette-operating-system
1030 %activation-os))
1031
1032 (define test
1033 (with-imported-modules '((gnu build marionette))
1034 #~(begin
1035 (use-modules (gnu build marionette)
1036 (srfi srfi-64))
1037
1038 (define marionette
1039 (make-marionette (list #$(virtual-machine os))))
1040
1041 (test-runner-current (system-test-runner #$output))
1042 (test-begin "activation")
1043
1044 (test-assert "directory exists"
1045 (marionette-eval
1046 '(file-exists? "/run/hello")
1047 marionette))
1048
1049 (test-assert "directory correct permissions and owner"
1050 (marionette-eval
1051 '(let ((dir (stat "/run/hello"))
1052 (user (getpwnam "hello")))
1053 (and (eqv? (stat:uid dir)
1054 (passwd:uid user))
1055 (eqv? (stat:gid dir)
1056 (passwd:gid user))
1057 (= (stat:perms dir)
1058 #o0755)))
1059 marionette))
1060
1061 (test-assert "privileged-program exists"
1062 (marionette-eval
1063 '(file-exists? "/run/privileged/bin/hello")
1064 marionette))
1065
1066 (test-assert "privileged-program correct permissions and owner"
1067 (marionette-eval
1068 '(let ((binary (stat "/run/privileged/bin/hello"))
1069 (user (getpwnam "hello"))
1070 (group (getgrnam "hello")))
1071 (and (eqv? (stat:uid binary)
1072 (passwd:uid user))
1073 (eqv? (stat:gid binary)
1074 (group:gid group))
1075 (= (stat:perms binary)
1076 (+ #o0555 ;; base
1077 #o4000 ;; setuid
1078 #o2000)))) ;; setgid
1079 marionette))
1080
1081 (test-end))))
1082
1083 (gexp->derivation name test))
1084
1085(define %test-activation
1086 (system-test
1087 (name "activation")
1088 (description "Test that activation scripts are run in the correct order")
1089 (value (run-activation-test name))))
1090
1091
1092;;;
1093;;; Build daemon.
1094;;;
1095
1096(define %daemon-os
1097 (operating-system-with-console-syslog
1098 (simple-operating-system)))
1099
1100(define (manifest-entry-without-grafts entry)
1101 "Return ENTRY with grafts disabled on its contents."
1102 (manifest-entry
1103 (inherit entry)
1104 (item (with-parameters ((%graft? #f))
1105 (manifest-entry-item entry)))))
1106
1107(define %hello-dependencies-manifest
1108 ;; Build dependencies of 'hello' needed to test 'guix build hello'.
1109 (concatenate-manifests
1110 (list (map-manifest-entries
1111 manifest-entry-without-grafts
1112 (package->development-manifest hello))
1113
1114 ;; Add the source of 'hello'.
1115 (manifest
1116 (list (manifest-entry
1117 (name "hello-source")
1118 (version (package-version hello))
1119 (item (let ((file (origin-actual-file-name
1120 (package-source hello))))
1121 (computed-file
1122 "hello-source"
1123 #~(begin
1124 ;; Put the tarball in a subdirectory since
1125 ;; profile union crashes otherwise.
1126 (mkdir #$output)
1127 (mkdir (in-vicinity #$output "src"))
1128 (symlink #$(package-source hello)
1129 (in-vicinity #$output
1130 (string-append "src/"
1131 #$file))))))))))
1132
1133 ;; Include 'guile-final', which is needed when building derivations
1134 ;; such as that of 'hello' but missing from the development manifest.
1135 ;; Add '%bootstrap-guile', used by 'guix install --bootstrap'.
1136 (map-manifest-entries
1137 manifest-entry-without-grafts
1138 (packages->manifest (list (canonical-package guile-3.0)
1139 %bootstrap-guile))))))
1140
1141(define (guix-daemon-test-cases marionette)
1142 "Return a gexp with SRFI-64 test cases testing guix-daemon. Those test are
1143evaluated in MARIONETTE, a gexp denoting a marionette (system under test).
1144Assume that an unprivileged account for 'user' exists on the system under
1145test."
1146 (define chown-snippet
1147 ;; XXX: This snippet exists primarily so that #$output is understood in
1148 ;; the right context.
1149 '(object->string
1150 `(begin
1151 (use-modules (guix)
1152 (gnu packages bootstrap))
1153 (computed-file "chown-to-supplementary-group"
1154 #~(begin
1155 (use-modules (srfi srfi-1))
1156
1157 ',(gettimeofday) ;nonce
1158 (let* ((groups (getgroups))
1159 (other (find (lambda (gid)
1160 (not (= gid (getgid))))
1161 (vector->list groups))))
1162 (format #t "attempting to chown \
1163to supplementary group ~a...~%" other)
1164 (pk 'supplementary-groups (getgroups)
1165 'gid (getgid) 'other other)
1166 (force-output)
1167 (mkdir "test")
1168 (chown "test" (getuid) other)
1169 (mkdir #$output)))
1170 #:guile %bootstrap-guile))))
1171
1172 #~(begin
1173 (test-equal "guix describe"
1174 0
1175 (marionette-eval '(system* "guix" "describe")
1176 #$marionette))
1177
1178 (test-equal "hello not already built"
1179 #f
1180 ;; Check that the next test will really build 'hello'.
1181 (marionette-eval '(file-exists?
1182 #$(with-parameters ((%graft? #f))
1183 hello))
1184 #$marionette))
1185
1186 (test-equal "guix build hello"
1187 0
1188 ;; Check that guix-daemon is up and running and that the build
1189 ;; environment is properly set up (build users, etc.).
1190 (marionette-eval '(system* "guix" "build" "hello" "--no-grafts")
1191 #$marionette))
1192
1193 (test-assert "hello indeed built"
1194 (marionette-eval '(file-exists?
1195 #$(with-parameters ((%graft? #f))
1196 hello))
1197 #$marionette))
1198
1199 (test-equal "kvm GID mapped"
1200 0
1201 ;; The "kvm" group should be among the supplementary groups of the
1202 ;; build user. Try to chown a file to that group; this fails with
1203 ;; EINVAL when running the unprivileged guix-daemon and the "kvm" GID
1204 ;; is not mapped in its user namespace. See
1205 ;; <https://bugs.gnu.org/77862>.
1206 (marionette-eval
1207 '(system* "guix" "build" "--no-grafts" "-e" #$chown-snippet)
1208 #$marionette))
1209
1210 (test-equal "guix install hello"
1211 0
1212 ;; Check that ~/.guix-profile & co. are properly created.
1213 (marionette-eval '(let ((pw (getpwuid (getuid))))
1214 (setenv "USER" (passwd:name pw))
1215 (setenv "HOME" (pk 'home (passwd:dir pw)))
1216 (system* "guix" "install" "hello"
1217 "--no-grafts" "--bootstrap"))
1218 #$marionette))
1219
1220 (test-equal "user profile created"
1221 0
1222 (marionette-eval '(system "ls -lad ~/.guix-profile")
1223 #$marionette))
1224
1225 (test-equal "hello"
1226 0
1227 (marionette-eval '(system "~/.guix-profile/bin/hello")
1228 #$marionette))
1229
1230 (test-equal "guix install hello, unprivileged user"
1231 0
1232 ;; Check that 'guix' is in $PATH for new users and that
1233 ;; ~user/.guix-profile also gets created, assuming that 'user' exists
1234 ;; as an unprivileged user account.
1235 (marionette-eval '(system "su - user -c \
1236'guix install hello --no-grafts --bootstrap'")
1237 #$marionette))
1238
1239 (test-equal "user hello"
1240 0
1241 (marionette-eval '(system "~user/.guix-profile/bin/hello")
1242 #$marionette))
1243
1244 (test-equal "unprivileged user profile created"
1245 0
1246 (marionette-eval '(system "ls -lad ~user/.guix-profile")
1247 #$marionette))
1248
1249 (test-equal "store is read-only"
1250 EROFS
1251 (marionette-eval '(catch 'system-error
1252 (lambda ()
1253 (mkdir (in-vicinity #$(%store-prefix)
1254 "whatever"))
1255 0)
1256 (lambda args
1257 (system-error-errno args)))
1258 #$marionette))))
1259
1260(define (run-guix-daemon-test os name)
1261 (define test-image
1262 (image (operating-system os)
1263 (format 'compressed-qcow2)
1264 (volatile-root? #f)
1265 (shared-store? #f)
1266 (partition-table-type 'mbr)
1267 (partitions
1268 (list (partition
1269 (size (* 4 (expt 2 30)))
1270 (offset (* 512 2048)) ;leave room for GRUB
1271 (flags '(boot))
1272 (label "root"))))))
1273
1274 (define test
1275 (with-imported-modules (source-module-closure
1276 '((gnu build marionette)
1277 (guix build utils)))
1278 #~(begin
1279 (use-modules (gnu build marionette)
1280 (guix build utils)
1281 (srfi srfi-64))
1282
1283 (define marionette
1284 (make-marionette
1285 (list (string-append #$qemu-minimal "/bin/" (qemu-command))
1286 #$@(common-qemu-options (system-image test-image) '()
1287 #:image-format "qcow2"
1288 #:rw-image? #t)
1289 "-m" "512"
1290 "-nographic" "-serial" "stdio"
1291 "-snapshot")))
1292
1293 (test-runner-current (system-test-runner #$output))
1294 (test-begin "guix-daemon")
1295
1296 (test-assert "guix-service is running"
1297 ;; Wait for 'guix-daemon' to be up.
1298 (marionette-eval '(begin
1299 (use-modules (gnu services herd))
1300 (start-service 'guix-daemon))
1301 marionette))
1302
1303 #$(guix-daemon-test-cases #~marionette)
1304
1305 (test-end))))
1306
1307 (gexp->derivation name test))
1308
1309(define %test-guix-daemon
1310 (system-test
1311 (name "guix-daemon")
1312 (description
1313 "Test 'guix-daemon' behavior on a multi-user system.")
1314 (value
1315 (let ((os (marionette-operating-system
1316 (operating-system
1317 (inherit (operating-system-with-gc-roots
1318 %daemon-os
1319 (list (profile
1320 (name "hello-build-dependencies")
1321 (content %hello-dependencies-manifest)))))
1322 (kernel-arguments '("console=ttyS0"))
1323 (users (cons (user-account
1324 (name "user")
1325 (group "users"))
1326 %base-user-accounts)))
1327 #:imported-modules '((gnu services herd)
1328 (guix combinators)))))
1329 (run-guix-daemon-test os "guix-daemon-test")))))
1330
1331(define %test-guix-daemon-unprivileged
1332 (system-test
1333 (name "guix-daemon-unprivileged")
1334 (description
1335 "Test 'guix-daemon' behavior on a multi-user system, where 'guix-daemon'
1336runs unprivileged.")
1337 (value
1338 (let ((os (marionette-operating-system
1339 (let ((base (operating-system-with-gc-roots
1340 %daemon-os
1341 (list (profile
1342 (name "hello-build-dependencies")
1343 (content %hello-dependencies-manifest))))))
1344 (operating-system
1345 (inherit base)
1346 (kernel-arguments '("console=ttyS0"))
1347 (users (cons (user-account
1348 (name "user")
1349 (group "users"))
1350 %base-user-accounts))
1351 (services
1352 (modify-services (operating-system-user-services base)
1353 (guix-service-type
1354 config => (guix-configuration
1355 (inherit config)
1356 (privileged? #f)))))))
1357 #:imported-modules '((gnu services herd)
1358 (guix combinators)))))
1359 (run-guix-daemon-test os "guix-daemon-unprivileged-test")))))