|
| 1 | +# Captcha Protect CI Integration Tests |
| 2 | + |
| 3 | +This directory contains integration tests for the captcha-protect plugin that verify state persistence and sharing functionality. |
| 4 | + |
| 5 | +## Test Modes |
| 6 | + |
| 7 | +There are two separate test modes: |
| 8 | + |
| 9 | +### 1. File-Based State Persistence Test (`test-file.go`) |
| 10 | + |
| 11 | +Tests that state is correctly persisted to a file and reloaded after container restarts. |
| 12 | + |
| 13 | +- **Docker Compose:** `docker-compose-file.yml` |
| 14 | +- **Single nginx instance** |
| 15 | +- **Tests:** |
| 16 | + - Rate limiting works correctly |
| 17 | + - State saves to `/tmp/state.json` |
| 18 | + - State is reloaded after container restart |
| 19 | + - All bot entries are preserved |
| 20 | + |
| 21 | +**Run with:** |
| 22 | +```bash |
| 23 | +TEST_MODE=file go run . |
| 24 | +``` |
| 25 | + |
| 26 | +### 2. Redis-Based State Sharing Test (`test-redis.go`) |
| 27 | + |
| 28 | +Tests that state is shared between multiple plugin instances via Redis pub/sub. |
| 29 | + |
| 30 | +- **Docker Compose:** `docker-compose-redis.yml` |
| 31 | +- **Two nginx instances** (`nginx` and `nginx2`) |
| 32 | +- **Redis instance** for state sharing |
| 33 | +- **Tests:** |
| 34 | + - Rate limiting works on first instance |
| 35 | + - State is shared to second instance via Redis |
| 36 | + - Both instances see the same rate limit state in near real-time |
| 37 | + - State persists across container restarts via Redis |
| 38 | + |
| 39 | +**Run with:** |
| 40 | +```bash |
| 41 | +TEST_MODE=redis go run . |
| 42 | +``` |
| 43 | + |
| 44 | +### 3. Run Both Tests (Default) |
| 45 | + |
| 46 | +If no `TEST_MODE` is specified, both tests run sequentially: |
| 47 | + |
| 48 | +```bash |
| 49 | +go run . |
| 50 | +``` |
| 51 | + |
| 52 | +## Configuration |
| 53 | + |
| 54 | +Environment variables: |
| 55 | + |
| 56 | +- `TEST_MODE` - Test mode: `file`, `redis`, or empty for both |
| 57 | +- `RATE_LIMIT` - Rate limit threshold (default: 5) |
| 58 | +- `TRAEFIK_TAG` - Traefik Docker image tag (e.g., `v3.0`) |
| 59 | +- `NGINX_TAG` - Nginx Docker image tag |
| 60 | +- `TURNSTILE_SITE_KEY` - Cloudflare Turnstile site key |
| 61 | +- `TURNSTILE_SECRET_KEY` - Cloudflare Turnstile secret key |
| 62 | + |
| 63 | +## How It Works |
| 64 | + |
| 65 | +### File-Based Test |
| 66 | + |
| 67 | +1. Starts Traefik + 1 nginx instance with `persistentStateFile: "/tmp/state.json"` |
| 68 | +2. Generates 100 unique public IPs (different /16 subnets) |
| 69 | +3. Makes parallel requests from each IP until rate limit is hit |
| 70 | +4. Verifies challenge redirect occurs on next request |
| 71 | +5. Waits for state to save to disk |
| 72 | +6. Restarts containers |
| 73 | +7. Verifies all 100 IPs are still in the bot cache (state was reloaded) |
| 74 | + |
| 75 | +### Redis-Based Test |
| 76 | + |
| 77 | +1. Starts Traefik + 2 nginx instances + Redis with `redisAddr: "redis:6379"` |
| 78 | +2. Generates 100 unique public IPs |
| 79 | +3. Makes parallel requests to first nginx instance until rate limit is hit |
| 80 | +4. Verifies challenge redirect on first instance |
| 81 | +5. Waits for Redis sync (5 second interval + buffer) |
| 82 | +6. Tests same IP on second nginx instance (`/app2`) |
| 83 | +7. Verifies second instance also shows rate limit (state was shared via Redis) |
| 84 | +8. Restarts both nginx instances |
| 85 | +9. Verifies state was reloaded from Redis |
| 86 | + |
| 87 | +## File Structure |
| 88 | + |
| 89 | +``` |
| 90 | +ci/ |
| 91 | +├── test.go # Main dispatcher |
| 92 | +├── test-file.go # File-based persistence test |
| 93 | +├── test-redis.go # Redis-based sharing test |
| 94 | +├── docker-compose-file.yml # Single nginx + file persistence |
| 95 | +├── docker-compose-redis.yml # Two nginx + Redis |
| 96 | +├── conf/nginx/default.conf # Nginx configuration |
| 97 | +└── tmp/ # State files (gitignored) |
| 98 | +``` |
| 99 | + |
| 100 | +## Example Usage |
| 101 | + |
| 102 | +**Important:** Always use `go run .` (not `go run test.go`) to compile all source files in the package. |
| 103 | + |
| 104 | +```bash |
| 105 | +# Run only file-based test |
| 106 | +TEST_MODE=file go run . |
| 107 | + |
| 108 | +# Run only Redis test |
| 109 | +TEST_MODE=redis go run . |
| 110 | + |
| 111 | +# Run both tests |
| 112 | +go run . |
| 113 | + |
| 114 | +# Use specific Traefik version |
| 115 | +TRAEFIK_TAG=v3.0 TEST_MODE=redis go run . |
| 116 | +``` |
| 117 | + |
| 118 | +### Common Errors |
| 119 | + |
| 120 | +**Error:** `undefined: runFileBasedTest` |
| 121 | +**Solution:** Use `go run .` instead of `go run test.go` to compile all `.go` files. |
| 122 | + |
| 123 | +## CI Integration |
| 124 | + |
| 125 | +The GitHub Actions workflow runs these tests against multiple Traefik versions: |
| 126 | + |
| 127 | +```yaml |
| 128 | +- name: Run integration tests |
| 129 | + run: | |
| 130 | + cd ci |
| 131 | + go run . |
| 132 | + env: |
| 133 | + TRAEFIK_TAG: v3.0 |
| 134 | +``` |
| 135 | +
|
| 136 | +Both file and Redis tests run by default in CI to ensure both state persistence mechanisms work correctly. |
0 commit comments