diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c25ff8b..ea6d70f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -7,6 +7,11 @@ on: jobs: build: runs-on: ubuntu-latest + strategy: + matrix: + microservices: + - user_service + - product_service steps: - uses: actions/checkout@v3 - name: Set up Go @@ -18,4 +23,6 @@ jobs: run: curl -fsSL https://github.com/raw/pressly/goose/master/install.sh | sh - name: Execute Test Cases - run: bash ./run_unit_test.sh + run: | + cd ./src/${{ matrix.microservices }} + bash run_unit_test.sh diff --git a/run_unit_test.sh b/src/product_service/run_unit_test.sh similarity index 71% rename from run_unit_test.sh rename to src/product_service/run_unit_test.sh index 8c0001f..625f48e 100644 --- a/run_unit_test.sh +++ b/src/product_service/run_unit_test.sh @@ -1,11 +1,9 @@ #!/bin/bash/ -container_name=test_db +container_name=test_products_db db_user=db_user db_password=1234 -db_name=test - -services=("user_service" "product_service") +db_name=products_test_db # Run the testing database container docker run --name $container_name \ @@ -32,16 +30,11 @@ done sleep 30 -for service in ${services[@]}; -do - echo "Runnig test for $service service" - - cd src/$service - goose -dir sql/schema postgres postgres://$db_user:$db_password@localhost:5432/$db_name up - go test ./api/ +# Run migrations +goose -dir sql/schema postgres postgres://$db_user:$db_password@localhost:5432/$db_name up - echo "---------------------" -done +# Run test case +go test ./api/ # Stop & remove docker container docker container stop $container_name diff --git a/src/user_service/run_unit_test.sh b/src/user_service/run_unit_test.sh new file mode 100644 index 0000000..4dedd37 --- /dev/null +++ b/src/user_service/run_unit_test.sh @@ -0,0 +1,42 @@ +#!/bin/bash/ + +container_name=test_users_db +db_user=db_user +db_password=1234 +db_name=users_test_db + +# Run the testing database container +docker run --name $container_name \ + -e POSTGRES_USER=$db_user \ + -e POSTGRES_PASSWORD=$db_password \ + -e POSTGRES_DB=$db_name \ + -p 5432:5432 \ + --health-cmd='pg_isready -d $db_name -U $db_user' \ + --health-interval=10s \ + --health-timeout=5s \ + --health-retries=5 \ + -d postgres:16.1-alpine3.18 + +echo "Waiting for DB..." + +while true; do + # Check the health of the database container + if docker inspect --format '{{json .State.Health.Status}}' $container_name | grep -q "healthy" + then + break + fi + sleep 1 +done + +sleep 30 + +# Run migrations +goose -dir sql/schema postgres postgres://$db_user:$db_password@localhost:5432/$db_name up + +# Run test case +go test ./api/ + +# Stop & remove docker container +docker container stop $container_name +docker container rm $container_name +docker volume prune -a -f \ No newline at end of file