ganeti.scm 10 KB

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