unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
blob 390c0189bbbf06bf5df14ed2c8cfe64a9b555943 17201 bytes (raw)
name: tests/machine.scm 	 # note: path name is non-authoritative(*)

  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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
 
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2019 Jakob L. Kreuze <zerodaysfordays@sdf.lonestar.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 machine)
  #:use-module (gnu bootloader grub)
  #:use-module (gnu bootloader)
  #:use-module (gnu build marionette)
  #:use-module (gnu build vm)
  #:use-module (gnu machine)
  #:use-module (gnu machine ssh)
  #:use-module (gnu packages bash)
  #:use-module (gnu packages virtualization)
  #:use-module (gnu services base)
  #:use-module (gnu services networking)
  #:use-module (gnu services ssh)
  #:use-module (gnu services)
  #:use-module (gnu system file-systems)
  #:use-module (gnu system vm)
  #:use-module (gnu system)
  #:use-module (gnu tests)
  #:use-module (guix derivations)
  #:use-module (guix gexp)
  #:use-module (guix monads)
  #:use-module (guix pki)
  #:use-module (guix store)
  #:use-module (guix utils)
  #:use-module (ice-9 ftw)
  #:use-module (ice-9 match)
  #:use-module (ice-9 textual-ports)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-26)
  #:use-module (srfi srfi-64)
  #:use-module (ssh auth)
  #:use-module (ssh channel)
  #:use-module (ssh key)
  #:use-module (ssh session))

\f
;;;
;;; Virtual machine scaffolding.
;;;

(define marionette-pid (@@ (gnu build marionette) marionette-pid))

