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