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