Makefile 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. # CORS Integration Tests Makefile
  2. # This Makefile provides comprehensive targets for running CORS integration tests
  3. .PHONY: help build-weed setup-server start-server stop-server test-cors test-cors-quick test-cors-comprehensive test-all clean logs check-deps
  4. # Configuration
  5. WEED_BINARY := ../../../weed/weed_binary
  6. S3_PORT := 8333
  7. MASTER_PORT := 9333
  8. VOLUME_PORT := 8080
  9. FILER_PORT := 8888
  10. TEST_TIMEOUT := 10m
  11. TEST_PATTERN := TestCORS
  12. # Default target
  13. help:
  14. @echo "CORS Integration Tests Makefile"
  15. @echo ""
  16. @echo "Available targets:"
  17. @echo " help - Show this help message"
  18. @echo " build-weed - Build the SeaweedFS binary"
  19. @echo " check-deps - Check dependencies and build binary if needed"
  20. @echo " start-server - Start SeaweedFS server for testing"
  21. @echo " start-server-simple - Start server without process cleanup (for CI)"
  22. @echo " stop-server - Stop SeaweedFS server"
  23. @echo " test-cors - Run all CORS tests"
  24. @echo " test-cors-quick - Run core CORS tests only"
  25. @echo " test-cors-simple - Run tests without server management"
  26. @echo " test-cors-comprehensive - Run comprehensive CORS tests"
  27. @echo " test-with-server - Start server, run tests, stop server"
  28. @echo " logs - Show server logs"
  29. @echo " clean - Clean up test artifacts and stop server"
  30. @echo " health-check - Check if server is accessible"
  31. @echo ""
  32. @echo "Configuration:"
  33. @echo " S3_PORT=${S3_PORT}"
  34. @echo " TEST_TIMEOUT=${TEST_TIMEOUT}"
  35. # Build the SeaweedFS binary
  36. build-weed:
  37. @echo "Building SeaweedFS binary..."
  38. @cd ../../../weed && go build -o weed_binary .
  39. @chmod +x $(WEED_BINARY)
  40. @echo "✅ SeaweedFS binary built at $(WEED_BINARY)"
  41. check-deps: build-weed
  42. @echo "Checking dependencies..."
  43. @echo "🔍 DEBUG: Checking Go installation..."
  44. @command -v go >/dev/null 2>&1 || (echo "Go is required but not installed" && exit 1)
  45. @echo "🔍 DEBUG: Go version: $$(go version)"
  46. @echo "🔍 DEBUG: Checking binary at $(WEED_BINARY)..."
  47. @test -f $(WEED_BINARY) || (echo "SeaweedFS binary not found at $(WEED_BINARY)" && exit 1)
  48. @echo "🔍 DEBUG: Binary size: $$(ls -lh $(WEED_BINARY) | awk '{print $$5}')"
  49. @echo "🔍 DEBUG: Binary permissions: $$(ls -la $(WEED_BINARY) | awk '{print $$1}')"
  50. @echo "🔍 DEBUG: Checking Go module dependencies..."
  51. @go list -m github.com/aws/aws-sdk-go-v2 >/dev/null 2>&1 || (echo "AWS SDK Go v2 not found. Run 'go mod tidy'." && exit 1)
  52. @go list -m github.com/stretchr/testify >/dev/null 2>&1 || (echo "Testify not found. Run 'go mod tidy'." && exit 1)
  53. @echo "✅ All dependencies are available"
  54. # Start SeaweedFS server for testing
  55. start-server: check-deps
  56. @echo "Starting SeaweedFS server..."
  57. @echo "🔍 DEBUG: Current working directory: $$(pwd)"
  58. @echo "🔍 DEBUG: Checking for existing weed processes..."
  59. @ps aux | grep weed | grep -v grep || echo "No existing weed processes found"
  60. @echo "🔍 DEBUG: Cleaning up any existing PID file..."
  61. @rm -f weed-server.pid
  62. @echo "🔍 DEBUG: Checking for port conflicts..."
  63. @if netstat -tlnp 2>/dev/null | grep $(S3_PORT) >/dev/null; then \
  64. echo "⚠️ Port $(S3_PORT) is already in use, trying to find the process..."; \
  65. netstat -tlnp 2>/dev/null | grep $(S3_PORT) || true; \
  66. else \
  67. echo "✅ Port $(S3_PORT) is available"; \
  68. fi
  69. @echo "🔍 DEBUG: Checking binary at $(WEED_BINARY)"
  70. @ls -la $(WEED_BINARY) || (echo "❌ Binary not found!" && exit 1)
  71. @echo "🔍 DEBUG: Checking config file at ../../../docker/compose/s3.json"
  72. @ls -la ../../../docker/compose/s3.json || echo "⚠️ Config file not found, continuing without it"
  73. @echo "🔍 DEBUG: Creating volume directory..."
  74. @mkdir -p ./test-volume-data
  75. @echo "🔍 DEBUG: Launching SeaweedFS server in background..."
  76. @echo "🔍 DEBUG: Command: $(WEED_BINARY) server -debug -s3 -s3.port=$(S3_PORT) -s3.allowEmptyFolder=false -s3.allowDeleteBucketNotEmpty=true -s3.config=../../../docker/compose/s3.json -filer -filer.maxMB=64 -master.volumeSizeLimitMB=50 -volume.max=100 -dir=./test-volume-data -volume.preStopSeconds=1 -metricsPort=9324"
  77. @$(WEED_BINARY) server \
  78. -debug \
  79. -s3 \
  80. -s3.port=$(S3_PORT) \
  81. -s3.allowEmptyFolder=false \
  82. -s3.allowDeleteBucketNotEmpty=true \
  83. -s3.config=../../../docker/compose/s3.json \
  84. -filer \
  85. -filer.maxMB=64 \
  86. -master.volumeSizeLimitMB=50 \
  87. -volume.max=100 \
  88. -dir=./test-volume-data \
  89. -volume.preStopSeconds=1 \
  90. -metricsPort=9324 \
  91. > weed-test.log 2>&1 & echo $$! > weed-server.pid
  92. @echo "🔍 DEBUG: Server PID: $$(cat weed-server.pid 2>/dev/null || echo 'PID file not found')"
  93. @echo "🔍 DEBUG: Checking if PID is still running..."
  94. @sleep 2
  95. @if [ -f weed-server.pid ]; then \
  96. SERVER_PID=$$(cat weed-server.pid); \
  97. ps -p $$SERVER_PID || echo "⚠️ Server PID $$SERVER_PID not found after 2 seconds"; \
  98. else \
  99. echo "⚠️ PID file not found"; \
  100. fi
  101. @echo "🔍 DEBUG: Waiting for server to start (up to 90 seconds)..."
  102. @for i in $$(seq 1 90); do \
  103. echo "🔍 DEBUG: Attempt $$i/90 - checking port $(S3_PORT)"; \
  104. if curl -s http://localhost:$(S3_PORT) >/dev/null 2>&1; then \
  105. echo "✅ SeaweedFS server started successfully on port $(S3_PORT) after $$i seconds"; \
  106. exit 0; \
  107. fi; \
  108. if [ $$i -eq 5 ]; then \
  109. echo "🔍 DEBUG: After 5 seconds, checking process and logs..."; \
  110. ps aux | grep weed | grep -v grep || echo "No weed processes found"; \
  111. if [ -f weed-test.log ]; then \
  112. echo "=== First server logs ==="; \
  113. head -20 weed-test.log; \
  114. fi; \
  115. fi; \
  116. if [ $$i -eq 15 ]; then \
  117. echo "🔍 DEBUG: After 15 seconds, checking port bindings..."; \
  118. netstat -tlnp 2>/dev/null | grep $(S3_PORT) || echo "Port $(S3_PORT) not bound"; \
  119. netstat -tlnp 2>/dev/null | grep 9333 || echo "Port 9333 not bound"; \
  120. netstat -tlnp 2>/dev/null | grep 8080 || echo "Port 8080 not bound"; \
  121. fi; \
  122. if [ $$i -eq 30 ]; then \
  123. echo "⚠️ Server taking longer than expected (30s), checking logs..."; \
  124. if [ -f weed-test.log ]; then \
  125. echo "=== Recent server logs ==="; \
  126. tail -20 weed-test.log; \
  127. fi; \
  128. fi; \
  129. sleep 1; \
  130. done; \
  131. echo "❌ Server failed to start within 90 seconds"; \
  132. echo "🔍 DEBUG: Final process check:"; \
  133. ps aux | grep weed | grep -v grep || echo "No weed processes found"; \
  134. echo "🔍 DEBUG: Final port check:"; \
  135. netstat -tlnp 2>/dev/null | grep -E "(8333|9333|8080)" || echo "No ports bound"; \
  136. echo "=== Full server logs ==="; \
  137. if [ -f weed-test.log ]; then \
  138. cat weed-test.log; \
  139. else \
  140. echo "No log file found"; \
  141. fi; \
  142. exit 1
  143. # Stop SeaweedFS server
  144. stop-server:
  145. @echo "Stopping SeaweedFS server..."
  146. @if [ -f weed-server.pid ]; then \
  147. SERVER_PID=$$(cat weed-server.pid); \
  148. echo "Killing server PID $$SERVER_PID"; \
  149. if ps -p $$SERVER_PID >/dev/null 2>&1; then \
  150. kill -TERM $$SERVER_PID 2>/dev/null || true; \
  151. sleep 2; \
  152. if ps -p $$SERVER_PID >/dev/null 2>&1; then \
  153. echo "Process still running, sending KILL signal..."; \
  154. kill -KILL $$SERVER_PID 2>/dev/null || true; \
  155. sleep 1; \
  156. fi; \
  157. else \
  158. echo "Process $$SERVER_PID not found (already stopped)"; \
  159. fi; \
  160. rm -f weed-server.pid; \
  161. else \
  162. echo "No PID file found, checking for running processes..."; \
  163. echo "⚠️ Skipping automatic process cleanup to avoid CI issues"; \
  164. echo "Note: Any remaining weed processes should be cleaned up by the CI environment"; \
  165. fi
  166. @echo "✅ SeaweedFS server stopped"
  167. # Show server logs
  168. logs:
  169. @if test -f weed-test.log; then \
  170. echo "=== SeaweedFS Server Logs ==="; \
  171. tail -f weed-test.log; \
  172. else \
  173. echo "No log file found. Server may not be running."; \
  174. fi
  175. # Core CORS tests (basic functionality)
  176. test-cors-quick: check-deps
  177. @echo "Running core CORS tests..."
  178. @go test -v -timeout=$(TEST_TIMEOUT) -run "TestCORSConfigurationManagement|TestCORSPreflightRequest|TestCORSActualRequest" .
  179. @echo "✅ Core CORS tests completed"
  180. # All CORS tests (comprehensive)
  181. test-cors: check-deps
  182. @echo "Running all CORS tests..."
  183. @go test -v -timeout=$(TEST_TIMEOUT) -run "$(TEST_PATTERN)" .
  184. @echo "✅ All CORS tests completed"
  185. # Comprehensive CORS tests (all features)
  186. test-cors-comprehensive: check-deps
  187. @echo "Running comprehensive CORS tests..."
  188. @go test -v -timeout=$(TEST_TIMEOUT) -run "TestCORS" .
  189. @echo "✅ Comprehensive CORS tests completed"
  190. # All tests without server management
  191. test-cors-simple: check-deps
  192. @echo "Running CORS tests (assuming server is already running)..."
  193. @go test -v -timeout=$(TEST_TIMEOUT) .
  194. @echo "✅ All CORS tests completed"
  195. # Start server, run tests, stop server
  196. test-with-server: start-server
  197. @echo "Running CORS tests with managed server..."
  198. @sleep 5 # Give server time to fully start
  199. @make test-cors-comprehensive || (echo "Tests failed, stopping server..." && make stop-server && exit 1)
  200. @make stop-server
  201. @echo "✅ All tests completed with managed server"
  202. # Health check
  203. health-check:
  204. @echo "Checking server health..."
  205. @if curl -s http://localhost:$(S3_PORT) >/dev/null 2>&1; then \
  206. echo "✅ Server is accessible on port $(S3_PORT)"; \
  207. else \
  208. echo "❌ Server is not accessible on port $(S3_PORT)"; \
  209. exit 1; \
  210. fi
  211. # Clean up
  212. clean:
  213. @echo "Cleaning up test artifacts..."
  214. @make stop-server
  215. @rm -f weed-test.log
  216. @rm -f weed-server.pid
  217. @rm -rf ./test-volume-data
  218. @rm -f cors.test
  219. @go clean -testcache
  220. @echo "✅ Cleanup completed"
  221. # Individual test targets for specific functionality
  222. test-basic-cors:
  223. @echo "Running basic CORS tests..."
  224. @go test -v -timeout=$(TEST_TIMEOUT) -run "TestCORSConfigurationManagement" .
  225. test-preflight-cors:
  226. @echo "Running preflight CORS tests..."
  227. @go test -v -timeout=$(TEST_TIMEOUT) -run "TestCORSPreflightRequest" .
  228. test-actual-cors:
  229. @echo "Running actual CORS request tests..."
  230. @go test -v -timeout=$(TEST_TIMEOUT) -run "TestCORSActualRequest" .
  231. test-origin-matching:
  232. @echo "Running origin matching tests..."
  233. @go test -v -timeout=$(TEST_TIMEOUT) -run "TestCORSOriginMatching" .
  234. test-header-matching:
  235. @echo "Running header matching tests..."
  236. @go test -v -timeout=$(TEST_TIMEOUT) -run "TestCORSHeaderMatching" .
  237. test-method-matching:
  238. @echo "Running method matching tests..."
  239. @go test -v -timeout=$(TEST_TIMEOUT) -run "TestCORSMethodMatching" .
  240. test-multiple-rules:
  241. @echo "Running multiple rules tests..."
  242. @go test -v -timeout=$(TEST_TIMEOUT) -run "TestCORSMultipleRulesMatching" .
  243. test-validation:
  244. @echo "Running validation tests..."
  245. @go test -v -timeout=$(TEST_TIMEOUT) -run "TestCORSValidation" .
  246. test-caching:
  247. @echo "Running caching tests..."
  248. @go test -v -timeout=$(TEST_TIMEOUT) -run "TestCORSCaching" .
  249. test-error-handling:
  250. @echo "Running error handling tests..."
  251. @go test -v -timeout=$(TEST_TIMEOUT) -run "TestCORSErrorHandling" .
  252. # Development targets
  253. dev-start: start-server
  254. @echo "Development server started. Access S3 API at http://localhost:$(S3_PORT)"
  255. @echo "To stop: make stop-server"
  256. dev-test: check-deps
  257. @echo "Running tests in development mode..."
  258. @go test -v -timeout=$(TEST_TIMEOUT) -run "TestCORSConfigurationManagement" .
  259. # CI targets
  260. ci-test: check-deps
  261. @echo "Running tests in CI mode..."
  262. @go test -v -timeout=$(TEST_TIMEOUT) -race .
  263. # All targets
  264. test-all: test-cors test-cors-comprehensive
  265. @echo "✅ All CORS tests completed"
  266. # Benchmark targets
  267. benchmark-cors:
  268. @echo "Running CORS performance benchmarks..."
  269. @go test -v -timeout=$(TEST_TIMEOUT) -bench=. -benchmem .
  270. # Coverage targets
  271. coverage:
  272. @echo "Running tests with coverage..."
  273. @go test -v -timeout=$(TEST_TIMEOUT) -coverprofile=coverage.out .
  274. @go tool cover -html=coverage.out -o coverage.html
  275. @echo "Coverage report generated: coverage.html"
  276. # Format and lint
  277. fmt:
  278. @echo "Formatting Go code..."
  279. @go fmt .
  280. lint:
  281. @echo "Running linter..."
  282. @golint . || echo "golint not available, skipping..."
  283. # Install dependencies for development
  284. install-deps:
  285. @echo "Installing Go dependencies..."
  286. @go mod tidy
  287. @go mod download
  288. # Show current configuration
  289. show-config:
  290. @echo "Current configuration:"
  291. @echo " WEED_BINARY: $(WEED_BINARY)"
  292. @echo " S3_PORT: $(S3_PORT)"
  293. @echo " TEST_TIMEOUT: $(TEST_TIMEOUT)"
  294. @echo " TEST_PATTERN: $(TEST_PATTERN)"
  295. # Legacy targets for backward compatibility
  296. test: test-with-server
  297. test-verbose: test-cors-comprehensive
  298. test-single: test-basic-cors
  299. test-clean: clean
  300. build: check-deps
  301. setup: check-deps