summaryrefslogtreecommitdiffstats
path: root/nixos/tests/mysql/mysql.nix
blob: 4f3208796fca8ce102777a4f46bf23ae3eaef500 (plain)
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
{
  system ? builtins.currentSystem,
  config ? { },
  pkgs ? import ../../.. { inherit system config; },
  lib ? pkgs.lib,
}:

let
  inherit (import ./common.nix { inherit pkgs lib; })
    mkTestName
    mariadbPackages
    mysqlPackages
    perconaPackages
    ;

  makeTest = import ./../make-test-python.nix;
  # Setup common users
  makeMySQLTest =
    {
      package,
      name ? mkTestName package,
      useSocketAuth ? true,
      hasMroonga ? true,
      hasRocksDB ? pkgs.stdenv.hostPlatform.is64bit,
    }:
    makeTest {
      inherit name;
      meta.maintainers = with lib.maintainers; [
        conni2461
        das_j
        helsinki-Jo
      ];

      nodes = {
        ${name} =
          { pkgs, config, ... }:
          {

            users = {
              groups.testusers = { };

              users.testuser = {
                isSystemUser = true;
                group = "testusers";
              };

              users.testuser2 = {
                isSystemUser = true;
                group = "testusers";
              };
            };

            services.mysql = {
              enable = true;
              initialDatabases = [
                {
                  name = "testdb3";
                  schema = ./testdb.sql;
                }
              ];
              # note that using pkgs.writeText here is generally not a good idea,
              # as it will store the password in world-readable /nix/store ;)
              initialScript = pkgs.writeText "mysql-init.sql" (
                if config.services.mysql.package.pname == "mariadb" then
                  ''
                    ALTER USER root@localhost IDENTIFIED WITH unix_socket;
                    DELETE FROM mysql.user WHERE password = ''' AND plugin = ''';
                    DELETE FROM mysql.user WHERE user = ''';
                    FLUSH PRIVILEGES;
                  ''
                else
                  # just some smoketest initial script sql statement
                  ''
                    SELECT * FROM mysql.user;
                  ''
              );

              ensureDatabases = [
                "testdb"
                "testdb2"
              ];
              ensureUsers = [
                {
                  name = "testuser";
                  ensurePermissions = {
                    "testdb.*" = "ALL PRIVILEGES";
                  };
                }
                {
                  name = "testuser2";
                  ensurePermissions = {
                    "testdb2.*" = "ALL PRIVILEGES";
                  };
                }
              ];
              package = package;
              settings = {
                mysqld = {
                  plugin-load-add =
                    lib.optional hasMroonga "ha_mroonga.so" ++ lib.optional hasRocksDB "ha_rocksdb.so";
                };
              };
            };
          };
      };

      testScript =
        { nodes, ... }:
        let
          mysqlClient = lib.getExe nodes.${name}.services.mysql.package.client;
        in
        ''
          start_all()

          machine = ${name}
          machine.wait_for_unit("mysql")
          machine.succeed(
              "echo 'use testdb; create table tests (test_id INT, PRIMARY KEY (test_id));' | sudo -u testuser ${mysqlClient} -u testuser"
          )
          machine.succeed(
              "echo 'use testdb; insert into tests values (42);' | sudo -u testuser ${mysqlClient} -u testuser"
          )
          # Ensure testuser2 is not able to insert into testdb as mysql testuser2
          machine.fail(
              "echo 'use testdb; insert into tests values (23);' | sudo -u testuser2 ${mysqlClient} -u testuser2"
          )
          # Ensure testuser2 is not able to authenticate as mysql testuser
          machine.fail(
              "echo 'use testdb; insert into tests values (23);' | sudo -u testuser2 ${mysqlClient} -u testuser"
          )
          machine.succeed(
              "echo 'use testdb; select test_id from tests;' | sudo -u testuser ${mysqlClient} -u testuser -N | grep 42"
          )

          ${lib.optionalString hasMroonga ''
            # Check if Mroonga plugin works
            machine.succeed(
                "echo 'use testdb; create table mroongadb (test_id INT, PRIMARY KEY (test_id)) ENGINE = Mroonga;' | sudo -u testuser ${mysqlClient} -u testuser"
            )
            machine.succeed(
                "echo 'use testdb; insert into mroongadb values (25);' | sudo -u testuser ${mysqlClient} -u testuser"
            )
            machine.succeed(
                "echo 'use testdb; select test_id from mroongadb;' | sudo -u testuser ${mysqlClient} -u testuser -N | grep 25"
            )
            machine.succeed(
                "echo 'use testdb; drop table mroongadb;' | sudo -u testuser ${mysqlClient} -u testuser"
            )
          ''}

          ${lib.optionalString hasRocksDB ''
            # Check if RocksDB plugin works
            machine.succeed(
                "echo 'use testdb; create table rocksdb (test_id INT, PRIMARY KEY (test_id)) ENGINE = RocksDB;' | sudo -u testuser ${mysqlClient} -u testuser"
            )
            machine.succeed(
                "echo 'use testdb; insert into rocksdb values (28);' | sudo -u testuser ${mysqlClient} -u testuser"
            )
            machine.succeed(
                "echo 'use testdb; select test_id from rocksdb;' | sudo -u testuser ${mysqlClient} -u testuser -N | grep 28"
            )
            machine.succeed(
                "echo 'use testdb; drop table rocksdb;' | sudo -u testuser ${mysqlClient} -u testuser"
            )
          ''}
        '';
    };
in
lib.mapAttrs (
  _: package:
  makeMySQLTest {
    inherit package;
    hasRocksDB = false;
    hasMroonga = false;
    useSocketAuth = false;
  }
) mysqlPackages
// (lib.mapAttrs (
  _: package:
  makeMySQLTest {
    inherit package;
  }
) mariadbPackages)
// (lib.mapAttrs (
  _: package:
  makeMySQLTest {
    inherit package;
    hasMroonga = false;
    useSocketAuth = false;
  }
) perconaPackages)