1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2019 Jakob L. Kreuze <zerodaysfordays@sdf.lonestar.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 reconfigure)
20 #:use-module (gnu bootloader)
21 #:use-module (gnu services shepherd)
22 #:use-module (gnu system)
23 #:use-module (gnu system accounts)
24 #:use-module (gnu system shadow)
25 #:use-module (gnu system vm)
26 #:use-module (gnu tests)
27 #:use-module (guix derivations)
28 #:use-module (guix gexp)
29 #:use-module (guix monads)
30 #:use-module (guix scripts system reconfigure)
31 #:use-module (guix store)
32 #:export (%test-switch-to-system
33 %test-upgrade-services
34 %test-install-bootloader))
38 ;;; Test in-place system reconfiguration: advancing the system generation on a
39 ;;; running instance of the Guix System.
43 (define* (run-switch-to-system-test)
44 "Run a test of an OS running SWITCH-SYSTEM-PROGRAM, which creates a new
45 generation of the system profile."
47 (marionette-operating-system
49 (inherit (simple-operating-system))
50 (users (cons (user-account
53 (home-directory "/home/jakob"))
54 %base-user-accounts)))
55 #:imported-modules '((gnu services herd)
58 (define vm (virtual-machine os))
61 (with-imported-modules '((gnu build marionette))
63 (use-modules (gnu build marionette)
67 (make-marionette (list #$vm)))
69 ;; Return the names of the generation symlinks on MARIONETTE.
70 (define (system-generations marionette)
73 (use-modules (ice-9 ftw)
75 (let* ((profile-dir "/var/guix/profiles/")
76 (entries (map first (cddr (file-system-tree profile-dir)))))
77 (remove (lambda (entry)
78 (member entry '("per-user" "system")))
85 (test-begin "switch-to-system")
87 (let ((generations-prior (system-generations marionette)))
88 (test-assert "script successfully evaluated"
90 '(primitive-load #$script)
93 (test-equal "script created new generation"
94 (length (system-generations marionette))
95 (1+ (length generations-prior)))
97 (test-assert "script activated the new generation"
100 '(stat:type (lstat "/run/current-system"))
104 '(readlink "/run/current-system")
107 (test-assert "script activated user accounts"
109 '(string-contains (call-with-input-file "/etc/passwd"
111 (get-string-all port)))
116 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
118 (gexp->derivation "switch-to-system" (test (switch-system-program os))))
120 (define* (run-upgrade-services-test)
121 "Run a test of an OS running UPGRADE-SERVICES-PROGRAM, which upgrades the
122 Shepherd (PID 1) by unloading obsolete services and loading new services."
124 (marionette-operating-system
125 (simple-operating-system)
126 #:imported-modules '((gnu services herd)
127 (guix combinators))))
129 (define vm (virtual-machine os))
131 (define dummy-service
132 ;; Shepherd service that does nothing, for the sole purpose of ensuring
133 ;; that it is properly installed and started by the script.
134 (shepherd-service (provision '(dummy))
139 ;; Return the Shepherd service file for SERVICE, after ensuring that it
140 ;; exists in the store.
141 (define (ensure-service-file service)
142 (let ((file (shepherd-service-file service)))
143 (mlet* %store-monad ((store-object (lower-object file))
144 (_ (built-derivations (list store-object))))
147 (define (test enable-dummy disable-dummy)
148 (with-imported-modules '((gnu build marionette))
150 (use-modules (gnu build marionette)
154 (make-marionette (list #$vm)))
156 ;; Return the names of the running services on MARIONETTE.
157 (define (running-services marionette)
160 (use-modules (gnu services herd))
161 (map live-service-canonical-name (current-services)))
167 (test-begin "upgrade-services")
169 (let ((services-prior (running-services marionette)))
170 (test-assert "script successfully evaluated"
172 '(primitive-load #$enable-dummy)
175 (test-assert "script started new service"
176 (and (not (memq 'dummy services-prior))
177 (memq 'dummy (running-services marionette))))
179 (test-assert "script successfully evaluated"
181 '(primitive-load #$disable-dummy)
184 (test-assert "script stopped obsolete service"
185 (not (memq 'dummy (running-services marionette)))))
188 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
190 (mlet* %store-monad ((file (ensure-service-file dummy-service)))
191 (let ((enable (upgrade-services-program (list file) '(dummy) '() '()))
192 (disable (upgrade-services-program '() '() '(dummy) '())))
193 (gexp->derivation "upgrade-services" (test enable disable)))))
195 (define* (run-install-bootloader-test)
196 "Run a test of an OS running INSTALL-BOOTLOADER-PROGRAM, which installs a
197 bootloader's configuration file."
199 (marionette-operating-system
200 (simple-operating-system)
201 #:imported-modules '((gnu services herd)
202 (guix combinators))))
204 (define vm (virtual-machine os))
206 (define (test script)
207 (with-imported-modules '((gnu build marionette))
209 (use-modules (gnu build marionette)
215 (make-marionette (list #$vm)))
217 ;; Return the system generation paths that have GRUB menu entries.
218 (define (generations-in-grub-cfg marionette)
219 (let ((grub-cfg (marionette-eval
221 (call-with-input-file "/boot/grub/grub.cfg"
223 (get-string-all port))))
225 (map (lambda (parameter)
226 (second (string-split (match:substring parameter) #\=)))
227 (list-matches "system=[^ ]*" grub-cfg))))
232 (test-begin "install-bootloader")
234 (test-assert "no prior menu entry for system generation"
235 (not (member #$os (generations-in-grub-cfg marionette))))
237 (test-assert "script successfully evaluated"
239 '(primitive-load #$script)
242 (test-assert "menu entry created for system generation"
243 (member #$os (generations-in-grub-cfg marionette)))
246 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
248 (let* ((bootloader ((compose bootloader-configuration-bootloader
249 operating-system-bootloader)
251 ;; The typical use-case for 'install-bootloader-program' is to read
252 ;; the boot parameters for the existing menu entries on the system,
253 ;; parse them with 'boot-parameters->menu-entry', and pass the
254 ;; results to 'operating-system-bootcfg'. However, to obtain boot
255 ;; parameters, we would need to start the marionette, which we should
256 ;; ideally avoid doing outside of the 'test' G-Expression. Thus, we
257 ;; generate a bootloader configuration for the script as if there
258 ;; were no existing menu entries. In the grand scheme of things, this
259 ;; matters little -- these tests should not make assertions about the
260 ;; behavior of 'operating-system-bootcfg'.
261 (bootcfg (operating-system-bootcfg os '()))
262 (bootcfg-file (bootloader-configuration-file bootloader)))
265 ;; Due to the read-only nature of the virtual machines used in the system
266 ;; test suite, the bootloader installer script is omitted. 'grub-install'
267 ;; would attempt to write directly to the virtual disk if the
268 ;; installation script were run.
269 (test (install-bootloader-program #f #f bootcfg bootcfg-file #f "/")))))
271 (define %test-switch-to-system
273 (name "switch-to-system")
274 (description "Create a new generation of the system profile.")
275 (value (run-switch-to-system-test))))
277 (define %test-upgrade-services
279 (name "upgrade-services")
280 (description "Upgrade the Shepherd by unloading obsolete services and
281 loading new services.")
282 (value (run-upgrade-services-test))))
284 (define %test-install-bootloader
286 (name "install-bootloader")
287 (description "Install a bootloader and its configuration file.")
288 (value (run-install-bootloader-test))))