Skip to content

Commit

Permalink
Process as test (VM -> Native test) (#38)
Browse files Browse the repository at this point in the history
Co-authored-by: Sridhar Ratnakumar <[email protected]>
  • Loading branch information
shivaraj-bh and srid authored Aug 23, 2023
1 parent 3a8dcaf commit 72a3eaa
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 74 deletions.
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
6 changes: 3 additions & 3 deletions example/flake.lock

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

21 changes: 11 additions & 10 deletions example/flake.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
description = "A demo of sqlite-web";
description = "A demo of sqlite-web and multiple postgres services";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
flake-parts.url = "github:hercules-ci/flake-parts";
Expand Down Expand Up @@ -53,15 +53,16 @@
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}")
'';
settings.processes.test = {
command = pkgs.writeShellApplication {
name = "pg1-test";
runtimeInputs = [ config.services.postgres.pg1.package ];
text = ''
echo 'SELECT version();' | psql -h 127.0.0.1 ${dbName}
'';
};
depends_on."pg1".condition = "process_healthy";
};
};

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";
};
}
12 changes: 6 additions & 6 deletions test/flake.lock

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

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

0 comments on commit 72a3eaa

Please sign in to comment.