projects/guix·blob version-1.4.0 · refs

381 lines · 12.5 KiB

1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2017 Christopher Baines <[email protected]>
3;;; Copyright © 2020-2022 Ludovic Courtès <[email protected]>
4;;; Copyright © 2020 Jan (janneke) Nieuwenhuizen <[email protected]>
5;;; Copyright © 2021 Pierre Langlois <[email protected]>
6;;; Copyright © 2022 Marius Bakke <[email protected]>
7;;;
8;;; This file is part of GNU Guix.
9;;;
10;;; GNU Guix is free software; you can redistribute it and/or modify it
11;;; under the terms of the GNU General Public License as published by
12;;; the Free Software Foundation; either version 3 of the License, or (at
13;;; your option) any later version.
14;;;
15;;; GNU Guix is distributed in the hope that it will be useful, but
16;;; WITHOUT ANY WARRANTY; without even the implied warranty of
17;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;;; GNU General Public License for more details.
19;;;
20;;; You should have received a copy of the GNU General Public License
21;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
22
23(define-module (gnu tests virtualization)
24 #:use-module (gnu tests)
25 #:use-module (gnu image)
26 #:use-module (gnu system)
27 #:use-module (gnu system file-systems)
28 #:use-module (gnu system image)
29 #:use-module (gnu system images hurd)
30 #:use-module (gnu system vm)
31 #:use-module (gnu services)
32 #:use-module (gnu services dbus)
33 #:use-module (gnu services networking)
34 #:use-module (gnu services virtualization)
35 #:use-module (gnu packages ssh)
36 #:use-module (gnu packages virtualization)
37 #:use-module (guix gexp)
38 #:use-module (guix records)
39 #:use-module (guix store)
40 #:export (%test-libvirt
41 %test-qemu-guest-agent
42 %test-childhurd))
43
44
45;;;
46;;; Libvirt.
47;;;
48
49(define %libvirt-os
50 (simple-operating-system
51 (service dhcp-client-service-type)
52 (dbus-service)
53 (polkit-service)
54 (service libvirt-service-type)))
55
56(define (run-libvirt-test)
57 "Run tests in %LIBVIRT-OS."
58 (define os
59 (marionette-operating-system
60 %libvirt-os
61 #:imported-modules '((gnu services herd)
62 (guix combinators))))
63
64 (define vm
65 (virtual-machine
66 (operating-system os)
67 (port-forwardings '())))
68
69 (define test
70 (with-imported-modules '((gnu build marionette))
71 #~(begin
72 (use-modules (srfi srfi-11) (srfi srfi-64)
73 (gnu build marionette))
74
75 (define marionette
76 (make-marionette (list #$vm)))
77
78 (test-runner-current (system-test-runner #$output))
79 (test-begin "libvirt")
80
81 (test-assert "service running"
82 (marionette-eval
83 '(begin
84 (use-modules (gnu services herd))
85 (match (start-service 'libvirtd)
86 (#f #f)
87 (('service response-parts ...)
88 (match (assq-ref response-parts 'running)
89 ((pid) (number? pid))))))
90 marionette))
91
92 (test-eq "fetch version"
93 0
94 (marionette-eval
95 `(begin
96 (chdir "/tmp")
97 (system* ,(string-append #$libvirt "/bin/virsh")
98 "-c" "qemu:///system" "version"))
99 marionette))
100
101 (test-eq "connect"
102 0
103 (marionette-eval
104 `(begin
105 (chdir "/tmp")
106 (system* ,(string-append #$libvirt "/bin/virsh")
107 "-c" "qemu:///system" "connect"))
108 marionette))
109
110 (test-eq "create default network"
111 0
112 (marionette-eval
113 '(begin
114 (chdir "/tmp")
115 (system* #$(file-append libvirt "/bin/virsh")
116 "-c" "qemu:///system" "net-define"
117 #$(file-append libvirt
118 "/etc/libvirt/qemu/networks/default.xml")))
119 marionette))
120
121 (test-eq "start default network"
122 0
123 (marionette-eval
124 '(begin
125 (chdir "/tmp")
126 (system* #$(file-append libvirt "/bin/virsh")
127 "-c" "qemu:///system" "net-start" "default"))
128 marionette))
129
130 (test-end))))
131
132 (gexp->derivation "libvirt-test" test))
133
134(define %test-libvirt
135 (system-test
136 (name "libvirt")
137 (description "Connect to the running LIBVIRT service.")
138 (value (run-libvirt-test))))
139
140
141;;;
142;;; QEMU Guest Agent service.
143;;;
144
145(define %qemu-guest-agent-os
146 (simple-operating-system
147 (service qemu-guest-agent-service-type)))
148
149(define (run-qemu-guest-agent-test)
150 "Run tests in %QEMU-GUEST-AGENT-OS."
151 (define os
152 (marionette-operating-system
153 %qemu-guest-agent-os
154 #:imported-modules '((gnu services herd))))
155
156 (define vm
157 (virtual-machine
158 (operating-system os)
159 (port-forwardings '())))
160
161 (define test
162 (with-imported-modules '((gnu build marionette))
163 #~(begin
164 (use-modules (gnu build marionette)
165 (ice-9 rdelim)
166 (srfi srfi-64))
167
168 (define marionette
169 ;; Ensure we look for the socket in the correct place below.
170 (make-marionette (list #$vm) #:socket-directory "/tmp"))
171
172 (define* (try-read port #:optional (attempts 10))
173 ;; Try reading from a port several times before giving up.
174 (cond ((char-ready? port)
175 (let ((response (read-line port)))
176 (close-port port)
177 response))
178 ((> attempts 1)
179 (sleep 1)
180 (try-read port (- attempts 1)))
181 (else "")))
182
183 (define (run command)
184 ;; Run a QEMU guest agent command and return the response.
185 (let ((s (socket PF_UNIX SOCK_STREAM 0)))
186 (connect s AF_UNIX "/tmp/qemu-ga")
187 (display command s)
188 (try-read s)))
189
190 (test-runner-current (system-test-runner #$output))
191 (test-begin "qemu-guest-agent")
192
193 (test-assert "service running"
194 (marionette-eval
195 '(begin
196 (use-modules (gnu services herd))
197 (match (start-service 'qemu-guest-agent)
198 (#f #f)
199 (('service response-parts ...)
200 (match (assq-ref response-parts 'running)
201 ((pid) (number? pid))))))
202 marionette))
203
204 (test-equal "ping guest"
205 "{\"return\": {}}"
206 (run "{\"execute\": \"guest-ping\"}"))
207
208 (test-assert "get network interfaces"
209 (string-contains
210 (run "{\"execute\": \"guest-network-get-interfaces\"}")
211 "127.0.0.1"))
212
213 (test-end))))
214
215 (gexp->derivation "qemu-guest-agent-test" test))
216
217(define %test-qemu-guest-agent
218 (system-test
219 (name "qemu-guest-agent")
220 (description "Run commands in a virtual machine using QEMU guest agent.")
221 (value (run-qemu-guest-agent-test))))
222
223
224;;;
225;;; GNU/Hurd virtual machines, aka. childhurds.
226;;;
227
228;; Copy of `hurd-vm-disk-image', using plain disk-image for test
229(define (hurd-vm-disk-image-raw config)
230 (let ((os ((@@ (gnu services virtualization) secret-service-operating-system)
231 (hurd-vm-configuration-os config)))
232 (disk-size (hurd-vm-configuration-disk-size config)))
233 (system-image
234 (image
235 (inherit hurd-disk-image)
236 (format 'disk-image)
237 (size disk-size)
238 (operating-system os)))))
239
240(define %childhurd-os
241 (simple-operating-system
242 (service dhcp-client-service-type)
243 (service hurd-vm-service-type
244 (hurd-vm-configuration
245 (image (hurd-vm-disk-image-raw this-record))))))
246
247(define (run-childhurd-test)
248 (define os
249 (marionette-operating-system
250 %childhurd-os
251 #:imported-modules '((gnu services herd)
252 (guix combinators))))
253
254 (define vm
255 (virtual-machine
256 (operating-system os)
257 (memory-size (* 1024 3))))
258
259 (define (run-command-over-ssh . command)
260 ;; Program that runs COMMAND over SSH and prints the result on standard
261 ;; output.
262 (let ()
263 (define run
264 (with-extensions (list guile-ssh)
265 #~(begin
266 (use-modules (ssh session)
267 (ssh auth)
268 (ssh popen)
269 (ice-9 match)
270 (ice-9 textual-ports))
271
272 (let ((session (make-session #:user "root"
273 #:port 10022
274 #:host "localhost"
275 #:log-verbosity 'rare)))
276 (match (connect! session)
277 ('ok
278 (userauth-password! session "")
279 (display
280 (get-string-all
281 (open-remote-input-pipe* session #$@command))))
282 (status
283 (error "could not connect to childhurd over SSH"
284 session status)))))))
285
286 (program-file "run-command-over-ssh" run)))
287
288 (define test
289 (with-imported-modules '((gnu build marionette))
290 #~(begin
291 (use-modules (gnu build marionette)
292 (srfi srfi-64)
293 (ice-9 match))
294
295 (define marionette
296 (make-marionette (list #$vm)))
297
298 (test-runner-current (system-test-runner #$output))
299 (test-begin "childhurd")
300
301 (test-assert "service running"
302 (marionette-eval
303 '(begin
304 (use-modules (gnu services herd))
305 (match (start-service 'childhurd)
306 (#f #f)
307 (('service response-parts ...)
308 (match (assq-ref response-parts 'running)
309 ((pid) (number? pid))))))
310 marionette))
311
312 (test-equal "childhurd SSH server replies"
313 "SSH"
314 ;; Check from within the guest whether its childhurd's SSH
315 ;; server is reachable. Do that from the guest: port forwarding
316 ;; to the host won't work because QEMU listens on 127.0.0.1.
317 (marionette-eval
318 '(begin
319 (use-modules (ice-9 match))
320
321 (let loop ((n 60))
322 (if (zero? n)
323 'all-attempts-failed
324 (let ((s (socket PF_INET SOCK_STREAM 0))
325 (a (make-socket-address AF_INET
326 INADDR_LOOPBACK
327 10022)))
328 (format #t "connecting to childhurd SSH server...~%")
329 (connect s a)
330 (match (get-string-n s 3)
331 ((? eof-object?)
332 (close-port s)
333 (sleep 1)
334 (loop (- n 1)))
335 (str
336 (close-port s)
337 str))))))
338 marionette))
339
340 (test-equal "SSH up and running"
341 "childhurd GNU\n"
342
343 ;; Connect from the guest to the chidhurd over SSH and run the
344 ;; 'uname' command.
345 (marionette-eval
346 '(begin
347 (use-modules (ice-9 popen))
348
349 (get-string-all
350 (open-input-pipe #$(run-command-over-ssh "uname" "-on"))))
351 marionette))
352
353 (test-assert "guix-daemon up and running"
354 (let ((drv (marionette-eval
355 '(begin
356 (use-modules (ice-9 popen))
357
358 (get-string-all
359 (open-input-pipe
360 #$(run-command-over-ssh "guix" "build" "coreutils"
361 "--no-grafts" "-d"))))
362 marionette)))
363 ;; We cannot compare the .drv with (raw-derivation-file
364 ;; coreutils) on the host: they may differ due to fixed-output
365 ;; derivations and changes introduced compared to the 'guix'
366 ;; package snapshot.
367 (and (string-suffix? ".drv"
368 (pk 'drv (string-trim-right drv)))
369 drv)))
370
371 (test-end))))
372
373 (gexp->derivation "childhurd-test" test))
374
375(define %test-childhurd
376 (system-test
377 (name "childhurd")
378 (description
379 "Connect to the GNU/Hurd virtual machine service, aka. a childhurd, making
380sure that the childhurd boots and runs its SSH server.")
381 (value (run-childhurd-test))))