From 7db49f19cd38d99c79e3f2b6c4f0cfa961aea147 Mon Sep 17 00:00:00 2001 From: Ilja Date: Thu, 16 Nov 2023 13:01:47 +0200 Subject: [PATCH] Run tests in CI --- .github/workflows/build.yml | 85 ++++++++++++++++++++- api-gateway/.dockerignore | 5 ++ compose/db/test-db.sh | 7 ++ compose/docker-compose.e2e.yml | 2 + compose/docker-compose.test-service.yml | 16 ++++ compose/docker-compose.yml | 14 +--- compose/test-compose | 11 +++ service/.dockerignore | 6 ++ service/src/test/resources/application.yaml | 2 +- 9 files changed, 131 insertions(+), 17 deletions(-) create mode 100644 api-gateway/.dockerignore create mode 100644 compose/db/test-db.sh create mode 100644 compose/docker-compose.test-service.yml create mode 100755 compose/test-compose create mode 100644 service/.dockerignore diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index ed04ede..fbed036 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -5,6 +5,7 @@ on: env: AWS_REGION: eu-north-1 ECR_REGISTRY: 095341522062.dkr.ecr.eu-north-1.amazonaws.com + concurrency: group: ${{ github.workflow }}-${{ github.ref }} cancel-in-progress: true @@ -30,8 +31,10 @@ jobs: path: frontend - name: oppivelvollisuus/api-gateway path: api-gateway + test_target: "test" - name: oppivelvollisuus/service path: service + builder: builder steps: - uses: actions/checkout@v3 @@ -51,6 +54,82 @@ jobs: build=${{ github.run_number }} commit=${{ github.sha }} - outputs: - image: ${{ steps.build.outputs.image }} - image_name: ${{ steps.build.outputs.image_name }} + - name: Build builder + if: ${{ matrix.builder != '' }} + uses: espoon-voltti/voltti-actions/docker-build-push@master + id: builder + with: + path: ${{ matrix.path }} + DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} + DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }} + AWS_ROLE_TO_ASSUME: ${{ secrets.AWS_ROLE }} + AWS_REGION: ${{ env.AWS_REGION }} + registry: ${{ env.ECR_REGISTRY }} + name: ${{ matrix.name }}-${{ matrix.builder }} + build-args: | + build=${{ github.run_number }} + commit=${{ github.sha }} + target: ${{ matrix.builder }} + + - name: Build and run unit tests + if: ${{ matrix.test_target != '' }} + uses: espoon-voltti/voltti-actions/docker-build-push@master + id: test + with: + push: false + path: ${{ matrix.path }} + DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} + DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }} + AWS_ROLE_TO_ASSUME: ${{ secrets.AWS_ROLE }} + AWS_REGION: ${{ env.AWS_REGION }} + registry: ${{ env.ECR_REGISTRY }} + name: ${{ matrix.name }} + build-args: | + build=${{ github.run_number }} + commit=${{ github.sha }} + target: ${{ matrix.test_target }} + + test: + runs-on: ubuntu-latest + needs: dockerize + env: + BUILD: "false" + TAG: "${{ github.event.pull_request.head.sha || github.sha }}" + + defaults: + run: + working-directory: compose + + steps: + - uses: actions/checkout@v3 + + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v4 + with: + aws-region: ${{ env.AWS_REGION }} + role-to-assume: ${{ secrets.AWS_ROLE }} + role-duration-seconds: 1200 + + - name: Login to Amazon ECR + id: login-ecr + uses: aws-actions/amazon-ecr-login@v2 + + - name: Run tests + run: | + ./test-compose pull + ./test-compose run service-tests | tee tests.log + + - name: Get logs + if: always() + run: | + ./test-compose logs > tests-all.log + + - name: Store logs + if: always() + uses: actions/upload-artifact@v3 + with: + name: integration-test-results + path: | + compose/tests.log + compose/tests-all.log + retention-days: 2 diff --git a/api-gateway/.dockerignore b/api-gateway/.dockerignore new file mode 100644 index 0000000..37e24a5 --- /dev/null +++ b/api-gateway/.dockerignore @@ -0,0 +1,5 @@ +* + +!src +!*.json +!*.lock diff --git a/compose/db/test-db.sh b/compose/db/test-db.sh new file mode 100644 index 0000000..38dc6e7 --- /dev/null +++ b/compose/db/test-db.sh @@ -0,0 +1,7 @@ +#!/bin/bash +set -e + +psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL + CREATE DATABASE oppivelvollisuus_it; + GRANT ALL PRIVILEGES ON DATABASE oppivelvollisuus_it TO oppivelvollisuus; +EOSQL diff --git a/compose/docker-compose.e2e.yml b/compose/docker-compose.e2e.yml index 4cce316..9eff44d 100644 --- a/compose/docker-compose.e2e.yml +++ b/compose/docker-compose.e2e.yml @@ -2,6 +2,7 @@ version: '3.5' services: service: + image: 095341522062.dkr.ecr.eu-north-1.amazonaws.com/oppivelvollisuus/service:${TAG:-master} build: context: ../service/ ports: @@ -13,6 +14,7 @@ services: SPRING_DATASOURCE_PASSWORD: postgres api-gateway: + image: 095341522062.dkr.ecr.eu-north-1.amazonaws.com/oppivelvollisuus/api-gateway:${TAG:-master} build: context: ../api-gateway/ ports: diff --git a/compose/docker-compose.test-service.yml b/compose/docker-compose.test-service.yml new file mode 100644 index 0000000..2391109 --- /dev/null +++ b/compose/docker-compose.test-service.yml @@ -0,0 +1,16 @@ +version: '3.5' + +services: + service-tests: + image: 095341522062.dkr.ecr.eu-north-1.amazonaws.com/oppivelvollisuus/service-builder:${TAG:-master} + build: + context: ../service/ + target: builder + ports: + - "8080:8080" + environment: + JAVA_OPTS: -server -Djava.security.egd=file:/dev/./urandom -Xms1024m -Xss512k -Xmx1024m -XX:TieredStopAtLevel=1 + SPRING_DATASOURCE_URL: jdbc:postgresql://oppivelvollisuus-db:5432/oppivelvollisuus + entrypoint: ./gradlew test + depends_on: + - oppivelvollisuus-db diff --git a/compose/docker-compose.yml b/compose/docker-compose.yml index bc851ce..eb5adac 100644 --- a/compose/docker-compose.yml +++ b/compose/docker-compose.yml @@ -7,24 +7,12 @@ services: - "5432:5432" volumes: - db-data:/var/lib/postgresql/data + - ./db/test-db.sh:/docker-entrypoint-initdb.d/test-db.sh environment: POSTGRES_DB: oppivelvollisuus POSTGRES_USER: oppivelvollisuus POSTGRES_PASSWORD: postgres - oppivelvollisuus-db-it: - image: postgres:15.4-alpine3.18 - ports: - - "6432:5432" - volumes: - - db-it-data:/var/lib/postgresql/data - environment: - POSTGRES_DB: oppivelvollisuus_it - POSTGRES_USER: oppivelvollisuus - POSTGRES_PASSWORD: postgres - volumes: db-data: driver: local - db-it-data: - driver: local diff --git a/compose/test-compose b/compose/test-compose new file mode 100755 index 0000000..a51f0f2 --- /dev/null +++ b/compose/test-compose @@ -0,0 +1,11 @@ +#!/bin/bash + +set -euo pipefail + +COMPOSE="${COMPOSE:-docker compose}" + +if [ "${BUILD:-true}" = "true" ] && ( [ "$1" = "up" ] || [ "$1" = "run" ] ); then + $COMPOSE -f docker-compose.yml -f docker-compose.test-service.yml build --parallel +fi + +$COMPOSE -f docker-compose.yml -f docker-compose.test-service.yml $@ diff --git a/service/.dockerignore b/service/.dockerignore new file mode 100644 index 0000000..a818a6f --- /dev/null +++ b/service/.dockerignore @@ -0,0 +1,6 @@ +* + +!*.gradle.kts +!entrypoint.sh +!gradle* +!src diff --git a/service/src/test/resources/application.yaml b/service/src/test/resources/application.yaml index 3a5cacd..d5f90a5 100644 --- a/service/src/test/resources/application.yaml +++ b/service/src/test/resources/application.yaml @@ -1,5 +1,5 @@ spring: datasource: - url: "jdbc:postgresql://localhost:6432/oppivelvollisuus_it" + url: "jdbc:postgresql://localhost:5432/oppivelvollisuus_it" username: "oppivelvollisuus" password: "postgres"