Remove traces of "GuixSD".
[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
197 (test-eq "postgres create zabbix user"
198 0
199 (marionette-eval '(begin (system #$%psql-user-create-zabbix))
200 marionette))
201
202 (test-equal "postgres find zabbix user"
203 "List of roles
204Role name|Attributes|Member of
205postgres|Superuser, Create role, Create DB, Replication, Bypass RLS|{}
206zabbix||{}
207"
208 (marionette-eval
209 '(begin (let* ((port (open-pipe #$%psql-db-zabbix-create-script
210 OPEN_READ))
211 (output (read-string port))
212 (status (close-pipe port)))
213 output))
214 marionette))
215
216 (test-eq "postgres create zabbix db"
217 0
218 (marionette-eval '(begin (system #$%psql-db-create-zabbix))
219 marionette))
220
221 (test-eq "postgres import zabbix db"
222 0
223 (marionette-eval '(begin (system #$%psql-db-import-zabbix))
224 marionette))
225
226 ;; Wait for zabbix-server to be up and running.
227 (test-assert "zabbix-server running"
228 (marionette-eval
229 '(begin
230 (use-modules (gnu services herd))
231 (start-service 'zabbix-server))
232 marionette))
233
234 ;; Make sure the PID file is created.
235 (test-assert "zabbix-server PID file"
236 (marionette-eval
237 '(file-exists? "/var/run/zabbix/zabbix_server.pid")
238 marionette))
239
6106d7ca
OP
240 ;; Wait for zabbix-agent to be up and running.
241 (test-assert "zabbix-agent running"
242 (marionette-eval
243 '(begin
244 (use-modules (gnu services herd))
245 (start-service 'zabbix-agent))
246 marionette))
247
248 ;; Make sure the PID file is created.
249 (test-assert "zabbix-agent PID file"
250 (marionette-eval
251 '(file-exists? "/var/run/zabbix/zabbix_agent.pid")
252 marionette))
253
85c07cff
OP
254 ;; Wait for php-fpm to be up and running.
255 (test-assert "php-fpm running"
256 (marionette-eval
257 '(begin
258 (use-modules (gnu services herd))
259 (start-service 'php-fpm))
260 marionette))
261
262 ;; Wait for nginx to be up and running.
263 (test-assert "nginx running"
264 (marionette-eval
265 '(begin
266 (use-modules (gnu services herd))
267 (start-service 'nginx))
268 marionette))
269
270 ;; Make sure the PID file is created.
271 (test-assert "nginx PID file"
272 (marionette-eval
273 '(file-exists? "/var/run/nginx/pid")
274 marionette))
275
276 ;; Make sure we can access pages that correspond to our repository.
277 (letrec-syntax ((test-url
278 (syntax-rules ()
279 ((_ path code)
280 (test-equal (string-append "GET " path)
281 code
282 (let-values (((response body)
283 (http-get (string-append
284 "http://localhost:8080"
285 path))))
286 (response-code response))))
287 ((_ path)
288 (test-url path 200)))))
289 (test-url "/")
290 (test-url "/does-not-exist" 404))
291
6b1c4179
OP
292 (test-end)
293
294 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
295
296 (gexp->derivation (string-append name "-test") test))
297
298(define %zabbix-os
299 ;; Return operating system under test.
300 (let ((base-os
301 (simple-operating-system
302 (service dhcp-client-service-type)
303 (postgresql-service)
85c07cff
OP
304 (service zabbix-front-end-service-type
305 (zabbix-front-end-configuration
306 (db-password "zabbix")))
307
308 (service php-fpm-service-type
309 (php-fpm-configuration
8b6c91ba 310 (timezone "Europe/Paris")))
85c07cff 311
6b1c4179
OP
312 (service zabbix-server-service-type
313 (zabbix-server-configuration
314 (db-password "zabbix")
6106d7ca
OP
315 (log-type "console")))
316
317 (service zabbix-agent-service-type))))
6b1c4179
OP
318 (operating-system
319 (inherit base-os)
320 (packages (cons* postgresql (operating-system-packages base-os))))))
321
322(define %test-zabbix
323 (system-test
324 (name "zabbix")
325 (description "Connect to a running Zabbix")
326 (value (run-zabbix-server-test name %zabbix-os))))