summaryrefslogtreecommitdiffstats
path: root/nixos/tests/postgresql/postgresql-replication.nix
blob: 6df0becdf115d3a7a82e6acb563ce37e56c81c79 (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
{
  runTest,
  genTests,
  ...
}:

let

  makeTestFor =
    package:
    runTest (
      { lib, ... }:
      {
        name = "postgresql-replication-${package.name}";
        meta.maintainers = with lib.maintainers; [ bouk ];

        nodes = {
          primary =
            { ... }:
            {
              services.postgresql = {
                inherit package;
                enable = true;
                enableTCPIP = true;
                settings = {
                  wal_level = "replica";
                  max_wal_senders = 10;
                  max_replication_slots = 10;
                };
                authentication = ''
                  local replication postgres peer
                  host replication replication all trust
                '';
                ensureUsers = [
                  {
                    name = "replication";
                    ensureClauses.replication = true;
                  }
                ];
              };
              networking.firewall.allowedTCPPorts = [ 5432 ];
            };

          replica =
            { nodes, ... }:
            {
              services.postgresql = {
                inherit package;
                enable = true;
                settings = {
                  hot_standby = "on";
                  primary_conninfo = "host=${nodes.primary.networking.primaryIPAddress} user=replication";
                  primary_slot_name = "replica_slot";
                };
              };
            };
        };

        testScript = ''
          start_all()
          primary.wait_for_unit("postgresql.target")

          primary.succeed(
              "sudo -u postgres psql -c \"SELECT * FROM pg_create_physical_replication_slot('replica_slot');\""
          )

          primary.succeed(
              "sudo -u postgres pg_basebackup -D /tmp/basebackup -S replica_slot -X stream"
          )
          primary.succeed("tar -C /tmp -cf /tmp/shared/basebackup.tar basebackup")

          replica.wait_for_unit("postgresql.target")
          replica.succeed("systemctl stop postgresql")

          replica_data_dir = "/var/lib/postgresql/${package.psqlSchema}"
          replica.succeed(f"rm -rf {replica_data_dir}")
          replica.succeed(f"mkdir -p {replica_data_dir}")
          replica.succeed(f"tar -C {replica_data_dir} --strip-components=1 -xf /tmp/shared/basebackup.tar")
          replica.succeed(f"touch {replica_data_dir}/standby.signal")
          replica.succeed(f"chown -R postgres:postgres {replica_data_dir}")
          replica.succeed(f"chmod 700 {replica_data_dir}")

          replica.succeed("systemctl start postgresql")
          replica.wait_for_unit("postgresql.target")

          replica.wait_until_succeeds(
              "sudo -u postgres psql -tAc 'SELECT pg_is_in_recovery();' | grep t"
          )

          primary.succeed(
              "sudo -u postgres psql -c 'CREATE TABLE test_replication (id serial PRIMARY KEY, data text);'"
          )
          primary.succeed(
              "sudo -u postgres psql -c \"INSERT INTO test_replication (data) VALUES ('hello');\""
          )

          replica.wait_until_succeeds(
              "sudo -u postgres psql -c 'SELECT * FROM test_replication;' | grep hello",
              timeout=30
          )

          with subtest("Verify replica is in recovery mode"):
              result = replica.succeed("sudo -u postgres psql -tAc 'SELECT pg_is_in_recovery();'")
              t.assertEqual(result.strip(), "t")

          with subtest("Verify replication slot is active"):
              result = primary.succeed(
                  "sudo -u postgres psql -tAc \"SELECT active FROM pg_replication_slots WHERE slot_name = 'replica_slot';\""
              )
              t.assertEqual(result.strip(), "t")

          with subtest("Insert more data and verify replication"):
              primary.succeed(
                  "sudo -u postgres psql -c \"INSERT INTO test_replication (data) VALUES ('world');\""
              )
              replica.wait_until_succeeds(
                  "sudo -u postgres psql -c 'SELECT * FROM test_replication;' | grep world",
                  timeout=30
              )

          primary.shutdown()
          replica.shutdown()
        '';
      }
    );
in
genTests { inherit makeTestFor; }