1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018 Gábor Boskovits <boskovits@gmail.com>
3 ;;; Copyright © 2018 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 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 system vm)
29 #:use-module (gnu system)
30 #:use-module (gnu tests)
31 #:use-module (guix gexp)
32 #:export (%test-prometheus-node-exporter
37 ;;; Prometheus Node Exporter
40 (define* (run-prometheus-node-exporter-server-test name test-os)
41 "Run tests in %PROMETHEUS-NODE-EXPORTER-OS, which has prometheus-node-exporter running."
43 (marionette-operating-system
45 #:imported-modules '((gnu services herd))))
50 (port-forwardings '((8080 . 9100)))))
53 (with-imported-modules '((gnu build marionette))
55 (use-modules (srfi srfi-11)
57 (gnu build marionette)
62 (make-marionette (list #$vm)))
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))))
89 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
91 (gexp->derivation (string-append name "-test") test))
93 (define %prometheus-node-exporter-os
94 (simple-operating-system
95 (service dhcp-client-service-type)
96 (service prometheus-node-exporter-service-type
97 (prometheus-node-exporter-configuration))))
99 (define %test-prometheus-node-exporter
101 (name "prometheus-node-exporter")
102 (description "Connect to a running prometheus-node-exporter server.")
103 (value (run-prometheus-node-exporter-server-test
104 name %prometheus-node-exporter-os))))
111 (define %psql-user-create-zabbix
113 sudo -u postgres psql <<< \"create user zabbix password 'zabbix';\"
116 (define %psql-db-zabbix-create-script
118 sudo -u postgres psql --no-align <<< \\\\du
121 (define %psql-db-create-zabbix
123 sudo -u postgres createdb -O zabbix -E Unicode -T template0 zabbix
126 (define %psql-db-import-zabbix
128 cat ~a | sudo -u zabbix psql zabbix;
129 cat ~a | sudo -u zabbix psql zabbix;
130 cat ~a | sudo -u zabbix psql zabbix;
132 (string-append #$zabbix-server:schema
133 "/database/postgresql/schema.sql")
134 (string-append #$zabbix-server:schema
135 "/database/postgresql/images.sql")
136 (string-append #$zabbix-server:schema
137 "/database/postgresql/data.sql")))
139 (define* (run-zabbix-server-test name test-os)
140 "Run tests in %ZABBIX-OS, which has zabbix running."
142 (marionette-operating-system
144 #:imported-modules '((gnu services herd))))
148 (operating-system os)
149 (port-forwardings '((8080 . 80)))
153 (with-imported-modules '((gnu build marionette))
155 (use-modules (srfi srfi-11)
157 (gnu build marionette)
164 (make-marionette (list #$vm)))
171 ;; XXX: Shepherd reads the config file *before* binding its control
172 ;; socket, so /var/run/shepherd/socket might not exist yet when the
173 ;; 'marionette' service is started.
174 (test-assert "shepherd socket ready"
177 (use-modules (gnu services herd))
179 (cond ((file-exists? (%shepherd-socket-file))
188 (test-assert "postgres service running"
191 (use-modules (gnu services herd))
192 (start-service 'postgres))
195 (test-eq "postgres create zabbix user"
197 (marionette-eval '(begin (system #$%psql-user-create-zabbix))
200 (test-equal "postgres find zabbix user"
202 Role name|Attributes|Member of
203 postgres|Superuser, Create role, Create DB, Replication, Bypass RLS|{}
207 '(begin (let* ((port (open-pipe #$%psql-db-zabbix-create-script
209 (output (read-string port))
210 (status (close-pipe port)))
214 (test-eq "postgres create zabbix db"
216 (marionette-eval '(begin (system #$%psql-db-create-zabbix))
219 (test-eq "postgres import zabbix db"
221 (marionette-eval '(begin (system #$%psql-db-import-zabbix))
224 ;; Wait for zabbix-server to be up and running.
225 (test-assert "zabbix-server running"
228 (use-modules (gnu services herd))
229 (start-service 'zabbix-server))
232 ;; Make sure the PID file is created.
233 (test-assert "zabbix-server PID file"
235 '(file-exists? "/var/run/zabbix/zabbix_server.pid")
238 ;; Wait for zabbix-agent to be up and running.
239 (test-assert "zabbix-agent running"
242 (use-modules (gnu services herd))
243 (start-service 'zabbix-agent))
246 ;; Make sure the PID file is created.
247 (test-assert "zabbix-agent PID file"
249 '(file-exists? "/var/run/zabbix/zabbix_agent.pid")
254 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
256 (gexp->derivation (string-append name "-test") test))
259 ;; Return operating system under test.
261 (simple-operating-system
262 (service dhcp-client-service-type)
264 (service zabbix-server-service-type
265 (zabbix-server-configuration
266 (db-password "zabbix")
267 (log-type "console")))
269 (service zabbix-agent-service-type))))
272 (packages (cons* postgresql (operating-system-packages base-os))))))
277 (description "Connect to a running Zabbix")
278 (value (run-zabbix-server-test name %zabbix-os))))