Skip to content

Commit

Permalink
add just test; support native test for rest of the services
Browse files Browse the repository at this point in the history
  • Loading branch information
shivaraj-bh committed Aug 10, 2023
1 parent a95805a commit 32a12fe
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 52 deletions.
4 changes: 4 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ ex:
# Auto-format the project tree
fmt:
treefmt

# Run native tests
test:
nix flake check test/ --override-input services-flake . -L
36 changes: 17 additions & 19 deletions nix/postgres_test.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,24 @@
initialScript.before = "CREATE USER bar;";
initialScript.after = "CREATE DATABASE foo OWNER bar;";
};
settings.processes.test =
let
cfg = config.services.postgres."pg1";
in
{
disabled = true;
command = pkgs.writeShellApplication {
text = ''
echo 'SELECT version();' | ${cfg.package}/bin/psql -h 127.0.0.1
echo 'SHOW hba_file;' | ${cfg.package}/bin/psql -h 127.0.0.1 | ${pkgs.gawk}/bin/awk 'NR==3' | ${pkgs.gnugrep}/bin/grep '^ /nix/store'
settings.processes.test =
let
cfg = config.services.postgres."pg1";
in
{
command = pkgs.writeShellApplication {
text = ''
echo 'SELECT version();' | ${cfg.package}/bin/psql -h 127.0.0.1
echo 'SHOW hba_file;' | ${cfg.package}/bin/psql -h 127.0.0.1 | ${pkgs.gawk}/bin/awk 'NR==3' | ${pkgs.gnugrep}/bin/grep '^ /nix/store'
# initialScript.before test
echo "SELECT 1 FROM pg_roles WHERE rolname = 'bar';" | ${cfg.package}/bin/psql -h 127.0.0.1 | ${pkgs.gnugrep}/bin/grep -q 1
# initialScript.before test
echo "SELECT 1 FROM pg_roles WHERE rolname = 'bar';" | ${cfg.package}/bin/psql -h 127.0.0.1 | ${pkgs.gnugrep}/bin/grep -q 1
# initialScript.after test
echo "SELECT 1 FROM pg_database WHERE datname = 'foo';" | ${cfg.package}/bin/psql -h 127.0.0.1 | ${pkgs.gnugrep}/bin/grep -q 1
'';
name = "postgres-test";
# initialScript.after test
echo "SELECT 1 FROM pg_database WHERE datname = 'foo';" | ${cfg.package}/bin/psql -h 127.0.0.1 | ${pkgs.gnugrep}/bin/grep -q 1
'';
name = "postgres-test";
};
depends_on."pg1".condition = "process_healthy";
};
depends_on."pg1".condition = "process_healthy";
availability.exit_on_end = true;
};
}
35 changes: 21 additions & 14 deletions nix/redis-cluster_test.nix
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
{ config, ... }: {
{ pkgs, config, ... }: {
services.redis-cluster."c1".enable = true;
testScript = ''
process_compose.wait_until(lambda procs:
# TODO: Check for 'is_ready' of `c1-cluster-create` instead of `c1-n1` (status of `c1-cluster-create` determines whether the hashslots are assigned).
# This should be easy after https://github.com/juspay/services-flake/issues/32
procs["c1-n1"]["status"] == "Running"
)
machine.succeed("${config.services.redis-cluster.c1.package}/bin/redis-cli -p 30001 ping | grep -q 'PONG'")
machine.succeed("${config.services.redis-cluster.c1.package}/bin/redis-cli -p 30002 ping | grep -q 'PONG'")
machine.succeed("${config.services.redis-cluster.c1.package}/bin/redis-cli -p 30003 ping | grep -q 'PONG'")
machine.succeed("${config.services.redis-cluster.c1.package}/bin/redis-cli -p 30004 ping | grep -q 'PONG'")
machine.succeed("${config.services.redis-cluster.c1.package}/bin/redis-cli -p 30005 ping | grep -q 'PONG'")
machine.succeed("${config.services.redis-cluster.c1.package}/bin/redis-cli -p 30006 ping | grep -q 'PONG'")
'';

settings.processes.test =
let
cfg = config.services.redis-cluster."c1";
in
{
command = pkgs.writeShellApplication {
text = ''
${cfg.package}/bin/redis-cli -p 30001 ping | ${pkgs.gnugrep}/bin/grep -q "PONG"
${cfg.package}/bin/redis-cli -p 30002 ping | ${pkgs.gnugrep}/bin/grep -q "PONG"
${cfg.package}/bin/redis-cli -p 30003 ping | ${pkgs.gnugrep}/bin/grep -q "PONG"
${cfg.package}/bin/redis-cli -p 30004 ping | ${pkgs.gnugrep}/bin/grep -q "PONG"
${cfg.package}/bin/redis-cli -p 30005 ping | ${pkgs.gnugrep}/bin/grep -q "PONG"
${cfg.package}/bin/redis-cli -p 30006 ping | ${pkgs.gnugrep}/bin/grep -q "PONG"
'';
name = "redis-cluster-test";
};
depends_on."c1-cluster-create".condition = "process_completed";
};

}
31 changes: 15 additions & 16 deletions nix/redis_test.nix
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
{ config, ... }: {
{ pkgs, config, ... }: {
services.redis."redis1".enable = true;
services.redis."redis2" = {
enable = true;
port = 6380;
};
testScript = ''
process_compose.wait_until(lambda procs:
# TODO: Check for 'is_ready' instead of 'status'
procs["redis1"]["status"] == "Running"
)
process_compose.wait_until(lambda procs:
procs["redis2"]["status"] == "Running"
)
machine.succeed("${config.services.redis.redis1.package}/bin/redis-cli ping | grep -q 'PONG'")
machine.succeed("${config.services.redis.redis2.package}/bin/redis-cli -p 6380 ping | grep -q 'PONG'")
'';

settings.processes.test =
let
cfg = config.services.redis."redis1";
in
{
command = pkgs.writeShellApplication {
text = ''
${cfg.package}/bin/redis-cli ping | ${pkgs.gnugrep}/bin/grep -q "PONG"
'';
name = "postgres-test";
};
depends_on."redis1".condition = "process_healthy";
};
}
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.

0 comments on commit 32a12fe

Please sign in to comment.