#!/bin/bash # gRPC Cross-Service Integration Test # Tests actual gRPC communication flows set -e echo "======================================" echo "gRPC Cross-Service Integration Tests" echo "======================================" echo "" RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' TOTAL=0 PASSED=0 FAILED=0 # Check if grpcurl is available if ! command -v grpcurl &> /dev/null; then echo -e "${YELLOW}⚠${NC} grpcurl not found, installing..." go install github.com/fullstorydev/grpcurl/cmd/grpcurl@latest export PATH=$PATH:~/go/bin fi function test_api_gateway_grpc() { echo "Test 1: API Gateway gRPC Health" echo "===============================" TOTAL=$((TOTAL + 1)) if timeout 5 nc -z localhost 50051 2>/dev/null; then echo -e "${GREEN}✓${NC} API Gateway gRPC port 50051 is open" PASSED=$((PASSED + 1)) # Try to list services echo " Available services:" grpcurl -plaintext localhost:50051 list 2>/dev/null | head -5 || echo " (service reflection not enabled)" else echo -e "${RED}✗${NC} API Gateway gRPC port 50051 not accessible" FAILED=$((FAILED + 1)) fi echo "" } function test_trading_service_grpc() { echo "Test 2: Trading Service gRPC Health" echo "===================================" TOTAL=$((TOTAL + 1)) if timeout 5 nc -z localhost 50052 2>/dev/null; then echo -e "${GREEN}✓${NC} Trading Service gRPC port 50052 is open" PASSED=$((PASSED + 1)) echo " Available services:" grpcurl -plaintext localhost:50052 list 2>/dev/null | head -5 || echo " (service reflection not enabled)" else echo -e "${RED}✗${NC} Trading Service gRPC port 50052 not accessible" FAILED=$((FAILED + 1)) fi echo "" } function test_backtesting_service_grpc() { echo "Test 3: Backtesting Service gRPC Health" echo "=======================================" TOTAL=$((TOTAL + 1)) if timeout 5 nc -z localhost 50053 2>/dev/null; then echo -e "${GREEN}✓${NC} Backtesting Service gRPC port 50053 is open" PASSED=$((PASSED + 1)) echo " Available services:" grpcurl -plaintext localhost:50053 list 2>/dev/null | head -5 || echo " (service reflection not enabled)" else echo -e "${RED}✗${NC} Backtesting Service gRPC port 50053 not accessible" FAILED=$((FAILED + 1)) fi echo "" } function test_ml_training_service_grpc() { echo "Test 4: ML Training Service gRPC Health" echo "=======================================" TOTAL=$((TOTAL + 1)) if timeout 5 nc -z localhost 50054 2>/dev/null; then echo -e "${GREEN}✓${NC} ML Training Service gRPC port 50054 is open" PASSED=$((PASSED + 1)) echo " Available services:" grpcurl -plaintext localhost:50054 list 2>/dev/null | head -5 || echo " (service reflection not enabled)" else echo -e "${RED}✗${NC} ML Training Service gRPC port 50054 not accessible" FAILED=$((FAILED + 1)) fi echo "" } function test_database_write_performance() { echo "Test 5: PostgreSQL Write Performance" echo "====================================" TOTAL=$((TOTAL + 1)) # Measure order insertion time START=$(date +%s%N) psql postgresql://foxhunt:foxhunt_dev_password@localhost:5432/foxhunt -c " INSERT INTO orders ( order_id, symbol, side, quantity, order_type, status, account_id, created_at, updated_at ) VALUES ( gen_random_uuid(), 'TEST', 'buy', 100, 'market', 'pending', 'test_account', NOW(), NOW() ) " > /dev/null 2>&1 END=$(date +%s%N) LATENCY=$(( (END - START) / 1000000 )) if [ $? -eq 0 ]; then echo -e "${GREEN}✓${NC} Order insertion successful: ${LATENCY}ms" PASSED=$((PASSED + 1)) # Clean up test order psql postgresql://foxhunt:foxhunt_dev_password@localhost:5432/foxhunt -c " DELETE FROM orders WHERE symbol = 'TEST' " > /dev/null 2>&1 else echo -e "${RED}✗${NC} Order insertion failed" FAILED=$((FAILED + 1)) fi echo "" } function test_database_read_performance() { echo "Test 6: PostgreSQL Read Performance" echo "===================================" TOTAL=$((TOTAL + 1)) START=$(date +%s%N) RESULT=$(psql postgresql://foxhunt:foxhunt_dev_password@localhost:5432/foxhunt -t -c " SELECT COUNT(*) FROM orders LIMIT 1000 " 2>/dev/null) END=$(date +%s%N) LATENCY=$(( (END - START) / 1000000 )) if [ $? -eq 0 ]; then echo -e "${GREEN}✓${NC} Order query successful: ${LATENCY}ms (${RESULT} orders)" PASSED=$((PASSED + 1)) else echo -e "${RED}✗${NC} Order query failed" FAILED=$((FAILED + 1)) fi echo "" } function test_redis_cache_performance() { echo "Test 7: Redis Cache Performance" echo "===============================" TOTAL=$((TOTAL + 1)) # Write test START=$(date +%s%N) docker exec 496d979ef7da redis-cli SET test_key_grpc "integration_test_value" > /dev/null 2>&1 END=$(date +%s%N) WRITE_LATENCY=$(( (END - START) / 1000000 )) # Read test START=$(date +%s%N) VALUE=$(docker exec 496d979ef7da redis-cli GET test_key_grpc 2>/dev/null) END=$(date +%s%N) READ_LATENCY=$(( (END - START) / 1000000 )) # Cleanup docker exec 496d979ef7da redis-cli DEL test_key_grpc > /dev/null 2>&1 if [ "$VALUE" = "integration_test_value" ]; then echo -e "${GREEN}✓${NC} Redis SET/GET successful" echo " Write: ${WRITE_LATENCY}ms, Read: ${READ_LATENCY}ms" PASSED=$((PASSED + 1)) else echo -e "${RED}✗${NC} Redis cache test failed" FAILED=$((FAILED + 1)) fi echo "" } function test_prometheus_scraping() { echo "Test 8: Prometheus Metrics Scraping" echo "===================================" TOTAL=$((TOTAL + 4)) # Check each service's metrics for SERVICE in "API Gateway:9091" "Trading:9092" "Backtesting:9093" "ML Training:9094"; do IFS=':' read -r NAME PORT <<< "$SERVICE" START=$(date +%s%N) METRIC_COUNT=$(curl -s http://localhost:$PORT/metrics 2>/dev/null | grep "# TYPE" | wc -l) END=$(date +%s%N) LATENCY=$(( (END - START) / 1000000 )) if [ $METRIC_COUNT -gt 0 ]; then echo -e "${GREEN}✓${NC} $NAME metrics: $METRIC_COUNT types (${LATENCY}ms)" PASSED=$((PASSED + 1)) else echo -e "${RED}✗${NC} $NAME metrics unavailable" FAILED=$((FAILED + 1)) fi done echo "" } function test_service_mesh_connectivity() { echo "Test 9: Service Mesh Connectivity" echo "=================================" TOTAL=$((TOTAL + 1)) # Check if all services can reach each other via Docker network SERVICES_UP=$(docker ps --format "{{.Names}}" | grep -E "api-gateway|trading-service|backtesting|ml-training" | wc -l) if [ $SERVICES_UP -eq 4 ]; then echo -e "${GREEN}✓${NC} All 4 services are running in Docker network" echo " Services: api-gateway, trading-service, backtesting-service, ml-training-service" PASSED=$((PASSED + 1)) else echo -e "${RED}✗${NC} Only $SERVICES_UP/4 services running" FAILED=$((FAILED + 1)) fi echo "" } function measure_e2e_latency() { echo "Test 10: End-to-End Latency Profile" echo "====================================" echo "Component latencies:" # Database latency START=$(date +%s%N) psql postgresql://foxhunt:foxhunt_dev_password@localhost:5432/foxhunt -c "SELECT 1" > /dev/null 2>&1 END=$(date +%s%N) DB_LATENCY=$(( (END - START) / 1000000 )) echo " PostgreSQL: ${DB_LATENCY}ms" # Redis latency START=$(date +%s%N) docker exec 496d979ef7da redis-cli PING > /dev/null 2>&1 END=$(date +%s%N) REDIS_LATENCY=$(( (END - START) / 1000000 )) echo " Redis: ${REDIS_LATENCY}ms" # HTTP health endpoints START=$(date +%s%N) curl -s http://localhost:9092/health > /dev/null 2>&1 END=$(date +%s%N) HTTP_LATENCY=$(( (END - START) / 1000000 )) echo " HTTP Health: ${HTTP_LATENCY}ms" # Metrics endpoint START=$(date +%s%N) curl -s http://localhost:9092/metrics > /dev/null 2>&1 END=$(date +%s%N) METRICS_LATENCY=$(( (END - START) / 1000000 )) echo " Metrics: ${METRICS_LATENCY}ms" TOTAL_E2E=$(( DB_LATENCY + REDIS_LATENCY + HTTP_LATENCY )) echo "" echo " Estimated E2E latency: ${TOTAL_E2E}ms" echo "" } # Run all tests test_api_gateway_grpc test_trading_service_grpc test_backtesting_service_grpc test_ml_training_service_grpc test_database_write_performance test_database_read_performance test_redis_cache_performance test_prometheus_scraping test_service_mesh_connectivity measure_e2e_latency # Summary echo "======================================" echo "gRPC Integration Test Summary" echo "======================================" echo "Total Tests: $TOTAL" echo -e "${GREEN}Passed: $PASSED${NC}" echo -e "${RED}Failed: $FAILED${NC}" echo "" PASS_RATE=$(awk "BEGIN {printf \"%.1f\", ($PASSED / $TOTAL) * 100}") echo "Pass Rate: $PASS_RATE%" echo "" if [ $FAILED -eq 0 ]; then echo -e "${GREEN}✓ ALL gRPC TESTS PASSED${NC}" exit 0 else echo -e "${YELLOW}⚠ SOME TESTS FAILED (likely Docker port mapping)${NC}" exit 1 fi