lint: Check for trailing whitespace in synopsis.
[jackhill/guix/guix.git] / gnu / installer / tests.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2020 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2020 Mathieu Othacehe <m.othacehe@gmail.com>
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 installer tests)
21 #:use-module (srfi srfi-1)
22 #:use-module (srfi srfi-34)
23 #:use-module (srfi srfi-35)
24 #:use-module (ice-9 match)
25 #:use-module (ice-9 regex)
26 #:use-module (ice-9 pretty-print)
27 #:export (&pattern-not-matched
28 pattern-not-matched?
29
30 %installer-socket-file
31 open-installer-socket
32
33 converse
34 conversation-log-port
35
36 choose-locale+keyboard
37 enter-host-name+passwords
38 choose-services
39 choose-partitioning
40 start-installation
41 complete-installation
42
43 edit-configuration-file))
44
45 ;;; Commentary:
46 ;;;
47 ;;; This module provides tools to test the guided "graphical" installer in a
48 ;;; non-interactive fashion. The core of it is 'converse': it allows you to
49 ;;; state Expect-style dialogues, which happen over the Unix-domain socket the
50 ;;; installer listens to. Higher-level procedures such as
51 ;;; 'choose-locale+keyboard' are provided to perform specific parts of the
52 ;;; dialogue.
53 ;;;
54 ;;; Code:
55
56 (define %installer-socket-file
57 ;; Socket the installer listens to.
58 "/var/guix/installer-socket")
59
60 (define* (open-installer-socket #:optional (file %installer-socket-file))
61 "Return a socket connected to the installer."
62 (let ((sock (socket AF_UNIX SOCK_STREAM 0)))
63 (connect sock AF_UNIX file)
64 sock))
65
66 (define-condition-type &pattern-not-matched &error
67 pattern-not-matched?
68 (pattern pattern-not-matched-pattern)
69 (sexp pattern-not-matched-sexp))
70
71 (define (pattern-error pattern sexp)
72 (raise (condition
73 (&pattern-not-matched
74 (pattern pattern) (sexp sexp)))))
75
76 (define conversation-log-port
77 ;; Port where debugging info is logged
78 (make-parameter (current-error-port)))
79
80 (define (converse-debug pattern)
81 (format (conversation-log-port)
82 "conversation expecting pattern ~s~%"
83 pattern))
84
85 (define-syntax converse
86 (lambda (s)
87 "Convert over PORT: read sexps from there, match them against each
88 PATTERN, and send the corresponding REPLY. Raise to '&pattern-not-matched'
89 when one of the PATTERNs is not matched."
90
91 ;; XXX: Strings that appear in PATTERNs must be in the language the
92 ;; installer is running in. In the future, we should add support to allow
93 ;; writing English strings in PATTERNs and have the pattern matcher
94 ;; automatically translate them.
95
96 ;; Here we emulate 'pmatch' syntax on top of 'match'. This is ridiculous
97 ;; but that's because 'pmatch' compares objects with 'eq?', making it
98 ;; pretty useless, and it doesn't support ellipses and such.
99
100 (define (quote-pattern s)
101 ;; Rewrite the pattern S from pmatch style (a ,b) to match style like
102 ;; ('a b).
103 (with-ellipsis :::
104 (syntax-case s (unquote _ ...)
105 ((unquote id) #'id)
106 (_ #'_)
107 (... #'...)
108 (id
109 (identifier? #'id)
110 #''id)
111 ((lst :::) (map quote-pattern #'(lst :::)))
112 (pattern #'pattern))))
113
114 (define (match-pattern s)
115 ;; Match one pattern without a guard.
116 (syntax-case s ()
117 ((port (pattern reply) continuation)
118 (with-syntax ((pattern (quote-pattern #'pattern)))
119 #'(let ((pat 'pattern))
120 (converse-debug pat)
121 (match (read port)
122 (pattern
123 (let ((data (call-with-values (lambda () reply)
124 list)))
125 (for-each (lambda (obj)
126 (write obj port)
127 (newline port))
128 data)
129 (force-output port)
130 (continuation port)))
131 (sexp
132 (pattern-error pat sexp))))))))
133
134 (syntax-case s ()
135 ((_ port (pattern reply) rest ...)
136 (match-pattern #'(port (pattern reply)
137 (lambda (port)
138 (converse port rest ...)))))
139 ((_ port (pattern guard reply) rest ...)
140 #`(let ((skip? (not guard))
141 (next (lambda (p)
142 (converse p rest ...))))
143 (if skip?
144 (next port)
145 #,(match-pattern #'(port (pattern reply) next)))))
146 ((_ port)
147 #t))))
148
149 (define* (choose-locale+keyboard port
150 #:key
151 (language "English")
152 (location "Hong Kong")
153 (timezone '("Europe" "Zagreb"))
154 (keyboard
155 '("English (US)"
156 "English (intl., with AltGr dead keys)")))
157 "Converse over PORT with the guided installer to choose the specified
158 LANGUAGE, LOCATION, TIMEZONE, and KEYBOARD."
159 (converse port
160 ((list-selection (title "Locale language")
161 (multiple-choices? #f)
162 (items _))
163 language)
164 ((list-selection (title "Locale location")
165 (multiple-choices? #f)
166 (items _))
167 location)
168 ((menu (title "GNU Guix install")
169 (text _)
170 (items (,guided _ ...))) ;"Guided graphical installation"
171 guided)
172 ((list-selection (title "Timezone")
173 (multiple-choices? #f)
174 (items _))
175 (first timezone))
176 ((list-selection (title "Timezone")
177 (multiple-choices? #f)
178 (items _))
179 (second timezone))
180 ((list-selection (title "Layout")
181 (multiple-choices? #f)
182 (items _))
183 (first keyboard))
184 ((list-selection (title "Variant")
185 (multiple-choices? #f)
186 (items _))
187 (second keyboard))))
188
189 (define* (enter-host-name+passwords port
190 #:key
191 (host-name "guix")
192 (root-password "foo")
193 (users '(("alice" "pass1")
194 ("bob" "pass2")
195 ("charlie" "pass3"))))
196 "Converse over PORT with the guided installer to choose HOST-NAME,
197 ROOT-PASSWORD, and USERS."
198 (converse port
199 ((input (title "Hostname") (text _) (default _))
200 host-name)
201 ((input (title "System administrator password") (text _) (default _))
202 root-password)
203 ((input (title "Password confirmation required") (text _) (default _))
204 root-password)
205 ((add-users)
206 (match users
207 (((names passwords) ...)
208 (map (lambda (name password)
209 `(user (name ,name) (real-name ,(string-titlecase name))
210 (home-directory ,(string-append "/home/" name))
211 (password ,password)))
212 names passwords))))))
213
214 (define* (choose-services port
215 #:key
216 (choose-desktop-environment? (const #f))
217 (choose-network-service?
218 (lambda (service)
219 (or (string-contains service "SSH")
220 (string-contains service "NSS"))))
221 (choose-network-management-tool?
222 (lambda (service)
223 (string-contains service "DHCP"))))
224 "Converse over PORT to choose networking services."
225 (define desktop-environments '())
226
227 (converse port
228 ((checkbox-list (title "Desktop environment") (text _)
229 (items ,services))
230 (let ((desktops (filter choose-desktop-environment? services)))
231 (set! desktop-environments desktops)
232 desktops))
233 ((checkbox-list (title "Network service") (text _)
234 (items ,services))
235 (filter choose-network-service? services))
236
237 ;; The "Network management" dialog shows up only when no desktop
238 ;; environments have been selected, hence the guard.
239 ((list-selection (title "Network management")
240 (multiple-choices? #f)
241 (items ,services))
242 (null? desktop-environments)
243 (find choose-network-management-tool? services))))
244
245 (define (edit-configuration-file file)
246 "Edit FILE, an operating system configuration file generated by the
247 installer, by adding a marionette service such that the installed OS is
248 instrumented for further testing."
249 (define (read-expressions port)
250 (let loop ((result '()))
251 (match (read port)
252 ((? eof-object?)
253 (reverse result))
254 (exp
255 (loop (cons exp result))))))
256
257 (define (edit exp)
258 (match exp
259 (('operating-system _ ...)
260 `(marionette-operating-system ,exp
261 #:imported-modules
262 '((gnu services herd)
263 (guix build utils)
264 (guix combinators))))
265 (_
266 exp)))
267
268 (let ((content (call-with-input-file file read-expressions)))
269 (call-with-output-file file
270 (lambda (port)
271 (format port "\
272 ;; Operating system configuration edited for automated testing.~%~%")
273
274 (pretty-print '(use-modules (gnu tests)) port)
275 (for-each (lambda (exp)
276 (pretty-print (edit exp) port)
277 (newline port))
278 content)))
279
280 #t))
281
282 (define* (choose-partitioning port
283 #:key
284 (encrypted? #t)
285 (uefi-support? #f)
286 (passphrase "thepassphrase")
287 (edit-configuration-file
288 edit-configuration-file))
289 "Converse over PORT to choose the partitioning method. When ENCRYPTED? is
290 true, choose full-disk encryption with PASSPHRASE as the LUKS passphrase.
291
292 When UEFI-SUPPORT? is true, assume that we are running the installation tests
293 on an UEFI capable machine.
294
295 This conversation stops when the user partitions have been formatted, right
296 before the installer generates the configuration file and shows it in a dialog
297 box. "
298 (converse port
299 ((list-selection (title "Partitioning method")
300 (multiple-choices? #f)
301 (items (,not-encrypted ,encrypted _ ...)))
302 (if encrypted?
303 encrypted
304 not-encrypted))
305 ((list-selection (title "Disk") (multiple-choices? #f)
306 (items (,disks ...)))
307 ;; When running the installation from an ISO image, the CD/DVD drive
308 ;; shows up in the list. Avoid it.
309 (find (lambda (disk)
310 (not (or (string-contains disk "DVD")
311 (string-contains disk "CD-ROM"))))
312 disks))
313
314 ;; The "Partition table" dialog pops up only if there's not already a
315 ;; partition table and if the system does not support UEFI.
316 ((list-selection (title "Partition table")
317 (multiple-choices? #f)
318 (items _))
319 ;; When UEFI is supported, the partition is forced to GPT by the
320 ;; installer.
321 (not uefi-support?)
322 "gpt")
323
324 ((list-selection (title "Partition scheme")
325 (multiple-choices? #f)
326 (items (,one-partition _ ...)))
327 one-partition)
328 ((list-selection (title "Guided partitioning")
329 (multiple-choices? #f)
330 (items (,disk _ ...)))
331 disk)
332 ((input (title "Password required")
333 (text _) (default #f))
334 encrypted? ;only when ENCRYPTED?
335 passphrase)
336 ((input (title "Password confirmation required")
337 (text _) (default #f))
338 encrypted?
339 passphrase)
340 ((confirmation (title "Format disk?") (text _))
341 #t)
342 ((info (title "Preparing partitions") _ ...)
343 (values)) ;nothing to return
344 ((starting-final-step)
345 ;; Do not return anything. The reply will be sent by
346 ;; 'conclude-installation' and in the meantime the installer just waits
347 ;; for us, giving us a chance to do things such as changing partition
348 ;; UUIDs before it generates the configuration file.
349 (values))))
350
351 (define (start-installation port)
352 "Start the installation by checking over PORT that we get the generated
353 configuration file, accepting it and starting the installation, and then
354 receiving the pause message once the 'guix system init' process has
355 completed."
356 ;; Assume the previous message received was 'starting-final-step'; here we
357 ;; send the reply to that message, which lets the installer continue.
358 (write #t port)
359 (newline port)
360 (force-output port)
361
362 (converse port
363 ((file-dialog (title "Configuration file")
364 (text _)
365 (file ,configuration-file))
366 (edit-configuration-file configuration-file))
367 ((pause) ;"Press Enter to continue."
368 (values))))
369
370 (define (complete-installation port)
371 "Complete the installation by replying to the installer pause message and
372 waiting for the installation-complete message."
373 ;; Assume the previous message received was 'pause'; here we send the reply
374 ;; to that message, which lets the installer continue.
375 (write #t port)
376 (newline port)
377 (force-output port)
378
379 (converse port
380 ((installation-complete)
381 (values))))
382
383 ;;; Local Variables:
384 ;;; eval: (put 'converse 'scheme-indent-function 1)
385 ;;; eval: (put 'with-ellipsis 'scheme-indent-function 1)
386 ;;; End: