monitoring.scm 11 KB

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