1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2016 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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)
25 #:use-module (gnu system vm)
26 #:use-module (gnu services)
27 #:use-module (gnu services mcron)
28 #:use-module (gnu services shepherd)
29 #:use-module (guix gexp)
30 #:use-module (guix store)
31 #:use-module (guix monads)
32 #:use-module (guix packages)
33 #:use-module (srfi srfi-1)
34 #:export (run-basic-test
40 (host-name "komputilo")
41 (timezone "Europe/Berlin")
42 (locale "en_US.UTF-8")
44 (bootloader (grub-configuration (device "/dev/sdX")))
45 (file-systems (cons (file-system
53 (users (cons (user-account
55 (comment "Bob's sister")
57 (supplementary-groups '("wheel" "audio" "video"))
58 (home-directory "/home/alice"))
59 %base-user-accounts))))
62 (define* (run-basic-test os command #:optional (name "basic"))
63 "Return a derivation called NAME that tests basic features of the OS started
64 using COMMAND, a gexp that evaluates to a list of strings. Compare some
65 properties of running system to what's declared in OS, an <operating-system>."
68 (use-modules (gnu build marionette)
75 (make-marionette #$command))
83 (match (marionette-eval '(uname) marionette)
84 (#("Linux" host-name version _ "x86_64")
85 (and (string=? host-name
86 #$(operating-system-host-name os))
87 (string-prefix? #$(package-version
88 (operating-system-kernel os))
91 (test-assert "shell and user commands"
92 ;; Is everything in $PATH?
93 (zero? (marionette-eval '(system "
102 (test-assert "accounts"
103 (let ((users (marionette-eval '(begin
104 (use-modules (ice-9 match))
105 (let loop ((result '()))
107 (#f (reverse result))
108 (x (loop (cons x result))))))
111 (map passwd:name users)
113 #$@(map user-account-name
114 (operating-system-user-accounts os))))))
116 (test-assert "shepherd services"
117 (let ((services (marionette-eval '(begin
118 (use-modules (gnu services herd))
119 (call-with-values current-services
123 (pk 'services services)
124 '(root #$@(operating-system-shepherd-service-names os)))))
126 (test-equal "login on tty1"
129 (marionette-control "sendkey ctrl-alt-f1" marionette)
130 ;; Wait for the 'term-tty1' service to be running (using
131 ;; 'start-service' is the simplest and most reliable way to do
135 (use-modules (gnu services herd))
136 (start-service 'term-tty1))
140 (marionette-type "root\n\nid -un > logged-in\n" marionette)
142 ;; It can take a while before the shell commands are executed.
144 (unless (or (file-exists? "/root/logged-in") (> i 15))
147 (marionette-eval '(use-modules (rnrs io ports)) marionette)
148 (marionette-eval '(call-with-input-file "/root/logged-in"
152 (test-assert "screendump"
154 (marionette-control (string-append "screendump " #$output
157 (file-exists? "tty1.ppm")))
160 (exit (= (test-runner-fail-count (test-runner-current)) 0))))
162 (gexp->derivation name test
163 #:modules '((gnu build marionette))))
165 (define %test-basic-os
169 "Instrument %SIMPLE-OS, run it in a VM, and runs a series of basic
170 functionality tests.")
172 (mlet* %store-monad ((os -> (marionette-operating-system
174 #:imported-modules '((gnu services herd)
175 (guix combinators))))
176 (run (system-qemu-image/shared-store-script
178 ;; XXX: Add call to 'virtualized-operating-system' to get the exact same
179 ;; set of services as the OS produced by
180 ;; 'system-qemu-image/shared-store-script'.
181 (run-basic-test (virtualized-operating-system os '())
190 ;; System with an mcron service, with one mcron job for "root" and one mcron
191 ;; job for an unprivileged user (note: #:user is an 'mcron2' thing.)
192 (let ((job1 #~(job next-second-from
194 (call-with-output-file "witness"
196 (display (list (getuid) (getgid)) port))))))
197 (job2 #~(job next-second-from
199 (call-with-output-file "witness"
201 (display (list (getuid) (getgid)) port))))
203 (job3 #~(job next-second-from ;to test $PATH
204 "touch witness-touch")))
207 (services (cons (mcron-service (list job1 job2 job3))
208 (operating-system-user-services %simple-os))))))
210 (define (run-mcron-test name)
211 (mlet* %store-monad ((os -> (marionette-operating-system
213 #:imported-modules '((gnu services herd)
214 (guix combinators))))
215 (command (system-qemu-image/shared-store-script
219 (use-modules (gnu build marionette)
224 (make-marionette (list #$command)))
226 (define (wait-for-file file)
227 ;; Wait until FILE exists in the guest; 'read' its content and
231 (cond ((file-exists? ,file)
232 (call-with-input-file ,file read))
237 (error "file didn't show up" ,file))))
245 (test-eq "service running"
249 (use-modules (gnu services herd))
250 (start-service 'mcron)
254 ;; Make sure root's mcron job runs, has its cwd set to "/root", and
255 ;; runs with the right UID/GID.
256 (test-equal "root's job"
258 (wait-for-file "/root/witness"))
260 ;; Likewise for Alice's job. We cannot know what its GID is since
261 ;; it's chosen by 'groupadd', but it's strictly positive.
262 (test-assert "alice's job"
263 (match (wait-for-file "/home/alice/witness")
267 ;; Last, the job that uses a command; allows us to test whether
268 ;; $PATH is sane. (Note that 'marionette-eval' stringifies objects
269 ;; that don't have a read syntax, hence the string.)
270 (test-equal "root's job with command"
272 (wait-for-file "/root/witness-touch"))
275 (exit (= (test-runner-fail-count (test-runner-current)) 0))))
277 (gexp->derivation name test
278 #:modules '((gnu build marionette)))))
283 (description "Make sure the mcron service works as advertised.")
284 (value (run-mcron-test name))))