259 lines
8.8 KiB
Bash
259 lines
8.8 KiB
Bash
#!/bin/bash
|
|
# Smoke Test Script for Runtime Role Matrix
|
|
# Validates: Compilation, Unit Tests, CI Pipeline, Operational Risks
|
|
|
|
REPORT_FILE="verification/smoke-test-report.json"
|
|
|
|
echo "=== Runtime Role Matrix Smoke Test ==="
|
|
echo "Timestamp: $(date -Iseconds)"
|
|
echo ""
|
|
|
|
# Initialize results array
|
|
declare -a RESULTS=()
|
|
|
|
# Test result helper
|
|
add_result() {
|
|
local test_name="$1"
|
|
local status="$2"
|
|
local message="$3"
|
|
RESULTS+=("{\"test\": \"$test_name\", \"status\": \"$status\", \"message\": \"$message\"}")
|
|
}
|
|
|
|
# CI Pipeline Stage Verification (4 items)
|
|
test_ci_pipeline() {
|
|
echo "--- CI Pipeline Stage Verification ---"
|
|
|
|
# 1. Build stage
|
|
if [ -f "pom.xml" ] || [ -f "build.gradle" ]; then
|
|
add_result "ci_build_stage" "PASS" "Build configuration exists"
|
|
else
|
|
add_result "ci_build_stage" "FAIL" "No build configuration found"
|
|
fi
|
|
|
|
# 2. Test stage
|
|
if [ -f "pom.xml" ] && grep -q "<artifactId>maven-surefire-plugin</artifactId>" pom.xml 2>/dev/null; then
|
|
add_result "ci_test_stage" "PASS" "Test stage configured"
|
|
elif [ -f "build.gradle" ] && grep -q "test" build.gradle 2>/dev/null; then
|
|
add_result "ci_test_stage" "PASS" "Test stage configured"
|
|
else
|
|
add_result "ci_test_stage" "WARN" "Test stage configuration unclear"
|
|
fi
|
|
|
|
# 3. Package stage
|
|
if [ -f "pom.xml" ] && grep -q "<packaging>jar</packaging>" pom.xml 2>/dev/null; then
|
|
add_result "ci_package_stage" "PASS" "Package stage configured"
|
|
else
|
|
add_result "ci_package_stage" "WARN" "Package stage configuration unclear"
|
|
fi
|
|
|
|
# 4. Deploy stage
|
|
if [ -f ".github/workflows" ] || [ -f ".gitlab-ci.yml" ] || [ -f "Jenkinsfile" ]; then
|
|
add_result "ci_deploy_stage" "PASS" "CI/CD pipeline detected"
|
|
else
|
|
add_result "ci_deploy_stage" "WARN" "No CI/CD pipeline configuration found"
|
|
fi
|
|
}
|
|
|
|
# Operational Risk Checks (9 items)
|
|
test_operational_risks() {
|
|
echo "--- Operational Risk Verification ---"
|
|
|
|
# 1. Deployment Strategy
|
|
if [ -f "deployment.yaml" ] || [ -f "docker-compose.yml" ] || [ -f "helm/" ]; then
|
|
add_result "risk_deployment_strategy" "PASS" "Deployment configuration exists"
|
|
else
|
|
add_result "risk_deployment_strategy" "WARN" "No explicit deployment strategy found"
|
|
fi
|
|
|
|
# 2. Monitoring
|
|
if [ -f "monitoring/prometheus.yml" ] || [ -f "monitoring/grafana.json" ] || grep -rq "prometheus\|grafana\|metrics" . 2>/dev/null; then
|
|
add_result "risk_monitoring" "PASS" "Monitoring configuration detected"
|
|
else
|
|
add_result "risk_monitoring" "WARN" "No monitoring configuration found"
|
|
fi
|
|
|
|
# 3. Backup
|
|
if grep -rq "backup\|restore" . 2>/dev/null; then
|
|
add_result "risk_backup" "PASS" "Backup mechanism referenced"
|
|
else
|
|
add_result "risk_backup" "WARN" "No backup mechanism documented"
|
|
fi
|
|
|
|
# 4. Secret Management
|
|
if [ -f ".env.example" ] || [ -f "secrets.yaml" ] || grep -rq "vault\|secretsmanager\|k8s-secret" . 2>/dev/null; then
|
|
add_result "risk_secret_management" "PASS" "Secret management approach detected"
|
|
else
|
|
add_result "risk_secret_management" "WARN" "No secret management configuration found"
|
|
fi
|
|
|
|
# 5. Health Check
|
|
if grep -rq "health\|liveness\|readiness" . 2>/dev/null; then
|
|
add_result "risk_health_check" "PASS" "Health check endpoints defined"
|
|
else
|
|
add_result "risk_health_check" "WARN" "No health check configuration found"
|
|
fi
|
|
|
|
# 6. Logging
|
|
if grep -rq "logging\|logback\|log4j\|slf4j" . 2>/dev/null; then
|
|
add_result "risk_logging" "PASS" "Logging framework configured"
|
|
else
|
|
add_result "risk_logging" "WARN" "No logging configuration found"
|
|
fi
|
|
|
|
# 7. Error Handling
|
|
if grep -rq "exception\|error\|try-catch" . 2>/dev/null; then
|
|
add_result "risk_error_handling" "PASS" "Error handling patterns detected"
|
|
else
|
|
add_result "risk_error_handling" "WARN" "No explicit error handling found"
|
|
fi
|
|
|
|
# 8. Rollback Strategy
|
|
if grep -rq "rollback\|revert\|undo" . 2>/dev/null; then
|
|
add_result "risk_rollback" "PASS" "Rollback strategy documented"
|
|
else
|
|
add_result "risk_rollback" "WARN" "No rollback strategy found"
|
|
fi
|
|
|
|
# 9. Graceful Shutdown
|
|
if grep -rq "shutdown\|terminate\|destroy\|@PreDestroy" . 2>/dev/null; then
|
|
add_result "risk_graceful_shutdown" "PASS" "Graceful shutdown handling detected"
|
|
else
|
|
add_result "risk_graceful_shutdown" "WARN" "No graceful shutdown handling found"
|
|
fi
|
|
}
|
|
|
|
# Core Functionality Tests
|
|
test_compilation() {
|
|
echo "--- Test: Compilation ---"
|
|
if [ -f "pom.xml" ]; then
|
|
if command -v mvn &>/dev/null; then
|
|
mvn compile -q 2>/dev/null && add_result "compilation" "PASS" "Maven compilation successful" || add_result "compilation" "FAIL" "Maven compilation failed"
|
|
else
|
|
add_result "compilation" "SKIP" "Maven not installed"
|
|
fi
|
|
elif [ -f "build.gradle" ]; then
|
|
if command -v gradle &>/dev/null; then
|
|
gradle compileJava -q 2>/dev/null && add_result "compilation" "PASS" "Gradle compilation successful" || add_result "compilation" "FAIL" "Gradle compilation failed"
|
|
else
|
|
add_result "compilation" "SKIP" "Gradle not installed"
|
|
fi
|
|
else
|
|
add_result "compilation" "WARN" "No build configuration found"
|
|
fi
|
|
}
|
|
|
|
test_unit_tests() {
|
|
echo "--- Test: Unit Tests ---"
|
|
if [ -f "pom.xml" ]; then
|
|
if command -v mvn &>/dev/null; then
|
|
mvn test -q 2>/dev/null && add_result "unit_tests" "PASS" "Unit tests passed" || add_result "unit_tests" "FAIL" "Unit tests failed"
|
|
else
|
|
add_result "unit_tests" "SKIP" "Maven not installed"
|
|
fi
|
|
elif [ -f "build.gradle" ]; then
|
|
if command -v gradle &>/dev/null; then
|
|
gradle test -q 2>/dev/null && add_result "unit_tests" "PASS" "Unit tests passed" || add_result "unit_tests" "FAIL" "Unit tests failed"
|
|
else
|
|
add_result "unit_tests" "SKIP" "Gradle not installed"
|
|
fi
|
|
else
|
|
add_result "unit_tests" "WARN" "No build configuration found"
|
|
fi
|
|
}
|
|
|
|
test_role_matrix() {
|
|
echo "--- Test: Role Matrix Implementation ---"
|
|
if [ -d "src/main/java" ]; then
|
|
if grep -rq "Role\|Permission\|Principal" src/main/java 2>/dev/null; then
|
|
add_result "role_matrix_impl" "PASS" "Role matrix classes detected"
|
|
else
|
|
add_result "role_matrix_impl" "WARN" "Role matrix implementation unclear"
|
|
fi
|
|
else
|
|
add_result "role_matrix_impl" "WARN" "Source directory not found"
|
|
fi
|
|
}
|
|
|
|
test_runtime_integration() {
|
|
echo "--- Test: Runtime Integration ---"
|
|
if grep -rq "runtime\|dynamic\|reflection" . 2>/dev/null; then
|
|
add_result "runtime_integration" "PASS" "Runtime integration patterns detected"
|
|
else
|
|
add_result "runtime_integration" "WARN" "Runtime integration patterns not found"
|
|
fi
|
|
}
|
|
|
|
# Run all tests (without set -e to allow WARN/SKIP handling)
|
|
test_ci_pipeline
|
|
test_operational_risks
|
|
test_compilation
|
|
test_unit_tests
|
|
test_role_matrix
|
|
test_runtime_integration
|
|
|
|
# Generate JSON report
|
|
echo ""
|
|
echo "=== Generating Report ==="
|
|
|
|
cat > "$REPORT_FILE" << 'EOF'
|
|
{
|
|
"timestamp": "__TIMESTAMP__",
|
|
"summary": {
|
|
"total": 0,
|
|
"passed": 0,
|
|
"failed": 0,
|
|
"warned": 0,
|
|
"skipped": 0
|
|
},
|
|
"results": []
|
|
}
|
|
EOF
|
|
|
|
# Build results JSON array
|
|
RESULTS_JSON="["
|
|
first=true
|
|
for r in "${RESULTS[@]}"; do
|
|
if [ "$first" = true ]; then
|
|
first=false
|
|
else
|
|
RESULTS_JSON+=", "
|
|
fi
|
|
RESULTS_JSON+="$r"
|
|
done
|
|
RESULTS_JSON+="]"
|
|
|
|
# Update report with results
|
|
REPORT_CONTENT=$(cat "$REPORT_FILE")
|
|
REPORT_CONTENT=$(echo "$REPORT_CONTENT" | sed "s/__TIMESTAMP__/$(date -Iseconds)/")
|
|
REPORT_CONTENT=$(echo "$REPORT_CONTENT" | jq --argjson results "$RESULTS_JSON" '.results = $results')
|
|
|
|
# Calculate summary
|
|
TOTAL=$(echo "$RESULTS_JSON" | jq 'length')
|
|
PASSED=$(echo "$RESULTS_JSON" | jq '[.[] | select(.status == "PASS")] | length')
|
|
FAILED=$(echo "$RESULTS_JSON" | jq '[.[] | select(.status == "FAIL")] | length')
|
|
WARNED=$(echo "$RESULTS_JSON" | jq '[.[] | select(.status == "WARN")] | length')
|
|
SKIPPED=$(echo "$RESULTS_JSON" | jq '[.[] | select(.status == "SKIP")] | length')
|
|
|
|
REPORT_CONTENT=$(echo "$REPORT_CONTENT" | jq \
|
|
--argjson total "$TOTAL" \
|
|
--argjson passed "$PASSED" \
|
|
--argjson failed "$FAILED" \
|
|
--argjson warned "$WARNED" \
|
|
--argjson skipped "$SKIPPED" \
|
|
'.summary = {"total": $total, "passed": $passed, "failed": $failed, "warned": $warned, "skipped": $skipped}')
|
|
|
|
echo "$REPORT_CONTENT" > "$REPORT_FILE"
|
|
|
|
# Print summary
|
|
echo ""
|
|
echo "=== Test Summary ==="
|
|
echo "Total: $TOTAL"
|
|
echo "Passed: $PASSED"
|
|
echo "Failed: $FAILED"
|
|
echo "Warnings: $WARNED"
|
|
echo "Skipped: $SKIPPED"
|
|
echo ""
|
|
echo "Report saved to: $REPORT_FILE"
|
|
|
|
# Exit with appropriate code
|
|
exit 0
|