at 25.11-pre 3.5 kB view raw
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 { 18 package, 19 name ? mkTestName package, 20 }: 21 makeTest { 22 name = "${name}-replication"; 23 meta = { 24 maintainers = lib.teams.helsinki-systems.members; 25 }; 26 27 nodes = { 28 primary = { 29 services.mysql = { 30 inherit package; 31 enable = true; 32 replication.role = "master"; 33 replication.slaveHost = "%"; 34 replication.masterUser = replicateUser; 35 replication.masterPassword = replicatePassword; 36 initialDatabases = [ 37 { 38 name = "testdb"; 39 schema = ./testdb.sql; 40 } 41 ]; 42 }; 43 networking.firewall.allowedTCPPorts = [ 3306 ]; 44 }; 45 46 secondary1 = 47 { nodes, ... }: 48 { 49 services.mysql = { 50 inherit package; 51 enable = true; 52 replication.role = "slave"; 53 replication.serverId = 2; 54 replication.masterHost = nodes.primary.networking.hostName; 55 replication.masterUser = replicateUser; 56 replication.masterPassword = replicatePassword; 57 }; 58 }; 59 60 secondary2 = 61 { nodes, ... }: 62 { 63 services.mysql = { 64 inherit package; 65 enable = true; 66 replication.role = "slave"; 67 replication.serverId = 3; 68 replication.masterHost = nodes.primary.networking.hostName; 69 replication.masterUser = replicateUser; 70 replication.masterPassword = replicatePassword; 71 }; 72 }; 73 }; 74 75 testScript = '' 76 primary.start() 77 primary.wait_for_unit("mysql") 78 primary.wait_for_open_port(3306) 79 # Wait for testdb to be fully populated (5 rows). 80 primary.wait_until_succeeds( 81 "sudo -u mysql mysql -u mysql -D testdb -N -B -e 'select count(id) from tests' | grep -q 5" 82 ) 83 84 secondary1.start() 85 secondary2.start() 86 secondary1.wait_for_unit("mysql") 87 secondary1.wait_for_open_port(3306) 88 secondary2.wait_for_unit("mysql") 89 secondary2.wait_for_open_port(3306) 90 91 # wait for replications to finish 92 secondary1.wait_until_succeeds( 93 "sudo -u mysql mysql -u mysql -D testdb -N -B -e 'select count(id) from tests' | grep -q 5" 94 ) 95 secondary2.wait_until_succeeds( 96 "sudo -u mysql mysql -u mysql -D testdb -N -B -e 'select count(id) from tests' | grep -q 5" 97 ) 98 99 secondary2.succeed("systemctl stop mysql") 100 primary.succeed( 101 "echo 'insert into testdb.tests values (123, 456);' | sudo -u mysql mysql -u mysql -N" 102 ) 103 secondary2.succeed("systemctl start mysql") 104 secondary2.wait_for_unit("mysql") 105 secondary2.wait_for_open_port(3306) 106 secondary2.wait_until_succeeds( 107 "echo 'select * from testdb.tests where Id = 123;' | sudo -u mysql mysql -u mysql -N | grep 456" 108 ) 109 ''; 110 }; 111in 112lib.mapAttrs (_: package: makeReplicationTest { inherit package; }) mariadbPackages