ganeti.scm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2020 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.2 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 (static-networking-service "eth0" "10.0.2.2"
  58. #:netmask "255.255.255.0"
  59. #:gateway "10.0.2.1"
  60. #:name-servers '("10.0.2.1"))
  61. (service openssh-service-type
  62. (openssh-configuration
  63. (permit-root-login 'prohibit-password)))
  64. (service ganeti-service-type
  65. (ganeti-configuration
  66. (file-storage-paths '("/srv/ganeti/file-storage"))
  67. (rapi-configuration
  68. (ganeti-rapi-configuration
  69. ;; Disable TLS so we can test the RAPI without
  70. ;; pulling in GnuTLS.
  71. (ssl? #f)))
  72. (os %default-ganeti-os))))
  73. %base-services))))
  74. (define* (run-ganeti-test hypervisor #:key
  75. (master-netdev "eth0")
  76. (hvparams '())
  77. (extra-packages '())
  78. (rapi-port 5080)
  79. (noded-port 1811))
  80. "Run tests in %GANETI-OS."
  81. (define os
  82. (marionette-operating-system
  83. (operating-system
  84. (inherit %ganeti-os)
  85. (packages (append extra-packages
  86. (operating-system-packages %ganeti-os))))
  87. #:imported-modules '((gnu services herd)
  88. (guix combinators))))
  89. (define %forwarded-rapi-port 5080)
  90. (define %forwarded-noded-port 1811)
  91. (define vm
  92. (virtual-machine
  93. (operating-system os)
  94. ;; Some of the daemons are fairly memory-hungry.
  95. (memory-size 512)
  96. ;; Forward HTTP ports so we can access them from the "outside".
  97. (port-forwardings `((,%forwarded-rapi-port . ,rapi-port)
  98. (,%forwarded-noded-port . ,noded-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. (gnu build marionette))
  105. (define marionette
  106. (make-marionette (list #$vm)))
  107. (mkdir #$output)
  108. (chdir #$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. This causes an expected failure:
  193. ;; https://github.com/ganeti/ganeti/issues/1502
  194. ;; Run it anyway for easy testing of potential fixes.
  195. (test-equal "http-get RAPI version"
  196. '(200 "2")
  197. (let-values
  198. (((response text)
  199. (http-get #$(simple-format
  200. #f "http://localhost:~A/version"
  201. %forwarded-rapi-port)
  202. #:decode-body? #t)))
  203. (list (response-code response) text)))
  204. (test-equal "gnt-os list"
  205. "debootstrap+default\nguix+default\n"
  206. (marionette-eval
  207. '(begin
  208. (use-modules (ice-9 popen))
  209. (let* ((port (open-pipe*
  210. OPEN_READ
  211. #$(file-append ganeti "/sbin/gnt-os")
  212. "list" "--no-headers"))
  213. (output (get-string-all port)))
  214. (close-pipe port)
  215. output))
  216. marionette))
  217. (test-eq "gnt-cluster destroy"
  218. 0
  219. (marionette-eval
  220. '(begin
  221. (system* #$(file-append ganeti "/sbin/gnt-cluster")
  222. "destroy" "--yes-do-it"))
  223. marionette))
  224. (test-end)
  225. (exit (= (test-runner-fail-count (test-runner-current)) 1)))))
  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)))))