path: root/gnu/tests/reconfigure.scm
blob: ec845fe4b064af849b66a6622b0a1bf46f9b71f3 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2019 Jakob L. Kreuze <zerodaysfordays@sdf.org>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (gnu tests reconfigure)
  #:use-module (gnu bootloader)
  #:use-module (gnu services shepherd)
  #:use-module (gnu system)
  #:use-module (gnu system accounts)
  #:use-module (gnu system shadow)
  #:use-module (gnu system vm)
  #:use-module (gnu tests)
  #:use-module (guix derivations)
  #:use-module (guix gexp)
  #:use-module (guix monads)
  #:use-module (guix scripts system reconfigure)
  #:use-module (guix store)
  #:export (%test-switch-to-system
            %test-upgrade-services
            %test-install-bootloader))

;;; Commentary:
;;;
;;; Test in-place system reconfiguration: advancing the system generation on a
;;; running instance of the Guix System.
;;;
;;; Code:

(define* (run-switch-to-system-test)
  "Run a test of an OS running SWITCH-SYSTEM-PROGRAM, which creates a new
generation of the system profile."
  (define os
    (marionette-operating-system
     (operating-system
       (inherit (simple-operating-system))
       (users (cons (user-account
                     (name "jakob")
                     (group "users")
                     (home-directory "/home/jakob"))
                    %base-user-accounts)))
     #:imported-modules '((gnu services herd)
                          (guix combinators))))

  (define vm (virtual-machine os))

  (define (test script)
    (with-imported-modules '((gnu build marionette))
      #~(begin
          (use-modules (gnu build marionette)
                       (srfi srfi-64))

          (define marionette
            (make-marionette (list #$vm)))

          ;; Return the names of the generation symlinks on MARIONETTE.
          (define (system-generations marionette)
            (marionette-eval
             '(begin
                (use-modules (ice-9 ftw)
                             (srfi srfi-1))
                (let* ((profile-dir "/var/guix/profiles/")
                       (entries (map first (cddr (file-system-tree profile-dir)))))
                  (remove (lambda (entry)
                            (member entry '("per-user" "system")))
                          entries)))
             marionette))

          (test-runner-current (system-test-runner #$output))
          (test-begin "switch-to-system")

          (let ((generations-prior (system-generations marionette)))
            (test-assert "script successfully evaluated"
              (marionette-eval
               '(primitive-load #$script)
               marionette))

            (test-equal "script created new generation"
              (length (system-generations marionette))
              (1+ (length generations-prior)))

            (test-assert "script activated the new generation"
              (and (eqv? 'symlink
                         (marionette-eval
                          '(stat:type (lstat "/run/current-system"))
                          marionette))
                   (string= #$os
                            (marionette-eval
                             '(readlink "/run/current-system")
                             marionette))))

            (test-assert "script activated user accounts"
              (marionette-eval
               '(string-contains (call-with-input-file "/etc/passwd"
                                   (lambda (port)
                                     (get-string-all port)))
                                 "jakob")
               marionette)))

          (test-end))))

  (gexp->derivation "switch-to-system" (test (switch-system-program os))))

(define* (run-upgrade-services-test)
  "Run a test of an OS running UPGRADE-SERVICES-PROGRAM, which upgrades the
Shepherd (PID 1) by unloading obsolete services and loading new services."
  (define os
    (marionette-operating-system
     (simple-operating-system)
     #:imported-modules '((gnu services herd)
                          (guix combinators))))

  (define vm (virtual-machine os))

  (define dummy-service
    ;; Shepherd service that does nothing, for the sole purpose of ensuring
    ;; that it is properly installed and started by the script.
    (shepherd-service (provision '(dummy))
                      (start #~(const #t))
                      (stop #~(const #t))
                      (respawn? #f)))

  (define (test enable-dummy disable-dummy)
    (with-imported-modules '((gnu build marionette))
      #~(begin
          (use-modules (gnu build marionette)
                       (srfi srfi-64))

          (define marionette
            (make-marionette (list #$vm)))

          ;; Return the names of the running services on MARIONETTE.
          (define (running-services marionette)
            (marionette-eval
             '(begin
                (use-modules (gnu services herd))
                (map live-service-canonical-name (current-services)))
             marionette))

          (test-runner-current (system-test-runner #$output))
          (test-begin "upgrade-services")

          (let ((services-prior (running-services marionette)))
            (test-assert "script successfully evaluated"
              (marionette-eval
               '(primitive-load #$enable-dummy)
               marionette))

            (test-assert "script started new service"
              (and (not (memq 'dummy services-prior))
                   (memq 'dummy (running-services marionette))))

            (test-assert "script successfully evaluated"
              (marionette-eval
               '(primitive-load #$disable-dummy)
               marionette))

            (test-assert "script stopped obsolete service"
              (not (memq 'dummy (running-services marionette)))))

          (test-end))))

  (gexp->derivation
   "upgrade-services"
   (let* ((file (shepherd-service-file dummy-service))
          (enable (upgrade-services-program (list file) '(dummy) '() '()))
          (disable (upgrade-services-program '() '() '(dummy) '())))
     (test enable disable))))

(define* (run-install-bootloader-test)
  "Run a test of an OS running INSTALL-BOOTLOADER-PROGRAM, which installs a
bootloader's configuration file."
  (define os
    (marionette-operating-system
     (simple-operating-system)
     #:imported-modules '((gnu services herd)
                          (guix combinators))))

  (define vm (virtual-machine
              (operating-system os)
              (volatile? #f)))

  (define (test script)
    (with-imported-modules '((gnu build marionette))
      #~(begin
          (use-modules (gnu build marionette)
                       (ice-9 regex)
                       (srfi srfi-1)
                       (srfi srfi-64))

          (define marionette
            (make-marionette (list #$vm)))

          ;; Return the system generation paths that have GRUB menu entries.
          (define (generations-in-grub-cfg marionette)
            (let ((grub-cfg (marionette-eval
                             '(begin
                                (call-with-input-file "/boot/grub/grub.cfg"
                                  (lambda (port)
                                    (get-string-all port))))
                             marionette)))
              (map (lambda (parameter)
                     (second (string-split (match:substring parameter) #\=)))
                   (list-matches "system=[^ ]*" grub-cfg))))

          (test-runner-current (system-test-runner #$output))
          (test-begin "install-bootloader")

          (test-assert "no prior menu entry for system generation"
            (not (member #$os (generations-in-grub-cfg marionette))))

          (test-assert "script successfully evaluated"
            (marionette-eval
             '(primitive-load #$script)
             marionette))

          (test-assert "menu entry created for system generation"
            (member #$os (generations-in-grub-cfg marionette)))

          (test-end))))

  (let* ((bootloader ((compose bootloader-configuration-bootloader
                               operating-system-bootloader)
                      os))
         ;; The typical use-case for 'install-bootloader-program' is to read
         ;; the boot parameters for the existing menu entries on the system,
         ;; parse them with 'boot-parameters->menu-entry', and pass the
         ;; results to 'operating-system-bootcfg'. However, to obtain boot
         ;; parameters, we would need to start the marionette, which we should
         ;; ideally avoid doing outside of the 'test' G-Expression. Thus, we
         ;; generate a bootloader configuration for the script as if there
         ;; were no existing menu entries. In the grand scheme of things, this
         ;; matters little -- these tests should not make assertions about the
         ;; behavior of 'operating-system-bootcfg'.
         (bootcfg (operating-system-bootcfg os '()))
         (bootcfg-file (bootloader-configuration-file bootloader)))
    (gexp->derivation
     "install-bootloader"
     ;; Due to the read-only nature of the virtual machines used in the system
     ;; test suite, the bootloader installer script is omitted. 'grub-install'
     ;; would attempt to write directly to the virtual disk if the
     ;; installation script were run.
     (test
      (install-bootloader-program #f #f #f bootcfg bootcfg-file '(#f) "/")))))


(define %test-switch-to-system
  (system-test
   (name "switch-to-system")
   (description "Create a new generation of the system profile.")
   (value (run-switch-to-system-test))))

(define %test-upgrade-services
  (system-test
   (name "upgrade-services")
   (description "Upgrade the Shepherd by unloading obsolete services and
loading new services.")
   (value (run-upgrade-services-test))))

(define %test-install-bootloader
  (system-test
   (name "install-bootloader")
   (description "Install a bootloader and its configuration file.")
   (value (run-install-bootloader-test))))
,which))) (arguments `(;; Parallel builds appear to be unsafe, see ;; <http://hydra.gnu.org/build/49331/nixlog/1/raw>. #:parallel-build? #f #:tests? #f)) ; no check target (home-page "http://dvdisaster.net/en/index.html") (synopsis "Error correcting codes for optical media images") (description "Optical media (CD,DVD,BD) keep their data only for a finite time (typically for many years). After that time, data loss develops slowly with read errors growing from the outer media region towards the inside. Dvdisaster stores data on CD/DVD/BD (supported media) in a way that it is fully recoverable even after some read errors have developed. This enables you to rescue the complete data to a new medium. Data loss is prevented by using error correcting codes. Error correction data is either added to the medium or kept in separate error correction files. Dvdisaster works at the image level so that the recovery does not depend on the file system of the medium. The maximum error correction capacity is user-selectable.") (license gpl2+))) (define-public libcue (package (name "libcue") (version "2.1.0") (source (origin (method url-fetch) (uri (string-append "https://github.com/lipnitsk/libcue/archive/v" version ".tar.gz")) (file-name (string-append name "-" version ".tar.gz")) (sha256 (base32 "1fradl3dx0pyy9rn1a0gak9gzgg40wax61f2s00zks7rwl0xv398")))) (build-system cmake-build-system) (native-inputs `(("bison" ,bison) ("flex" ,flex))) (home-page "https://github.com/lipnitsk/libcue") (synopsis "C library to parse cue sheets") (description "Libcue is a C library to parse so-called @dfn{cue sheets} which contain meta-data for CD/DVD tracks. It provides an API to manipulate the data.") (license gpl2+))) (define-public cd-discid (package (name "cd-discid") (version "1.4") (home-page "http://linukz.org/cd-discid.shtml") (source (origin (method url-fetch) (uri (string-append "http://linukz.org/download/cd-discid-" version ".tar.gz")) (sha256 (base32 "0qrcvn7227qaayjcd5rm7z0k5q89qfy5qkdgwr5pd7ih0va8rmpz")) (modules '((guix build utils))) (snippet '(substitute* "Makefile" (("/usr/bin/install") "install"))))) (build-system gnu-build-system) (arguments '(#:tests? #f #:phases (alist-delete 'configure %standard-phases) #:make-flags (list "CC=gcc" (string-append "PREFIX=" (assoc-ref %outputs "out"))))) (synopsis "Get CDDB discid information from an audio CD") (description "cd-discid is a command-line tool to retrieve CDDB discid information from an audio CD.") (license gpl2+))) (define-public abcde (package (name "abcde") (version "2.7.1") (home-page "https://abcde.einval.com/") (source (origin (method url-fetch) (uri (string-append home-page "/download/abcde-" version ".tar.gz")) (sha256 (base32 "0l7j0nk8p30s97285i418rv9ym9bgns7bn6l8gldw3mjhnby609l")) (modules '((guix build utils))) (snippet '(substitute* "Makefile" (("/usr/bin/install") "install") (("^etcdir = .*$") (string-append "etcdir = $(prefix)/etc\n")))))) (build-system gnu-build-system) (arguments '(#:phases (modify-phases %standard-phases (replace 'configure (lambda* (#:key outputs inputs #:allow-other-keys) (substitute* "Makefile" (("^prefix = .*$") (string-append "prefix = " (assoc-ref outputs "out") "\n"))))) (add-after 'install 'wrap (lambda* (#:key inputs outputs #:allow-other-keys) (let ((wget (assoc-ref inputs "wget")) (vorbis (assoc-ref inputs "vorbis-tools")) (parano (assoc-ref inputs "cdparanoia")) (which (assoc-ref inputs "which")) (discid (assoc-ref inputs "cd-discid")) (out (assoc-ref outputs "out"))) (define (wrap file) (wrap-program file `("PATH" ":" prefix (,(string-append out "/bin:" wget "/bin:" which "/bin:" vorbis "/bin:" discid "/bin:" parano "/bin"))))) (for-each wrap (find-files (string-append out "/bin") ".*")))))) #:tests? #f)) (inputs `(("wget" ,wget) ("which" ,which) ("cdparanoia" ,cdparanoia) ("cd-discid" ,cd-discid) ("vorbis-tools" ,vorbis-tools) ;; A couple of Python and Perl scripts are included. ("python" ,python) ("perl" ,perl))) (synopsis "Command-line audio CD ripper") (description "abcde is a front-end command-line utility (actually, a shell script) that grabs tracks off a CD, encodes them to Ogg/Vorbis, MP3, FLAC, Ogg/Speex and/or MPP/MP+ (Musepack) format, and tags them, all in one go.") (license gpl2+)))