ci-integration-test.sh•7.65 kB
#!/bin/bash
# Integration tests for CI/CD pipeline
# Tests the deployed application in different environments
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Default values
ENVIRONMENT="dev"
NAMESPACE="radius-mcp-server"
TIMEOUT=60
BASE_URL=""
# Function to print colored output
print_status() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Function to show usage
show_usage() {
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " -e, --environment ENV Target environment (dev|testing|prod20) [default: dev]"
echo " -n, --namespace NS Kubernetes namespace [default: auto-detect]"
echo " -u, --url URL Base URL for testing [default: auto-detect]"
echo " -t, --timeout SECONDS Timeout for tests [default: 60]"
echo " -h, --help Show this help message"
echo ""
echo "Examples:"
echo " $0 -e dev # Test development environment"
echo " $0 -e testing -u https://test.example.com # Test with custom URL"
}
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
-e|--environment)
ENVIRONMENT="$2"
shift 2
;;
-n|--namespace)
NAMESPACE="$2"
shift 2
;;
-u|--url)
BASE_URL="$2"
shift 2
;;
-t|--timeout)
TIMEOUT="$2"
shift 2
;;
-h|--help)
show_usage
exit 0
;;
*)
print_error "Unknown option: $1"
show_usage
exit 1
;;
esac
done
# Set namespace based on environment if not specified
if [[ "$NAMESPACE" == "radius-mcp-server" ]]; then
case $ENVIRONMENT in
dev)
NAMESPACE="radius-mcp-server-dev"
;;
testing)
NAMESPACE="radius-mcp-server-testing"
;;
prod20)
NAMESPACE="radius-mcp-server-prod"
;;
esac
fi
# Set base URL if not specified
if [[ -z "$BASE_URL" ]]; then
case $ENVIRONMENT in
dev)
BASE_URL="https://radius-mcp-server-dev.myradius.ru"
;;
testing)
BASE_URL="https://radius-mcp-server-testing.myradius.ru"
;;
prod20)
BASE_URL="https://radius-mcp-server.myradius.ru"
;;
esac
fi
print_status "Running integration tests for environment: $ENVIRONMENT"
print_status "Namespace: $NAMESPACE"
print_status "Base URL: $BASE_URL"
print_status "Timeout: ${TIMEOUT}s"
# Function to test HTTP endpoint
test_endpoint() {
local endpoint="$1"
local expected_status="$2"
local description="$3"
print_status "Testing $description: $endpoint"
local response=$(curl -s -w "%{http_code}" -o /dev/null --max-time $TIMEOUT "$BASE_URL$endpoint" || echo "000")
if [[ "$response" == "$expected_status" ]]; then
print_success "$description: HTTP $response"
return 0
else
print_error "$description: Expected HTTP $expected_status, got $response"
return 1
fi
}
# Function to test MCP endpoint
test_mcp_endpoint() {
print_status "Testing MCP endpoint..."
local response=$(curl -s --max-time $TIMEOUT "$BASE_URL/mcp" || echo "")
if [[ -n "$response" ]]; then
print_success "MCP endpoint responded"
return 0
else
print_error "MCP endpoint did not respond"
return 1
fi
}
# Function to test health endpoint
test_health_endpoint() {
print_status "Testing health endpoint..."
local response=$(curl -s --max-time $TIMEOUT "$BASE_URL/health" || echo "")
if [[ -n "$response" ]]; then
print_success "Health endpoint responded"
return 0
else
print_warning "Health endpoint not available (this is optional)"
return 0
fi
}
# Function to test employee API endpoints (if available)
test_employee_endpoints() {
print_status "Testing employee API endpoints..."
# Test employee search endpoint (should return 401 without auth)
local response=$(curl -s -w "%{http_code}" -o /dev/null --max-time $TIMEOUT "$BASE_URL/api/employees/search" || echo "000")
if [[ "$response" == "401" ]]; then
print_success "Employee API: Authentication required (HTTP 401)"
return 0
elif [[ "$response" == "404" ]]; then
print_warning "Employee API: Not found (HTTP 404) - may not be implemented"
return 0
else
print_warning "Employee API: Unexpected response (HTTP $response)"
return 0
fi
}
# Function to test with port-forward (fallback)
test_with_port_forward() {
print_status "Testing with port-forward (fallback method)..."
# Get service name
local service_name=$(kubectl get service -n "$NAMESPACE" -l app.kubernetes.io/name=radius-mcp-server --no-headers | head -1 | awk '{print $1}')
if [[ -z "$service_name" ]]; then
print_error "No service found in namespace $NAMESPACE"
return 1
fi
# Get service port
local port=$(kubectl get service -n "$NAMESPACE" "$service_name" -o jsonpath='{.spec.ports[0].port}')
print_status "Port-forwarding service $service_name:$port to localhost:8080"
# Start port-forward in background
kubectl port-forward -n "$NAMESPACE" "service/$service_name" 8080:$port &
local pf_pid=$!
# Wait for port-forward to be ready
sleep 5
# Test localhost endpoint
local response=$(curl -s -w "%{http_code}" -o /dev/null --max-time $TIMEOUT "http://localhost:8080/mcp" || echo "000")
# Clean up port-forward
kill $pf_pid 2>/dev/null || true
if [[ "$response" == "200" ]]; then
print_success "Port-forward test: HTTP $response"
return 0
else
print_error "Port-forward test: Expected HTTP 200, got $response"
return 1
fi
}
# Main test execution
main() {
local tests_passed=0
local tests_total=0
print_status "Starting integration tests..."
# Test 1: MCP endpoint
tests_total=$((tests_total + 1))
if test_mcp_endpoint; then
tests_passed=$((tests_passed + 1))
fi
# Test 2: Health endpoint (optional)
tests_total=$((tests_total + 1))
if test_health_endpoint; then
tests_passed=$((tests_passed + 1))
fi
# Test 3: Employee API endpoints
tests_total=$((tests_total + 1))
if test_employee_endpoints; then
tests_passed=$((tests_passed + 1))
fi
# Test 4: Port-forward fallback
tests_total=$((tests_total + 1))
if test_with_port_forward; then
tests_passed=$((tests_passed + 1))
fi
# Summary
echo ""
print_status "=== Integration Test Summary ==="
print_status "Tests passed: $tests_passed/$tests_total"
if [[ $tests_passed -eq $tests_total ]]; then
print_success "All integration tests passed!"
exit 0
else
print_error "Some integration tests failed"
exit 1
fi
}
# Check if kubectl is available
if ! command -v kubectl &> /dev/null; then
print_error "kubectl is not installed or not in PATH"
exit 1
fi
# Check if curl is available
if ! command -v curl &> /dev/null; then
print_error "curl is not installed or not in PATH"
exit 1
fi
# Run tests
main