Skip to content

Commit

Permalink
Run tests in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
ilja-git committed Nov 16, 2023
1 parent d28cb6b commit 7db49f1
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 17 deletions.
85 changes: 82 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
5 changes: 5 additions & 0 deletions api-gateway/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*

!src
!*.json
!*.lock
7 changes: 7 additions & 0 deletions compose/db/test-db.sh
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions compose/docker-compose.e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
16 changes: 16 additions & 0 deletions compose/docker-compose.test-service.yml
Original file line number Diff line number Diff line change
@@ -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
14 changes: 1 addition & 13 deletions compose/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 11 additions & 0 deletions compose/test-compose
Original file line number Diff line number Diff line change
@@ -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 $@
6 changes: 6 additions & 0 deletions service/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*

!*.gradle.kts
!entrypoint.sh
!gradle*
!src
2 changes: 1 addition & 1 deletion service/src/test/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
spring:
datasource:
url: "jdbc:postgresql://localhost:6432/oppivelvollisuus_it"
url: "jdbc:postgresql://localhost:5432/oppivelvollisuus_it"
username: "oppivelvollisuus"
password: "postgres"

0 comments on commit 7db49f1

Please sign in to comment.