From 37fe99418c6edc2ab1b5c93884862a7e91b682e1 Mon Sep 17 00:00:00 2001 From: Samir Talwar Date: Fri, 5 May 2023 15:00:31 +0200 Subject: [PATCH] dc-agents/postgres: Test on random ports. The ports we use for testing often collide with other containers that other test suites leave lying around. This is annoying. This fixes the problem by ensuring that PostgreSQL runs a on random port. FWe can query Docker to find out the allocated port. I also changed the `make` target to run the agent too (though I kept the old behavior under a different target name). PR-URL: https://github.com/hasura/graphql-engine-mono/pull/9053 GitOrigin-RevId: 3ddea297905c0019c5ac42d896f5609f0d079e93 --- scripts/make/tests.mk | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/scripts/make/tests.mk b/scripts/make/tests.mk index bfbd82af600..b7fdb23853b 100644 --- a/scripts/make/tests.mk +++ b/scripts/make/tests.mk @@ -243,6 +243,36 @@ upgrade-tests: .PHONY: test-dc-postgres-agent ## test-dc-postgres-agent: run the dc-api test suite against the postgres agent +# The commands below are obtuse, so here they are, explained. (We can't comment inline.) +# 1. Build the agent. +# 2. Bring up the PostgreSQL container. +# 3. Run the agent on port 8889, and wait a second for it to start. +# 4. Ensure we clean up after ourselves. +# 5. Get the port of the PostgreSQL container. +# 6. Run the tests, pointing to the correct port for the DB. test-dc-postgres-agent: + cabal build postgres-agent:exe:postgres-agent $(DC_POSTGRES_DOCKER_COMPOSE) up --wait - cabal run test:tests-dc-api -- test --agent-base-url "http://localhost:8888" --agent-config '{ "connection": "postgresql://hasura:hasura@localhost:65002/hasura"}' sandwich --tui + @ echo 'cabal run postgres-agent:exe:postgres-agent'; \ + cabal run postgres-agent:exe:postgres-agent -- --port 8889 & trap "kill $$!" EXIT; \ + sleep 1; \ + PG_PORT="$$($(DC_POSTGRES_DOCKER_COMPOSE) port postgres 5432 | sed 's/.*://')"; \ + echo 'cabal run test:tests-dc-api -- test'; \ + cabal run test:tests-dc-api -- \ + test \ + --agent-base-url 'http://localhost:8889' \ + --agent-config "{\"connection\": \"postgresql://postgres:password@localhost:$${PG_PORT}\"}" \ + sandwich + +.PHONY: test-dc-postgres-agent-only +## test-dc-postgres-agent-only: run the dc-api test suite against an already-running postgres agent on port 8888 +test-dc-postgres-agent-only: + # See above for an explanation. + $(DC_POSTGRES_DOCKER_COMPOSE) up --wait + @ echo 'cabal run test:tests-dc-api -- test' + @ PG_PORT="$$($(DC_POSTGRES_DOCKER_COMPOSE) port postgres 5432 | sed 's/.*://')"; \ + cabal run test:tests-dc-api -- \ + test \ + --agent-base-url "http://localhost:8888" \ + --agent-config "{\"connection\": \"postgresql://postgres:password@localhost:$${PG_PORT}\"}" \ + sandwich --tui