From e46a232fd7760194d99241bf9a674fa7edbbbab1 Mon Sep 17 00:00:00 2001 From: jc <46619361+juancwu@users.noreply.github.com> Date: Sat, 9 Nov 2024 16:05:42 -0500 Subject: [PATCH 1/4] added basic postgres service for unit test action --- .github/workflows/run-tests.yaml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/.github/workflows/run-tests.yaml b/.github/workflows/run-tests.yaml index 3fb25f3..503b809 100644 --- a/.github/workflows/run-tests.yaml +++ b/.github/workflows/run-tests.yaml @@ -6,6 +6,15 @@ jobs: test: name: Run Tests runs-on: ubuntu-latest + services: + postgres: + image: postgres + env: + POSTGRES_PASSWORD: postgres + options: >- + --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5 + ports: + - 5432:5432 steps: - uses: actions/checkout@v4 - name: Set up Go From 9b93a9c22d5fc24348d0ea1a52a8e25a23b241bf Mon Sep 17 00:00:00 2001 From: jc <46619361+juancwu@users.noreply.github.com> Date: Sat, 9 Nov 2024 21:21:13 -0500 Subject: [PATCH 2/4] run migrations on test db in CI/CD --- .github/workflows/run-tests.yaml | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/.github/workflows/run-tests.yaml b/.github/workflows/run-tests.yaml index 503b809..ef40ef5 100644 --- a/.github/workflows/run-tests.yaml +++ b/.github/workflows/run-tests.yaml @@ -8,9 +8,12 @@ jobs: runs-on: ubuntu-latest services: postgres: - image: postgres + image: postgres:16 env: + POSTGRES_USER: postgres POSTGRES_PASSWORD: postgres + POSTGRES_PORT: 5432 + POSTGRES_DB: postgres options: >- --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5 ports: @@ -24,6 +27,10 @@ jobs: cache: true - name: Install dependencies run: go mod download + - name: Install Goose + run: go install github.com/pressly/goose/v3/cmd/goose@v3.22.1 + - name: Run migrations + run: goose -dir .sqlc/migrations postgres "postgres://postgres:postgres@localhost:postgres/postgres?sslmode=disable" up - name: Run tests run: go test -v ./... # Comment test results on the PR From 239a3630e9fc7bd30656734866fa504c9825226d Mon Sep 17 00:00:00 2001 From: jc <46619361+juancwu@users.noreply.github.com> Date: Sat, 9 Nov 2024 21:21:29 -0500 Subject: [PATCH 3/4] update make test command --- Makefile | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index ad3ac81..3a8f7b3 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,12 @@ # migration name NAME ?= name -TEST_DB_PASSWORD ?= password +# default database configuration +TEST_DB_USER ?= postgres +TEST_DB_PASSWORD ?= postgres +TEST_DB_NAME ?= postgres TEST_DB_HOST_PORT ?= 5432 -TEST_DB_URL ?= postgres://postgres:$(TEST_DB_PASSWORD)@localhost:$(TEST_DB_HOST_PORT)/postgres?sslmode=disable -POSTGRESQL_VERSION=16 +TEST_DB_URL ?= postgres://$(TEST_DB_USER):$(TEST_DB_PASSWORD)@localhost:$(TEST_DB_HOST_PORT)/$(TEST_DB_NAME)?sslmode=disable +POSTGRESQL_VERSION ?= 16 .PHONY: query @@ -54,14 +57,31 @@ test: start-testdb start-testdb: @echo "Setting up PostgreSQL database..." - @docker run --name test-postgres -e POSTGRES_PASSWORD=$(TEST_DB_PASSWORD) -d -p "$(TEST_DB_HOST_PORT):5432" postgres:$(POSTGRESQL_VERSION) + @if ! docker ps -a --filter "name=test-postgres" --format '{{.Names}}' | grep -q '^test-postgres$$'; then \ + docker run --name test-postgres \ + -e POSTGRES_USER=$(TEST_DB_USER) \ + -e POSTGRES_PASSWORD=$(TEST_DB_PASSWORD) \ + -e POSTGRES_DB=$(TEST_DB_NAME) \ + -d -p "$(TEST_DB_HOST_PORT):5432" \ + postgres:$(POSTGRESQL_VERSION); \ + elif ! docker ps --filter "name=test-postgres" --format '{{.Names}}' | grep -q '^test-postgres$$'; then \ + docker start test-postgres; \ + fi @echo "Waiting for PostgreSQL to be ready..." - @until docker exec test-postgres pg_isready -U postgres; do sleep 1; done - @goose -dir ./migrations postgres $(TEST_DB_URL) up + @timeout 30s bash -c 'until docker exec test-postgres pg_isready -U $(TEST_DB_USER); do sleep 1; done' + @echo "Running migrations..." + @goose -dir .sqlc/migrations postgres "$(TEST_DB_URL)" up run-tests: - APP_ENV=test VERSION=0.0.0-test PASS_ENCRYPT_ALGO=md5 DB_URL=$(TEST_DB_URL) go test -v ./router ./store + @go test -v ./... stop-testdb: - @docker container stop test-postgres - @docker container rm -f test-postgres + @echo "Stopping test db..." + @docker container stop test-postgres || true + @echo "Removing test db..." + @docker container rm -f test-postgres || true + +# helper target to check database connection +check-testdb: + @echo "Testing database connection..." + @docker exec test-postgres psql -U $(TEST_DB_USER) -d $(TEST_DB_NAME) -c "SELECT 1" From 031580d9570e063a472073b06e7d14f7b7693237 Mon Sep 17 00:00:00 2001 From: jc <46619361+juancwu@users.noreply.github.com> Date: Sat, 9 Nov 2024 21:24:38 -0500 Subject: [PATCH 4/4] fix incorrect port in run-tests.yml --- .github/workflows/run-tests.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/run-tests.yaml b/.github/workflows/run-tests.yaml index ef40ef5..2ab4c7d 100644 --- a/.github/workflows/run-tests.yaml +++ b/.github/workflows/run-tests.yaml @@ -30,7 +30,7 @@ jobs: - name: Install Goose run: go install github.com/pressly/goose/v3/cmd/goose@v3.22.1 - name: Run migrations - run: goose -dir .sqlc/migrations postgres "postgres://postgres:postgres@localhost:postgres/postgres?sslmode=disable" up + run: goose -dir .sqlc/migrations postgres "postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable" up - name: Run tests run: go test -v ./... # Comment test results on the PR