diff --git a/nix/services/postgres/bad_test.sql b/nix/services/postgres/bad_test.sql new file mode 100644 index 00000000..cbc29ee5 --- /dev/null +++ b/nix/services/postgres/bad_test.sql @@ -0,0 +1,3 @@ +-- Tests if the postgres process gets skipped when there is an error while setting up inital databases +CREATE STABLE users (id INT PRIMARY KEY, user_name VARCHAR(25)); + diff --git a/nix/services/postgres/postgres_test.nix b/nix/services/postgres/postgres_test.nix index 1adf9789..c3ad015e 100644 --- a/nix/services/postgres/postgres_test.nix +++ b/nix/services/postgres/postgres_test.nix @@ -1,4 +1,4 @@ -{ pkgs, config, ... }: { +{ pkgs, config, name, ... }: { services.postgres."pg1" = { enable = true; initialScript.before = "CREATE USER bar;"; @@ -26,16 +26,31 @@ } ]; }; + + # Tests if `pg4-init` fails due to `./bad_test.sql` + services.postgres."pg4" = { + enable = true; + socketDir = "./test/new/socket/path3"; + listen_addresses = ""; + initialDatabases = [ + { + name = "test-db"; + schemas = [ ./bad_test.sql ]; + } + ]; + }; # avoid both the processes trying to create `data` directory at the same time settings.processes."pg2-init".depends_on."pg1-init".condition = "process_completed_successfully"; settings.processes."pg3-init".depends_on."pg2-init".condition = "process_completed_successfully"; + settings.processes."pg4-init".depends_on."pg3-init".condition = "process_completed_successfully"; + settings.processes.test = let cfg = config.services.postgres."pg1"; in { command = pkgs.writeShellApplication { - runtimeInputs = [ cfg.package pkgs.gnugrep ]; + runtimeInputs = [ cfg.package pkgs.gnugrep pkgs.curl pkgs.jq ]; text = '' echo 'SELECT version();' | psql -h 127.0.0.1 echo 'SHOW hba_file;' | psql -h 127.0.0.1 | ${pkgs.gawk}/bin/awk 'NR==3' | grep '^ /nix/store' @@ -48,9 +63,17 @@ # schemas test echo "SELECT * from users where user_name = 'test_user';" | psql -h 127.0.0.1 -p 5433 -d sample-db | grep -q test_user - + # listen_addresses test echo "SELECT 1 FROM pg_database where datname = 'test-db';" | psql -h "$(readlink -f ${config.services.postgres.pg3.socketDir})" -d postgres | grep -q 1 + + # Test if `pg4-init` fails due to `bad_test.sql` + # + # The curl to process-compose server is documented in the swagger URL, http://localhost:8080, but since we are listening on unix socket here, you can use `socat` to temporarily pipe the port `8080` to the `pc-${name}.sock` (`socat TCP-LISTEN:8080,fork UNIX-CONNECT:/path/to/your/socket.sock + + # OpenAPI documentation from the swagger URL: + # /process/logs/{name}/{endOffset}/{limit} + curl --unix-socket pc-${name}.sock http://localhost/process/logs/pg4-init/30/0 | jq '.logs | contains(["ERROR: syntax error at or near \"STABLE\""])' | grep "true" ''; name = "postgres-test"; }; diff --git a/test/flake.lock b/test/flake.lock index 12a8dae8..19d690e9 100644 --- a/test/flake.lock +++ b/test/flake.lock @@ -48,11 +48,11 @@ }, "process-compose-flake": { "locked": { - "lastModified": 1727723765, - "narHash": "sha256-/3az0h2Zp02bjJQKliR0PEQiT+FMLOR1GumhH42jA4w=", + "lastModified": 1729544733, + "narHash": "sha256-ir7WTVpG999N07wkOCs1kwZsQKitOv3CNDqNalCMK3c=", "owner": "Platonic-Systems", "repo": "process-compose-flake", - "rev": "22f2aeb7e0b88006d1d27065f8422f2790d99e47", + "rev": "7241e6b247b65cfa579d6f838a8efc18e5401924", "type": "github" }, "original": { diff --git a/test/flake.nix b/test/flake.nix index f5a43b54..fbb1f732 100644 --- a/test/flake.nix +++ b/test/flake.nix @@ -30,6 +30,12 @@ inputs.services-flake.processComposeModules.default mod ]; + cli = { + options = { + use-uds = true; + unix-socket = "pc-${name}.sock"; + }; + }; }; in builtins.listToAttrs (builtins.map mkPackageFor ([