Skip to content

Commit

Permalink
test(postgres): Error while init'ing database should fail the init pr…
Browse files Browse the repository at this point in the history
…ocess
  • Loading branch information
shivaraj-bh committed Nov 4, 2024
1 parent eb239b0 commit 9598598
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
3 changes: 3 additions & 0 deletions nix/services/postgres/bad_test.sql
Original file line number Diff line number Diff line change
@@ -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));

29 changes: 26 additions & 3 deletions nix/services/postgres/postgres_test.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ pkgs, config, ... }: {
{ pkgs, config, name, ... }: {
services.postgres."pg1" = {
enable = true;
initialScript.before = "CREATE USER bar;";
Expand Down Expand Up @@ -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'
Expand All @@ -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";
};
Expand Down
6 changes: 3 additions & 3 deletions test/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions test/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 ([
Expand Down

0 comments on commit 9598598

Please sign in to comment.