virtualization.scm 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017 Christopher Baines <mail@cbaines.net>
  3. ;;; Copyright © 2020, 2021 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. (test-runner-current (system-test-runner #$output))
  68. (test-begin "libvirt")
  69. (test-assert "service running"
  70. (marionette-eval
  71. '(begin
  72. (use-modules (gnu services herd))
  73. (match (start-service 'libvirtd)
  74. (#f #f)
  75. (('service response-parts ...)
  76. (match (assq-ref response-parts 'running)
  77. ((pid) (number? pid))))))
  78. marionette))
  79. (test-eq "fetch version"
  80. 0
  81. (marionette-eval
  82. `(begin
  83. (chdir "/tmp")
  84. (system* ,(string-append #$libvirt "/bin/virsh")
  85. "-c" "qemu:///system" "version"))
  86. marionette))
  87. (test-eq "connect"
  88. 0
  89. (marionette-eval
  90. `(begin
  91. (chdir "/tmp")
  92. (system* ,(string-append #$libvirt "/bin/virsh")
  93. "-c" "qemu:///system" "connect"))
  94. marionette))
  95. (test-end))))
  96. (gexp->derivation "libvirt-test" test))
  97. (define %test-libvirt
  98. (system-test
  99. (name "libvirt")
  100. (description "Connect to the running LIBVIRT service.")
  101. (value (run-libvirt-test))))
  102. ;;;
  103. ;;; GNU/Hurd virtual machines, aka. childhurds.
  104. ;;;
  105. ;; Copy of `hurd-vm-disk-image', using plain disk-image for test
  106. (define (hurd-vm-disk-image-raw config)
  107. (let ((os ((@@ (gnu services virtualization) secret-service-operating-system)
  108. (hurd-vm-configuration-os config)))
  109. (disk-size (hurd-vm-configuration-disk-size config)))
  110. (system-image
  111. (image
  112. (inherit hurd-disk-image)
  113. (format 'disk-image)
  114. (size disk-size)
  115. (operating-system os)))))
  116. (define %childhurd-os
  117. (simple-operating-system
  118. (service dhcp-client-service-type)
  119. (service hurd-vm-service-type
  120. (hurd-vm-configuration
  121. (image (hurd-vm-disk-image-raw this-record))))))
  122. (define (run-childhurd-test)
  123. (define os
  124. (marionette-operating-system
  125. %childhurd-os
  126. #:imported-modules '((gnu services herd)
  127. (guix combinators))))
  128. (define vm
  129. (virtual-machine
  130. (operating-system os)
  131. (memory-size (* 1024 3))))
  132. (define run-uname-over-ssh
  133. ;; Program that runs 'uname' over SSH and prints the result on standard
  134. ;; output.
  135. (let ()
  136. (define run
  137. (with-extensions (list guile-ssh)
  138. #~(begin
  139. (use-modules (ssh session)
  140. (ssh auth)
  141. (ssh popen)
  142. (ice-9 match)
  143. (ice-9 textual-ports))
  144. (let ((session (make-session #:user "root"
  145. #:port 10022
  146. #:host "localhost"
  147. #:log-verbosity 'rare)))
  148. (match (connect! session)
  149. ('ok
  150. (userauth-password! session "")
  151. (display
  152. (get-string-all
  153. (open-remote-input-pipe* session "uname" "-on"))))
  154. (status
  155. (error "could not connect to childhurd over SSH"
  156. session status)))))))
  157. (program-file "run-uname-over-ssh" run)))
  158. (define test
  159. (with-imported-modules '((gnu build marionette))
  160. #~(begin
  161. (use-modules (gnu build marionette)
  162. (srfi srfi-64)
  163. (ice-9 match))
  164. (define marionette
  165. (make-marionette (list #$vm)))
  166. (test-runner-current (system-test-runner #$output))
  167. (test-begin "childhurd")
  168. (test-assert "service running"
  169. (marionette-eval
  170. '(begin
  171. (use-modules (gnu services herd))
  172. (match (start-service 'childhurd)
  173. (#f #f)
  174. (('service response-parts ...)
  175. (match (assq-ref response-parts 'running)
  176. ((pid) (number? pid))))))
  177. marionette))
  178. (test-equal "childhurd SSH server replies"
  179. "SSH"
  180. ;; Check from within the guest whether its childhurd's SSH
  181. ;; server is reachable. Do that from the guest: port forwarding
  182. ;; to the host won't work because QEMU listens on 127.0.0.1.
  183. (marionette-eval
  184. '(begin
  185. (use-modules (ice-9 match))
  186. (let loop ((n 60))
  187. (if (zero? n)
  188. 'all-attempts-failed
  189. (let ((s (socket PF_INET SOCK_STREAM 0))
  190. (a (make-socket-address AF_INET
  191. INADDR_LOOPBACK
  192. 10022)))
  193. (format #t "connecting to childhurd SSH server...~%")
  194. (connect s a)
  195. (match (get-string-n s 3)
  196. ((? eof-object?)
  197. (close-port s)
  198. (sleep 1)
  199. (loop (- n 1)))
  200. (str
  201. (close-port s)
  202. str))))))
  203. marionette))
  204. (test-equal "SSH up and running"
  205. "childhurd GNU\n"
  206. ;; Connect from the guest to the chidhurd over SSH and run the
  207. ;; 'uname' command.
  208. (marionette-eval
  209. '(begin
  210. (use-modules (ice-9 popen))
  211. (get-string-all
  212. (open-input-pipe #$run-uname-over-ssh)))
  213. marionette))
  214. (test-end))))
  215. (gexp->derivation "childhurd-test" test))
  216. (define %test-childhurd
  217. (system-test
  218. (name "childhurd")
  219. (description
  220. "Connect to the GNU/Hurd virtual machine service, aka. a childhurd, making
  221. sure that the childhurd boots and runs its SSH server.")
  222. (value (run-childhurd-test))))