ganeti.scm 11 KB

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