monitoring.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2018 Gábor Boskovits <boskovits@gmail.com>
  3. ;;; Copyright © 2018, 2019 Oleg Pykhalov <go.wigust@gmail.com>
  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 monitoring)
  20. #:use-module (gnu packages databases)
  21. #:use-module (gnu packages monitoring)
  22. #:use-module (gnu packages php)
  23. #:use-module (gnu services)
  24. #:use-module (gnu services monitoring)
  25. #:use-module (gnu services networking)
  26. #:use-module (gnu services databases)
  27. #:use-module (gnu services shepherd)
  28. #:use-module (gnu services web)
  29. #:use-module (gnu system vm)
  30. #:use-module (gnu system)
  31. #:use-module (gnu tests)
  32. #:use-module (guix gexp)
  33. #:export (%test-prometheus-node-exporter
  34. %test-zabbix))
  35. ;;;
  36. ;;; Prometheus Node Exporter
  37. ;;;
  38. (define* (run-prometheus-node-exporter-server-test name test-os)
  39. "Run tests in %PROMETHEUS-NODE-EXPORTER-OS, which has prometheus-node-exporter running."
  40. (define os
  41. (marionette-operating-system
  42. test-os
  43. #:imported-modules '((gnu services herd))))
  44. (define vm
  45. (virtual-machine
  46. (operating-system os)
  47. (port-forwardings '((8080 . 9100)))))
  48. (define test
  49. (with-imported-modules '((gnu build marionette))
  50. #~(begin
  51. (use-modules (srfi srfi-11)
  52. (srfi srfi-64)
  53. (gnu build marionette)
  54. (web client)
  55. (web response))
  56. (define marionette
  57. (make-marionette (list #$vm)))
  58. (mkdir #$output)
  59. (chdir #$output)
  60. (test-begin #$name)
  61. (test-assert "prometheus-node-exporter running"
  62. (marionette-eval
  63. '(begin
  64. (use-modules (gnu services herd))
  65. (match (start-service 'prometheus-node-exporter)
  66. (#f #f)
  67. (('service response-parts ...)
  68. (match (assq-ref response-parts 'running)
  69. ((pid) (number? pid))))))
  70. marionette))
  71. (test-equal "http-get"
  72. 200
  73. (begin
  74. (wait-for-tcp-port 9100 marionette)
  75. (let-values (((response text)
  76. (http-get "http://localhost:8080")))
  77. (response-code response))))
  78. (test-end)
  79. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  80. (gexp->derivation (string-append name "-test") test))
  81. (define %prometheus-node-exporter-os
  82. (simple-operating-system
  83. (service dhcp-client-service-type)
  84. (service prometheus-node-exporter-service-type
  85. (prometheus-node-exporter-configuration))))
  86. (define %test-prometheus-node-exporter
  87. (system-test
  88. (name "prometheus-node-exporter")
  89. (description "Connect to a running prometheus-node-exporter server.")
  90. (value (run-prometheus-node-exporter-server-test
  91. name %prometheus-node-exporter-os))))
  92. ;;;
  93. ;;; Zabbix
  94. ;;;
  95. (define %psql-user-create-zabbix
  96. "\
  97. sudo -u postgres psql <<< \"create user zabbix password 'zabbix';\"
  98. ")
  99. (define %psql-db-zabbix-create-script
  100. "\
  101. sudo -u postgres psql --no-align <<< \\\\du
  102. ")
  103. (define %psql-db-create-zabbix
  104. "\
  105. sudo -u postgres createdb -O zabbix -E Unicode -T template0 zabbix
  106. ")
  107. (define %psql-db-import-zabbix
  108. #~(format #f "\
  109. cat ~a | sudo -u zabbix psql zabbix;
  110. cat ~a | sudo -u zabbix psql zabbix;
  111. cat ~a | sudo -u zabbix psql zabbix;
  112. "
  113. (string-append #$zabbix-server:schema
  114. "/database/postgresql/schema.sql")
  115. (string-append #$zabbix-server:schema
  116. "/database/postgresql/images.sql")
  117. (string-append #$zabbix-server:schema
  118. "/database/postgresql/data.sql")))
  119. (define* (run-zabbix-server-test name test-os)
  120. "Run tests in %ZABBIX-OS, which has zabbix running."
  121. (define os
  122. (marionette-operating-system
  123. test-os
  124. #:imported-modules '((gnu services herd))))
  125. (define vm
  126. (virtual-machine
  127. (operating-system os)
  128. (port-forwardings '((8080 . 80)))
  129. (memory-size 1024)))
  130. (define test
  131. (with-imported-modules '((gnu build marionette))
  132. #~(begin
  133. (use-modules (srfi srfi-11)
  134. (srfi srfi-64)
  135. (gnu build marionette)
  136. (web client)
  137. (web response)
  138. (ice-9 popen)
  139. (ice-9 rdelim))
  140. (define marionette
  141. (make-marionette (list #$vm)))
  142. (mkdir #$output)
  143. (chdir #$output)
  144. (test-begin #$name)
  145. ;; XXX: Shepherd reads the config file *before* binding its control
  146. ;; socket, so /var/run/shepherd/socket might not exist yet when the
  147. ;; 'marionette' service is started.
  148. (test-assert "shepherd socket ready"
  149. (marionette-eval
  150. `(begin
  151. (use-modules (gnu services herd))
  152. (let loop ((i 10))
  153. (cond ((file-exists? (%shepherd-socket-file))
  154. #t)
  155. ((> i 0)
  156. (sleep 1)
  157. (loop (- i 1)))
  158. (else
  159. 'failure))))
  160. marionette))
  161. (test-assert "postgres service running"
  162. (marionette-eval
  163. '(begin
  164. (use-modules (gnu services herd))
  165. (start-service 'postgres))
  166. marionette))
  167. (test-eq "postgres create zabbix user"
  168. 0
  169. (marionette-eval '(begin (system #$%psql-user-create-zabbix))
  170. marionette))
  171. (test-equal "postgres find zabbix user"
  172. "List of roles
  173. Role name|Attributes|Member of
  174. postgres|Superuser, Create role, Create DB, Replication, Bypass RLS|{}
  175. zabbix||{}
  176. "
  177. (marionette-eval
  178. '(begin (let* ((port (open-pipe #$%psql-db-zabbix-create-script
  179. OPEN_READ))
  180. (output (read-string port))
  181. (status (close-pipe port)))
  182. output))
  183. marionette))
  184. (test-eq "postgres create zabbix db"
  185. 0
  186. (marionette-eval '(begin (system #$%psql-db-create-zabbix))
  187. marionette))
  188. (test-eq "postgres import zabbix db"
  189. 0
  190. (marionette-eval '(begin (system #$%psql-db-import-zabbix))
  191. marionette))
  192. ;; Wait for zabbix-server to be up and running.
  193. (test-assert "zabbix-server running"
  194. (marionette-eval
  195. '(begin
  196. (use-modules (gnu services herd))
  197. (start-service 'zabbix-server))
  198. marionette))
  199. ;; Make sure the PID file is created.
  200. (test-assert "zabbix-server PID file"
  201. (marionette-eval
  202. '(file-exists? "/var/run/zabbix/zabbix_server.pid")
  203. marionette))
  204. ;; Wait for zabbix-agent to be up and running.
  205. (test-assert "zabbix-agent running"
  206. (marionette-eval
  207. '(begin
  208. (use-modules (gnu services herd))
  209. (start-service 'zabbix-agent))
  210. marionette))
  211. ;; Make sure the PID file is created.
  212. (test-assert "zabbix-agent PID file"
  213. (marionette-eval
  214. '(file-exists? "/var/run/zabbix/zabbix_agent.pid")
  215. marionette))
  216. ;; Wait for php-fpm to be up and running.
  217. (test-assert "php-fpm running"
  218. (marionette-eval
  219. '(begin
  220. (use-modules (gnu services herd))
  221. (start-service 'php-fpm))
  222. marionette))
  223. ;; Wait for nginx to be up and running.
  224. (test-assert "nginx running"
  225. (marionette-eval
  226. '(begin
  227. (use-modules (gnu services herd))
  228. (start-service 'nginx))
  229. marionette))
  230. ;; Make sure the PID file is created.
  231. (test-assert "nginx PID file"
  232. (marionette-eval
  233. '(file-exists? "/var/run/nginx/pid")
  234. marionette))
  235. ;; Make sure we can access pages that correspond to our repository.
  236. (letrec-syntax ((test-url
  237. (syntax-rules ()
  238. ((_ path code)
  239. (test-equal (string-append "GET " path)
  240. code
  241. (let-values (((response body)
  242. (http-get (string-append
  243. "http://localhost:8080"
  244. path))))
  245. (response-code response))))
  246. ((_ path)
  247. (test-url path 200)))))
  248. (test-url "/")
  249. (test-url "/does-not-exist" 404))
  250. (test-end)
  251. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  252. (gexp->derivation (string-append name "-test") test))
  253. (define %zabbix-os
  254. ;; Return operating system under test.
  255. (let ((base-os
  256. (simple-operating-system
  257. (service dhcp-client-service-type)
  258. (postgresql-service)
  259. (service zabbix-front-end-service-type
  260. (zabbix-front-end-configuration
  261. (db-password "zabbix")))
  262. (service php-fpm-service-type
  263. (php-fpm-configuration
  264. (timezone "Europe/Paris")))
  265. (service zabbix-server-service-type
  266. (zabbix-server-configuration
  267. (db-password "zabbix")
  268. (log-type "console")))
  269. (service zabbix-agent-service-type))))
  270. (operating-system
  271. (inherit base-os)
  272. (packages (cons* postgresql (operating-system-packages base-os))))))
  273. (define %test-zabbix
  274. (system-test
  275. (name "zabbix")
  276. (description "Connect to a running Zabbix")
  277. (value (run-zabbix-server-test name %zabbix-os))))