Skip to content

Commit cadd413

Browse files
committed
feat: add smoke tests and actuator configuration for improved service health monitoring
1 parent 5018bd8 commit cadd413

5 files changed

Lines changed: 122 additions & 0 deletions

File tree

.github/workflows/build-test.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,43 @@ 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_support_service \
91+
postgres:15-alpine
92+
93+
# Start MongoDB
94+
docker run -d --name mongodb --network smoke-test-net \
95+
mongo:6-jammy
96+
97+
# Wait a bit for databases
98+
sleep 10
99+
100+
# Start App
101+
docker run -d --name app --network smoke-test-net \
102+
-e SERVER_PORT=8080 \
103+
-e DATABASE_URL=jdbc:postgresql://postgres:5432/CSO2_support_service \
104+
-e DATABASE_USERNAME=cso2 \
105+
-e DATABASE_PASSWORD=password123 \
106+
-e MONGODB_URI=mongodb://mongodb:27017/CSO2_support_service \
107+
-p 8080:8080 \
108+
local-image-scan:latest
109+
110+
# Run smoke test script
111+
chmod +x ./scripts/smoke-test.sh
112+
./scripts/smoke-test.sh http://localhost:8080
113+
114+
# Cleanup
115+
docker stop app postgres mongodb
116+
docker rm app postgres mongodb
117+
docker network rm smoke-test-net
118+
82119
- name: Log in to GHCR
83120
uses: docker/login-action@v3
84121
with:

pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@
8282
<artifactId>spring-security-test</artifactId>
8383
<scope>test</scope>
8484
</dependency>
85+
<dependency>
86+
<groupId>org.springframework.boot</groupId>
87+
<artifactId>spring-boot-starter-actuator</artifactId>
88+
</dependency>
8589
</dependencies>
8690

8791
<dependencyManagement>

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
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.CSO2.supportservice.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
6+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
7+
import org.springframework.security.config.http.SessionCreationPolicy;
8+
import org.springframework.security.web.SecurityFilterChain;
9+
10+
@Configuration
11+
@EnableWebSecurity
12+
public class SecurityConfig {
13+
14+
@Bean
15+
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
16+
http.csrf(csrf -> csrf.disable())
17+
.authorizeHttpRequests(auth -> auth
18+
.requestMatchers("/actuator/**").permitAll()
19+
.anyRequest().permitAll())
20+
.sessionManagement(session -> session
21+
.sessionCreationPolicy(SessionCreationPolicy.STATELESS));
22+
23+
return http.build();
24+
}
25+
}

src/main/resources/application.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ spring.cloud.openfeign.client.config.default.readTimeout=5000
1818
# Service URLs for Feign clients (override via env vars)
1919
order.service.url=${ORDER_SERVICE_URL:http://localhost:8083}
2020

21+
# Actuator Configuration
22+
management.endpoints.web.exposure.include=health,info
23+
management.endpoint.health.show-details=always

0 commit comments

Comments
 (0)