monitoring.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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
  182. (use-modules (ice-9 popen)
  183. (ice-9 rdelim))
  184. (let* ((port (open-pipe #$%psql-db-zabbix-create-script
  185. OPEN_READ))
  186. (output (read-string port))
  187. (status (close-pipe port)))
  188. output))
  189. marionette))
  190. (test-eq "postgres create zabbix db"
  191. 0
  192. (marionette-eval '(begin (system #$%psql-db-create-zabbix))
  193. marionette))
  194. (test-eq "postgres import zabbix db"
  195. 0
  196. (marionette-eval '(begin (system #$%psql-db-import-zabbix))
  197. marionette))
  198. ;; Wait for zabbix-server to be up and running.
  199. (test-assert "zabbix-server running"
  200. (marionette-eval
  201. '(begin
  202. (use-modules (gnu services herd))
  203. (start-service 'zabbix-server))
  204. marionette))
  205. ;; Make sure the PID file is created.
  206. (test-assert "zabbix-server PID file"
  207. (marionette-eval
  208. '(file-exists? "/var/run/zabbix/zabbix_server.pid")
  209. marionette))
  210. ;; Wait for zabbix-agent to be up and running.
  211. (test-assert "zabbix-agent running"
  212. (marionette-eval
  213. '(begin
  214. (use-modules (gnu services herd))
  215. (start-service 'zabbix-agent))
  216. marionette))
  217. ;; Make sure the PID file is created.
  218. (test-assert "zabbix-agent PID file"
  219. (marionette-eval
  220. '(file-exists? "/var/run/zabbix/zabbix_agent.pid")
  221. marionette))
  222. ;; Wait for php-fpm to be up and running.
  223. (test-assert "php-fpm running"
  224. (marionette-eval
  225. '(begin
  226. (use-modules (gnu services herd))
  227. (start-service 'php-fpm))
  228. marionette))
  229. ;; Wait for nginx to be up and running.
  230. (test-assert "nginx running"
  231. (marionette-eval
  232. '(begin
  233. (use-modules (gnu services herd))
  234. (start-service 'nginx))
  235. marionette))
  236. ;; Make sure the PID file is created.
  237. (test-assert "nginx PID file"
  238. (marionette-eval
  239. '(file-exists? "/var/run/nginx/pid")
  240. marionette))
  241. ;; Make sure we can access pages that correspond to our Zabbix instance.
  242. (letrec-syntax ((test-url
  243. (syntax-rules ()
  244. ((_ path code)
  245. (test-equal (string-append "GET " path)
  246. code
  247. (let-values (((response body)
  248. (http-get (string-append
  249. "http://localhost:8080"
  250. path))))
  251. (response-code response))))
  252. ((_ path)
  253. (test-url path 200)))))
  254. (test-url "/")
  255. (test-url "/does-not-exist" 404))
  256. (test-end))))
  257. (gexp->derivation (string-append name "-test") test))
  258. (define %zabbix-os
  259. ;; Return operating system under test.
  260. (let ((base-os
  261. (simple-operating-system
  262. (service dhcp-client-service-type)
  263. (service postgresql-service-type
  264. (postgresql-configuration
  265. (postgresql postgresql)))
  266. (service zabbix-front-end-service-type
  267. (zabbix-front-end-configuration
  268. (db-password "zabbix")))
  269. (service php-fpm-service-type
  270. (php-fpm-configuration
  271. (timezone "Europe/Paris")))
  272. (service zabbix-server-service-type
  273. (zabbix-server-configuration
  274. (db-password "zabbix")
  275. (log-type "console")))
  276. (service zabbix-agent-service-type))))
  277. (operating-system
  278. (inherit base-os)
  279. (packages (cons* postgresql (operating-system-packages base-os))))))
  280. (define %test-zabbix
  281. (system-test
  282. (name "zabbix")
  283. (description "Connect to a running Zabbix")
  284. (value (run-zabbix-server-test name %zabbix-os))))