Commit 554ec1c0 authored by Euan Kemp's avatar Euan Kemp
Browse files

nixos/tests: add postgresql wal2json test

This test should ensure wal2json functions.
I'm planning to upgrade wal2json, so it seems nice to have a test here.

It passes on my machine.
parent 864bb34b
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -805,6 +805,7 @@ in {
  postgresql-jit = handleTest ./postgresql-jit.nix {};
  postgresql-wal-receiver = handleTest ./postgresql-wal-receiver.nix {};
  postgresql-tls-client-cert = handleTest ./postgresql-tls-client-cert.nix {};
  postgresql-wal2json = handleTest ./postgresql-wal2json.nix {};
  powerdns = handleTest ./powerdns.nix {};
  powerdns-admin = handleTest ./powerdns-admin.nix {};
  power-profiles-daemon = handleTest ./power-profiles-daemon.nix {};
+60 −0
Original line number Diff line number Diff line
{
  system ? builtins.currentSystem,
  config ? { },
  pkgs ? import ../.. { inherit system config; },
  postgresql ? null,
}:

let
  makeTest = import ./make-test-python.nix;
  # Makes a test for a PostgreSQL package, given by name and looked up from `pkgs`.
  makeTestAttribute = name: {
    inherit name;
    value = makePostgresqlWal2jsonTest pkgs."${name}";
  };

  makePostgresqlWal2jsonTest =
    postgresqlPackage:
    makeTest {
      name = "postgresql-wal2json-${postgresqlPackage.name}";
      meta.maintainers = with pkgs.lib.maintainers; [ euank ];

      nodes.machine = {
        services.postgresql = {
          package = postgresqlPackage;
          enable = true;
          extraPlugins = with postgresqlPackage.pkgs; [ wal2json ];
          settings = {
            wal_level = "logical";
            max_replication_slots = "10";
            max_wal_senders = "10";
          };
        };
      };

      testScript = ''
        machine.wait_for_unit("postgresql")
        machine.succeed(
            "sudo -u postgres psql -qAt -f ${./postgresql/wal2json/example2.sql} postgres > /tmp/example2.out"
        )
        machine.succeed(
            "diff ${./postgresql/wal2json/example2.out} /tmp/example2.out"
        )
        machine.succeed(
            "sudo -u postgres psql -qAt -f ${./postgresql/wal2json/example3.sql} postgres > /tmp/example3.out"
        )
        machine.succeed(
            "diff ${./postgresql/wal2json/example3.out} /tmp/example3.out"
        )
      '';
    };

in
# By default, create one test per postgresql version
if postgresql == null then
  builtins.listToAttrs (
    map makeTestAttribute (builtins.attrNames (import ../../pkgs/servers/sql/postgresql pkgs))
  )
# but if postgresql is set, we're being made as a passthru test for a specific postgres + wal2json version, just run one
else
  makePostgresqlWal2jsonTest postgresql
+27 −0
Original line number Diff line number Diff line
Copyright (c) 2013-2024, Euler Taveira de Oliveira
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, this
  list of conditions and the following disclaimer in the documentation and/or
  other materials provided with the distribution.

* Neither the name of the Euler Taveira de Oliveira nor the names of its
  contributors may be used to endorse or promote products derived from
  this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+11 −0
Original line number Diff line number Diff line
Data in this folder taken from the wal2json README's examples [here](https://github.com/eulerto/wal2json/tree/75629c2e1e81a12350cc9d63782fc53252185d8d#sql-functions)

They are used under the terms of the BSD-3 License, a copy of which is included
in this directory.

These files have been lightly modified in order to make their output more reproducible.

Changes:
- `\o /dev/null` has been added before commands that print LSNs since LSNs aren't reproducible
- `now()` has been replaced with a hardcoded timestamp string for reproducibility
- The test is run with `--quiet`, and the expected output has been trimmed accordingly
+74 −0
Original line number Diff line number Diff line
init
{
	"change": [
		{
			"kind": "message",
			"transactional": false,
			"prefix": "wal2json",
			"content": "this non-transactional message will be delivered even if you rollback the transaction"
		}
	]
}
{
	"change": [
		{
			"kind": "insert",
			"schema": "public",
			"table": "table2_with_pk",
			"columnnames": ["a", "b", "c"],
			"columntypes": ["integer", "character varying(30)", "timestamp without time zone"],
			"columnvalues": [1, "Backup and Restore", "2018-03-27 12:05:29.914496"]
		}
		,{
			"kind": "insert",
			"schema": "public",
			"table": "table2_with_pk",
			"columnnames": ["a", "b", "c"],
			"columntypes": ["integer", "character varying(30)", "timestamp without time zone"],
			"columnvalues": [2, "Tuning", "2018-03-27 12:05:29.914496"]
		}
		,{
			"kind": "insert",
			"schema": "public",
			"table": "table2_with_pk",
			"columnnames": ["a", "b", "c"],
			"columntypes": ["integer", "character varying(30)", "timestamp without time zone"],
			"columnvalues": [3, "Replication", "2018-03-27 12:05:29.914496"]
		}
		,{
			"kind": "message",
			"transactional": true,
			"prefix": "wal2json",
			"content": "this message will be delivered"
		}
		,{
			"kind": "delete",
			"schema": "public",
			"table": "table2_with_pk",
			"oldkeys": {
				"keynames": ["a", "c"],
				"keytypes": ["integer", "timestamp without time zone"],
				"keyvalues": [1, "2018-03-27 12:05:29.914496"]
			}
		}
		,{
			"kind": "delete",
			"schema": "public",
			"table": "table2_with_pk",
			"oldkeys": {
				"keynames": ["a", "c"],
				"keytypes": ["integer", "timestamp without time zone"],
				"keyvalues": [2, "2018-03-27 12:05:29.914496"]
			}
		}
		,{
			"kind": "insert",
			"schema": "public",
			"table": "table2_without_pk",
			"columnnames": ["a", "b", "c"],
			"columntypes": ["integer", "numeric(5,2)", "text"],
			"columnvalues": [1, 2.34, "Tapir"]
		}
	]
}
stop
Loading