1{
2 system ? builtins.currentSystem,
3 config ? {},
4 pkgs ? import ../../.. { inherit system config; },
5 lib ? pkgs.lib
6}:
7
8let
9 inherit (import ./common.nix { inherit pkgs lib; }) mkTestName mariadbPackages;
10
11 replicateUser = "replicate";
12 replicatePassword = "secret";
13
14 makeTest = import ./../make-test-python.nix;
15
16 makeReplicationTest = {
17 package,
18 name ? mkTestName package,
19 }: makeTest {
20 name = "${name}-replication";
21 meta = with pkgs.lib.maintainers; {
22 maintainers = [ ajs124 das_j ];
23 };
24
25 nodes = {
26 primary = {
27 services.mysql = {
28 inherit package;
29 enable = true;
30 replication.role = "master";
31 replication.slaveHost = "%";
32 replication.masterUser = replicateUser;
33 replication.masterPassword = replicatePassword;
34 initialDatabases = [ { name = "testdb"; schema = ./testdb.sql; } ];
35 };
36 networking.firewall.allowedTCPPorts = [ 3306 ];
37 };
38
39 secondary1 = { nodes, ... }: {
40 services.mysql = {
41 inherit package;
42 enable = true;
43 replication.role = "slave";
44 replication.serverId = 2;
45 replication.masterHost = nodes.primary.networking.hostName;
46 replication.masterUser = replicateUser;
47 replication.masterPassword = replicatePassword;
48 };
49 };
50
51 secondary2 = { nodes, ... }: {
52 services.mysql = {
53 inherit package;
54 enable = true;
55 replication.role = "slave";
56 replication.serverId = 3;
57 replication.masterHost = nodes.primary.networking.hostName;
58 replication.masterUser = replicateUser;
59 replication.masterPassword = replicatePassword;
60 };
61 };
62 };
63
64 testScript = ''
65 primary.start()
66 primary.wait_for_unit("mysql")
67 primary.wait_for_open_port(3306)
68 # Wait for testdb to be fully populated (5 rows).
69 primary.wait_until_succeeds(
70 "sudo -u mysql mysql -u mysql -D testdb -N -B -e 'select count(id) from tests' | grep -q 5"
71 )
72
73 secondary1.start()
74 secondary2.start()
75 secondary1.wait_for_unit("mysql")
76 secondary1.wait_for_open_port(3306)
77 secondary2.wait_for_unit("mysql")
78 secondary2.wait_for_open_port(3306)
79
80 # wait for replications to finish
81 secondary1.wait_until_succeeds(
82 "sudo -u mysql mysql -u mysql -D testdb -N -B -e 'select count(id) from tests' | grep -q 5"
83 )
84 secondary2.wait_until_succeeds(
85 "sudo -u mysql mysql -u mysql -D testdb -N -B -e 'select count(id) from tests' | grep -q 5"
86 )
87
88 secondary2.succeed("systemctl stop mysql")
89 primary.succeed(
90 "echo 'insert into testdb.tests values (123, 456);' | sudo -u mysql mysql -u mysql -N"
91 )
92 secondary2.succeed("systemctl start mysql")
93 secondary2.wait_for_unit("mysql")
94 secondary2.wait_for_open_port(3306)
95 secondary2.wait_until_succeeds(
96 "echo 'select * from testdb.tests where Id = 123;' | sudo -u mysql mysql -u mysql -N | grep 456"
97 )
98 '';
99 };
100in
101 lib.mapAttrs (_: package: makeReplicationTest { inherit package; }) mariadbPackages