git.iko.soy/typeable/nixpkgs
on master help sign in
nixos/tests: add test for prometheus exporters
commit0febc64ed1928e163d9747ee1a7d3d1c7c0f9c6d authorWilliButz <wbutz@cyberfnord.de> dateMon 8 Oct 2018, 16:38 (8 years ago) parentfbb7e0c
2 files changed+298
nixos/release.nix+1 nixos/tests/prometheus-exporters.nix+297
nixos/release.nix+1
@@ -390,6 +390,7 @@ in rec {
390390 tests.predictable-interface-names = callSubTests tests/predictable-interface-names.nix {};
391391 tests.printing = callTest tests/printing.nix {};
392392 tests.prometheus = callTest tests/prometheus.nix {};
393+ tests.prometheus-exporters = callTest tests/prometheus-exporters.nix {};
393394 tests.prosody = callTest tests/prosody.nix {};
394395 tests.proxy = callTest tests/proxy.nix {};
395396 tests.quagga = callTest tests/quagga.nix {};
nixos/tests/prometheus-exporters.nixnew file+297
@@ -0,0 +1,297 @@
1+import ./make-test.nix ({ lib, pkgs, ... }:
2+let
3+ escape' = str: lib.replaceChars [''"'' "$" "\n"] [''\\\"'' "\\$" ""] str;
4+
5+/*
6+ * The attrset `exporterTests` contains one attribute
7+ * for each exporter test. Each of these attributes
8+ * is expected to be an attrset containing:
9+ *
10+ * `exporterConfig`:
11+ * this attribute set contains config for the exporter itself
12+ *
13+ * `exporterTest`
14+ * this attribute set contains test instructions
15+ *
16+ * `metricProvider` (optional)
17+ * this attribute contains additional machine config
18+ *
19+ * Example:
20+ * exporterTests.<exporterName> = {
21+ * exporterConfig = {
22+ * enable = true;
23+ * };
24+ * metricProvider = {
25+ * services.<metricProvider>.enable = true;
26+ * };
27+ * exporterTest = ''
28+ * waitForUnit("prometheus-<exporterName>-exporter.service");
29+ * waitForOpenPort("1234");
30+ * succeed("curl -sSf 'localhost:1234/metrics'");
31+ * '';
32+ * };
33+ *
34+ * # this would generate the following test config:
35+ *
36+ * nodes.<exporterName> = {
37+ * services.prometheus.<exporterName> = {
38+ * enable = true;
39+ * };
40+ * services.<metricProvider>.enable = true;
41+ * };
42+ *
43+ * testScript = ''
44+ * $<exporterName>->start();
45+ * $<exporterName>->waitForUnit("prometheus-<exporterName>-exporter.service");
46+ * $<exporterName>->waitForOpenPort("1234");
47+ * $<exporterName>->succeed("curl -sSf 'localhost:1234/metrics'");
48+ * $<exporterName>->shutdown();
49+ * '';
50+ */
51+
52+ exporterTests = {
53+
54+ blackbox = {
55+ exporterConfig = {
56+ enable = true;
57+ configFile = pkgs.writeText "config.yml" (builtins.toJSON {
58+ modules.icmp_v6 = {
59+ prober = "icmp";
60+ icmp.preferred_ip_protocol = "ip6";
61+ };
62+ });
63+ };
64+ exporterTest = ''
65+ waitForUnit("prometheus-blackbox-exporter.service");
66+ waitForOpenPort(9115);
67+ succeed("curl -sSf 'http://localhost:9115/probe?target=localhost&module=icmp_v6' | grep -q 'probe_success 1'");
68+ '';
69+ };
70+
71+ collectd = {
72+ exporterConfig = {
73+ enable = true;
74+ extraFlags = [ "--web.collectd-push-path /collectd" ];
75+ };
76+ exporterTest =let postData = escape' ''
77+ [{
78+ "values":[23],
79+ "dstypes":["gauge"],
80+ "type":"gauge",
81+ "interval":1000,
82+ "host":"testhost",
83+ "plugin":"testplugin",
84+ "time":$(date +%s)
85+ }]
86+ ''; in ''
87+ waitForUnit("prometheus-collectd-exporter.service");
88+ waitForOpenPort(9103);
89+ succeed("curl -sSfH 'Content-Type: application/json' -X POST --data \"${postData}\" localhost:9103/collectd");
90+ succeed("curl -sSf localhost:9103/metrics | grep -q 'collectd_testplugin_gauge{instance=\"testhost\"} 23'");
91+ '';
92+ };
93+
94+ dnsmasq = {
95+ exporterConfig = {
96+ enable = true;
97+ leasesPath = "/var/lib/dnsmasq/dnsmasq.leases";
98+ };
99+ metricProvider = {
100+ services.dnsmasq.enable = true;
101+ };
102+ exporterTest = ''
103+ waitForUnit("prometheus-dnsmasq-exporter.service");
104+ waitForOpenPort(9153);
105+ succeed("curl -sSf http://localhost:9153/metrics | grep -q 'dnsmasq_leases 0'");
106+ '';
107+ };
108+
109+ dovecot = {
110+ exporterConfig = {
111+ enable = true;
112+ scopes = [ "global" ];
113+ socketPath = "/var/run/dovecot2/old-stats";
114+ user = "root"; # <- don't use user root in production
115+ };
116+ metricProvider = {
117+ services.dovecot2.enable = true;
118+ };
119+ exporterTest = ''
120+ waitForUnit("prometheus-dovecot-exporter.service");
121+ waitForOpenPort(9166);
122+ succeed("curl -sSf http://localhost:9166/metrics | grep -q 'dovecot_up{scope=\"global\"} 1'");
123+ '';
124+ };
125+
126+ fritzbox = { # TODO add proper test case
127+ exporterConfig = {
128+ enable = true;
129+ };
130+ exporterTest = ''
131+ waitForUnit("prometheus-fritzbox-exporter.service");
132+ waitForOpenPort(9133);
133+ succeed("curl -sSf http://localhost:9133/metrics | grep -q 'fritzbox_exporter_collect_errors 0'");
134+ '';
135+ };
136+
137+ json = {
138+ exporterConfig = {
139+ enable = true;
140+ url = "http://localhost";
141+ configFile = pkgs.writeText "json-exporter-conf.json" (builtins.toJSON [{
142+ name = "json_test_metric";
143+ path = "$.test";
144+ }]);
145+ };
146+ metricProvider = {
147+ systemd.services.prometheus-json-exporter.after = [ "nginx.service" ];
148+ services.nginx = {
149+ enable = true;
150+ virtualHosts.localhost.locations."/".extraConfig = ''
151+ return 200 "{\"test\":1}";
152+ '';
153+ };
154+ };
155+ exporterTest = ''
156+ waitForUnit("nginx.service");
157+ waitForOpenPort(80);
158+ waitForUnit("prometheus-json-exporter.service");
159+ waitForOpenPort(7979);
160+ succeed("curl -sSf localhost:7979/metrics | grep -q 'json_test_metric 1'");
161+ '';
162+ };
163+
164+ nginx = {
165+ exporterConfig = {
166+ enable = true;
167+ };
168+ metricProvider = {
169+ services.nginx = {
170+ enable = true;
171+ statusPage = true;
172+ virtualHosts."/".extraConfig = "return 204;";
173+ };
174+ };
175+ exporterTest = ''
176+ waitForUnit("nginx.service")
177+ waitForUnit("prometheus-nginx-exporter.service")
178+ waitForOpenPort(9113)
179+ succeed("curl -sSf http://localhost:9113/metrics | grep -q 'nginx_up 1'")
180+ '';
181+ };
182+
183+ node = {
184+ exporterConfig = {
185+ enable = true;
186+ };
187+ exporterTest = ''
188+ waitForUnit("prometheus-node-exporter.service");
189+ waitForOpenPort(9100);
190+ succeed("curl -sSf http://localhost:9100/metrics | grep -q 'node_exporter_build_info{.\\+} 1'");
191+ '';
192+ };
193+
194+ postfix = {
195+ exporterConfig = {
196+ enable = true;
197+ };
198+ metricProvider = {
199+ services.postfix.enable = true;
200+ };
201+ exporterTest = ''
202+ waitForUnit("prometheus-postfix-exporter.service");
203+ waitForOpenPort(9154);
204+ succeed("curl -sSf http://localhost:9154/metrics | grep -q 'postfix_smtpd_connects_total 0'");
205+ '';
206+ };
207+
208+ snmp = {
209+ exporterConfig = {
210+ enable = true;
211+ configuration.default = {
212+ version = 2;
213+ auth.community = "public";
214+ };
215+ };
216+ exporterTest = ''
217+ waitForUnit("prometheus-snmp-exporter.service");
218+ waitForOpenPort(9116);
219+ succeed("curl -sSf localhost:9116/metrics | grep -q 'snmp_request_errors_total 0'");
220+ '';
221+ };
222+
223+ surfboard = {
224+ exporterConfig = {
225+ enable = true;
226+ modemAddress = "localhost";
227+ };
228+ metricProvider = {
229+ systemd.services.prometheus-surfboard-exporter.after = [ "nginx.service" ];
230+ services.nginx = {
231+ enable = true;
232+ virtualHosts.localhost.locations."/cgi-bin/status".extraConfig = ''
233+ return 204;
234+ '';
235+ };
236+ };
237+ exporterTest = ''
238+ waitForUnit("nginx.service");
239+ waitForOpenPort(80);
240+ waitForUnit("prometheus-surfboard-exporter.service");
241+ waitForOpenPort(9239);
242+ succeed("curl -sSf localhost:9239/metrics | grep -q 'surfboard_up 1'");
243+ '';
244+ };
245+
246+ varnish = {
247+ exporterConfig = {
248+ enable = true;
249+ instance = "/var/spool/varnish/varnish";
250+ group = "varnish";
251+ };
252+ metricProvider = {
253+ systemd.services.prometheus-varnish-exporter.after = [
254+ "varnish.service"
255+ ];
256+ services.varnish = {
257+ enable = true;
258+ config = ''
259+ vcl 4.0;
260+ backend default {
261+ .host = "127.0.0.1";
262+ .port = "80";
263+ }
264+ '';
265+ };
266+ };
267+ exporterTest = ''
268+ waitForUnit("prometheus-varnish-exporter.service");
269+ waitForOpenPort(9131);
270+ succeed("curl -sSf http://localhost:9131/metrics | grep -q 'varnish_up 1'");
271+ '';
272+ };
273+ };
274+
275+ nodes = lib.mapAttrs (exporter: testConfig: lib.mkMerge [{
276+ services.prometheus.exporters.${exporter} = testConfig.exporterConfig;
277+ } testConfig.metricProvider or {}]) exporterTests;
278+
279+ testScript = lib.concatStrings (lib.mapAttrsToList (exporter: testConfig: (''
280+ subtest "${exporter}", sub {
281+ ${"$"+exporter}->start();
282+ ${lib.concatStringsSep " " (map (line: ''
283+ ${"$"+exporter}->${line};
284+ '') (lib.splitString "\n" (lib.removeSuffix "\n" testConfig.exporterTest)))}
285+ ${"$"+exporter}->shutdown();
286+ };
287+ '')) exporterTests);
288+in
289+{
290+ name = "prometheus-exporters";
291+
292+ inherit nodes testScript;
293+
294+ meta = with lib.maintainers; {
295+ maintainers = [ willibutz ];
296+ };
297+})