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)))
71 (test-assert "prometheus-node-exporter running"
74 (use-modules (gnu services herd))
75 (match (start-service 'prometheus-node-exporter)
77 (('service response-parts ...)
78 (match (assq-ref response-parts 'running)
79 ((pid) (number? pid))))))
82 (test-equal "http-get"
85 (wait-for-tcp-port 9100 marionette)
86 (let-values (((response text)
87 (http-get "http://localhost:8080")))
88 (response-code response))))
91 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
93 (gexp->derivation (string-append name "-test") test))
95 (define %prometheus-node-exporter-os
96 (simple-operating-system
97 (service dhcp-client-service-type)
98 (service prometheus-node-exporter-service-type
99 (prometheus-node-exporter-configuration))))
101 (define %test-prometheus-node-exporter
103 (name "prometheus-node-exporter")
104 (description "Connect to a running prometheus-node-exporter server.")
105 (value (run-prometheus-node-exporter-server-test
106 name %prometheus-node-exporter-os))))
113 (define %psql-user-create-zabbix
115 sudo -u postgres psql <<< \"create user zabbix password 'zabbix';\"
118 (define %psql-db-zabbix-create-script
120 sudo -u postgres psql --no-align <<< \\\\du
123 (define %psql-db-create-zabbix
125 sudo -u postgres createdb -O zabbix -E Unicode -T template0 zabbix
128 (define %psql-db-import-zabbix
130 cat ~a | sudo -u zabbix psql zabbix;
131 cat ~a | sudo -u zabbix psql zabbix;
132 cat ~a | sudo -u zabbix psql zabbix;
134 (string-append #$zabbix-server:schema
135 "/database/postgresql/schema.sql")
136 (string-append #$zabbix-server:schema
137 "/database/postgresql/images.sql")
138 (string-append #$zabbix-server:schema
139 "/database/postgresql/data.sql")))
141 (define* (run-zabbix-server-test name test-os)
142 "Run tests in %ZABBIX-OS, which has zabbix running."
144 (marionette-operating-system
146 #:imported-modules '((gnu services herd))))
150 (operating-system os)
151 (port-forwardings '((8080 . 80)))
155 (with-imported-modules '((gnu build marionette))
157 (use-modules (srfi srfi-11)
159 (gnu build marionette)
166 (make-marionette (list #$vm)))
173 ;; XXX: Shepherd reads the config file *before* binding its control
174 ;; socket, so /var/run/shepherd/socket might not exist yet when the
175 ;; 'marionette' service is started.
176 (test-assert "shepherd socket ready"
179 (use-modules (gnu services herd))
181 (cond ((file-exists? (%shepherd-socket-file))
190 (test-assert "postgres service running"
193 (use-modules (gnu services herd))
194 (start-service 'postgres))
197 ;; Add /run/setuid-programs to $PATH so that the scripts passed to
198 ;; 'system' can find 'sudo'.
201 "/run/setuid-programs:/run/current-system/profile/bin")
204 (test-eq "postgres create zabbix user"
206 (marionette-eval '(begin (system #$%psql-user-create-zabbix))
209 (test-equal "postgres find zabbix user"
211 Role name|Attributes|Member of
212 postgres|Superuser, Create role, Create DB, Replication, Bypass RLS|{}
216 '(begin (let* ((port (open-pipe #$%psql-db-zabbix-create-script
218 (output (read-string port))
219 (status (close-pipe port)))
223 (test-eq "postgres create zabbix db"
225 (marionette-eval '(begin (system #$%psql-db-create-zabbix))
228 (test-eq "postgres import zabbix db"
230 (marionette-eval '(begin (system #$%psql-db-import-zabbix))
233 ;; Wait for zabbix-server to be up and running.
234 (test-assert "zabbix-server running"
237 (use-modules (gnu services herd))
238 (start-service 'zabbix-server))
241 ;; Make sure the PID file is created.
242 (test-assert "zabbix-server PID file"
244 '(file-exists? "/var/run/zabbix/zabbix_server.pid")
247 ;; Wait for zabbix-agent to be up and running.
248 (test-assert "zabbix-agent running"
251 (use-modules (gnu services herd))
252 (start-service 'zabbix-agent))
255 ;; Make sure the PID file is created.
256 (test-assert "zabbix-agent PID file"
258 '(file-exists? "/var/run/zabbix/zabbix_agent.pid")
261 ;; Wait for php-fpm to be up and running.
262 (test-assert "php-fpm running"
265 (use-modules (gnu services herd))
266 (start-service 'php-fpm))
269 ;; Wait for nginx to be up and running.
270 (test-assert "nginx running"
273 (use-modules (gnu services herd))
274 (start-service 'nginx))
277 ;; Make sure the PID file is created.
278 (test-assert "nginx PID file"
280 '(file-exists? "/var/run/nginx/pid")
283 ;; Make sure we can access pages that correspond to our Zabbix instance.
284 (letrec-syntax ((test-url
287 (test-equal (string-append "GET " path)
289 (let-values (((response body)
290 (http-get (string-append
291 "http://localhost:8080"
293 (response-code response))))
295 (test-url path 200)))))
297 (test-url "/does-not-exist" 404))
301 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
303 (gexp->derivation (string-append name "-test") test))
306 ;; Return operating system under test.
308 (simple-operating-system
309 (service dhcp-client-service-type)
310 (service postgresql-service-type
311 (postgresql-configuration
312 (postgresql postgresql-10)))
313 (service zabbix-front-end-service-type
314 (zabbix-front-end-configuration
315 (db-password "zabbix")))
317 (service php-fpm-service-type
318 (php-fpm-configuration
319 (timezone "Europe/Paris")))
321 (service zabbix-server-service-type
322 (zabbix-server-configuration
323 (db-password "zabbix")
324 (log-type "console")))
326 (service zabbix-agent-service-type))))
329 (packages (cons* postgresql (operating-system-packages base-os))))))
334 (description "Connect to a running Zabbix")
335 (value (run-zabbix-server-test name %zabbix-os))))