summary refs log tree commit diff
path: root/nixos/tests/grafana/provision/default.nix
blob: d33d16ce12099d1910d00d4859d29d94447b0fec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import ../../make-test-python.nix ({ lib, pkgs, ... }:

let
  inherit (lib) mkMerge nameValuePair maintainers;

  baseGrafanaConf = {
    services.grafana = {
      enable = true;
      provision.enable = true;
      settings = {
        analytics.reporting_enabled = false;

        server = {
          http_addr = "localhost";
          domain = "localhost";
        };

        security = {
          admin_user = "testadmin";
          admin_password = "$__file{${pkgs.writeText "pwd" "snakeoilpwd"}}";
        };
      };
    };

    systemd.tmpfiles.rules =
      let
        dashboard = pkgs.writeText "test.json" (builtins.readFile ./test_dashboard.json);
      in
      [
        "d /var/lib/grafana/dashboards 0700 grafana grafana -"
        "C+ /var/lib/grafana/dashboards/test.json - - - - ${dashboard}"
      ];
  };

  extraNodeConfs = {
    provisionLegacyNotifiers = {
      services.grafana.provision = {
        datasources.settings = {
          apiVersion = 1;
          datasources = [{
            name = "Test Datasource";
            type = "testdata";
            access = "proxy";
            uid = "test_datasource";
          }];
        };
        dashboards.settings = {
          apiVersion = 1;
          providers = [{
            name = "default";
            options.path = "/var/lib/grafana/dashboards";
          }];
        };
        notifiers = [{
          uid = "test_notifiers";
          name = "Test Notifiers";
          type = "email";
          settings = {
            singleEmail = true;
            addresses = "test@test.com";
          };
        }];
      };
    };
    provisionNix = {
      services.grafana.provision = {
        datasources.settings = {
          apiVersion = 1;
          datasources = [{
            name = "Test Datasource";
            type = "testdata";
            access = "proxy";
            uid = "test_datasource";
          }];
        };

        dashboards.settings = {
          apiVersion = 1;
          providers = [{
            name = "default";
            options.path = "/var/lib/grafana/dashboards";
          }];
        };

        alerting = {
          rules.settings = {
            groups = [{
              name = "test_rule_group";
              folder = "test_folder";
              interval = "60s";
              rules = [{
                uid = "test_rule";
                title = "Test Rule";
                condition = "A";
                data = [{
                  refId = "A";
                  datasourceUid = "-100";
                  model = {
                    conditions = [{
                      evaluator = {
                        params = [ 3 ];
                        type = "git";
                      };
                      operator.type = "and";
                      query.params = [ "A" ];
                      reducer.type = "last";
                      type = "query";
                    }];
                    datasource = {
                      type = "__expr__";
                      uid = "-100";
                    };
                    expression = "1==0";
                    intervalMs = 1000;
                    maxDataPoints = 43200;
                    refId = "A";
                    type = "math";
                  };
                }];
                for = "60s";
              }];
            }];
          };

          contactPoints.settings = {
            contactPoints = [{
              name = "Test Contact Point";
              receivers = [{
                uid = "test_contact_point";
                type = "prometheus-alertmanager";
                settings.url = "http://localhost:9000";
              }];
            }];
          };

          policies.settings = {
            policies = [{
              receiver = "Test Contact Point";
            }];
          };

          templates.settings = {
            templates = [{
              name = "Test Template";
              template = "Test message";
            }];
          };

          muteTimings.settings = {
            muteTimes = [{
              name = "Test Mute Timing";
            }];
          };
        };
      };
    };

    provisionYaml = {
      services.grafana.provision = {
        datasources.path = ./datasources.yaml;
        dashboards.path = ./dashboards.yaml;
        alerting = {
          rules.path = ./rules.yaml;
          contactPoints.path = ./contact-points.yaml;
          policies.path = ./policies.yaml;
          templates.path = ./templates.yaml;
          muteTimings.path = ./mute-timings.yaml;
        };
      };
    };

    provisionYamlDirs = let
      mkdir = p: pkgs.writeTextDir (baseNameOf p) (builtins.readFile p);
    in {
      services.grafana.provision = {
        datasources.path = mkdir ./datasources.yaml;
        dashboards.path = mkdir ./dashboards.yaml;
        alerting = {
          rules.path = mkdir ./rules.yaml;
          contactPoints.path = mkdir ./contact-points.yaml;
          policies.path = mkdir ./policies.yaml;
          templates.path = mkdir ./templates.yaml;
          muteTimings.path = mkdir ./mute-timings.yaml;
        };
      };
    };
  };

  nodes = builtins.mapAttrs (_: val: mkMerge [ val baseGrafanaConf ]) extraNodeConfs;
in {
  name = "grafana-provision";

  meta = with maintainers; {
    maintainers = [ kfears willibutz ];
  };

  inherit nodes;

  testScript = ''
    start_all()

    nodeNix = ("Nix (new format)", provisionNix)
    nodeYaml = ("Nix (YAML)", provisionYaml)
    nodeYamlDir = ("Nix (YAML in dirs)", provisionYamlDirs)

    for description, machine in [nodeNix, nodeYaml, nodeYamlDir]:
        with subtest(f"Should start provision node: {description}"):
            machine.wait_for_unit("grafana.service")
            machine.wait_for_open_port(3000)

        with subtest(f"Successful datasource provision with {description}"):
            machine.succeed(
                "curl -sSfN -u testadmin:snakeoilpwd http://127.0.0.1:3000/api/datasources/uid/test_datasource | grep Test\ Datasource"
            )

        with subtest(f"Successful dashboard provision with {description}"):
            machine.succeed(
                "curl -sSfN -u testadmin:snakeoilpwd http://127.0.0.1:3000/api/dashboards/uid/test_dashboard | grep Test\ Dashboard"
            )

        with subtest(f"Successful rule provision with {description}"):
            machine.succeed(
                "curl -sSfN -u testadmin:snakeoilpwd http://127.0.0.1:3000/api/v1/provisioning/alert-rules/test_rule | grep Test\ Rule"
            )

        with subtest(f"Successful contact point provision with {description}"):
            machine.succeed(
                "curl -sSfN -u testadmin:snakeoilpwd http://127.0.0.1:3000/api/v1/provisioning/contact-points | grep Test\ Contact\ Point"
            )

        with subtest(f"Successful policy provision with {description}"):
            machine.succeed(
                "curl -sSfN -u testadmin:snakeoilpwd http://127.0.0.1:3000/api/v1/provisioning/policies | grep Test\ Contact\ Point"
            )

        with subtest(f"Successful template provision with {description}"):
            machine.succeed(
                "curl -sSfN -u testadmin:snakeoilpwd http://127.0.0.1:3000/api/v1/provisioning/templates | grep Test\ Template"
            )

        with subtest("Successful mute timings provision with {description}"):
            machine.succeed(
                "curl -sSfN -u testadmin:snakeoilpwd http://127.0.0.1:3000/api/v1/provisioning/mute-timings | grep Test\ Mute\ Timing"
            )

    with subtest("Successful notifiers provision"):
        provisionLegacyNotifiers.wait_for_unit("grafana.service")
        provisionLegacyNotifiers.wait_for_open_port(3000)
        print(provisionLegacyNotifiers.succeed(
            "curl -sSfN -u testadmin:snakeoilpwd http://127.0.0.1:3000/api/alert-notifications/uid/test_notifiers"
        ))
        provisionLegacyNotifiers.succeed(
            "curl -sSfN -u testadmin:snakeoilpwd http://127.0.0.1:3000/api/alert-notifications/uid/test_notifiers | grep Test\ Notifiers"
        )
  '';
})