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 Oct 27, 2024
1 parent 0cb6a66 commit e9e2f96
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 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));

22 changes: 20 additions & 2 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 the `pg4` process is skipped due to errors in `pg4-init`
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 @@ -51,6 +66,9 @@
# 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` is Skipped as a result of `pg4-init` failing due to queries in `bad_test.sql`
curl --unix-socket pc-${name}.sock http://localhost/process/pg4 | jq '.status == "Skipped"' | 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 e9e2f96

Please sign in to comment.