diff --git a/justfile b/justfile index 7b0caf92..d606f35a 100644 --- a/justfile +++ b/justfile @@ -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 diff --git a/nix/postgres_test.nix b/nix/postgres_test.nix index 78bb3e82..73261d27 100644 --- a/nix/postgres_test.nix +++ b/nix/postgres_test.nix @@ -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; - }; } diff --git a/nix/redis-cluster_test.nix b/nix/redis-cluster_test.nix index 1427c82d..ba5f54e0 100644 --- a/nix/redis-cluster_test.nix +++ b/nix/redis-cluster_test.nix @@ -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"; + }; + } diff --git a/nix/redis_test.nix b/nix/redis_test.nix index 904035a9..73a1e55b 100644 --- a/nix/redis_test.nix +++ b/nix/redis_test.nix @@ -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"; + }; } diff --git a/test/flake.lock b/test/flake.lock index 80437a01..aa2c5fab 100644 --- a/test/flake.lock +++ b/test/flake.lock @@ -54,11 +54,11 @@ }, "process-compose-flake": { "locked": { - "lastModified": 1691511241, - "narHash": "sha256-sp6YH8WjBb6AquOS7dKqBEtX+sq8rIKAuYUjGjkzLzo=", + "lastModified": 1691673752, + "narHash": "sha256-2VcPgMkqw1+G/WR2QeKM/0QKa5idAwAxCnaoP+SMfm4=", "owner": "shivaraj-bh", "repo": "process-compose-flake", - "rev": "2d37251414abe36e2c56718e74cce65940f326b6", + "rev": "9698efdaf9a6cfeb1687818c5c14051561675f0f", "type": "github" }, "original": {