From 72a3eaacc8309e6a8960d5d3b2ec4947f50851d1 Mon Sep 17 00:00:00 2001 From: Shivaraj B H Date: Wed, 23 Aug 2023 22:47:20 +0530 Subject: [PATCH] Process as test (VM -> Native test) (#38) Co-authored-by: Sridhar Ratnakumar <3998+srid@users.noreply.github.com> --- .github/workflows/ci.yaml | 17 +++++++++++++++ example/flake.lock | 6 +++--- example/flake.nix | 21 ++++++++++--------- justfile | 4 ++++ nix/postgres_test.nix | 43 ++++++++++++++++---------------------- nix/redis-cluster_test.nix | 36 ++++++++++++++++++------------- nix/redis_test.nix | 32 ++++++++++++++-------------- test/flake.lock | 12 +++++------ test/flake.nix | 4 ++++ 9 files changed, 101 insertions(+), 74 deletions(-) create mode 100644 .github/workflows/ci.yaml diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..10047ed --- /dev/null +++ b/.github/workflows/ci.yaml @@ -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 diff --git a/example/flake.lock b/example/flake.lock index 5f0ea4e..ff90248 100644 --- a/example/flake.lock +++ b/example/flake.lock @@ -70,11 +70,11 @@ }, "process-compose-flake": { "locked": { - "lastModified": 1689622628, - "narHash": "sha256-V2FEzYpjBYed5Sf6zngbyk7Z0mClxvSu8jawRs0fvRE=", + "lastModified": 1692810585, + "narHash": "sha256-Zc8nDSVkTUskB5mp9fh1mmOIUvx2pJ5V/Uq/0kxQ6B0=", "owner": "Platonic-Systems", "repo": "process-compose-flake", - "rev": "67949a2e1c720838e233d3e7397ccddd7c9d4188", + "rev": "2a84a6c2c91c6244e5db8fd46551d5e7f31a85d3", "type": "github" }, "original": { diff --git a/example/flake.nix b/example/flake.nix index d8ffc96..5406829 100644 --- a/example/flake.nix +++ b/example/flake.nix @@ -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"; @@ -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 { diff --git a/justfile b/justfile index 7b0caf9..d606f35 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 168f484..bd0308e 100644 --- a/nix/postgres_test.nix +++ b/nix/postgres_test.nix @@ -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"; + }; } diff --git a/nix/redis-cluster_test.nix b/nix/redis-cluster_test.nix index 1427c82..4b6afd3 100644 --- a/nix/redis-cluster_test.nix +++ b/nix/redis-cluster_test.nix @@ -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"; + }; + } diff --git a/nix/redis_test.nix b/nix/redis_test.nix index 904035a..c7f5626 100644 --- a/nix/redis_test.nix +++ b/nix/redis_test.nix @@ -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"; + }; } diff --git a/test/flake.lock b/test/flake.lock index 47c9cc9..ae98ebc 100644 --- a/test/flake.lock +++ b/test/flake.lock @@ -20,11 +20,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1686089707, - "narHash": "sha256-LTNlJcru2qJ0XhlhG9Acp5KyjB774Pza3tRH0pKIb3o=", + "lastModified": 1691464053, + "narHash": "sha256-D21ctOBjr2Y3vOFRXKRoFr6uNBvE8q5jC4RrMxRZXTM=", "owner": "nixos", "repo": "nixpkgs", - "rev": "af21c31b2a1ec5d361ed8050edd0303c31306397", + "rev": "844ffa82bbe2a2779c86ab3a72ff1b4176cec467", "type": "github" }, "original": { @@ -54,11 +54,11 @@ }, "process-compose-flake": { "locked": { - "lastModified": 1689622628, - "narHash": "sha256-V2FEzYpjBYed5Sf6zngbyk7Z0mClxvSu8jawRs0fvRE=", + "lastModified": 1692810585, + "narHash": "sha256-Zc8nDSVkTUskB5mp9fh1mmOIUvx2pJ5V/Uq/0kxQ6B0=", "owner": "Platonic-Systems", "repo": "process-compose-flake", - "rev": "67949a2e1c720838e233d3e7397ccddd7c9d4188", + "rev": "2a84a6c2c91c6244e5db8fd46551d5e7f31a85d3", "type": "github" }, "original": { diff --git a/test/flake.nix b/test/flake.nix index 39bae5c..28a90c6 100644 --- a/test/flake.nix +++ b/test/flake.nix @@ -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