Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process as test (VM -> Native test) #38

Merged
merged 14 commits into from
Aug 23, 2023
Merged
17 changes: 17 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: "CI"
on:
push:
branches:
- main
pull_request:
jobs:
checks:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install Nix
uses: DeterminateSystems/nix-installer-action@main
- uses: yaxitech/nix-install-pkgs-action@v3
with:
packages: "nixpkgs#nixci"
- run: nixci
9 changes: 0 additions & 9 deletions example/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,6 @@
command = pkgs.pgweb;
depends_on."pg1".condition = "process_healthy";
};

# Set this attribute and get NixOS VM tests, as a flake check, for free.
testScript = ''
# FIXME: pgweb is still pending, but only in VM tests for some reason.
process_compose.wait_until(lambda procs:
procs["pg1"]["status"] == "Running"
)
machine.succeed("echo 'SELECT version();' | ${config.services.postgres.pg1.package}/bin/psql -h 127.0.0.1 -U tester ${dbName}")
'';
shivaraj-bh marked this conversation as resolved.
Show resolved Hide resolved
};

devShells.default = pkgs.mkShell {
Expand Down
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
43 changes: 18 additions & 25 deletions nix/postgres_test.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,25 @@
initialScript.before = "CREATE USER bar;";
initialScript.after = "CREATE DATABASE foo OWNER bar;";
};
services.postgres."pg2" = {
enable = true;
listen_addresses = "127.0.0.1";
port = 5433;
};
testScript =
settings.processes.test =
let
cfg = config.services.postgres."pg1";
psql =
"${cfg.package}/bin/psql";
in
''
process_compose.wait_until(lambda procs:
# TODO: Check for 'ready'
procs["pg1"]["status"] == "Running"
)
process_compose.wait_until(lambda procs:
procs["pg2"]["status"] == "Running"
)
machine.succeed("echo 'SELECT version();' | ${psql} -h 127.0.0.1 -U tester")
# Test if `pg2` is listening on the correct port
machine.succeed("echo 'SELECT version();' | ${psql} -h 127.0.0.1 -p 5433 -U tester")
machine.succeed("echo 'SHOW hba_file;' | ${psql} -h 127.0.0.1 -U tester | ${pkgs.gawk}/bin/awk 'NR==3' | ${pkgs.gnugrep}/bin/grep '^ /nix/store'")
# initialScript.before test
machine.succeed("echo \"SELECT 1 FROM pg_roles WHERE rolname = 'bar';\" | ${psql} -h 127.0.0.1 -U tester | grep -q 1")
# initialScript.after test
machine.succeed("echo \"SELECT 1 FROM pg_database WHERE datname = 'foo';\" | ${psql} -h 127.0.0.1 -U tester | grep -q 1")
'';
{
command = pkgs.writeShellApplication {
runtimeInputs = [ cfg.package pkgs.gnugrep ];
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'

# initialScript.before test
echo "SELECT 1 FROM pg_roles WHERE rolname = 'bar';" | psql -h 127.0.0.1 | grep -q 1

# initialScript.after test
echo "SELECT 1 FROM pg_database WHERE datname = 'foo';" | psql -h 127.0.0.1 | grep -q 1
'';
name = "postgres-test";
};
depends_on."pg1".condition = "process_healthy";
};
}
36 changes: 22 additions & 14 deletions nix/redis-cluster_test.nix
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
{ 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 {
runtimeInputs = [ cfg.package pkgs.gnugrep ];
text = ''
redis-cli -p 30001 ping | grep -q "PONG"
redis-cli -p 30002 ping | grep -q "PONG"
redis-cli -p 30003 ping | grep -q "PONG"
redis-cli -p 30004 ping | grep -q "PONG"
redis-cli -p 30005 ping | grep -q "PONG"
redis-cli -p 30006 ping | grep -q "PONG"
'';
name = "redis-cluster-test";
};
depends_on."c1-cluster-create".condition = "process_completed";
};

}
32 changes: 16 additions & 16 deletions nix/redis_test.nix
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{ 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 {
runtimeInputs = [ cfg.package pkgs.gnugrep ];
text = ''
redis-cli ping | grep -q "PONG"
'';
name = "redis-test";
};
depends_on."redis1".condition = "process_healthy";
};
}
17 changes: 9 additions & 8 deletions test/flake.lock

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

6 changes: 5 additions & 1 deletion test/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
flake-parts.url = "github:hercules-ci/flake-parts";
systems.url = "github:nix-systems/default";
process-compose-flake.url = "github:Platonic-Systems/process-compose-flake";
process-compose-flake.url = "github:shivaraj-bh/process-compose-flake/process-as-test";
services-flake.url = "github:juspay/services-flake";
};
outputs = inputs:
Expand All @@ -15,18 +15,22 @@
perSystem = { self', pkgs, lib, ... }: {
process-compose = {
postgres = {
# TODO: remove `port = 0`; as it will be default after this: https://github.com/Platonic-Systems/process-compose-flake/pull/42
port = 0;
imports = [
inputs.services-flake.processComposeModules.default
../nix/postgres_test.nix
];
};
redis = {
port = 0;
imports = [
inputs.services-flake.processComposeModules.default
../nix/redis_test.nix
];
};
redis-cluster = {
port = 0;
imports = [
inputs.services-flake.processComposeModules.default
../nix/redis-cluster_test.nix
Expand Down