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>
5 ;;; This file is part of GNU Guix.
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.
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.
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/>.
20 (define-module (gnu tests monitoring)
21 #:use-module (gnu packages databases)
22 #:use-module (gnu packages monitoring)
23 #:use-module (gnu packages php)
24 #:use-module (gnu services)
25 #:use-module (gnu services monitoring)
26 #:use-module (gnu services networking)
27 #:use-module (gnu services databases)
28 #:use-module (gnu services shepherd)
29 #:use-module (gnu services web)
30 #:use-module (gnu system vm)
31 #:use-module (gnu system)
32 #:use-module (gnu tests)
33 #:use-module (guix gexp)
34 #:export (%test-prometheus-node-exporter
39 ;;; Prometheus Node Exporter
42 (define* (run-prometheus-node-exporter-server-test name test-os)
43 "Run tests in %PROMETHEUS-NODE-EXPORTER-OS, which has prometheus-node-exporter running."
45 (marionette-operating-system
47 #:imported-modules '((gnu services herd))))
52 (port-forwardings '((8080 . 9100)))))
55 (with-imported-modules '((gnu build marionette))
57 (use-modules (srfi srfi-11)
59 (gnu build marionette)
64 (make-marionette (list #$vm)))
66 (test-runner-current (system-test-runner #$output))
69 (test-assert "prometheus-node-exporter running"
72 (use-modules (gnu services herd))
73 (match (start-service 'prometheus-node-exporter)
75 (('service response-parts ...)
76 (match (assq-ref response-parts 'running)
77 ((pid) (number? pid))))))
80 (test-equal "http-get"
83 (wait-for-tcp-port 9100 marionette)
84 (let-values (((response text)
85 (http-get "http://localhost:8080")))
86 (response-code response))))
90 (gexp->derivation (string-append name "-test") test))
92 (define %prometheus-node-exporter-os
93 (simple-operating-system
94 (service dhcp-client-service-type)
95 (service prometheus-node-exporter-service-type
96 (prometheus-node-exporter-configuration))))
98 (define %test-prometheus-node-exporter
100 (name "prometheus-node-exporter")
101 (description "Connect to a running prometheus-node-exporter server.")
102 (value (run-prometheus-node-exporter-server-test
103 name %prometheus-node-exporter-os))))
110 (define %psql-user-create-zabbix
112 sudo -u postgres psql <<< \"create user zabbix password 'zabbix';\"
115 (define %psql-db-zabbix-create-script
117 sudo -u postgres psql --no-align <<< \\\\du
120 (define %psql-db-create-zabbix
122 sudo -u postgres createdb -O zabbix -E Unicode -T template0 zabbix
125 (define %psql-db-import-zabbix
127 cat ~a | sudo -u zabbix psql zabbix;
128 cat ~a | sudo -u zabbix psql zabbix;
129 cat ~a | sudo -u zabbix psql zabbix;
131 (string-append #$zabbix-server:schema
132 "/database/postgresql/schema.sql")
133 (string-append #$zabbix-server:schema
134 "/database/postgresql/images.sql")
135 (string-append #$zabbix-server:schema
136 "/database/postgresql/data.sql")))
138 (define* (run-zabbix-server-test name test-os)
139 "Run tests in %ZABBIX-OS, which has zabbix running."
141 (marionette-operating-system
143 #:imported-modules '((gnu services herd))))
147 (operating-system os)
148 (port-forwardings '((8080 . 80)))
152 (with-imported-modules '((gnu build marionette))
154 (use-modules (srfi srfi-11)
156 (gnu build marionette)
163 (make-marionette (list #$vm)))
165 (test-runner-current (system-test-runner #$output))
168 ;; XXX: Shepherd reads the config file *before* binding its control
169 ;; socket, so /var/run/shepherd/socket might not exist yet when the
170 ;; 'marionette' service is started.
171 (test-assert "shepherd socket ready"
174 (use-modules (gnu services herd))
176 (cond ((file-exists? (%shepherd-socket-file))
185 (test-assert "postgres service running"
188 (use-modules (gnu services herd))
189 (start-service 'postgres))
192 ;; Add /run/setuid-programs to $PATH so that the scripts passed to
193 ;; 'system' can find 'sudo'.
196 "/run/setuid-programs:/run/current-system/profile/bin")
199 (test-eq "postgres create zabbix user"
201 (marionette-eval '(begin (system #$%psql-user-create-zabbix))
204 (test-equal "postgres find zabbix user"
206 Role name|Attributes|Member of
207 postgres|Superuser, Create role, Create DB, Replication, Bypass RLS|{}
211 '(begin (let* ((port (open-pipe #$%psql-db-zabbix-create-script
213 (output (read-string port))
214 (status (close-pipe port)))
218 (test-eq "postgres create zabbix db"
220 (marionette-eval '(begin (system #$%psql-db-create-zabbix))
223 (test-eq "postgres import zabbix db"
225 (marionette-eval '(begin (system #$%psql-db-import-zabbix))
228 ;; Wait for zabbix-server to be up and running.
229 (test-assert "zabbix-server running"
232 (use-modules (gnu services herd))
233 (start-service 'zabbix-server))
236 ;; Make sure the PID file is created.
237 (test-assert "zabbix-server PID file"
239 '(file-exists? "/var/run/zabbix/zabbix_server.pid")
242 ;; Wait for zabbix-agent to be up and running.
243 (test-assert "zabbix-agent running"
246 (use-modules (gnu services herd))
247 (start-service 'zabbix-agent))
250 ;; Make sure the PID file is created.
251 (test-assert "zabbix-agent PID file"
253 '(file-exists? "/var/run/zabbix/zabbix_agent.pid")
256 ;; Wait for php-fpm to be up and running.
257 (test-assert "php-fpm running"
260 (use-modules (gnu services herd))
261 (start-service 'php-fpm))
264 ;; Wait for nginx to be up and running.
265 (test-assert "nginx running"
268 (use-modules (gnu services herd))
269 (start-service 'nginx))
272 ;; Make sure the PID file is created.
273 (test-assert "nginx PID file"
275 '(file-exists? "/var/run/nginx/pid")
278 ;; Make sure we can access pages that correspond to our Zabbix instance.
279 (letrec-syntax ((test-url
282 (test-equal (string-append "GET " path)
284 (let-values (((response body)
285 (http-get (string-append
286 "http://localhost:8080"
288 (response-code response))))
290 (test-url path 200)))))
292 (test-url "/does-not-exist" 404))
296 (gexp->derivation (string-append name "-test") test))
299 ;; Return operating system under test.
301 (simple-operating-system
302 (service dhcp-client-service-type)
303 (service postgresql-service-type
304 (postgresql-configuration
305 (postgresql postgresql)))
306 (service zabbix-front-end-service-type
307 (zabbix-front-end-configuration
308 (db-password "zabbix")))
310 (service php-fpm-service-type
311 (php-fpm-configuration
312 (timezone "Europe/Paris")))
314 (service zabbix-server-service-type
315 (zabbix-server-configuration
316 (db-password "zabbix")
317 (log-type "console")))
319 (service zabbix-agent-service-type))))
322 (packages (cons* postgresql (operating-system-packages base-os))))))
327 (description "Connect to a running Zabbix")
328 (value (run-zabbix-server-test name %zabbix-os))))