virtualization.scm 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017 Christopher Baines <mail@cbaines.net>
  3. ;;; Copyright © 2020 Ludovic Courtès <ludo@gnu.org>
  4. ;;; Copyright © 2020 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  5. ;;; Copyright © 2021 Pierre Langlois <pierre.langlois@gmx.com>
  6. ;;;
  7. ;;; This file is part of GNU Guix.
  8. ;;;
  9. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  10. ;;; under the terms of the GNU General Public License as published by
  11. ;;; the Free Software Foundation; either version 3 of the License, or (at
  12. ;;; your option) any later version.
  13. ;;;
  14. ;;; GNU Guix is distributed in the hope that it will be useful, but
  15. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;;; GNU General Public License for more details.
  18. ;;;
  19. ;;; You should have received a copy of the GNU General Public License
  20. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  21. (define-module (gnu tests virtualization)
  22. #:use-module (gnu tests)
  23. #:use-module (gnu image)
  24. #:use-module (gnu system)
  25. #:use-module (gnu system file-systems)
  26. #:use-module (gnu system image)
  27. #:use-module (gnu system images hurd)
  28. #:use-module (gnu system vm)
  29. #:use-module (gnu services)
  30. #:use-module (gnu services dbus)
  31. #:use-module (gnu services networking)
  32. #:use-module (gnu services virtualization)
  33. #:use-module (gnu packages virtualization)
  34. #:use-module (gnu packages ssh)
  35. #:use-module (guix gexp)
  36. #:use-module (guix records)
  37. #:use-module (guix store)
  38. #:export (%test-libvirt
  39. %test-childhurd))
  40. ;;;
  41. ;;; Libvirt.
  42. ;;;
  43. (define %libvirt-os
  44. (simple-operating-system
  45. (service dhcp-client-service-type)
  46. (dbus-service)
  47. (polkit-service)
  48. (service libvirt-service-type)))
  49. (define (run-libvirt-test)
  50. "Run tests in %LIBVIRT-OS."
  51. (define os
  52. (marionette-operating-system
  53. %libvirt-os
  54. #:imported-modules '((gnu services herd)
  55. (guix combinators))))
  56. (define vm
  57. (virtual-machine
  58. (operating-system os)
  59. (port-forwardings '())))
  60. (define test
  61. (with-imported-modules '((gnu build marionette))
  62. #~(begin
  63. (use-modules (srfi srfi-11) (srfi srfi-64)
  64. (gnu build marionette))
  65. (define marionette
  66. (make-marionette (list #$vm)))
  67. (mkdir #$output)
  68. (chdir #$output)
  69. (test-begin "libvirt")
  70. (test-assert "service running"
  71. (marionette-eval
  72. '(begin
  73. (use-modules (gnu services herd))
  74. (match (start-service 'libvirtd)
  75. (#f #f)
  76. (('service response-parts ...)
  77. (match (assq-ref response-parts 'running)
  78. ((pid) (number? pid))))))
  79. marionette))
  80. (test-eq "fetch version"
  81. 0
  82. (marionette-eval
  83. `(begin
  84. (chdir "/tmp")
  85. (system* ,(string-append #$libvirt "/bin/virsh")
  86. "-c" "qemu:///system" "version"))
  87. marionette))
  88. (test-eq "connect"
  89. 0
  90. (marionette-eval
  91. `(begin
  92. (chdir "/tmp")
  93. (system* ,(string-append #$libvirt "/bin/virsh")
  94. "-c" "qemu:///system" "connect"))
  95. marionette))
  96. (test-end)
  97. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  98. (gexp->derivation "libvirt-test" test))
  99. (define %test-libvirt
  100. (system-test
  101. (name "libvirt")
  102. (description "Connect to the running LIBVIRT service.")
  103. (value (run-libvirt-test))))
  104. ;;;
  105. ;;; GNU/Hurd virtual machines, aka. childhurds.
  106. ;;;
  107. ;; Copy of `hurd-vm-disk-image', using plain disk-image for test
  108. (define (hurd-vm-disk-image-raw config)
  109. (let ((os ((@@ (gnu services virtualization) secret-service-operating-system)
  110. (hurd-vm-configuration-os config)))
  111. (disk-size (hurd-vm-configuration-disk-size config)))
  112. (system-image
  113. (image
  114. (inherit hurd-disk-image)
  115. (format 'disk-image)
  116. (size disk-size)
  117. (operating-system os)))))
  118. (define %childhurd-os
  119. (simple-operating-system
  120. (service dhcp-client-service-type)
  121. (service hurd-vm-service-type
  122. (hurd-vm-configuration
  123. (image (hurd-vm-disk-image-raw this-record))))))
  124. (define (run-childhurd-test)
  125. (define os
  126. (marionette-operating-system
  127. %childhurd-os
  128. #:imported-modules '((gnu services herd)
  129. (guix combinators))))
  130. (define vm
  131. (virtual-machine
  132. (operating-system os)
  133. (memory-size (* 1024 3))))
  134. (define run-uname-over-ssh
  135. ;; Program that runs 'uname' over SSH and prints the result on standard
  136. ;; output.
  137. (let ()
  138. (define run
  139. (with-extensions (list guile-ssh)
  140. #~(begin
  141. (use-modules (ssh session)
  142. (ssh auth)
  143. (ssh popen)
  144. (ice-9 match)
  145. (ice-9 textual-ports))
  146. (let ((session (make-session #:user "root"
  147. #:port 10022
  148. #:host "localhost"
  149. #:log-verbosity 'rare)))
  150. (match (connect! session)
  151. ('ok
  152. (userauth-password! session "")
  153. (display
  154. (get-string-all
  155. (open-remote-input-pipe* session "uname" "-on"))))
  156. (status
  157. (error "could not connect to childhurd over SSH"
  158. session status)))))))
  159. (program-file "run-uname-over-ssh" run)))
  160. (define test
  161. (with-imported-modules '((gnu build marionette))
  162. #~(begin
  163. (use-modules (gnu build marionette)
  164. (srfi srfi-64)
  165. (ice-9 match))
  166. (define marionette
  167. (make-marionette (list #$vm)))
  168. (mkdir #$output)
  169. (chdir #$output)
  170. (test-begin "childhurd")
  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. (test-equal "childhurd SSH server replies"
  182. "SSH"
  183. ;; Check from within the guest whether its childhurd's SSH
  184. ;; server is reachable. Do that from the guest: port forwarding
  185. ;; to the host won't work because QEMU listens on 127.0.0.1.
  186. (marionette-eval
  187. '(begin
  188. (use-modules (ice-9 match))
  189. (let loop ((n 60))
  190. (if (zero? n)
  191. 'all-attempts-failed
  192. (let ((s (socket PF_INET SOCK_STREAM 0))
  193. (a (make-socket-address AF_INET
  194. INADDR_LOOPBACK
  195. 10022)))
  196. (format #t "connecting to childhurd SSH server...~%")
  197. (connect s a)
  198. (match (get-string-n s 3)
  199. ((? eof-object?)
  200. (close-port s)
  201. (sleep 1)
  202. (loop (- n 1)))
  203. (str
  204. (close-port s)
  205. str))))))
  206. marionette))
  207. (test-equal "SSH up and running"
  208. "childhurd GNU\n"
  209. ;; Connect from the guest to the chidhurd over SSH and run the
  210. ;; 'uname' command.
  211. (marionette-eval
  212. '(begin
  213. (use-modules (ice-9 popen))
  214. (get-string-all
  215. (open-input-pipe #$run-uname-over-ssh)))
  216. marionette))
  217. (test-end)
  218. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  219. (gexp->derivation "childhurd-test" test))
  220. (define %test-childhurd
  221. (system-test
  222. (name "childhurd")
  223. (description
  224. "Connect to the GNU/Hurd virtual machine service, aka. a childhurd, making
  225. sure that the childhurd boots and runs its SSH server.")
  226. (value (run-childhurd-test))))