about summary refs log tree commit diff
path: root/nixpkgs/nixos/tests/influxdb2.nix
blob: 1631ac1d94081004c9ad41bcdb98b3045d973ba3 (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
import ./make-test-python.nix ({ pkgs, ...} : {
  name = "influxdb2";
  meta = with pkgs.lib.maintainers; {
    maintainers = [ offline ];
  };

  nodes.machine = { lib, ... }: {
    environment.systemPackages = [ pkgs.influxdb2-cli ];
    # Make sure that the service is restarted immediately if tokens need to be rewritten
    # without relying on any Restart=on-failure behavior
    systemd.services.influxdb2.serviceConfig.RestartSec = 6000;
    services.influxdb2.enable = true;
    services.influxdb2.provision = {
      enable = true;
      initialSetup = {
        organization = "default";
        bucket = "default";
        passwordFile = pkgs.writeText "admin-pw" "ExAmPl3PA55W0rD";
        tokenFile = pkgs.writeText "admin-token" "verysecureadmintoken";
      };
      organizations.someorg = {
        buckets.somebucket = {};
        auths.sometoken = {
          description = "some auth token";
          readBuckets = ["somebucket"];
          writeBuckets = ["somebucket"];
        };
      };
      users.someuser.passwordFile = pkgs.writeText "tmp-pw" "abcgoiuhaoga";
    };

    specialisation.withModifications.configuration = { ... }: {
      services.influxdb2.provision = {
        organizations.someorg.buckets.somebucket.present = false;
        organizations.someorg.auths.sometoken.present = false;
        users.someuser.present = false;

        organizations.myorg = {
          description = "Myorg description";
          buckets.mybucket = {
            description = "Mybucket description";
          };
          auths.mytoken = {
            operator = true;
            description = "operator token";
            tokenFile = pkgs.writeText "tmp-tok" "someusertoken";
          };
        };
        users.myuser.passwordFile = pkgs.writeText "tmp-pw" "abcgoiuhaoga";
      };
    };

    specialisation.withParentDelete.configuration = { ... }: {
      services.influxdb2.provision = {
        organizations.someorg.present = false;
        # Deleting the parent implies:
        #organizations.someorg.buckets.somebucket.present = false;
        #organizations.someorg.auths.sometoken.present = false;
      };
    };

    specialisation.withNewTokens.configuration = { ... }: {
      services.influxdb2.provision = {
        organizations.default = {
          auths.operator = {
            operator = true;
            description = "new optoken";
            tokenFile = pkgs.writeText "tmp-tok" "newoptoken";
          };
          auths.allaccess = {
            operator = true;
            description = "new allaccess";
            tokenFile = pkgs.writeText "tmp-tok" "newallaccess";
          };
          auths.specifics = {
            description = "new specifics";
            readPermissions = ["users" "tasks"];
            writePermissions = ["tasks"];
            tokenFile = pkgs.writeText "tmp-tok" "newspecificstoken";
          };
        };
      };
    };
  };

  testScript = { nodes, ... }:
    let
      specialisations = "${nodes.machine.system.build.toplevel}/specialisation";
      tokenArg = "--token verysecureadmintoken";
    in ''
      def assert_contains(haystack, needle):
          if needle not in haystack:
              print("The haystack that will cause the following exception is:")
              print("---")
              print(haystack)
              print("---")
              raise Exception(f"Expected string '{needle}' was not found")

      def assert_lacks(haystack, needle):
          if needle in haystack:
              print("The haystack that will cause the following exception is:")
              print("---")
              print(haystack, end="")
              print("---")
              raise Exception(f"Unexpected string '{needle}' was found")

      machine.wait_for_unit("influxdb2.service")

      machine.fail("curl --fail -X POST 'http://localhost:8086/api/v2/signin' -u admin:wrongpassword")
      machine.succeed("curl --fail -X POST 'http://localhost:8086/api/v2/signin' -u admin:ExAmPl3PA55W0rD")

      out = machine.succeed("influx org list ${tokenArg}")
      assert_contains(out, "default")
      assert_lacks(out, "myorg")
      assert_contains(out, "someorg")

      out = machine.succeed("influx bucket list ${tokenArg} --org default")
      assert_contains(out, "default")

      machine.fail("influx bucket list ${tokenArg} --org myorg")

      out = machine.succeed("influx bucket list ${tokenArg} --org someorg")
      assert_contains(out, "somebucket")

      out = machine.succeed("influx user list ${tokenArg}")
      assert_contains(out, "admin")
      assert_lacks(out, "myuser")
      assert_contains(out, "someuser")

      out = machine.succeed("influx auth list ${tokenArg}")
      assert_lacks(out, "operator token")
      assert_contains(out, "some auth token")

      with subtest("withModifications"):
        machine.succeed('${specialisations}/withModifications/bin/switch-to-configuration test')
        machine.wait_for_unit("influxdb2.service")

        out = machine.succeed("influx org list ${tokenArg}")
        assert_contains(out, "default")
        assert_contains(out, "myorg")
        assert_contains(out, "someorg")

        out = machine.succeed("influx bucket list ${tokenArg} --org myorg")
        assert_contains(out, "mybucket")

        out = machine.succeed("influx bucket list ${tokenArg} --org someorg")
        assert_lacks(out, "somebucket")

        out = machine.succeed("influx user list ${tokenArg}")
        assert_contains(out, "admin")
        assert_contains(out, "myuser")
        assert_lacks(out, "someuser")

        out = machine.succeed("influx auth list ${tokenArg}")
        assert_contains(out, "operator token")
        assert_lacks(out, "some auth token")

        # Make sure the user token is also usable
        machine.succeed("influx auth list --token someusertoken")

      with subtest("keepsUnrelated"):
        machine.succeed('${nodes.machine.system.build.toplevel}/bin/switch-to-configuration test')
        machine.wait_for_unit("influxdb2.service")

        out = machine.succeed("influx org list ${tokenArg}")
        assert_contains(out, "default")
        assert_contains(out, "myorg")
        assert_contains(out, "someorg")

        out = machine.succeed("influx bucket list ${tokenArg} --org default")
        assert_contains(out, "default")

        out = machine.succeed("influx bucket list ${tokenArg} --org myorg")
        assert_contains(out, "mybucket")

        out = machine.succeed("influx bucket list ${tokenArg} --org someorg")
        assert_contains(out, "somebucket")

        out = machine.succeed("influx user list ${tokenArg}")
        assert_contains(out, "admin")
        assert_contains(out, "myuser")
        assert_contains(out, "someuser")

        out = machine.succeed("influx auth list ${tokenArg}")
        assert_contains(out, "operator token")
        assert_contains(out, "some auth token")

      with subtest("withParentDelete"):
        machine.succeed('${specialisations}/withParentDelete/bin/switch-to-configuration test')
        machine.wait_for_unit("influxdb2.service")

        out = machine.succeed("influx org list ${tokenArg}")
        assert_contains(out, "default")
        assert_contains(out, "myorg")
        assert_lacks(out, "someorg")

        out = machine.succeed("influx bucket list ${tokenArg} --org default")
        assert_contains(out, "default")

        out = machine.succeed("influx bucket list ${tokenArg} --org myorg")
        assert_contains(out, "mybucket")

        machine.fail("influx bucket list ${tokenArg} --org someorg")

        out = machine.succeed("influx user list ${tokenArg}")
        assert_contains(out, "admin")
        assert_contains(out, "myuser")
        assert_contains(out, "someuser")

        out = machine.succeed("influx auth list ${tokenArg}")
        assert_contains(out, "operator token")
        assert_lacks(out, "some auth token")

      with subtest("withNewTokens"):
        machine.succeed('${specialisations}/withNewTokens/bin/switch-to-configuration test')
        machine.wait_for_unit("influxdb2.service")

        out = machine.succeed("influx auth list ${tokenArg}")
        assert_contains(out, "operator token")
        assert_contains(out, "some auth token")
        assert_contains(out, "new optoken")
        assert_contains(out, "new allaccess")
        assert_contains(out, "new specifics")
    '';
})