(define (call-with-marionette path command proc)
  "Invoke PROC with a marionette running COMMAND in PATH."
  (let* ((marionette (make-marionette command #:socket-directory path))
         (pid (marionette-pid marionette)))
    (dynamic-wind
      (lambda ()
        (unless marionette
          (error "could not start marionette")))
      (lambda () (proc marionette))
      (lambda ()
        (kill pid SIGTERM)))))

(define (dir-join . components)
  "Join COMPONENTS with `file-name-separator-string'."
  (string-join components file-name-separator-string))

(define (call-with-machine-test-directory proc)
  "Run PROC with the path to a temporary directory that will be cleaned up
when PROC returns. Only files that can be passed to 'delete-file' should be
created within the temporary directory; cleanup will not recurse into
subdirectories."
  (let ((path (tmpnam)))
    (dynamic-wind
      (lambda ()
        (unless (mkdir path)
          (error (format #f "could not create directory '~a'" path))))
      (lambda () (proc path))
      (lambda ()
        (let ((children (map first (cddr (file-system-tree path)))))
          (for-each (lambda (child)
                      (false-if-exception
                       (delete-file (dir-join path child))))
                    children)
          (rmdir path))))))

(define (os-for-test os)
  "Return an <operating-system> record derived from OS that is appropriate for
use with 'qemu-image'."
  (define file-systems-to-keep
    ;; Keep only file systems other than root and not normally bound to real
    ;; devices.
    (remove (lambda (fs)
              (let ((target (file-system-mount-point fs))
                    (source (file-system-device fs)))
                (or (string=? target "/")
                    (string-prefix? "/dev/" source))))
            (operating-system-file-systems os)))

  (define root-uuid
    ;; UUID of the root file system.
    ((@@ (gnu system vm) operating-system-uuid) os 'dce))


  (operating-system
    (inherit os)
    ;; Assume we have an initrd with the whole QEMU shebang.

    ;; Force our own root file system.  Refer to it by UUID so that
    ;; it works regardless of how the image is used ("qemu -hda",
    ;; Xen, etc.).
    (file-systems (cons (file-system
                          (mount-point "/")
                          (device root-uuid)
                          (type "ext4"))
                        file-systems-to-keep))))

(define (qemu-image-for-test os)
  "Return a derivation producing a QEMU disk image running OS. This procedure
is similar to 'system-qemu-image' in (gnu system vm), but makes use of
'os-for-test' so that callers may obtain the same system derivation that will
be booted by the image."
  (define root-uuid ((@@ (gnu system vm) operating-system-uuid) os 'dce))
  (let* ((os (os-for-test os))
         (bootcfg (operating-system-bootcfg os)))
    (qemu-image #:os os
                #:bootcfg-drv bootcfg
                #:bootloader (bootloader-configuration-bootloader
                              (operating-system-bootloader os))
                #:disk-image-size (* 9000 (expt 2 20))
                #:file-system-type "ext4"
                #:file-system-uuid root-uuid
                #:inputs `(("system" ,os)
                           ("bootcfg" ,bootcfg))
                #:copy-inputs? #t)))

(define (make-writable-image image)
  "Return a derivation producing a script to create a writable disk image
overlay of IMAGE, writing the overlay to the the path given as a command-line
argument to the script."
  (define qemu-img-exec
    #~(list (string-append #$qemu-minimal "/bin/qemu-img")
            "create" "-f" "qcow2"
            "-o" (string-append "backing_file=" #$image)))

  (define builder
    #~(call-with-output-file #$output
        (lambda (port)
          (format port "#!~a~% exec ~a \"$@\"~%"
                  #$(file-append bash "/bin/sh")
                  (string-join #$qemu-img-exec " "))
          (chmod port #o555))))

  (gexp->derivation "make-writable-image.sh" builder))

(define (run-os-for-test os)
  "Return a derivation producing a script to run OS as a qemu guest, whose
first argument is the path to a writable disk image. Additional arguments are
passed as-is to qemu."
  (define kernel-arguments
    #~(list "console=ttyS0"
            #+@(operating-system-kernel-arguments os "/dev/sda1")))

  (define qemu-exec
    #~(begin
        (list (string-append #$qemu-minimal "/bin/" #$(qemu-command (%current-system)))
              "-kernel" #$(operating-system-kernel-file os)
              "-initrd" #$(file-append os "/initrd")
              (format #f "-append ~s"
                      (string-join #$kernel-arguments " "))
              #$@(if (file-exists? "/dev/kvm")
                     '("-enable-kvm")
                     '())
              "-no-reboot"
              "-net nic,model=virtio"
              "-object" "rng-random,filename=/dev/urandom,id=guixsd-vm-rng"
              "-device" "virtio-rng-pci,rng=guixsd-vm-rng"
              "-vga" "std"
              "-m" "256"
              "-net" "user,hostfwd=tcp::2222-:22")))

  (define builder
    #~(call-with-output-file #$output
        (lambda (port)
          (format port "#!~a~% exec ~a -drive \"file=$@\"~%"
                  #$(file-append bash "/bin/sh")
                  (string-join #$qemu-exec " "))
          (chmod port #o555))))

  (gexp->derivation "run-vm.sh" builder))

(define (scripts-for-test os)
  "Build and return a list containing the paths of:

- A script to make a writable disk image overlay of OS.
- A script to run that disk image overlay as a qemu guest."
  (let ((virtualized-os (os-for-test os)))
    (mlet* %store-monad ((osdrv (operating-system-derivation virtualized-os))
                         (imgdrv (qemu-image-for-test os))

                         ;; Ungexping 'imgdrv' or 'osdrv' will result in an
                         ;; error if the derivations don't exist in the store,
                         ;; so we ensure they're built prior to invoking
                         ;; 'run-vm' or 'make-image'.
                         (_ ((store-lift build-derivations) (list imgdrv)))

                         (run-vm (run-os-for-test virtualized-os))
                         (make-image
                          (make-writable-image (derivation->output-path imgdrv))))
      (mbegin %store-monad
        ((store-lift build-derivations) (list imgdrv make-image run-vm))
        (return (list (derivation->output-path make-image)
                      (derivation->output-path run-vm)))))))

(define (call-with-marionette-and-session os proc)
  "Construct a marionette backed by OS in a temporary test environment and
invoke PROC with two arguments: the marionette object, and an SSH session
connected to the marionette."
  (call-with-machine-test-directory
   (lambda (path)
     (match (with-store store
              (run-with-store store
                (scripts-for-test %system)))
       ((make-image run-vm)
        (let ((image (dir-join path "image")))
          ;; Create the writable image overlay.
          (system (string-join (list make-image image) " "))
          (call-with-marionette
           path
           (list run-vm image)
           (lambda (marionette)
             ;; XXX: The guest clearly has (gcrypt pk-crypto) since this
             ;; works, but trying to import it from 'marionette-eval' fails as
             ;; the Marionette REPL does not have 'guile-gcrypt' in its
             ;; %load-path.
             (marionette-eval
              `(begin
                 (use-modules (ice-9 popen))
                 (let ((port (open-pipe* OPEN_WRITE "guix" "archive" "--authorize")))
                   (put-string port ,%signing-key)
                   (close port)))
              marionette)
             ;; XXX: This is an absolute hack to work around potential quirks
             ;; in the operating system. For one, we invoke 'herd' from the
             ;; command-line to ensure that the Shepherd socket file
             ;; exists. Second, we enable 'ssh-daemon', as there's a chance
             ;; the service will be disabled upon booting the image.
             (marionette-eval
              `(system "herd enable ssh-daemon")
              marionette)
             (marionette-eval
              '(begin
                 (use-modules (gnu services herd))
                 (start-service 'ssh-daemon))
              marionette)
             (call-with-connected-session/auth
              (lambda (session)
                (proc marionette session)))))))))))

\f
;;;
;;; SSH session management. These are borrowed from (gnu tests ssh).
;;;

(define (make-session-for-test)
  "Make a session with predefined parameters for a test."
  (make-session #:user "root"
                #:port 2222
                #:host "localhost"))

(define (call-with-connected-session proc)
  "Call the one-argument procedure PROC with a freshly created and
connected SSH session object, return the result of the procedure call.  The
session is disconnected when the PROC is finished."
  (let ((session (make-session-for-test)))
    (dynamic-wind
      (lambda ()
        (let ((result (connect! session)))
          (unless (equal? result 'ok)
            (error "Could not connect to a server"
                   session result))))
      (lambda () (proc session))
      (lambda () (disconnect! session)))))

(define (call-with-connected-session/auth proc)
  "Make an authenticated session.  We should be able to connect as
root with an empty password."
  (call-with-connected-session
   (lambda (session)
     ;; Try the simple authentication methods.  Dropbear requires
     ;; 'none' when there are no passwords, whereas OpenSSH accepts
     ;; 'password' with an empty password.
     (let loop ((methods (list (cut userauth-password! <> "")
                               (cut userauth-none! <>))))
       (match methods
         (()
          (error "all the authentication methods failed"))
         ((auth rest ...)
          (match (pk 'auth (auth session))
            ('success
             (proc session))
            ('denied
             (loop rest)))))))))

\f
;;;
;;; Virtual machines for use in the test suite.
;;;

(define %system
  ;; A "bare bones" operating system running both an OpenSSH daemon and the
  ;; "marionette" service.
  (marionette-operating-system
   (operating-system
     (host-name "gnu")
     (timezone "Etc/UTC")
     (bootloader (bootloader-configuration
                  (bootloader grub-bootloader)
                  (target "/dev/sda")
                  (terminal-outputs '(console))))
     (file-systems (cons (file-system
                           (mount-point "/")
                           (device "/dev/vda1")
                           (type "ext4"))
                         %base-file-systems))
     (services
      (append (list (service dhcp-client-service-type)
                    (service openssh-service-type
                             (openssh-configuration
                              (permit-root-login #t)
                              (allow-empty-passwords? #t))))
              %base-services)))
   #:imported-modules '((gnu services herd)
                        (guix combinators))))

(define %signing-key
  ;; The host's signing key, encoded as a string. The "marionette" will reject
  ;; any files signed by an unauthorized host, so we'll need to send this key
  ;; over and authorize it.
  (call-with-input-file %public-key-file
    (lambda (port)
      (get-string-all port))))

\f
(test-begin "machine")

(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))

(define (running-services marionette)
  (marionette-eval
   '(begin
      (use-modules (gnu services herd)
                   (srfi srfi-1))
      (map (compose first live-service-provision)
           (filter live-service-running (current-services))))
   marionette))

(define (count-grub-cfg-entries marionette)
  (marionette-eval
   '(begin
      (define grub-cfg
        (call-with-input-file "/boot/grub/grub.cfg"
          (lambda (port)
            (get-string-all port))))

        (let loop ((n 0)
                   (start 0))
          (let ((index (string-contains grub-cfg "menuentry" start)))
            (if index
                (loop (1+ n) (1+ index))
                n))))
   marionette))

(define %target-system
  (marionette-operating-system
   (operating-system
     (host-name "gnu-deployed")
     (timezone "Etc/UTC")
     (bootloader (bootloader-configuration
                  (bootloader grub-bootloader)
                  (target "/dev/sda")
                  (terminal-outputs '(console))))
     (file-systems (cons (file-system
                           (mount-point "/")
                           (device "/dev/vda1")
                           (type "ext4"))
                         %base-file-systems))
     (services
      (append (list (service tor-service-type)
                    (service dhcp-client-service-type)
                    (service openssh-service-type
                             (openssh-configuration
                              (permit-root-login #t)
                              (allow-empty-passwords? #t))))
              %base-services)))
   #:imported-modules '((gnu services herd)
                        (guix combinators))))

(call-with-marionette-and-session
 (os-for-test %system)
 (lambda (marionette session)
   (let ((generations-prior (system-generations marionette))
         (services-prior (running-services marionette))
         (grub-entry-count-prior (count-grub-cfg-entries marionette))
         (machine (machine
                   (system %target-system)
                   (environment 'managed-host)
                   (configuration (machine-ssh-configuration
                                   (host-name "localhost")
                                   (session session))))))
     (with-store store
       (run-with-store store
         (build-machine machine))
       (run-with-store store
         (deploy-machine machine)))
     (test-equal "deployment created new generation"
       (length (system-generations marionette))
       (1+ (length generations-prior)))
     (test-assert "deployment started new service"
       (and (not (memq 'tor services-prior))
            (memq 'tor (running-services marionette))))
     (test-equal "deployment created new menu entry"
       (count-grub-cfg-entries marionette)
       ;; A Grub configuration that contains a single menu entry does not have
       ;; an "old configurations" submenu. Deployment, then, would result in
       ;; this submenu being created, meaning an additional two 'menuentry'
       ;; fields rather than just one.
       (if (= grub-entry-count-prior 1)
           (+ 2 grub-entry-count-prior)
           (1+ grub-entry-count-prior))))))

(test-end "machine")

debug log:

solving 390c0189bb ...
found 390c0189bb in https://yhetil.org/guix-patches/87a7e1j0hy.fsf_-_@sdf.lonestar.org/ ||
	https://yhetil.org/guix-patches/87a7e2an3h.fsf_-_@sdf.lonestar.org/

applying [1/1] https://yhetil.org/guix-patches/87a7e1j0hy.fsf_-_@sdf.lonestar.org/
diff --git a/tests/machine.scm b/tests/machine.scm
new file mode 100644
index 0000000000..390c0189bb

Checking patch tests/machine.scm...
Applied patch tests/machine.scm cleanly.

skipping https://yhetil.org/guix-patches/87a7e2an3h.fsf_-_@sdf.lonestar.org/ for 390c0189bb
index at:
100644 390c0189bbbf06bf5df14ed2c8cfe64a9b555943	tests/machine.scm

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).