1import ../../make-test-python.nix ({ lib, pkgs, ... }:
2
3let
4 inherit (lib) mkMerge nameValuePair maintainers;
5
6 baseGrafanaConf = {
7 services.grafana = {
8 enable = true;
9 provision.enable = true;
10 settings = {
11 analytics.reporting_enabled = false;
12
13 server = {
14 http_addr = "localhost";
15 domain = "localhost";
16 };
17
18 security = {
19 admin_user = "testadmin";
20 admin_password = "$__file{${pkgs.writeText "pwd" "snakeoilpwd"}}";
21 };
22 };
23 };
24
25 systemd.tmpfiles.rules =
26 let
27 dashboard = pkgs.writeText "test.json" (builtins.readFile ./test_dashboard.json);
28 in
29 [
30 "d /var/lib/grafana/dashboards 0700 grafana grafana -"
31 "C+ /var/lib/grafana/dashboards/test.json - - - - ${dashboard}"
32 ];
33 };
34
35 extraNodeConfs = {
36 provisionLegacyNotifiers = {
37 services.grafana.provision = {
38 datasources.settings = {
39 apiVersion = 1;
40 datasources = [{
41 name = "Test Datasource";
42 type = "testdata";
43 access = "proxy";
44 uid = "test_datasource";
45 }];
46 };
47 dashboards.settings = {
48 apiVersion = 1;
49 providers = [{
50 name = "default";
51 options.path = "/var/lib/grafana/dashboards";
52 }];
53 };
54 notifiers = [{
55 uid = "test_notifiers";
56 name = "Test Notifiers";
57 type = "email";
58 settings = {
59 singleEmail = true;
60 addresses = "test@test.com";
61 };
62 }];
63 };
64 };
65 provisionNix = {
66 services.grafana.provision = {
67 datasources.settings = {
68 apiVersion = 1;
69 datasources = [{
70 name = "Test Datasource";
71 type = "testdata";
72 access = "proxy";
73 uid = "test_datasource";
74 }];
75 };
76
77 dashboards.settings = {
78 apiVersion = 1;
79 providers = [{
80 name = "default";
81 options.path = "/var/lib/grafana/dashboards";
82 }];
83 };
84
85 alerting = {
86 rules.settings = {
87 groups = [{
88 name = "test_rule_group";
89 folder = "test_folder";
90 interval = "60s";
91 rules = [{
92 uid = "test_rule";
93 title = "Test Rule";
94 condition = "A";
95 data = [{
96 refId = "A";
97 datasourceUid = "-100";
98 model = {
99 conditions = [{
100 evaluator = {
101 params = [ 3 ];
102 type = "git";
103 };
104 operator.type = "and";
105 query.params = [ "A" ];
106 reducer.type = "last";
107 type = "query";
108 }];
109 datasource = {
110 type = "__expr__";
111 uid = "-100";
112 };
113 expression = "1==0";
114 intervalMs = 1000;
115 maxDataPoints = 43200;
116 refId = "A";
117 type = "math";
118 };
119 }];
120 for = "60s";
121 }];
122 }];
123 };
124
125 contactPoints.settings = {
126 contactPoints = [{
127 name = "Test Contact Point";
128 receivers = [{
129 uid = "test_contact_point";
130 type = "prometheus-alertmanager";
131 settings.url = "http://localhost:9000";
132 }];
133 }];
134 };
135
136 policies.settings = {
137 policies = [{
138 receiver = "Test Contact Point";
139 }];
140 };
141
142 templates.settings = {
143 templates = [{
144 name = "Test Template";
145 template = "Test message";
146 }];
147 };
148
149 muteTimings.settings = {
150 muteTimes = [{
151 name = "Test Mute Timing";
152 }];
153 };
154 };
155 };
156 };
157
158 provisionYaml = {
159 services.grafana.provision = {
160 datasources.path = ./datasources.yaml;
161 dashboards.path = ./dashboards.yaml;
162 alerting = {
163 rules.path = ./rules.yaml;
164 contactPoints.path = ./contact-points.yaml;
165 policies.path = ./policies.yaml;
166 templates.path = ./templates.yaml;
167 muteTimings.path = ./mute-timings.yaml;
168 };
169 };
170 };
171
172 provisionYamlDirs = let
173 mkdir = p: pkgs.writeTextDir (baseNameOf p) (builtins.readFile p);
174 in {
175 services.grafana.provision = {
176 datasources.path = mkdir ./datasources.yaml;
177 dashboards.path = mkdir ./dashboards.yaml;
178 alerting = {
179 rules.path = mkdir ./rules.yaml;
180 contactPoints.path = mkdir ./contact-points.yaml;
181 policies.path = mkdir ./policies.yaml;
182 templates.path = mkdir ./templates.yaml;
183 muteTimings.path = mkdir ./mute-timings.yaml;
184 };
185 };
186 };
187 };
188
189 nodes = builtins.mapAttrs (_: val: mkMerge [ val baseGrafanaConf ]) extraNodeConfs;
190in {
191 name = "grafana-provision";
192
193 meta = with maintainers; {
194 maintainers = [ kfears willibutz ];
195 };
196
197 inherit nodes;
198
199 testScript = ''
200 start_all()
201
202 nodeNix = ("Nix (new format)", provisionNix)
203 nodeYaml = ("Nix (YAML)", provisionYaml)
204 nodeYamlDir = ("Nix (YAML in dirs)", provisionYamlDirs)
205
206 for description, machine in [nodeNix, nodeYaml, nodeYamlDir]:
207 with subtest(f"Should start provision node: {description}"):
208 machine.wait_for_unit("grafana.service")
209 machine.wait_for_open_port(3000)
210
211 with subtest(f"Successful datasource provision with {description}"):
212 machine.succeed(
213 "curl -sSfN -u testadmin:snakeoilpwd http://127.0.0.1:3000/api/datasources/uid/test_datasource | grep Test\ Datasource"
214 )
215
216 with subtest(f"Successful dashboard provision with {description}"):
217 machine.succeed(
218 "curl -sSfN -u testadmin:snakeoilpwd http://127.0.0.1:3000/api/dashboards/uid/test_dashboard | grep Test\ Dashboard"
219 )
220
221 with subtest(f"Successful rule provision with {description}"):
222 machine.succeed(
223 "curl -sSfN -u testadmin:snakeoilpwd http://127.0.0.1:3000/api/v1/provisioning/alert-rules/test_rule | grep Test\ Rule"
224 )
225
226 with subtest(f"Successful contact point provision with {description}"):
227 machine.succeed(
228 "curl -sSfN -u testadmin:snakeoilpwd http://127.0.0.1:3000/api/v1/provisioning/contact-points | grep Test\ Contact\ Point"
229 )
230
231 with subtest(f"Successful policy provision with {description}"):
232 machine.succeed(
233 "curl -sSfN -u testadmin:snakeoilpwd http://127.0.0.1:3000/api/v1/provisioning/policies | grep Test\ Contact\ Point"
234 )
235
236 with subtest(f"Successful template provision with {description}"):
237 machine.succeed(
238 "curl -sSfN -u testadmin:snakeoilpwd http://127.0.0.1:3000/api/v1/provisioning/templates | grep Test\ Template"
239 )
240
241 with subtest("Successful mute timings provision with {description}"):
242 machine.succeed(
243 "curl -sSfN -u testadmin:snakeoilpwd http://127.0.0.1:3000/api/v1/provisioning/mute-timings | grep Test\ Mute\ Timing"
244 )
245
246 with subtest("Successful notifiers provision"):
247 provisionLegacyNotifiers.wait_for_unit("grafana.service")
248 provisionLegacyNotifiers.wait_for_open_port(3000)
249 print(provisionLegacyNotifiers.succeed(
250 "curl -sSfN -u testadmin:snakeoilpwd http://127.0.0.1:3000/api/alert-notifications/uid/test_notifiers"
251 ))
252 provisionLegacyNotifiers.succeed(
253 "curl -sSfN -u testadmin:snakeoilpwd http://127.0.0.1:3000/api/alert-notifications/uid/test_notifiers | grep Test\ Notifiers"
254 )
255 '';
256})