summary refs log tree commit diff
path: root/nixos/tests/grafana/basic.nix
blob: 8bf4caad7fbfcde5e6269dfa494a8d87cb5e31ef (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
import ../make-test-python.nix ({ lib, pkgs, ... }:

let
  inherit (lib) mkMerge nameValuePair maintainers;

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

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

        security = {
          admin_user = "testadmin";
          admin_password = "snakeoilpwd";
        };
      };
    };
  };

  extraNodeConfs = {
    sqlite = {};

    socket = { config, ... }: {
      services.grafana.settings.server = {
        protocol = "socket";
        socket = "/run/grafana/sock";
        socket_gid = config.users.groups.nginx.gid;
      };

      users.users.grafana.extraGroups = [ "nginx" ];

      services.nginx = {
        enable = true;
        recommendedProxySettings = true;
        virtualHosts."_".locations."/".proxyPass = "http://unix:/run/grafana/sock";
      };
    };

    declarativePlugins = {
      services.grafana.declarativePlugins = [ pkgs.grafanaPlugins.grafana-clock-panel ];
    };

    postgresql = {
      services.grafana.settings.database = {
        host = "127.0.0.1:5432";
        user = "grafana";
      };
      services.postgresql = {
        enable = true;
        ensureDatabases = [ "grafana" ];
        ensureUsers = [{
          name = "grafana";
          ensurePermissions."DATABASE grafana" = "ALL PRIVILEGES";
        }];
      };
      systemd.services.grafana.after = [ "postgresql.service" ];
    };

    mysql = {
      services.grafana.settings.database.user = "grafana";
      services.mysql = {
        enable = true;
        ensureDatabases = [ "grafana" ];
        ensureUsers = [{
          name = "grafana";
          ensurePermissions."grafana.*" = "ALL PRIVILEGES";
        }];
        package = pkgs.mariadb;
      };
      systemd.services.grafana.after = [ "mysql.service" ];
    };
  };

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

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

  inherit nodes;

  testScript = ''
    start_all()

    with subtest("Declarative plugins installed"):
        declarativePlugins.wait_for_unit("grafana.service")
        declarativePlugins.wait_for_open_port(3000)
        declarativePlugins.succeed(
            "curl -sSfN -u testadmin:snakeoilpwd http://127.0.0.1:3000/api/plugins | grep grafana-clock-panel"
        )
        declarativePlugins.shutdown()

    with subtest("Successful API query as admin user with sqlite db"):
        sqlite.wait_for_unit("grafana.service")
        sqlite.wait_for_open_port(3000)
        print(sqlite.succeed(
            "curl -sSfN -u testadmin:snakeoilpwd http://127.0.0.1:3000/api/org/users -i"
        ))
        sqlite.succeed(
            "curl -sSfN -u testadmin:snakeoilpwd http://127.0.0.1:3000/api/org/users | grep admin\@localhost"
        )
        sqlite.shutdown()

    with subtest("Successful API query as admin user with sqlite db listening on socket"):
        socket.wait_for_unit("grafana.service")
        socket.wait_for_open_port(80)
        print(socket.succeed(
            "curl -sSfN -u testadmin:snakeoilpwd http://127.0.0.1/api/org/users -i"
        ))
        socket.succeed(
            "curl -sSfN -u testadmin:snakeoilpwd http://127.0.0.1/api/org/users | grep admin\@localhost"
        )
        socket.shutdown()

    with subtest("Successful API query as admin user with postgresql db"):
        postgresql.wait_for_unit("grafana.service")
        postgresql.wait_for_unit("postgresql.service")
        postgresql.wait_for_open_port(3000)
        postgresql.wait_for_open_port(5432)
        postgresql.succeed(
            "curl -sSfN -u testadmin:snakeoilpwd http://127.0.0.1:3000/api/org/users | grep admin\@localhost"
        )
        postgresql.shutdown()

    with subtest("Successful API query as admin user with mysql db"):
        mysql.wait_for_unit("grafana.service")
        mysql.wait_for_unit("mysql.service")
        mysql.wait_for_open_port(3000)
        mysql.wait_for_open_port(3306)
        mysql.succeed(
            "curl -sSfN -u testadmin:snakeoilpwd http://127.0.0.1:3000/api/org/users | grep admin\@localhost"
        )
        mysql.shutdown()
  '';
})