setup_all_tests.sh 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #!/bin/bash
  2. # Complete Test Environment Setup Script
  3. # This script sets up all required services and configurations for S3 IAM integration tests
  4. set -e
  5. # Colors
  6. RED='\033[0;31m'
  7. GREEN='\033[0;32m'
  8. YELLOW='\033[1;33m'
  9. BLUE='\033[0;34m'
  10. NC='\033[0m'
  11. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  12. echo -e "${BLUE}🚀 Setting up complete test environment for SeaweedFS S3 IAM...${NC}"
  13. echo -e "${BLUE}==========================================================${NC}"
  14. # Check prerequisites
  15. check_prerequisites() {
  16. echo -e "${YELLOW}🔍 Checking prerequisites...${NC}"
  17. local missing_tools=()
  18. for tool in docker jq curl; do
  19. if ! command -v "$tool" >/dev/null 2>&1; then
  20. missing_tools+=("$tool")
  21. fi
  22. done
  23. if [ ${#missing_tools[@]} -gt 0 ]; then
  24. echo -e "${RED}❌ Missing required tools: ${missing_tools[*]}${NC}"
  25. echo -e "${YELLOW}Please install the missing tools and try again${NC}"
  26. exit 1
  27. fi
  28. echo -e "${GREEN}✅ All prerequisites met${NC}"
  29. }
  30. # Set up Keycloak for OIDC testing
  31. setup_keycloak() {
  32. echo -e "\n${BLUE}1. Setting up Keycloak for OIDC testing...${NC}"
  33. if ! "${SCRIPT_DIR}/setup_keycloak.sh"; then
  34. echo -e "${RED}❌ Failed to set up Keycloak${NC}"
  35. return 1
  36. fi
  37. echo -e "${GREEN}✅ Keycloak setup completed${NC}"
  38. }
  39. # Set up SeaweedFS test cluster
  40. setup_seaweedfs_cluster() {
  41. echo -e "\n${BLUE}2. Setting up SeaweedFS test cluster...${NC}"
  42. # Build SeaweedFS binary if needed
  43. echo -e "${YELLOW}🔧 Building SeaweedFS binary...${NC}"
  44. cd "${SCRIPT_DIR}/../../../" # Go to seaweedfs root
  45. if ! make > /dev/null 2>&1; then
  46. echo -e "${RED}❌ Failed to build SeaweedFS binary${NC}"
  47. return 1
  48. fi
  49. cd "${SCRIPT_DIR}" # Return to test directory
  50. # Clean up any existing test data
  51. echo -e "${YELLOW}🧹 Cleaning up existing test data...${NC}"
  52. rm -rf test-volume-data/* 2>/dev/null || true
  53. echo -e "${GREEN}✅ SeaweedFS cluster setup completed${NC}"
  54. }
  55. # Set up test data and configurations
  56. setup_test_configurations() {
  57. echo -e "\n${BLUE}3. Setting up test configurations...${NC}"
  58. # Ensure IAM configuration is properly set up
  59. if [ ! -f "${SCRIPT_DIR}/iam_config.json" ]; then
  60. echo -e "${YELLOW}⚠️ IAM configuration not found, using default config${NC}"
  61. cp "${SCRIPT_DIR}/iam_config.local.json" "${SCRIPT_DIR}/iam_config.json" 2>/dev/null || {
  62. echo -e "${RED}❌ No IAM configuration files found${NC}"
  63. return 1
  64. }
  65. fi
  66. # Validate configuration
  67. if ! jq . "${SCRIPT_DIR}/iam_config.json" >/dev/null; then
  68. echo -e "${RED}❌ Invalid IAM configuration JSON${NC}"
  69. return 1
  70. fi
  71. echo -e "${GREEN}✅ Test configurations set up${NC}"
  72. }
  73. # Verify services are ready
  74. verify_services() {
  75. echo -e "\n${BLUE}4. Verifying services are ready...${NC}"
  76. # Check if Keycloak is responding
  77. echo -e "${YELLOW}🔍 Checking Keycloak availability...${NC}"
  78. local keycloak_ready=false
  79. for i in $(seq 1 30); do
  80. if curl -sf "http://localhost:8080/health/ready" >/dev/null 2>&1; then
  81. keycloak_ready=true
  82. break
  83. fi
  84. if curl -sf "http://localhost:8080/realms/master" >/dev/null 2>&1; then
  85. keycloak_ready=true
  86. break
  87. fi
  88. sleep 2
  89. done
  90. if [ "$keycloak_ready" = true ]; then
  91. echo -e "${GREEN}✅ Keycloak is ready${NC}"
  92. else
  93. echo -e "${YELLOW}⚠️ Keycloak may not be fully ready yet${NC}"
  94. echo -e "${YELLOW}This is okay - tests will wait for Keycloak when needed${NC}"
  95. fi
  96. echo -e "${GREEN}✅ Service verification completed${NC}"
  97. }
  98. # Set up environment variables
  99. setup_environment() {
  100. echo -e "\n${BLUE}5. Setting up environment variables...${NC}"
  101. export ENABLE_DISTRIBUTED_TESTS=true
  102. export ENABLE_PERFORMANCE_TESTS=true
  103. export ENABLE_STRESS_TESTS=true
  104. export KEYCLOAK_URL="http://localhost:8080"
  105. export S3_ENDPOINT="http://localhost:8333"
  106. export TEST_TIMEOUT=60m
  107. export CGO_ENABLED=0
  108. # Write environment to a file for other scripts to source
  109. cat > "${SCRIPT_DIR}/.test_env" << EOF
  110. export ENABLE_DISTRIBUTED_TESTS=true
  111. export ENABLE_PERFORMANCE_TESTS=true
  112. export ENABLE_STRESS_TESTS=true
  113. export KEYCLOAK_URL="http://localhost:8080"
  114. export S3_ENDPOINT="http://localhost:8333"
  115. export TEST_TIMEOUT=60m
  116. export CGO_ENABLED=0
  117. EOF
  118. echo -e "${GREEN}✅ Environment variables set${NC}"
  119. }
  120. # Display setup summary
  121. display_summary() {
  122. echo -e "\n${BLUE}📊 Setup Summary${NC}"
  123. echo -e "${BLUE}=================${NC}"
  124. echo -e "Keycloak URL: ${KEYCLOAK_URL:-http://localhost:8080}"
  125. echo -e "S3 Endpoint: ${S3_ENDPOINT:-http://localhost:8333}"
  126. echo -e "Test Timeout: ${TEST_TIMEOUT:-60m}"
  127. echo -e "IAM Config: ${SCRIPT_DIR}/iam_config.json"
  128. echo -e ""
  129. echo -e "${GREEN}✅ Complete test environment setup finished!${NC}"
  130. echo -e "${YELLOW}💡 You can now run tests with: make run-all-tests${NC}"
  131. echo -e "${YELLOW}💡 Or run specific tests with: go test -v -timeout=60m -run TestName${NC}"
  132. echo -e "${YELLOW}💡 To stop Keycloak: docker stop keycloak-iam-test${NC}"
  133. }
  134. # Main execution
  135. main() {
  136. check_prerequisites
  137. # Track what was set up for cleanup on failure
  138. local setup_steps=()
  139. if setup_keycloak; then
  140. setup_steps+=("keycloak")
  141. else
  142. echo -e "${RED}❌ Failed to set up Keycloak${NC}"
  143. exit 1
  144. fi
  145. if setup_seaweedfs_cluster; then
  146. setup_steps+=("seaweedfs")
  147. else
  148. echo -e "${RED}❌ Failed to set up SeaweedFS cluster${NC}"
  149. exit 1
  150. fi
  151. if setup_test_configurations; then
  152. setup_steps+=("config")
  153. else
  154. echo -e "${RED}❌ Failed to set up test configurations${NC}"
  155. exit 1
  156. fi
  157. setup_environment
  158. verify_services
  159. display_summary
  160. echo -e "${GREEN}🎉 All setup completed successfully!${NC}"
  161. }
  162. # Cleanup on script interruption
  163. cleanup() {
  164. echo -e "\n${YELLOW}🧹 Cleaning up on script interruption...${NC}"
  165. # Note: We don't automatically stop Keycloak as it might be shared
  166. echo -e "${YELLOW}💡 If you want to stop Keycloak: docker stop keycloak-iam-test${NC}"
  167. exit 1
  168. }
  169. trap cleanup INT TERM
  170. # Execute main function
  171. main "$@"