services: Create /var/run/utmpx upon activation.
[jackhill/guix/guix.git] / gnu / tests / base.scm
CommitLineData
e9f693d0 1;;; GNU Guix --- Functional package management for GNU
caa78166 2;;; Copyright © 2016, 2017 Ludovic Courtès <ludo@gnu.org>
e9f693d0
LC
3;;;
4;;; This file is part of GNU Guix.
5;;;
6;;; GNU Guix is free software; you can redistribute it and/or modify it
7;;; under the terms of the GNU General Public License as published by
8;;; the Free Software Foundation; either version 3 of the License, or (at
9;;; your option) any later version.
10;;;
11;;; GNU Guix is distributed in the hope that it will be useful, but
12;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19(define-module (gnu tests base)
20 #:use-module (gnu tests)
21 #:use-module (gnu system)
22 #:use-module (gnu system grub)
23 #:use-module (gnu system file-systems)
24 #:use-module (gnu system shadow)
d2fa61bc 25 #:use-module (gnu system nss)
e9f693d0
LC
26 #:use-module (gnu system vm)
27 #:use-module (gnu services)
d2fa61bc
LC
28 #:use-module (gnu services base)
29 #:use-module (gnu services dbus)
30 #:use-module (gnu services avahi)
c311089b 31 #:use-module (gnu services mcron)
e9f693d0 32 #:use-module (gnu services shepherd)
d2fa61bc 33 #:use-module (gnu services networking)
fe933833
LC
34 #:use-module (gnu packages imagemagick)
35 #:use-module (gnu packages ocr)
e9f693d0
LC
36 #:use-module (guix gexp)
37 #:use-module (guix store)
38 #:use-module (guix monads)
39 #:use-module (guix packages)
40 #:use-module (srfi srfi-1)
e3de272a 41 #:export (run-basic-test
c311089b 42 %test-basic-os
d2fa61bc
LC
43 %test-mcron
44 %test-nss-mdns))
e9f693d0
LC
45
46(define %simple-os
47 (operating-system
48 (host-name "komputilo")
49 (timezone "Europe/Berlin")
50 (locale "en_US.UTF-8")
51
52 (bootloader (grub-configuration (device "/dev/sdX")))
53 (file-systems (cons (file-system
54 (device "my-root")
55 (title 'label)
56 (mount-point "/")
57 (type "ext4"))
58 %base-file-systems))
59 (firmware '())
60
61 (users (cons (user-account
62 (name "alice")
63 (comment "Bob's sister")
64 (group "users")
65 (supplementary-groups '("wheel" "audio" "video"))
66 (home-directory "/home/alice"))
67 %base-user-accounts))))
68
69\f
f7f292d3
LC
70(define* (run-basic-test os command #:optional (name "basic")
71 #:key initialization)
e3de272a
LC
72 "Return a derivation called NAME that tests basic features of the OS started
73using COMMAND, a gexp that evaluates to a list of strings. Compare some
f7f292d3
LC
74properties of running system to what's declared in OS, an <operating-system>.
75
76When INITIALIZATION is true, it must be a one-argument procedure that is
77passed a gexp denoting the marionette, and it must return gexp that is
78inserted before the first test. This is used to introduce an extra
79initialization step, such as entering a LUKS passphrase."
e3de272a 80 (define test
caa78166
LC
81 (with-imported-modules '((gnu build marionette)
82 (guix build syscalls))
4ee96a79
LC
83 #~(begin
84 (use-modules (gnu build marionette)
caa78166 85 (guix build syscalls)
4ee96a79
LC
86 (srfi srfi-1)
87 (srfi srfi-26)
88 (srfi srfi-64)
89 (ice-9 match))
90
91 (define marionette
92 (make-marionette #$command))
93
94 (mkdir #$output)
95 (chdir #$output)
96
97 (test-begin "basic")
98
f7f292d3
LC
99 #$(and initialization
100 (initialization #~marionette))
101
4ee96a79
LC
102 (test-assert "uname"
103 (match (marionette-eval '(uname) marionette)
104 (#("Linux" host-name version _ architecture)
105 (and (string=? host-name
106 #$(operating-system-host-name os))
107 (string-prefix? #$(package-version
108 (operating-system-kernel os))
109 version)
110 (string-prefix? architecture %host-type)))))
111
112 (test-assert "shell and user commands"
113 ;; Is everything in $PATH?
114 (zero? (marionette-eval '(system "
e3de272a
LC
115. /etc/profile
116set -e -x
117guix --version
118ls --version
119grep --version
120info --version")
4ee96a79
LC
121 marionette)))
122
123 (test-assert "accounts"
124 (let ((users (marionette-eval '(begin
125 (use-modules (ice-9 match))
126 (let loop ((result '()))
127 (match (getpw)
128 (#f (reverse result))
129 (x (loop (cons x result))))))
130 marionette)))
131 (lset= string=?
132 (map passwd:name users)
133 (list
134 #$@(map user-account-name
135 (operating-system-user-accounts os))))))
136
137 (test-assert "shepherd services"
183605c8
LC
138 (let ((services (marionette-eval
139 '(begin
140 (use-modules (gnu services herd))
141
142 (map (compose car live-service-provision)
143 (current-services)))
144 marionette)))
4ee96a79
LC
145 (lset= eq?
146 (pk 'services services)
147 '(root #$@(operating-system-shepherd-service-names os)))))
148
149 (test-equal "login on tty1"
150 "root\n"
151 (begin
152 (marionette-control "sendkey ctrl-alt-f1" marionette)
153 ;; Wait for the 'term-tty1' service to be running (using
154 ;; 'start-service' is the simplest and most reliable way to do
155 ;; that.)
156 (marionette-eval
157 '(begin
158 (use-modules (gnu services herd))
159 (start-service 'term-tty1))
160 marionette)
161
162 ;; Now we can type.
163 (marionette-type "root\n\nid -un > logged-in\n" marionette)
164
165 ;; It can take a while before the shell commands are executed.
4ee96a79 166 (marionette-eval '(use-modules (rnrs io ports)) marionette)
056d0b40
LC
167 (marionette-eval
168 '(let loop ((i 0))
169 (catch 'system-error
170 (lambda ()
171 (call-with-input-file "/root/logged-in"
172 get-string-all))
173 (lambda args
174 (if (and (< i 15) (= ENOENT (system-error-errno args)))
175 (begin
176 (sleep 1)
177 (loop (+ i 1)))
178 (apply throw args)))))
179 marionette)))
4ee96a79 180
caa78166
LC
181 ;; There should be one utmpx entry for the user logged in on tty1.
182 (test-equal "utmpx entry"
183 '(("root" "tty1" #f))
184 (marionette-eval
185 '(begin
186 (use-modules (guix build syscalls)
187 (srfi srfi-1))
188
189 (filter-map (lambda (entry)
190 (and (equal? (login-type USER_PROCESS)
191 (utmpx-login-type entry))
192 (list (utmpx-user entry) (utmpx-line entry)
193 (utmpx-host entry))))
194 (utmpx-entries)))
195 marionette))
196
4ee96a79
LC
197 (test-assert "host name resolution"
198 (match (marionette-eval
199 '(begin
200 ;; Wait for nscd or our requests go through it.
201 (use-modules (gnu services herd))
202 (start-service 'nscd)
203
204 (list (getaddrinfo "localhost")
205 (getaddrinfo #$(operating-system-host-name os))))
206 marionette)
207 ((((? vector?) ..1) ((? vector?) ..1))
208 #t)
209 (x
210 (pk 'failure x #f))))
211
212 (test-equal "host not found"
213 #f
e3de272a 214 (marionette-eval
4ee96a79
LC
215 '(false-if-exception (getaddrinfo "does-not-exist"))
216 marionette))
217
ab3a6450
LC
218 (test-equal "locale"
219 "en_US.utf8"
cc73339b
LC
220 (marionette-eval '(let ((before (setlocale LC_ALL "en_US.utf8")))
221 (setlocale LC_ALL before))
ab3a6450
LC
222 marionette))
223
40d28609
LC
224 (test-assert "/run/current-system is a GC root"
225 (marionette-eval '(begin
226 ;; Make sure the (guix …) modules are found.
227 (eval-when (expand load eval)
228 (set! %load-path
229 (cons
230 (string-append
231 "/run/current-system/profile/share/guile/site/"
232 (effective-version))
233 %load-path))
234 (set! %load-compiled-path
235 (cons
236 (string-append
237 "/run/current-system/profile/share/guile/site/"
238 (effective-version))
239 %load-compiled-path)))
240
241 (use-modules (srfi srfi-34) (guix store))
242
243 (let ((system (readlink "/run/current-system")))
244 (guard (c ((nix-protocol-error? c)
245 (file-exists? system)))
246 (with-store store
247 (delete-paths store (list system))
248 #f))))
249 marionette))
250
334bda9a
LC
251 ;; This symlink is currently unused, but better have it point to the
252 ;; right place. See
253 ;; <https://lists.gnu.org/archive/html/guix-devel/2016-08/msg01641.html>.
254 (test-equal "/var/guix/gcroots/profiles is a valid symlink"
255 "/var/guix/profiles"
256 (marionette-eval '(readlink "/var/guix/gcroots/profiles")
257 marionette))
258
259
4ee96a79
LC
260 (test-assert "screendump"
261 (begin
262 (marionette-control (string-append "screendump " #$output
263 "/tty1.ppm")
264 marionette)
265 (file-exists? "tty1.ppm")))
266
fe933833
LC
267 (test-assert "screen text"
268 (let ((text (marionette-screen-text marionette
269 #:ocrad
270 #$(file-append ocrad
271 "/bin/ocrad"))))
272 ;; Check whether the welcome message and shell prompt are
273 ;; displayed. Note: OCR confuses "y" and "V" for instance, so
274 ;; we cannot reliably match the whole text.
275 (and (string-contains text "This is the GNU")
276 (string-contains text
277 (string-append
278 "root@"
279 #$(operating-system-host-name os))))))
280
4ee96a79
LC
281 (test-end)
282 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
283
284 (gexp->derivation name test))
e3de272a 285
e9f693d0 286(define %test-basic-os
98b65b5f
LC
287 (system-test
288 (name "basic")
289 (description
125af57e 290 "Instrument %SIMPLE-OS, run it in a VM, and run a series of basic
98b65b5f
LC
291functionality tests.")
292 (value
293 (mlet* %store-monad ((os -> (marionette-operating-system
294 %simple-os
295 #:imported-modules '((gnu services herd)
296 (guix combinators))))
297 (run (system-qemu-image/shared-store-script
298 os #:graphic? #f)))
299 ;; XXX: Add call to 'virtualized-operating-system' to get the exact same
300 ;; set of services as the OS produced by
301 ;; 'system-qemu-image/shared-store-script'.
302 (run-basic-test (virtualized-operating-system os '())
303 #~(list #$run))))))
c311089b
LC
304
305\f
306;;;
307;;; Mcron.
308;;;
309
310(define %mcron-os
311 ;; System with an mcron service, with one mcron job for "root" and one mcron
312 ;; job for an unprivileged user (note: #:user is an 'mcron2' thing.)
313 (let ((job1 #~(job next-second-from
314 (lambda ()
315 (call-with-output-file "witness"
316 (lambda (port)
317 (display (list (getuid) (getgid)) port))))))
318 (job2 #~(job next-second-from
319 (lambda ()
320 (call-with-output-file "witness"
321 (lambda (port)
322 (display (list (getuid) (getgid)) port))))
323 #:user "alice"))
324 (job3 #~(job next-second-from ;to test $PATH
325 "touch witness-touch")))
326 (operating-system
327 (inherit %simple-os)
328 (services (cons (mcron-service (list job1 job2 job3))
329 (operating-system-user-services %simple-os))))))
330
331(define (run-mcron-test name)
332 (mlet* %store-monad ((os -> (marionette-operating-system
333 %mcron-os
334 #:imported-modules '((gnu services herd)
335 (guix combinators))))
336 (command (system-qemu-image/shared-store-script
337 os #:graphic? #f)))
338 (define test
4ee96a79
LC
339 (with-imported-modules '((gnu build marionette))
340 #~(begin
341 (use-modules (gnu build marionette)
342 (srfi srfi-64)
343 (ice-9 match))
344
345 (define marionette
346 (make-marionette (list #$command)))
347
348 (define (wait-for-file file)
349 ;; Wait until FILE exists in the guest; 'read' its content and
350 ;; return it.
351 (marionette-eval
352 `(let loop ((i 10))
353 (cond ((file-exists? ,file)
354 (call-with-input-file ,file read))
355 ((> i 0)
356 (sleep 1)
357 (loop (- i 1)))
358 (else
359 (error "file didn't show up" ,file))))
360 marionette))
361
362 (mkdir #$output)
363 (chdir #$output)
364
365 (test-begin "mcron")
366
367 (test-eq "service running"
368 'running!
369 (marionette-eval
370 '(begin
371 (use-modules (gnu services herd))
372 (start-service 'mcron)
373 'running!)
374 marionette))
375
376 ;; Make sure root's mcron job runs, has its cwd set to "/root", and
377 ;; runs with the right UID/GID.
378 (test-equal "root's job"
379 '(0 0)
380 (wait-for-file "/root/witness"))
381
382 ;; Likewise for Alice's job. We cannot know what its GID is since
383 ;; it's chosen by 'groupadd', but it's strictly positive.
384 (test-assert "alice's job"
385 (match (wait-for-file "/home/alice/witness")
386 ((1000 gid)
387 (>= gid 100))))
388
389 ;; Last, the job that uses a command; allows us to test whether
390 ;; $PATH is sane. (Note that 'marionette-eval' stringifies objects
391 ;; that don't have a read syntax, hence the string.)
392 (test-equal "root's job with command"
393 "#<eof>"
394 (wait-for-file "/root/witness-touch"))
395
396 (test-end)
397 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
398
399 (gexp->derivation name test)))
c311089b
LC
400
401(define %test-mcron
402 (system-test
403 (name "mcron")
404 (description "Make sure the mcron service works as advertised.")
405 (value (run-mcron-test name))))
d2fa61bc
LC
406
407\f
408;;;
409;;; Avahi and NSS-mDNS.
410;;;
411
412(define %avahi-os
413 (operating-system
414 (inherit %simple-os)
415 (name-service-switch %mdns-host-lookup-nss)
416 (services (cons* (avahi-service #:debug? #t)
417 (dbus-service)
418 (dhcp-client-service) ;needed for multicast
419
420 ;; Enable heavyweight debugging output.
421 (modify-services (operating-system-user-services
422 %simple-os)
423 (nscd-service-type config
424 => (nscd-configuration
425 (inherit config)
426 (debug-level 3)
427 (log-file "/dev/console")))
428 (syslog-service-type config
429 =>
ec2e2f6c
DC
430 (syslog-configuration
431 (inherit config)
432 (config-file
433 (plain-file
434 "syslog.conf"
435 "*.* /dev/console\n")))))))))
d2fa61bc
LC
436
437(define (run-nss-mdns-test)
438 ;; Test resolution of '.local' names via libc. Start the marionette service
439 ;; *after* nscd. Failing to do that, libc will try to connect to nscd,
440 ;; fail, then never try again (see '__nss_not_use_nscd_hosts' in libc),
441 ;; leading to '.local' resolution failures.
442 (mlet* %store-monad ((os -> (marionette-operating-system
443 %avahi-os
444 #:requirements '(nscd)
445 #:imported-modules '((gnu services herd)
446 (guix combinators))))
447 (run (system-qemu-image/shared-store-script
448 os #:graphic? #f)))
449 (define mdns-host-name
450 (string-append (operating-system-host-name os)
451 ".local"))
452
453 (define test
4ee96a79
LC
454 (with-imported-modules '((gnu build marionette))
455 #~(begin
456 (use-modules (gnu build marionette)
457 (srfi srfi-1)
458 (srfi srfi-64)
459 (ice-9 match))
460
461 (define marionette
462 (make-marionette (list #$run)))
463
464 (mkdir #$output)
465 (chdir #$output)
466
467 (test-begin "avahi")
468
469 (test-assert "wait for services"
470 (marionette-eval
471 '(begin
472 (use-modules (gnu services herd))
473
474 (start-service 'nscd)
475
476 ;; XXX: Work around a race condition in nscd: nscd creates its
477 ;; PID file before it is listening on its socket.
478 (let ((sock (socket PF_UNIX SOCK_STREAM 0)))
479 (let try ()
480 (catch 'system-error
481 (lambda ()
482 (connect sock AF_UNIX "/var/run/nscd/socket")
483 (close-port sock)
484 (format #t "nscd is ready~%"))
485 (lambda args
486 (format #t "waiting for nscd...~%")
487 (usleep 500000)
488 (try)))))
489
490 ;; Wait for the other useful things.
491 (start-service 'avahi-daemon)
492 (start-service 'networking)
493
494 #t)
495 marionette))
496
497 (test-equal "avahi-resolve-host-name"
498 0
499 (marionette-eval
500 '(system*
501 "/run/current-system/profile/bin/avahi-resolve-host-name"
502 "-v" #$mdns-host-name)
503 marionette))
504
505 (test-equal "avahi-browse"
506 0
507 (marionette-eval
508 '(system* "avahi-browse" "-avt")
509 marionette))
510
511 (test-assert "getaddrinfo .local"
512 ;; Wait for the 'avahi-daemon' service and perform a resolution.
513 (match (marionette-eval
514 '(getaddrinfo #$mdns-host-name)
515 marionette)
516 (((? vector? addrinfos) ..1)
517 (pk 'getaddrinfo addrinfos)
518 (and (any (lambda (ai)
519 (= AF_INET (addrinfo:fam ai)))
520 addrinfos)
521 (any (lambda (ai)
522 (= AF_INET6 (addrinfo:fam ai)))
523 addrinfos)))))
524
525 (test-assert "gethostbyname .local"
526 (match (pk 'gethostbyname
527 (marionette-eval '(gethostbyname #$mdns-host-name)
528 marionette))
529 ((? vector? result)
530 (and (string=? (hostent:name result) #$mdns-host-name)
531 (= (hostent:addrtype result) AF_INET)))))
532
533
534 (test-end)
535 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
536
537 (gexp->derivation "nss-mdns" test)))
d2fa61bc
LC
538
539(define %test-nss-mdns
540 (system-test
541 (name "nss-mdns")
542 (description
543 "Test Avahi's multicast-DNS implementation, and in particular, test its
544glibc name service switch (NSS) module.")
545 (value (run-nss-mdns-test))))