Skip to content

Commit 06ef413

Browse files
committed
feat: add smoke tests and actuator configuration to enhance service reliability
1 parent 198190c commit 06ef413

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

.github/workflows/build-test.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,38 @@ jobs:
7979
vuln-type: 'os,library'
8080
severity: 'CRITICAL,HIGH' # Only fail on Critical and High issues
8181

82+
# --- NEW STEP: Run Smoke Tests ---
83+
- name: Start Services and Run Smoke Tests
84+
run: |
85+
# Create a network
86+
docker network create smoke-test-net
87+
88+
# Start Postgres
89+
docker run -d --name postgres --network smoke-test-net \
90+
-e POSTGRES_USER=cso2 -e POSTGRES_PASSWORD=password123 -e POSTGRES_DB=CSO2_order_service \
91+
postgres:15-alpine
92+
93+
# Wait a bit for Postgres
94+
sleep 10
95+
96+
# Start App
97+
docker run -d --name app --network smoke-test-net \
98+
-e SERVER_PORT=8080 \
99+
-e DATABASE_URL=jdbc:postgresql://postgres:5432/CSO2_order_service \
100+
-e DATABASE_USERNAME=cso2 \
101+
-e DATABASE_PASSWORD=password123 \
102+
-p 8080:8080 \
103+
local-image-scan:latest
104+
105+
# Run smoke test script
106+
chmod +x ./scripts/smoke-test.sh
107+
./scripts/smoke-test.sh http://localhost:8080
108+
109+
# Cleanup
110+
docker stop app postgres
111+
docker rm app postgres
112+
docker network rm smoke-test-net
113+
82114
- name: Log in to GHCR
83115
uses: docker/login-action@v3
84116
with:

scripts/smoke-test.sh

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/bin/bash
2+
3+
# Configuration
4+
SERVICE_URL=${1:-"http://localhost:8080"}
5+
MAX_RETRIES=30
6+
SLEEP_INTERVAL=5
7+
8+
echo "Starting smoke tests against $SERVICE_URL..."
9+
10+
# Function to check health
11+
check_health() {
12+
local url="$1/actuator/health"
13+
14+
# Get HTTP response code and body
15+
local response=$(curl -s -w "\n%{http_code}" "$url")
16+
local body=$(echo "$response" | sed '$d')
17+
local http_code=$(echo "$response" | tail -n1)
18+
19+
if [ "$http_code" == "200" ]; then
20+
# Check if status is UP
21+
if echo "$body" | grep -q '"status":"UP"'; then
22+
return 0
23+
else
24+
echo "Health check returned 200 but status is not UP: $body"
25+
return 1
26+
fi
27+
else
28+
echo "Health check returned HTTP $http_code"
29+
return 1
30+
fi
31+
}
32+
33+
# Wait for service to be ready
34+
echo "Waiting for service to be up..."
35+
for ((i=1; i<=MAX_RETRIES; i++)); do
36+
if check_health "$SERVICE_URL"; then
37+
echo "✅ Service is UP and healthy!"
38+
39+
# Print health details
40+
echo "Health endpoint response:"
41+
curl -s "$SERVICE_URL/actuator/health" | head -c 500
42+
echo ""
43+
44+
exit 0
45+
fi
46+
echo "Attempt $i/$MAX_RETRIES: Service not ready yet... waiting ${SLEEP_INTERVAL}s"
47+
sleep $SLEEP_INTERVAL
48+
done
49+
50+
echo "❌ Service failed to start within timeout."
51+
echo "Final health check attempt:"
52+
curl -s "$SERVICE_URL/actuator/health" || echo "Could not reach service"
53+
exit 1

src/main/resources/application.properties

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,7 @@ spring.jpa.properties.hibernate.format_sql=true
1616
# Feign Client Service URLs (override via env vars in production)
1717
catalog.service.url=${CATALOG_SERVICE_URL:http://localhost:8082}
1818
cart.service.url=${CART_SERVICE_URL:http://localhost:8084}
19+
20+
# Actuator Configuration
21+
management.endpoints.web.exposure.include=health,info
22+
management.endpoint.health.show-details=always

0 commit comments

Comments
 (0)