Skip to content

Commit

Permalink
Refactor the unit test script strucutre
Browse files Browse the repository at this point in the history
  • Loading branch information
thejasmeetsingh committed Dec 31, 2023
1 parent 2cfef26 commit 4f6ede1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 14 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
19 changes: 6 additions & 13 deletions run_unit_test.sh → src/product_service/run_unit_test.sh
Original file line number Diff line number Diff line change
@@ -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 \
Expand All @@ -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
Expand Down
42 changes: 42 additions & 0 deletions src/user_service/run_unit_test.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4f6ede1

Please sign in to comment.