services: Add 'mcron-service'.
[jackhill/guix/guix.git] / gnu / tests / base.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2016 Ludovic Courtès <ludo@gnu.org>
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)
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
35 %test-basic-os
36 %test-mcron))
37
38 (define %simple-os
39 (operating-system
40 (host-name "komputilo")
41 (timezone "Europe/Berlin")
42 (locale "en_US.UTF-8")
43
44 (bootloader (grub-configuration (device "/dev/sdX")))
45 (file-systems (cons (file-system
46 (device "my-root")
47 (title 'label)
48 (mount-point "/")
49 (type "ext4"))
50 %base-file-systems))
51 (firmware '())
52
53 (users (cons (user-account
54 (name "alice")
55 (comment "Bob's sister")
56 (group "users")
57 (supplementary-groups '("wheel" "audio" "video"))
58 (home-directory "/home/alice"))
59 %base-user-accounts))))
60
61 \f
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>."
66 (define test
67 #~(begin
68 (use-modules (gnu build marionette)
69 (srfi srfi-1)
70 (srfi srfi-26)
71 (srfi srfi-64)
72 (ice-9 match))
73
74 (define marionette
75 (make-marionette #$command))
76
77 (mkdir #$output)
78 (chdir #$output)
79
80 (test-begin "basic")
81
82 (test-assert "uname"
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))
89 version)))))
90
91 (test-assert "shell and user commands"
92 ;; Is everything in $PATH?
93 (zero? (marionette-eval '(system "
94 . /etc/profile
95 set -e -x
96 guix --version
97 ls --version
98 grep --version
99 info --version")
100 marionette)))
101
102 (test-assert "accounts"
103 (let ((users (marionette-eval '(begin
104 (use-modules (ice-9 match))
105 (let loop ((result '()))
106 (match (getpw)
107 (#f (reverse result))
108 (x (loop (cons x result))))))
109 marionette)))
110 (lset= string=?
111 (map passwd:name users)
112 (list
113 #$@(map user-account-name
114 (operating-system-user-accounts os))))))
115
116 (test-assert "shepherd services"
117 (let ((services (marionette-eval '(begin
118 (use-modules (gnu services herd))
119 (call-with-values current-services
120 append))
121 marionette)))
122 (lset= eq?
123 (pk 'services services)
124 '(root #$@(operating-system-shepherd-service-names os)))))
125
126 (test-equal "login on tty1"
127 "root\n"
128 (begin
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
132 ;; that.)
133 (marionette-eval
134 '(begin
135 (use-modules (gnu services herd))
136 (start-service 'term-tty1))
137 marionette)
138
139 ;; Now we can type.
140 (marionette-type "root\n\nid -un > logged-in\n" marionette)
141
142 ;; It can take a while before the shell commands are executed.
143 (let loop ((i 0))
144 (unless (or (file-exists? "/root/logged-in") (> i 15))
145 (sleep 1)
146 (loop (+ i 1))))
147 (marionette-eval '(use-modules (rnrs io ports)) marionette)
148 (marionette-eval '(call-with-input-file "/root/logged-in"
149 get-string-all)
150 marionette)))
151
152 (test-assert "screendump"
153 (begin
154 (marionette-control (string-append "screendump " #$output
155 "/tty1.ppm")
156 marionette)
157 (file-exists? "tty1.ppm")))
158
159 (test-end)
160 (exit (= (test-runner-fail-count (test-runner-current)) 0))))
161
162 (gexp->derivation name test
163 #:modules '((gnu build marionette))))
164
165 (define %test-basic-os
166 (system-test
167 (name "basic")
168 (description
169 "Instrument %SIMPLE-OS, run it in a VM, and runs a series of basic
170 functionality tests.")
171 (value
172 (mlet* %store-monad ((os -> (marionette-operating-system
173 %simple-os
174 #:imported-modules '((gnu services herd)
175 (guix combinators))))
176 (run (system-qemu-image/shared-store-script
177 os #:graphic? #f)))
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 '())
182 #~(list #$run))))))
183
184 \f
185 ;;;
186 ;;; Mcron.
187 ;;;
188
189 (define %mcron-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
193 (lambda ()
194 (call-with-output-file "witness"
195 (lambda (port)
196 (display (list (getuid) (getgid)) port))))))
197 (job2 #~(job next-second-from
198 (lambda ()
199 (call-with-output-file "witness"
200 (lambda (port)
201 (display (list (getuid) (getgid)) port))))
202 #:user "alice"))
203 (job3 #~(job next-second-from ;to test $PATH
204 "touch witness-touch")))
205 (operating-system
206 (inherit %simple-os)
207 (services (cons (mcron-service (list job1 job2 job3))
208 (operating-system-user-services %simple-os))))))
209
210 (define (run-mcron-test name)
211 (mlet* %store-monad ((os -> (marionette-operating-system
212 %mcron-os
213 #:imported-modules '((gnu services herd)
214 (guix combinators))))
215 (command (system-qemu-image/shared-store-script
216 os #:graphic? #f)))
217 (define test
218 #~(begin
219 (use-modules (gnu build marionette)
220 (srfi srfi-64)
221 (ice-9 match))
222
223 (define marionette
224 (make-marionette (list #$command)))
225
226 (define (wait-for-file file)
227 ;; Wait until FILE exists in the guest; 'read' its content and
228 ;; return it.
229 (marionette-eval
230 `(let loop ((i 10))
231 (cond ((file-exists? ,file)
232 (call-with-input-file ,file read))
233 ((> i 0)
234 (sleep 1)
235 (loop (- i 1)))
236 (else
237 (error "file didn't show up" ,file))))
238 marionette))
239
240 (mkdir #$output)
241 (chdir #$output)
242
243 (test-begin "mcron")
244
245 (test-eq "service running"
246 'running!
247 (marionette-eval
248 '(begin
249 (use-modules (gnu services herd))
250 (start-service 'mcron)
251 'running!)
252 marionette))
253
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"
257 '(0 0)
258 (wait-for-file "/root/witness"))
259
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")
264 ((1000 gid)
265 (>= gid 100))))
266
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"
271 "#<eof>"
272 (wait-for-file "/root/witness-touch"))
273
274 (test-end)
275 (exit (= (test-runner-fail-count (test-runner-current)) 0))))
276
277 (gexp->derivation name test
278 #:modules '((gnu build marionette)))))
279
280 (define %test-mcron
281 (system-test
282 (name "mcron")
283 (description "Make sure the mcron service works as advertised.")
284 (value (run-mcron-test name))))