From 2d37f02053ca7f02c9fa139b01a175fcbd735c4d Mon Sep 17 00:00:00 2001 From: Pedro Felix Date: Mon, 3 Oct 2022 17:42:01 +0100 Subject: [PATCH] gh-2: adds docker compose to the github action tests --- .github/workflows/ci.yaml | 2 +- code/tic-tac-tow-service/build.gradle.kts | 15 +++++++++++++++ .../tests/Dockerfile-db-test | 3 +++ .../tests/scripts/wait-for-postgres.sh | 17 +++++++++++++++++ 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 code/tic-tac-tow-service/tests/scripts/wait-for-postgres.sh diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index c27891b..472490d 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -13,4 +13,4 @@ jobs: with: java-version: 17 - name: Build with Gradle - run: code/tic-tac-tow-service/gradlew -p code/tic-tac-tow-service ktlint \ No newline at end of file + run: code/tic-tac-tow-service/gradlew -p code/tic-tac-tow-service check \ No newline at end of file diff --git a/code/tic-tac-tow-service/build.gradle.kts b/code/tic-tac-tow-service/build.gradle.kts index e47031a..a5fa285 100644 --- a/code/tic-tac-tow-service/build.gradle.kts +++ b/code/tic-tac-tow-service/build.gradle.kts @@ -49,6 +49,19 @@ tasks.withType { useJUnitPlatform() } +task("dbTestsUp") { + commandLine("docker-compose", "up", "-d", "--build", "--force-recreate", "db-tests") +} + +task("dbTestsWait") { + commandLine("docker", "exec", "db-tests", "/app/bin/wait-for-postgres.sh", "localhost") + dependsOn("dbTestsUp") +} + +task("dbTestsDown") { + commandLine("docker-compose", "down") +} + // from https://pinterest.github.io/ktlint/install/integrations/#custom-gradle-integration-with-kotlin-dsl val outputDir = "${project.buildDir}/reports/ktlint/" val inputFiles = project.fileTree(mapOf("dir" to "src", "include" to "**/*.kt")) @@ -66,4 +79,6 @@ val ktlintCheck by tasks.creating(JavaExec::class) { tasks.named("check") { dependsOn(ktlintCheck) + dependsOn("dbTestsWait") + finalizedBy("dbTestsDown") } \ No newline at end of file diff --git a/code/tic-tac-tow-service/tests/Dockerfile-db-test b/code/tic-tac-tow-service/tests/Dockerfile-db-test index d275db0..6810ceb 100644 --- a/code/tic-tac-tow-service/tests/Dockerfile-db-test +++ b/code/tic-tac-tow-service/tests/Dockerfile-db-test @@ -6,4 +6,7 @@ WORKDIR /app COPY sql/create-schema.sql /docker-entrypoint-initdb.d/1_create.sql COPY sql/insert-test-data.sql /docker-entrypoint-initdb.d/2_insert-test-data.sql +COPY --chown=postgres:postgres ./tests/scripts/wait-for-postgres.sh ./bin/wait-for-postgres.sh +RUN chmod +x ./bin/wait-for-postgres.sh + EXPOSE 5432 diff --git a/code/tic-tac-tow-service/tests/scripts/wait-for-postgres.sh b/code/tic-tac-tow-service/tests/scripts/wait-for-postgres.sh new file mode 100644 index 0000000..35eeba4 --- /dev/null +++ b/code/tic-tac-tow-service/tests/scripts/wait-for-postgres.sh @@ -0,0 +1,17 @@ +#!/bin/sh +# wait-for-postgres.sh host cmd + +set -e + +host="$1" +shift +cmd="$@" + +>&2 echo "waiting for postgres on $host" +until pg_isready -h $host; do + >&2 echo "Postgres is unavailable - sleeping" + sleep 1 +done + +>&2 echo "Postgres is up - executing command '$cmd'" +exec $cmd \ No newline at end of file