ganeti.scm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2020, 2021 Marius Bakke <marius@gnu.org>
  3. ;;; Copyright © 2020 Brice Waegeneire <brice@waegenei.re>
  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. (define-module (gnu tests ganeti)
  20. #:use-module (gnu)
  21. #:use-module (gnu tests)
  22. #:use-module (gnu system vm)
  23. #:use-module (gnu services)
  24. #:use-module (gnu services ganeti)
  25. #:use-module (gnu services networking)
  26. #:use-module (gnu services ssh)
  27. #:use-module (gnu packages virtualization)
  28. #:use-module (guix gexp)
  29. #:use-module (ice-9 format)
  30. #:export (%test-ganeti-kvm %test-ganeti-lxc))
  31. (define %ganeti-os
  32. (operating-system
  33. (host-name "gnt1")
  34. (timezone "Etc/UTC")
  35. (locale "en_US.UTF-8")
  36. (bootloader (bootloader-configuration
  37. (bootloader grub-bootloader)
  38. (targets '("/dev/vda"))))
  39. (file-systems (cons (file-system
  40. (device (file-system-label "my-root"))
  41. (mount-point "/")
  42. (type "ext4"))
  43. %base-file-systems))
  44. (firmware '())
  45. ;; The hosts file must contain a nonlocal IP for host-name.
  46. ;; In addition, the cluster name must resolve to an IP address that
  47. ;; is not currently provisioned.
  48. (hosts-file (plain-file "hosts" (format #f "
  49. 127.0.0.1 localhost
  50. ::1 localhost
  51. 10.0.2.15 gnt1.example.com gnt1
  52. 192.168.254.254 ganeti.example.com
  53. ")))
  54. (packages (append (list ganeti-instance-debootstrap ganeti-instance-guix)
  55. %base-packages))
  56. (services
  57. (append (list (service static-networking-service-type
  58. (list %qemu-static-networking))
  59. (service openssh-service-type
  60. (openssh-configuration
  61. (permit-root-login 'prohibit-password)))
  62. (service ganeti-service-type
  63. (ganeti-configuration
  64. (file-storage-paths '("/srv/ganeti/file-storage"))
  65. (rapi-configuration
  66. (ganeti-rapi-configuration
  67. ;; Disable TLS so we can test the RAPI without
  68. ;; pulling in GnuTLS.
  69. (ssl? #f)))
  70. (os %default-ganeti-os))))
  71. %base-services))))
  72. (define* (run-ganeti-test hypervisor #:key
  73. (master-netdev "eth0")
  74. (hvparams '())
  75. (extra-packages '())
  76. (rapi-port 5080))
  77. "Run tests in %GANETI-OS."
  78. (define os
  79. (marionette-operating-system
  80. (operating-system
  81. (inherit %ganeti-os)
  82. (packages (append extra-packages
  83. (operating-system-packages %ganeti-os))))
  84. #:imported-modules '((gnu services herd)
  85. (guix combinators))))
  86. (define %forwarded-rapi-port 5080)
  87. (define vm
  88. (virtual-machine
  89. (operating-system os)
  90. ;; Some of the daemons are fairly memory-hungry.
  91. (memory-size 512)
  92. ;; Forward HTTP ports so we can access them from the "outside".
  93. (port-forwardings `((,%forwarded-rapi-port . ,rapi-port)))))
  94. (define test
  95. (with-imported-modules '((gnu build marionette))
  96. #~(begin
  97. (use-modules (srfi srfi-11) (srfi srfi-64)
  98. (web uri) (web client) (web response)
  99. (ice-9 iconv)
  100. (gnu build marionette))
  101. (define marionette
  102. (make-marionette (list #$vm)))
  103. (test-runner-current (system-test-runner #$output))
  104. (test-begin "ganeti")
  105. ;; Ganeti uses the Shepherd to start/stop daemons, so make sure
  106. ;; it is ready before we begin. It takes a while because all
  107. ;; Ganeti daemons fail to start initially.
  108. (test-assert "shepherd is ready"
  109. (wait-for-unix-socket "/var/run/shepherd/socket" marionette))
  110. (test-eq "gnt-cluster init"
  111. 0
  112. (marionette-eval
  113. '(begin
  114. (setenv
  115. "PATH"
  116. ;; Init needs to run 'ssh-keygen', 'ip', etc.
  117. "/run/current-system/profile/sbin:/run/current-system/profile/bin")
  118. (system* #$(file-append ganeti "/sbin/gnt-cluster") "init"
  119. (string-append "--master-netdev=" #$master-netdev)
  120. ;; TODO: Enable more disk backends.
  121. "--enabled-disk-templates=file"
  122. (string-append "--enabled-hypervisors="
  123. #$hypervisor)
  124. (string-append "--hypervisor-parameters="
  125. #$hypervisor ":"
  126. (string-join '#$hvparams "\n"))
  127. ;; Set the default NIC mode to 'routed' to avoid having to
  128. ;; configure a full bridge to placate 'gnt-cluster verify'.
  129. "--nic-parameters=mode=routed,link=eth0"
  130. "ganeti.example.com"))
  131. marionette))
  132. ;; Disable the watcher while doing daemon tests to prevent interference.
  133. (test-eq "watcher pause"
  134. 0
  135. (marionette-eval
  136. '(begin
  137. (system* #$(file-append ganeti "/sbin/gnt-cluster")
  138. "watcher" "pause" "1h"))
  139. marionette))
  140. (test-assert "force-start wconfd"
  141. ;; Check that the 'force-start' Shepherd action works, used in a
  142. ;; master-failover scenario.
  143. (marionette-eval
  144. '(begin
  145. (setenv "PATH" "/run/current-system/profile/bin")
  146. (invoke "herd" "stop" "ganeti-wconfd")
  147. (invoke "herd" "disable" "ganeti-wconfd")
  148. (invoke "herd" "force-start" "ganeti-wconfd"))
  149. marionette))
  150. ;; Verify that the cluster is healthy.
  151. (test-eq "gnt-cluster verify 1"
  152. 0
  153. (marionette-eval
  154. '(begin
  155. (system* #$(file-append ganeti "/sbin/gnt-cluster") "verify"))
  156. marionette))
  157. ;; Try stopping and starting daemons with daemon-util like
  158. ;; 'gnt-node add', 'gnt-cluster init', etc.
  159. (test-eq "daemon-util stop-all"
  160. 0
  161. (marionette-eval
  162. '(begin
  163. (system* #$(file-append ganeti "/lib/ganeti/daemon-util")
  164. "stop-all"))
  165. marionette))
  166. (test-eq "daemon-util start-all"
  167. 0
  168. (marionette-eval
  169. '(begin
  170. (system* #$(file-append ganeti "/lib/ganeti/daemon-util")
  171. "start-all"))
  172. marionette))
  173. ;; Check that the cluster is still healthy after the daemon restarts.
  174. (test-eq "gnt-cluster verify 2"
  175. 0
  176. (marionette-eval
  177. '(begin
  178. (system* #$(file-append ganeti "/sbin/gnt-cluster") "verify"))
  179. marionette))
  180. (test-eq "watcher continue"
  181. 0
  182. (marionette-eval
  183. '(begin
  184. (system* #$(file-append ganeti "/sbin/gnt-cluster")
  185. "watcher" "continue"))
  186. marionette))
  187. ;; Try accessing the RAPI.
  188. (test-equal "http-get RAPI version"
  189. '(200 "2\n")
  190. (let-values
  191. (((response text)
  192. (http-get #$(simple-format
  193. #f "http://localhost:~A/version"
  194. %forwarded-rapi-port)
  195. #:decode-body? #f)))
  196. (list (response-code response)
  197. ;; The API response lacks a content-type, so
  198. ;; (http-client) won't decode it for us.
  199. (bytevector->string text "UTF-8"))))
  200. (test-equal "gnt-os list"
  201. "debootstrap+default\nguix+default\n"
  202. (marionette-eval
  203. '(begin
  204. (use-modules (ice-9 popen))
  205. (let* ((port (open-pipe*
  206. OPEN_READ
  207. #$(file-append ganeti "/sbin/gnt-os")
  208. "list" "--no-headers"))
  209. (output (get-string-all port)))
  210. (close-pipe port)
  211. output))
  212. marionette))
  213. (test-eq "gnt-cluster destroy"
  214. 0
  215. (marionette-eval
  216. '(begin
  217. (system* #$(file-append ganeti "/sbin/gnt-cluster")
  218. "destroy" "--yes-do-it"))
  219. marionette))
  220. (test-end))))
  221. (gexp->derivation (string-append "ganeti-" hypervisor "-test") test))
  222. (define %test-ganeti-kvm
  223. (system-test
  224. (name "ganeti-kvm")
  225. (description "Provision a Ganeti cluster using the KVM hypervisor.")
  226. (value (run-ganeti-test "kvm"
  227. ;; Set kernel_path to an empty string to prevent
  228. ;; 'gnt-cluster verify' from testing for its presence.
  229. #:hvparams '("kernel_path=")
  230. #:extra-packages (list qemu)))))
  231. (define %test-ganeti-lxc
  232. (system-test
  233. (name "ganeti-lxc")
  234. (description "Provision a Ganeti cluster using LXC as the hypervisor.")
  235. (value (run-ganeti-test "lxc"
  236. #:extra-packages (list lxc)))))