Files
Chris Lu 02574314f6 test(s3): force-drop collection after deleteBucket in tagging/versioning/cors/copying (#9270)
* test(s3): force-drop collection after deleteBucket across tagging/versioning/cors/copying

Each test creates a unique bucket (= new SeaweedFS collection) and the master's
warm-create issues a 7-volume grow batch. The S3 DeleteBucket-driven collection
sweep snapshots the layout once, but in-flight `volume_grow` requests keep
registering volumes after the snapshot, leaking 1-3 volumes per bucket. On a
single `weed mini` data node with the auto-derived volume cap, those leaks pile
up fast and every subsequent PutObject 500s with "Not enough data nodes found".

Mirror the retention-suite fix (commits ac3a756d, 363d5caa) into the four other
suites that share the same shape: defer a master-side /col/delete after each
bucket teardown, and sweep stale prefix-matching buckets before every new
createBucket. Each suite gets its own MASTER_ENDPOINT default plus the
allTestBucketPrefixes / cleanupAllTestBuckets / cleanupLeftoverTestBuckets /
forceDeleteCollection helpers.

Skipped: iam (separate framework + v1 SDK; already passes
-master.volumeSizeLimitMB=100), sse (different cleanup signature; docker-compose
already passes -volumeSizeLimitMB=50), policy (its TestCluster already uses
-master.volumeSizeLimitMB=32 and tears the whole cluster down). Suites under 5
tests cannot exhaust the cap and were left untouched.

* test(s3): default master endpoint to 127.0.0.1 to avoid IPv6 resolution

Mirror the explicit IPv4 default already used by test/s3/copying. On hosts
where `localhost` resolves to ::1 first the master HTTP listener (bound on
0.0.0.0) is unreachable, so the new force-delete-collection helper would silently
skip cleanup. Pin the default to 127.0.0.1 in tagging/cors/versioning; the
MASTER_ENDPOINT env var still wins when set.

* test(s3): skip buckets newer than this process in leftover-bucket sweep

cleanupAllTestBuckets/cleanupTestBuckets used to delete every bucket whose name
matched the suite's prefix. With shared S3/master endpoints, a second concurrent
`go test` run could have its live buckets torn down mid-test by the first run's
sweep.

Capture testRunStart at package init (with a 1-minute backdate for clock skew)
and skip any bucket whose CreationDate is newer than that. Stale buckets from
panicked or interrupted prior runs (the original target of the sweep) still get
collected because they were created before this process started.

* test(s3-copying): sweep stale prefix buckets from createBucket, not just from a few callers

The s3-copying suite already had cleanupTestBuckets that walks every bucket and
drops the test-copying-* prefix matches, but only four tests in the file invoke
it; the rest go straight to createBucket. So a panicked or interrupted prior run
could leak buckets that survive into the next run and exhaust the data node's
volume slots before any of the prefix-sweeping tests get a chance to run.

Hoist the sweep into createBucket so every test that creates a bucket starts on
a clean slate. The per-process CreationDate filter from the prior commit keeps
this safe under concurrent runs.

* test(s3): scope leftover-bucket sweep to this run via runID marker

The CreationDate filter only protected against runs that started later than this
process. A different `go test` against the same S3/master endpoints that started
*earlier* and is still active has buckets older than testRunStart, so the sweep
would still tear them down mid-test.

Replace the time window with a per-process runID baked into bucket names: every
bucket this run creates gets `r{runID}-` after the suite's BucketPrefix, and the
sweep only matches that owned subset. Concurrent runs each carry their own
runID and never see each other's buckets.

Trade-off: buckets left behind by a crashed prior run carry a different runID
and won't be cleaned up by this sweep. That recovery path now belongs to
`make clean` / data-dir wipe, which is what CI already does between jobs.
Fixed-name versioning buckets (e.g. test-versioning-directories) bypass
getNewBucketName and so also bypass this sweep — they are short-lived and
handled by their own deferred deleteBucket.

* test(s3): drop cleanupLeftoverTestBuckets wrapper, call cleanupAllTestBuckets directly

cleanupLeftoverTestBuckets was a one-line forwarder kept only as a semantic name
at the createBucket call site. Inline the call and update the doc comments to
point at the actual implementation. tagging/cors/versioning all had the wrapper;
copying never did.
2026-04-28 22:13:42 -07:00
..
2025-07-15 00:23:54 -07:00

CORS Integration Tests for SeaweedFS S3 API

This directory contains comprehensive integration tests for the CORS (Cross-Origin Resource Sharing) functionality in SeaweedFS S3 API.

Overview

The CORS integration tests validate the complete CORS implementation including:

  • CORS configuration management (PUT/GET/DELETE)
  • CORS rule validation
  • CORS middleware behavior
  • Caching functionality
  • Error handling
  • Real-world CORS scenarios

Prerequisites

  1. Go 1.19+: For building SeaweedFS and running tests
  2. Network Access: Tests use localhost:8333 by default
  3. System Dependencies: curl and netstat for health checks

Quick Start

The tests now automatically start their own SeaweedFS server, so you don't need to manually start one.

1. Run All Tests with Managed Server

# Run all tests with automatic server management
make test-with-server

# Run core CORS tests only
make test-cors-quick

# Run comprehensive CORS tests
make test-cors-comprehensive

2. Manual Server Management

If you prefer to manage the server manually:

# Start server
make start-server

# Run tests (assuming server is running)
make test-cors-simple

# Stop server
make stop-server

3. Individual Test Categories

# Run specific test types
make test-basic-cors           # Basic CORS configuration
make test-preflight-cors       # Preflight OPTIONS requests
make test-actual-cors          # Actual CORS request handling
make test-origin-matching      # Origin matching logic
make test-header-matching      # Header matching logic
make test-method-matching      # Method matching logic
make test-multiple-rules       # Multiple CORS rules
make test-validation           # CORS validation
make test-caching              # CORS caching behavior
make test-error-handling       # Error handling

Test Server Management

The tests use a comprehensive server management system similar to other SeaweedFS integration tests:

Server Configuration

  • S3 Port: 8333 (configurable via S3_PORT)
  • Master Port: 9333
  • Volume Port: 8080
  • Filer Port: 8888
  • Metrics Port: 9324
  • Data Directory: ./test-volume-data (auto-created)
  • Log File: weed-test.log

Server Lifecycle

  1. Build: Automatically builds ../../../weed/weed_binary
  2. Start: Launches SeaweedFS with S3 API enabled
  3. Health Check: Waits up to 90 seconds for server to be ready
  4. Test: Runs the requested tests
  5. Stop: Gracefully shuts down the server
  6. Cleanup: Removes temporary files and data

Available Commands

# Server management
make start-server              # Start SeaweedFS server
make stop-server               # Stop SeaweedFS server
make health-check              # Check server health
make logs                      # View server logs

# Test execution
make test-with-server          # Full test cycle with server management
make test-cors-simple          # Run tests without server management
make test-cors-quick           # Run core tests only
make test-cors-comprehensive   # Run all tests

# Development
make dev-start                 # Start server for development
make dev-test                  # Run development tests
make build-weed                # Build SeaweedFS binary
make check-deps                # Check dependencies

# Maintenance
make clean                     # Clean up all artifacts
make coverage                  # Generate coverage report
make fmt                       # Format code
make lint                      # Run linter

Test Configuration

Default Configuration

The tests use these default settings (configurable via environment variables):

WEED_BINARY=../../../weed/weed_binary
S3_PORT=8333
TEST_TIMEOUT=10m
TEST_PATTERN=TestCORS

Configuration File

The test_config.json file contains S3 client configuration:

{
  "endpoint": "http://localhost:8333",
  "access_key": "some_access_key1",
  "secret_key": "some_secret_key1",
  "region": "us-east-1",
  "bucket_prefix": "test-cors-",
  "use_ssl": false,
  "skip_verify_ssl": true
}

Troubleshooting

Compilation Issues

If you encounter compilation errors, the most common issues are:

  1. AWS SDK v2 Type Mismatches: The MaxAgeSeconds field in types.CORSRule expects int32, not *int32. Use direct values like 3600 instead of aws.Int32(3600).

  2. Field Name Issues: The GetBucketCorsOutput type has a CORSRules field directly, not a CORSConfiguration field.

Example fix:

// ❌ Incorrect
MaxAgeSeconds: aws.Int32(3600),
assert.Len(t, getResp.CORSConfiguration.CORSRules, 1)

// ✅ Correct
MaxAgeSeconds: 3600,
assert.Len(t, getResp.CORSRules, 1)

Server Issues

  1. Server Won't Start

    # Check for port conflicts
    netstat -tlnp | grep 8333
    
    # View server logs
    make logs
    
    # Force cleanup
    make clean
    
  2. Test Failures

    # Run with server management
    make test-with-server
    
    # Run specific test
    make test-basic-cors
    
    # Check server health
    make health-check
    
  3. Connection Issues

    # Verify server is running
    curl -s http://localhost:8333
    
    # Check server logs
    tail -f weed-test.log
    

Performance Issues

If tests are slow or timing out:

# Increase timeout
export TEST_TIMEOUT=30m
make test-with-server

# Run quick tests only
make test-cors-quick

# Check server resources
make debug-status

Test Coverage

Core Functionality Tests

1. CORS Configuration Management (TestCORSConfigurationManagement)

  • PUT CORS configuration
  • GET CORS configuration
  • DELETE CORS configuration
  • Configuration updates
  • Error handling for non-existent configurations

2. Multiple CORS Rules (TestCORSMultipleRules)

  • Multiple rules in single configuration
  • Rule precedence and ordering
  • Complex rule combinations

3. CORS Validation (TestCORSValidation)

  • Invalid HTTP methods
  • Empty origins validation
  • Negative MaxAge validation
  • Rule limit validation

4. Wildcard Support (TestCORSWithWildcards)

  • Wildcard origins (*, https://*.example.com)
  • Wildcard headers (*)
  • Wildcard expose headers

5. Rule Limits (TestCORSRuleLimit)

  • Maximum 100 rules per configuration
  • Rule limit enforcement
  • Large configuration handling

6. Error Handling (TestCORSErrorHandling)

  • Non-existent bucket operations
  • Invalid configurations
  • Malformed requests

HTTP-Level Tests

1. Preflight Requests (TestCORSPreflightRequest)

  • OPTIONS request handling
  • CORS headers in preflight responses
  • Access-Control-Request-Method validation
  • Access-Control-Request-Headers validation

2. Actual Requests (TestCORSActualRequest)

  • CORS headers in actual responses
  • Origin validation for real requests
  • Proper expose headers handling

3. Origin Matching (TestCORSOriginMatching)

  • Exact origin matching
  • Wildcard origin matching (*)
  • Subdomain wildcard matching (https://*.example.com)
  • Non-matching origins (should be rejected)

4. Header Matching (TestCORSHeaderMatching)

  • Wildcard header matching (*)
  • Specific header matching
  • Case-insensitive matching
  • Disallowed headers

5. Method Matching (TestCORSMethodMatching)

  • Allowed methods verification
  • Disallowed methods rejection
  • Method-specific CORS behavior

6. Multiple Rules (TestCORSMultipleRulesMatching)

  • Rule precedence and selection
  • Multiple rules with different configurations
  • Complex rule interactions

Integration Tests

1. Caching (TestCORSCaching)

  • CORS configuration caching
  • Cache invalidation
  • Cache performance

2. Object Operations (TestCORSObjectOperations)

  • CORS with actual S3 operations
  • PUT/GET/DELETE objects with CORS
  • CORS headers in object responses

3. Without Configuration (TestCORSWithoutConfiguration)

  • Behavior when no CORS configuration exists
  • Default CORS behavior
  • Graceful degradation

Development

Running Tests During Development

# Start server for development
make dev-start

# Run quick test
make dev-test

# View logs in real-time
make logs

Adding New Tests

  1. Follow the existing naming convention (TestCORSXxxYyy)
  2. Use the helper functions (getS3Client, createTestBucket, etc.)
  3. Add cleanup with defer cleanupTestBucket(t, client, bucketName)
  4. Include proper error checking with require.NoError(t, err)
  5. Use assertions with assert.Equal(t, expected, actual)
  6. Add the test to the appropriate Makefile target

Code Quality

# Format code
make fmt

# Run linter
make lint

# Generate coverage report
make coverage

Performance Notes

  • Tests create and destroy buckets for each test case
  • Large configuration tests may take several minutes
  • Server startup typically takes 15-30 seconds
  • Tests run in parallel where possible for efficiency

Integration with SeaweedFS

These tests validate the CORS implementation in:

  • weed/s3api/cors/ - Core CORS package
  • weed/s3api/s3api_bucket_cors_handlers.go - HTTP handlers
  • weed/s3api/s3api_server.go - Router integration
  • weed/s3api/s3api_bucket_config.go - Configuration management

The tests ensure AWS S3 API compatibility and proper CORS behavior across all supported scenarios.