gnu: skopeo: Update to 1.2.0.
[jackhill/guix/guix.git] / gnu / tests / virtualization.scm
CommitLineData
c075c8fd
CB
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2017 Christopher Baines <mail@cbaines.net>
c11c19bd 3;;; Copyright © 2020 Ludovic Courtès <ludo@gnu.org>
c075c8fd
CB
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 tests virtualization)
21 #:use-module (gnu tests)
22 #:use-module (gnu system)
23 #:use-module (gnu system file-systems)
24 #:use-module (gnu system vm)
25 #:use-module (gnu services)
26 #:use-module (gnu services dbus)
27 #:use-module (gnu services networking)
28 #:use-module (gnu services virtualization)
29 #:use-module (gnu packages virtualization)
c11c19bd 30 #:use-module (gnu packages ssh)
c075c8fd
CB
31 #:use-module (guix gexp)
32 #:use-module (guix store)
c11c19bd
LC
33 #:export (%test-libvirt
34 %test-childhurd))
35
36\f
37;;;
38;;; Libvirt.
39;;;
c075c8fd
CB
40
41(define %libvirt-os
42 (simple-operating-system
39d7fdce 43 (service dhcp-client-service-type)
c075c8fd
CB
44 (dbus-service)
45 (polkit-service)
46 (service libvirt-service-type)))
47
48(define (run-libvirt-test)
49 "Run tests in %LIBVIRT-OS."
50 (define os
51 (marionette-operating-system
52 %libvirt-os
53 #:imported-modules '((gnu services herd)
54 (guix combinators))))
55
56 (define vm
57 (virtual-machine
58 (operating-system os)
59 (port-forwardings '())))
60
61 (define test
62 (with-imported-modules '((gnu build marionette))
63 #~(begin
64 (use-modules (srfi srfi-11) (srfi srfi-64)
65 (gnu build marionette))
66
67 (define marionette
68 (make-marionette (list #$vm)))
69
70 (mkdir #$output)
71 (chdir #$output)
72
73 (test-begin "libvirt")
74
75 (test-assert "service running"
76 (marionette-eval
77 '(begin
78 (use-modules (gnu services herd))
79 (match (start-service 'libvirtd)
80 (#f #f)
81 (('service response-parts ...)
82 (match (assq-ref response-parts 'running)
83 ((pid) (number? pid))))))
84 marionette))
85
86 (test-eq "fetch version"
87 0
88 (marionette-eval
89 `(begin
90 (system* ,(string-append #$libvirt "/bin/virsh")
91 "-c" "qemu:///system" "version"))
92 marionette))
93
94 (test-end)
95 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
96
97 (gexp->derivation "libvirt-test" test))
98
99(define %test-libvirt
100 (system-test
101 (name "libvirt")
102 (description "Connect to the running LIBVIRT service.")
103 (value (run-libvirt-test))))
c11c19bd
LC
104
105\f
106;;;
107;;; GNU/Hurd virtual machines, aka. childhurds.
108;;;
109
110(define %childhurd-os
111 (simple-operating-system
112 (service dhcp-client-service-type)
113 (service hurd-vm-service-type)))
114
115(define (run-childhurd-test)
116 (define os
117 (marionette-operating-system
118 %childhurd-os
119 #:imported-modules '((gnu services herd)
120 (guix combinators))))
121
122 (define vm
123 (virtual-machine
124 (operating-system os)
125 (memory-size (* 1024 3))))
126
127 (define run-uname-over-ssh
128 ;; Program that runs 'uname' over SSH and prints the result on standard
129 ;; output.
130 (let ()
131 (define run
132 (with-extensions (list guile-ssh)
133 #~(begin
134 (use-modules (ssh session)
135 (ssh auth)
136 (ssh popen)
137 (ice-9 match)
138 (ice-9 textual-ports))
139
140 (let ((session (make-session #:user "root"
141 #:port 10022
142 #:host "localhost"
143 #:log-verbosity 'rare)))
144 (match (connect! session)
145 ('ok
146 (userauth-password! session "")
147 (display
148 (get-string-all
149 (open-remote-input-pipe* session "uname" "-on"))))
150 (status
151 (error "could not connect to childhurd over SSH"
152 session status)))))))
153
154 (program-file "run-uname-over-ssh" run)))
155
156 (define test
157 (with-imported-modules '((gnu build marionette))
158 #~(begin
159 (use-modules (gnu build marionette)
160 (srfi srfi-64)
161 (ice-9 match))
162
163 (define marionette
164 (make-marionette (list #$vm)))
165
166 (mkdir #$output)
167 (chdir #$output)
168
169 (test-begin "childhurd")
170
171 (test-assert "service running"
172 (marionette-eval
173 '(begin
174 (use-modules (gnu services herd))
175 (match (start-service 'childhurd)
176 (#f #f)
177 (('service response-parts ...)
178 (match (assq-ref response-parts 'running)
179 ((pid) (number? pid))))))
180 marionette))
181
182 (test-equal "childhurd SSH server replies"
183 "SSH"
184 ;; Check from within the guest whether its childhurd's SSH
185 ;; server is reachable. Do that from the guest: port forwarding
186 ;; to the host won't work because QEMU listens on 127.0.0.1.
187 (marionette-eval
188 '(begin
189 (use-modules (ice-9 match))
190
191 (let loop ((n 60))
192 (if (zero? n)
193 'all-attempts-failed
194 (let ((s (socket PF_INET SOCK_STREAM 0))
195 (a (make-socket-address AF_INET
196 INADDR_LOOPBACK
197 10022)))
198 (format #t "connecting to childhurd SSH server...~%")
199 (connect s a)
200 (match (get-string-n s 3)
201 ((? eof-object?)
202 (close-port s)
203 (sleep 1)
204 (loop (- n 1)))
205 (str
206 (close-port s)
207 str))))))
208 marionette))
209
210 (test-equal "SSH up and running"
211 "childhurd GNU\n"
212
213 ;; Connect from the guest to the chidhurd over SSH and run the
214 ;; 'uname' command.
215 (marionette-eval
216 '(begin
217 (use-modules (ice-9 popen))
218
219 (get-string-all
220 (open-input-pipe #$run-uname-over-ssh)))
221 marionette))
222
223 (test-end)
224 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
225
226 (gexp->derivation "childhurd-test" test))
227
228(define %test-childhurd
229 (system-test
230 (name "childhurd")
231 (description
232 "Connect to the GNU/Hurd virtual machine service, aka. a childhurd, making
233sure that the childhurd boots and runs its SSH server.")
234 (value (run-childhurd-test))))