From c01bdfa5b25b6d0e2530e06ac89fdb26d850b0c9 Mon Sep 17 00:00:00 2001 From: Vasilije Date: Sat, 11 Oct 2025 13:17:33 +0200 Subject: [PATCH 1/3] feat: Support `reservation-service` and `room_service` in integration tests --- Makefile | 10 +++- ci.override.env | 7 +++ compose.integration.yml | 119 +++++++++++++++++++++++++++++++++++++++- default.env | 14 +++++ run-integration.sh | 55 +++++++++++++++++++ 5 files changed, 201 insertions(+), 4 deletions(-) create mode 100644 ci.override.env create mode 100644 default.env create mode 100644 run-integration.sh diff --git a/Makefile b/Makefile index 1fdd78f..be7b594 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,12 @@ .PHONY: run +# Usage: +# make [command] {MODE} +# +# command := run (default) / test / test_unit / test_integration +# MODE := ci / local +# + run: echo "Run using infrastructure/" @@ -9,5 +16,4 @@ test_unit: ./run-tests.sh test_integration: - docker compose -f compose.integration.yml up --build --abort-on-container-exit --exit-code-from test-runner - docker compose -f compose.integration.yml down + ./run-integration.sh $(MODE) \ No newline at end of file diff --git a/ci.override.env b/ci.override.env new file mode 100644 index 0000000..7261b24 --- /dev/null +++ b/ci.override.env @@ -0,0 +1,7 @@ +RESERVATION_SERVICE_PATH=./services/reservation-service +RESERVATION_SERVICE_REPO=https://github.com/book-em/reservation-service.git +RESERVATION_SERVICE_BRANCH=develop + +ROOM_SERVICE_PATH=./services/room-service +ROOM_SERVICE_REPO=https://github.com/book-em/room-service.git +ROOM_SERVICE_BRANCH=develop \ No newline at end of file diff --git a/compose.integration.yml b/compose.integration.yml index a513f4d..ff29095 100644 --- a/compose.integration.yml +++ b/compose.integration.yml @@ -1,13 +1,23 @@ services: test-runner: build: - context: ./src + context: ${TEST_RUNNER_PATH}/src dockerfile: test.Dockerfile depends_on: db: condition: service_healthy user-service: condition: service_healthy + room-db: + condition: service_healthy + room-service: + condition: service_healthy + room-images: + condition: service_healthy + reservation-db: + condition: service_healthy + reservation-service: + condition: service_healthy volumes: - go-mod-cache:/go/pkg/mod @@ -58,5 +68,110 @@ services: timeout: 3s retries: 5 + room-service: + build: + context: ${ROOM_SERVICE_PATH}/src + dockerfile: Dockerfile + image: room-service-test + ports: + - "${ROOM_SERVICE_PORT}:8080" + environment: + DB_HOST: room-db + DB_PORT: 5432 + DB_NAME: bookem_roomdb_test + DB_USER: bookem_roomdb_user + DB_PASSWORD: testpass + JWT_PUBLIC_KEY_PATH: /app/keys/public_key.pem + ENABLE_TEST_MODE: "true" + depends_on: + room-db: + condition: service_healthy + volumes: + - ${ROOM_SERVICE_PATH}/keys/public_key.pem:/app/keys/public_key.pem:ro + - room_images:/app/images/ + + healthcheck: + test: ["CMD-SHELL", "wget --spider --tries=1 --no-verbose http://room-service:8080/healthz || exit 1"] + interval: 3s + timeout: 3s + retries: 5 + + room-db: + image: postgres:15-alpine + environment: + POSTGRES_DB: bookem_roomdb_test + POSTGRES_USER: bookem_roomdb_user + POSTGRES_PASSWORD: testpass + ports: + - "${ROOM_SERVICE_DB_PORT}:5432" + volumes: + - type: tmpfs + target: /var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB"] + interval: 3s + timeout: 3s + retries: 5 + + room-images: + restart: always + build: + context: ${ROOM_SERVICE_PATH}/nginx + dockerfile: Dockerfile + ports: + - "${ROOM_SERVICE_IMAGES_PORT}:80" + volumes: + - room_images:/usr/share/nginx/html/images/ + healthcheck: + test: service nginx status || exit 1 + interval: 15s + timeout: 3s + retries: 2 + + reservation-service: + build: + context: ${RESERVATION_SERVICE_PATH}/src + dockerfile: Dockerfile + image: reservation-service-test + ports: + - "${RESERVATION_SERVICE_PORT}:8080" + environment: + DB_HOST: reservation-db + DB_PORT: 5432 + DB_NAME: bookem_reservationdb_test + DB_USER: bookem_reservationdb_user + DB_PASSWORD: testpass + JWT_PUBLIC_KEY_PATH: /app/keys/public_key.pem + ENABLE_TEST_MODE: "true" + depends_on: + reservation-db: + condition: service_healthy + volumes: + - ${RESERVATION_SERVICE_PATH}/keys/public_key.pem:/app/keys/public_key.pem:ro + + healthcheck: + test: ["CMD-SHELL", "wget --spider --tries=1 --no-verbose http://reservation-service:8080/healthz || exit 1"] + interval: 3s + timeout: 3s + retries: 5 + + reservation-db: + image: postgres:15-alpine + environment: + POSTGRES_DB: bookem_reservationdb_test + POSTGRES_USER: bookem_reservationdb_user + POSTGRES_PASSWORD: testpass + ports: + - "${RESERVATION_SERVICE_DB_PORT}:5432" + volumes: + - type: tmpfs + target: /var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB"] + interval: 3s + timeout: 3s + retries: 5 + volumes: - go-mod-cache: \ No newline at end of file + go-mod-cache: + room_images: \ No newline at end of file diff --git a/default.env b/default.env new file mode 100644 index 0000000..3ab1ffc --- /dev/null +++ b/default.env @@ -0,0 +1,14 @@ +TEST_RUNNER_PATH=. + +RESERVATION_SERVICE_PATH=../reservation-service +RESERVATION_SERVICE_PORT=0 +RESERVATION_SERVICE_DB_PORT=0 + +USER_SERVICE_PATH=. +USER_SERVICE_PORT=0 +USER_SERVICE_DB_PORT=0 + +ROOM_SERVICE_PATH=../room-service +ROOM_SERVICE_PORT=0 +ROOM_SERVICE_DB_PORT=0 +ROOM_SERVICE_IMAGES_PORT=0 diff --git a/run-integration.sh b/run-integration.sh new file mode 100644 index 0000000..fcf09a6 --- /dev/null +++ b/run-integration.sh @@ -0,0 +1,55 @@ +#!/bin/bash +set -e + +# run-tests.sh [ci|local] +# +# - ci: Loads default + ci.override.env +# - local: Loads default + override.env + +# Determine mode + +mode="$1" +if [[ "$mode" != "ci" && "$mode" != "local" ]]; then + echo "Must specify 'ci' or 'local' as first argument" + exit 1 +fi + +# Determine which env files to read + +ENV_FILES=("./default.env") + +if [[ "$mode" == "ci" ]]; then + echo "Loading CI overrides..." + ENV_FILES+=("./ci.override.env") +else + echo "Loading local overrides..." + ENV_FILES+=("./override.env") +fi + +# Load env files + +for file in "${ENV_FILES[@]}"; do + if [[ -f "$file" ]]; then + echo "Loading env vars from $file" + set -o allexport + source "$file" + set +o allexport + else + echo "Skipping missing optional env file: $file" + fi +done + +# Clone repos if CI + +if [[ "$mode" == "ci" ]]; then + echo "Cloning $RESERVATION_SERVICE_REPO branch $RESERVATION_SERVICE_REPO into $RESERVATION_SERVICE_PATH" + git clone --branch "$RESERVATION_SERVICE_BRANCH" "$RESERVATION_SERVICE_REPO" "$RESERVATION_SERVICE_PATH" + + echo "Cloning $ROOM_SERVICE_REPO branch $ROOM_SERVICE_BRANCH into $ROOM_SERVICE_PATH" + git clone --branch "$ROOM_SERVICE_BRANCH" "$ROOM_SERVICE_REPO" "$ROOM_SERVICE_PATH" +fi + +# Run integration tests + +docker compose -f ./compose.integration.yml up --build --abort-on-container-exit --exit-code-from test-runner +docker compose -f ./compose.integration.yml down \ No newline at end of file From 0400a984bbefa1b10cccf1631bd1d7a970dd9da2 Mon Sep 17 00:00:00 2001 From: Vasilije Date: Sat, 11 Oct 2025 13:17:47 +0200 Subject: [PATCH 2/3] feat: Ignore `services` and `override.env`, and enforce `eol=lf` --- .gitattributes | 1 + .gitignore | 1 + services/.gitignore | 1 + 3 files changed, 3 insertions(+) create mode 100644 services/.gitignore diff --git a/.gitattributes b/.gitattributes index da23d37..0127dc5 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1,3 @@ *.sh text eol=lf +*.env text eol=lf Makefile text eol=lf \ No newline at end of file diff --git a/.gitignore b/.gitignore index ff92eb0..0249db8 100644 --- a/.gitignore +++ b/.gitignore @@ -28,6 +28,7 @@ go.work.sum # env file .env +override.env # Editor/IDE # .idea/ diff --git a/services/.gitignore b/services/.gitignore new file mode 100644 index 0000000..0a00d70 --- /dev/null +++ b/services/.gitignore @@ -0,0 +1 @@ +*/ \ No newline at end of file From 9b12ebde4c4920b15d566afed17f4d00a85cc17c Mon Sep 17 00:00:00 2001 From: Vasilije Date: Sat, 11 Oct 2025 13:18:18 +0200 Subject: [PATCH 3/3] feat: Run `test_integration` with `MODE=ci` in GitHub Actions --- .github/workflows/test.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 6009583..375ccf6 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -84,4 +84,6 @@ jobs: run: chmod 400 keys/private_key.key - name: Run tests - run: make test_integration \ No newline at end of file + run: | + chmod +x ./run-integration.sh + make test_integration MODE=ci \ No newline at end of file