Cannot connect to Postgres Database when running Quarkus Tests with Gitlab ci

44 views Asked by At

I am facing an issue when trying to config quarkus test running pipeline on GitLab. I usually get the error Caused by: org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections. I used maven to execute tests

And here is my .gitlab-ci.yml:

image: maven:latest

variables:
  MAVEN_OPTS: >-
    -Dhttps.protocols=TLSv1.2
    -Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository
    -Dorg.slf4j.simpleLogger.showDateTime=true
    -Djava.awt.headless=true
  MAVEN_CLI_OPTS: >-
    --batch-mode
    --errors
    --fail-at-end
    --show-version
    --no-transfer-progress

cache:
  paths:
    - .m2/repository

workflow:
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'

stages:
  - test

test:
  stage: test
  services:
    - name: docker:dind
      alias: localhost
      command: [ "--tls=false" ]
  variables:
    # Instruct Testcontainers to use the daemon of DinD, use port 2375 for non-tls connections.
    DOCKER_HOST: "tcp://docker:2375"
    POSTGRES_NETWORK_MODE: "host"
    DOCKER_DRIVER: overlay2

  script:
    - 'mvn test'

Also my application.properties file:

quarkus.http.root-path=/my-service/api

# Database Configuration
quarkus.datasource.db-kind=postgresql
quarkus.datasource.devservices.enabled=true
quarkus.datasource.devservices.db-name=my_db
quarkus.datasource.devservices.username=admin
quarkus.datasource.devservices.password=admin
quarkus.datasource.devservices.port=5432
quarkus.datasource.jdbc.url=jdbc:postgresql://localhost:5432/my_db
quarkus.datasource.jdbc.max-size=16

# Configuration for Swagger
quarkus.smallrye-openapi.security-scheme=jwt
quarkus.smallrye-openapi.security-scheme-name=accessToken

# OIDC Configuration for Dev Profile
%dev.quarkus.oidc.auth-server-url=http://localhost:8088/realms/my-realm
%dev.quarkus.oidc.client-id=my-service
%dev.quarkus.oidc.credentials.secret=aa12LO1abcjI6khjklSTUAZUF0xj123W
%dev.quarkus.oidc.tls.verification=none

# SWAGGER
mp.openapi.extensions.smallrye.operationIdStrategy=PACKAGE_CLASS_METHOD

I tried many ways:

  • Create a java class test to dynamically the host implementing QuarkusTestResourceLifecycleManager
  • Add quarkus.datasource.devservices.container-network=host into my application.properties
  • Add alias: localhost into dind services in .gitlab-ci.yml
  • Hardcoded the host of the jdbc url inside application.properties to docker
  • Declare variable for db host and tried to use it in application.properties as below:

.gitlab-ci.yml:

variable:
  TESTCONTAINERS_HOST_OVERRIDE: "host.docker.internal"

application.properties:

quarkus.datasource.jdbc.url=jdbc:postgresql://${TESTCONTAINERS_HOST_OVERRIDE}:5432/my_db

Please help me. Thank you so much for your effort.

0

There are 0 answers