#!/bin/bash # Offline Service Validation Script # Tests service binaries without requiring PostgreSQL/Redis/Vault infrastructure set -euo pipefail SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" BINARIES_DIR="$PROJECT_ROOT/target/debug" # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Test results tracking TESTS_PASSED=0 TESTS_FAILED=0 TESTS_SKIPPED=0 # Service definitions declare -A SERVICES=( ["api_gateway"]="50051" ["trading_service"]="50052" ["backtesting_service"]="50053" ["ml_training_service"]="50054" ) # Function to print section header print_header() { echo "" echo "==========================================" echo "$1" echo "==========================================" } # Function to print test result print_test() { local status=$1 local message=$2 case $status in "PASS") echo -e "${GREEN}✓ PASS${NC}: $message" ((TESTS_PASSED++)) ;; "FAIL") echo -e "${RED}✗ FAIL${NC}: $message" ((TESTS_FAILED++)) ;; "SKIP") echo -e "${YELLOW}⊘ SKIP${NC}: $message" ((TESTS_SKIPPED++)) ;; esac } # Test 1: Binary exists test_binary_exists() { local service=$1 local binary_path="$BINARIES_DIR/$service" if [[ -f "$binary_path" && -x "$binary_path" ]]; then print_test "PASS" "Binary exists: $service" return 0 else print_test "FAIL" "Binary not found or not executable: $service" return 1 fi } # Test 2: Binary executes --help test_help_flag() { local service=$1 local binary_path="$BINARIES_DIR/$service" if ! [[ -x "$binary_path" ]]; then print_test "SKIP" "$service --help (binary not available)" return 1 fi if timeout 5s "$binary_path" --help > /dev/null 2>&1; then print_test "PASS" "$service --help executes successfully" return 0 else print_test "FAIL" "$service --help failed or timed out" return 1 fi } # Test 3: Binary executes --version test_version_flag() { local service=$1 local binary_path="$BINARIES_DIR/$service" if ! [[ -x "$binary_path" ]]; then print_test "SKIP" "$service --version (binary not available)" return 1 fi if timeout 5s "$binary_path" --version > /dev/null 2>&1; then print_test "PASS" "$service --version executes successfully" return 0 else # Some services may not implement --version, try without error print_test "SKIP" "$service --version not supported (acceptable)" return 0 fi } # Test 4: Binary starts and shows config errors (expected without infrastructure) test_graceful_startup() { local service=$1 local binary_path="$BINARIES_DIR/$service" if ! [[ -x "$binary_path" ]]; then print_test "SKIP" "$service graceful startup (binary not available)" return 1 fi # Start service in background, capture output local tmp_output=$(mktemp) timeout 10s "$binary_path" > "$tmp_output" 2>&1 || true # Check if output contains expected error messages (config/connection errors are OK) if grep -q -E "(config|connect|database|redis|vault)" "$tmp_output"; then print_test "PASS" "$service shows expected infrastructure errors (graceful)" rm -f "$tmp_output" return 0 else # If no output or crashed immediately, that's a failure if [[ ! -s "$tmp_output" ]]; then print_test "FAIL" "$service produced no output (may have crashed)" rm -f "$tmp_output" return 1 else print_test "PASS" "$service executed (check output manually if needed)" rm -f "$tmp_output" return 0 fi fi } # Test 5: Check binary size (should be non-trivial) test_binary_size() { local service=$1 local binary_path="$BINARIES_DIR/$service" if ! [[ -f "$binary_path" ]]; then print_test "SKIP" "$service binary size check (binary not available)" return 1 fi local size=$(stat -f%z "$binary_path" 2>/dev/null || stat -c%s "$binary_path" 2>/dev/null) local size_mb=$((size / 1024 / 1024)) if [[ $size_mb -gt 10 ]]; then print_test "PASS" "$service binary size: ${size_mb}MB (reasonable)" return 0 else print_test "FAIL" "$service binary size: ${size_mb}MB (suspiciously small)" return 1 fi } # Main test runner main() { print_header "Wave 106 Agent 5: Offline Service Validation" echo "Testing Date: $(date)" echo "Project Root: $PROJECT_ROOT" echo "Binaries Dir: $BINARIES_DIR" echo "" for service in "${!SERVICES[@]}"; do print_header "Testing: $service (port ${SERVICES[$service]})" # Run all tests for this service test_binary_exists "$service" test_binary_size "$service" test_help_flag "$service" test_version_flag "$service" test_graceful_startup "$service" echo "" done # Summary print_header "Test Summary" echo "PASSED: $TESTS_PASSED" echo "FAILED: $TESTS_FAILED" echo "SKIPPED: $TESTS_SKIPPED" echo "" local total=$((TESTS_PASSED + TESTS_FAILED)) if [[ $total -gt 0 ]]; then local pass_rate=$((TESTS_PASSED * 100 / total)) echo "Pass Rate: ${pass_rate}%" fi # Exit code based on failures if [[ $TESTS_FAILED -eq 0 ]]; then echo -e "${GREEN}All non-skipped tests passed!${NC}" exit 0 else echo -e "${RED}Some tests failed${NC}" exit 1 fi } main "$@"