Merge branch 'master' into staging
[jackhill/guix/guix.git] / gnu / tests / monitoring.scm
CommitLineData
a33652ee
GB
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2018 Gábor Boskovits <boskovits@gmail.com>
8b6c91ba 3;;; Copyright © 2018, 2019 Oleg Pykhalov <go.wigust@gmail.com>
a33652ee
GB
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
20(define-module (gnu tests monitoring)
6b1c4179
OP
21 #:use-module (gnu packages databases)
22 #:use-module (gnu packages monitoring)
85c07cff 23 #:use-module (gnu packages php)
a33652ee
GB
24 #:use-module (gnu services)
25 #:use-module (gnu services monitoring)
26 #:use-module (gnu services networking)
6b1c4179
OP
27 #:use-module (gnu services databases)
28 #:use-module (gnu services shepherd)
85c07cff 29 #:use-module (gnu services web)
a33652ee 30 #:use-module (gnu system vm)
6b1c4179 31 #:use-module (gnu system)
a33652ee
GB
32 #:use-module (gnu tests)
33 #:use-module (guix gexp)
6b1c4179
OP
34 #:export (%test-prometheus-node-exporter
35 %test-zabbix))
a33652ee
GB
36
37\f
38;;;
39;;; Prometheus Node Exporter
40;;;
41
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."
44 (define os
45 (marionette-operating-system
46 test-os
47 #:imported-modules '((gnu services herd))))
48
49 (define vm
50 (virtual-machine
51 (operating-system os)
52 (port-forwardings '((8080 . 9100)))))
53
54 (define test
55 (with-imported-modules '((gnu build marionette))
56 #~(begin
57 (use-modules (srfi srfi-11)
58 (srfi srfi-64)
59 (gnu build marionette)
60 (web client)
61 (web response))
62
63 (define marionette
64 (make-marionette (list #$vm)))
65
66 (mkdir #$output)
67 (chdir #$output)
68
69 (test-begin #$name)
70
71 (test-assert "prometheus-node-exporter running"
72 (marionette-eval
73 '(begin
74 (use-modules (gnu services herd))
75 (match (start-service 'prometheus-node-exporter)
76 (#f #f)
77 (('service response-parts ...)
78 (match (assq-ref response-parts 'running)
79 ((pid) (number? pid))))))
80 marionette))
81
82 (test-equal "http-get"
83 200
84 (begin
85 (wait-for-tcp-port 9100 marionette)
86 (let-values (((response text)
87 (http-get "http://localhost:8080")))
88 (response-code response))))
89
90 (test-end)
91 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
92
93 (gexp->derivation (string-append name "-test") test))
94
95(define %prometheus-node-exporter-os
96 (simple-operating-system
39d7fdce 97 (service dhcp-client-service-type)
a33652ee
GB
98 (service prometheus-node-exporter-service-type
99 (prometheus-node-exporter-configuration))))
100
101(define %test-prometheus-node-exporter
102 (system-test
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))))
6b1c4179
OP
107
108\f
109;;;
110;;; Zabbix
111;;;
112
113(define %psql-user-create-zabbix
114 "\
115sudo -u postgres psql <<< \"create user zabbix password 'zabbix';\"
116")
117
118(define %psql-db-zabbix-create-script
119 "\
120sudo -u postgres psql --no-align <<< \\\\du
121")
122
123(define %psql-db-create-zabbix
124 "\
125sudo -u postgres createdb -O zabbix -E Unicode -T template0 zabbix
126")
127
128(define %psql-db-import-zabbix
129 #~(format #f "\
130cat ~a | sudo -u zabbix psql zabbix;
131cat ~a | sudo -u zabbix psql zabbix;
132cat ~a | sudo -u zabbix psql zabbix;
133"
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")))
140
141(define* (run-zabbix-server-test name test-os)
142 "Run tests in %ZABBIX-OS, which has zabbix running."
143 (define os
144 (marionette-operating-system
145 test-os
146 #:imported-modules '((gnu services herd))))
147
148 (define vm
149 (virtual-machine
150 (operating-system os)
151 (port-forwardings '((8080 . 80)))
152 (memory-size 1024)))
153
154 (define test
155 (with-imported-modules '((gnu build marionette))
156 #~(begin
157 (use-modules (srfi srfi-11)
158 (srfi srfi-64)
159 (gnu build marionette)
160 (web client)
161 (web response)
162 (ice-9 popen)
163 (ice-9 rdelim))
164
165 (define marionette
166 (make-marionette (list #$vm)))
167
168 (mkdir #$output)
169 (chdir #$output)
170
171 (test-begin #$name)
172
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"
177 (marionette-eval
178 `(begin
179 (use-modules (gnu services herd))
180 (let loop ((i 10))
181 (cond ((file-exists? (%shepherd-socket-file))
182 #t)
183 ((> i 0)
184 (sleep 1)
185 (loop (- i 1)))
186 (else
187 'failure))))
188 marionette))
189
190 (test-assert "postgres service running"
191 (marionette-eval
192 '(begin
193 (use-modules (gnu services herd))
194 (start-service 'postgres))
195 marionette))
196
7e5f016c
LC
197 ;; Add /run/setuid-programs to $PATH so that the scripts passed to
198 ;; 'system' can find 'sudo'.
199 (marionette-eval
200 '(setenv "PATH"
201 "/run/setuid-programs:/run/current-system/profile/bin")
202 marionette)
203
6b1c4179
OP
204 (test-eq "postgres create zabbix user"
205 0
206 (marionette-eval '(begin (system #$%psql-user-create-zabbix))
207 marionette))
208
209 (test-equal "postgres find zabbix user"
210 "List of roles
211Role name|Attributes|Member of
212postgres|Superuser, Create role, Create DB, Replication, Bypass RLS|{}
213zabbix||{}
214"
215 (marionette-eval
216 '(begin (let* ((port (open-pipe #$%psql-db-zabbix-create-script
217 OPEN_READ))
218 (output (read-string port))
219 (status (close-pipe port)))
220 output))
221 marionette))
222
223 (test-eq "postgres create zabbix db"
224 0
225 (marionette-eval '(begin (system #$%psql-db-create-zabbix))
226 marionette))
227
228 (test-eq "postgres import zabbix db"
229 0
230 (marionette-eval '(begin (system #$%psql-db-import-zabbix))
231 marionette))
232
233 ;; Wait for zabbix-server to be up and running.
234 (test-assert "zabbix-server running"
235 (marionette-eval
236 '(begin
237 (use-modules (gnu services herd))
238 (start-service 'zabbix-server))
239 marionette))
240
241 ;; Make sure the PID file is created.
242 (test-assert "zabbix-server PID file"
243 (marionette-eval
244 '(file-exists? "/var/run/zabbix/zabbix_server.pid")
245 marionette))
246
6106d7ca
OP
247 ;; Wait for zabbix-agent to be up and running.
248 (test-assert "zabbix-agent running"
249 (marionette-eval
250 '(begin
251 (use-modules (gnu services herd))
252 (start-service 'zabbix-agent))
253 marionette))
254
255 ;; Make sure the PID file is created.
256 (test-assert "zabbix-agent PID file"
257 (marionette-eval
258 '(file-exists? "/var/run/zabbix/zabbix_agent.pid")
259 marionette))
260
85c07cff
OP
261 ;; Wait for php-fpm to be up and running.
262 (test-assert "php-fpm running"
263 (marionette-eval
264 '(begin
265 (use-modules (gnu services herd))
266 (start-service 'php-fpm))
267 marionette))
268
269 ;; Wait for nginx to be up and running.
270 (test-assert "nginx running"
271 (marionette-eval
272 '(begin
273 (use-modules (gnu services herd))
274 (start-service 'nginx))
275 marionette))
276
277 ;; Make sure the PID file is created.
278 (test-assert "nginx PID file"
279 (marionette-eval
280 '(file-exists? "/var/run/nginx/pid")
281 marionette))
282
37fea3c3 283 ;; Make sure we can access pages that correspond to our Zabbix instance.
85c07cff
OP
284 (letrec-syntax ((test-url
285 (syntax-rules ()
286 ((_ path code)
287 (test-equal (string-append "GET " path)
288 code
289 (let-values (((response body)
290 (http-get (string-append
291 "http://localhost:8080"
292 path))))
293 (response-code response))))
294 ((_ path)
295 (test-url path 200)))))
296 (test-url "/")
297 (test-url "/does-not-exist" 404))
298
6b1c4179
OP
299 (test-end)
300
301 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
302
303 (gexp->derivation (string-append name "-test") test))
304
305(define %zabbix-os
306 ;; Return operating system under test.
307 (let ((base-os
308 (simple-operating-system
309 (service dhcp-client-service-type)
bdcf4d88
CB
310 (service postgresql-service-type
311 (postgresql-configuration
48d8d92c 312 (postgresql postgresql)))
85c07cff
OP
313 (service zabbix-front-end-service-type
314 (zabbix-front-end-configuration
315 (db-password "zabbix")))
316
317 (service php-fpm-service-type
318 (php-fpm-configuration
8b6c91ba 319 (timezone "Europe/Paris")))
85c07cff 320
6b1c4179
OP
321 (service zabbix-server-service-type
322 (zabbix-server-configuration
323 (db-password "zabbix")
6106d7ca
OP
324 (log-type "console")))
325
326 (service zabbix-agent-service-type))))
6b1c4179
OP
327 (operating-system
328 (inherit base-os)
329 (packages (cons* postgresql (operating-system-packages base-os))))))
330
331(define %test-zabbix
332 (system-test
333 (name "zabbix")
334 (description "Connect to a running Zabbix")
335 (value (run-zabbix-server-test name %zabbix-os))))