|
| 1 | +# Redis Configuration |
| 2 | + |
| 3 | +This guide covers how to configure Redis for integration testing with tomato. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +Redis is an in-memory data structure store used as a database, cache, and message broker. Tomato's Redis handler supports strings, hashes, lists, and sets. |
| 8 | + |
| 9 | +## Container Setup |
| 10 | + |
| 11 | +Add a Redis container to your `tomato.yml`: |
| 12 | + |
| 13 | +```yaml |
| 14 | +containers: |
| 15 | + redis: |
| 16 | + image: redis:7-alpine |
| 17 | + ports: |
| 18 | + - "6379/tcp" |
| 19 | + wait_for: |
| 20 | + type: port |
| 21 | + target: "6379" |
| 22 | + timeout: 30s |
| 23 | +``` |
| 24 | +
|
| 25 | +### With Password Authentication |
| 26 | +
|
| 27 | +```yaml |
| 28 | +containers: |
| 29 | + redis: |
| 30 | + image: redis:7-alpine |
| 31 | + command: redis-server --requirepass mysecretpassword |
| 32 | + ports: |
| 33 | + - "6379/tcp" |
| 34 | + wait_for: |
| 35 | + type: port |
| 36 | + target: "6379" |
| 37 | + timeout: 30s |
| 38 | +``` |
| 39 | +
|
| 40 | +### With Persistence (Optional) |
| 41 | +
|
| 42 | +For tests that need data persistence across container restarts: |
| 43 | +
|
| 44 | +```yaml |
| 45 | +containers: |
| 46 | + redis: |
| 47 | + image: redis:7-alpine |
| 48 | + command: redis-server --appendonly yes |
| 49 | + ports: |
| 50 | + - "6379/tcp" |
| 51 | + volumes: |
| 52 | + - redis-data:/data |
| 53 | + wait_for: |
| 54 | + type: port |
| 55 | + target: "6379" |
| 56 | + timeout: 30s |
| 57 | +``` |
| 58 | +
|
| 59 | +## Resource Configuration |
| 60 | +
|
| 61 | +Configure the Redis resource: |
| 62 | +
|
| 63 | +```yaml |
| 64 | +resources: |
| 65 | + cache: |
| 66 | + type: redis |
| 67 | + container: redis |
| 68 | + options: |
| 69 | + db: 0 |
| 70 | + password: "" |
| 71 | + reset_strategy: flush |
| 72 | +``` |
| 73 | +
|
| 74 | +### Resource Options |
| 75 | +
|
| 76 | +| Option | Type | Default | Description | |
| 77 | +|--------|------|---------|-------------| |
| 78 | +| `db` | int | `0` | Redis database number (0-15) | |
| 79 | +| `password` | string | `""` | Redis password (if authentication enabled) | |
| 80 | +| `reset_strategy` | string | `flush` | How to reset between scenarios | |
| 81 | +| `reset_pattern` | string | `*` | Pattern for selective key deletion (when using `pattern` strategy) | |
| 82 | + |
| 83 | +### Reset Strategies |
| 84 | + |
| 85 | +| Strategy | Description | |
| 86 | +|----------|-------------| |
| 87 | +| `flush` | Flush all keys in the database (recommended, fastest) | |
| 88 | +| `pattern` | Delete keys matching `reset_pattern` (useful for shared databases) | |
| 89 | + |
| 90 | +## Complete Example |
| 91 | + |
| 92 | +Here's a complete `tomato.yml` with Redis: |
| 93 | + |
| 94 | +```yaml |
| 95 | +version: 2 |
| 96 | +
|
| 97 | +settings: |
| 98 | + timeout: 5m |
| 99 | + fail_fast: false |
| 100 | + output: pretty |
| 101 | + reset: |
| 102 | + level: scenario |
| 103 | +
|
| 104 | +containers: |
| 105 | + redis: |
| 106 | + image: redis:7-alpine |
| 107 | + ports: |
| 108 | + - "6379/tcp" |
| 109 | + wait_for: |
| 110 | + type: port |
| 111 | + target: "6379" |
| 112 | + timeout: 30s |
| 113 | +
|
| 114 | +resources: |
| 115 | + cache: |
| 116 | + type: redis |
| 117 | + container: redis |
| 118 | + options: |
| 119 | + db: 0 |
| 120 | + reset_strategy: flush |
| 121 | +
|
| 122 | +features: |
| 123 | + paths: |
| 124 | + - ./features |
| 125 | +``` |
| 126 | + |
| 127 | +## Writing Redis Tests |
| 128 | + |
| 129 | +### String Operations |
| 130 | + |
| 131 | +```gherkin |
| 132 | +Feature: Caching |
| 133 | +
|
| 134 | + Scenario: Cache user data |
| 135 | + Given "cache" key "user:123" is "John Doe" |
| 136 | + Then "cache" key "user:123" exists |
| 137 | + And "cache" key "user:123" has value "John Doe" |
| 138 | +
|
| 139 | + Scenario: Cache with expiration |
| 140 | + Given "cache" key "session:abc" is "token123" with TTL "1h" |
| 141 | + Then "cache" key "session:abc" has TTL greater than "3500" seconds |
| 142 | +
|
| 143 | + Scenario: Store JSON data |
| 144 | + Given "cache" key "config" is: |
| 145 | + """ |
| 146 | + {"debug": true, "timeout": 30} |
| 147 | + """ |
| 148 | + Then "cache" key "config" contains "debug" |
| 149 | +``` |
| 150 | + |
| 151 | +### Hash Operations |
| 152 | + |
| 153 | +```gherkin |
| 154 | +Scenario: Store user profile as hash |
| 155 | + Given "cache" hash "user:100" has fields: |
| 156 | + | field | value | |
| 157 | + | name | Alice | |
| 158 | + | email | alice@test.com | |
| 159 | + | role | admin | |
| 160 | + Then "cache" hash "user:100" field "name" is "Alice" |
| 161 | + And "cache" hash "user:100" contains: |
| 162 | + | field | value | |
| 163 | + | email | alice@test.com | |
| 164 | +``` |
| 165 | + |
| 166 | +### List Operations |
| 167 | + |
| 168 | +```gherkin |
| 169 | +Scenario: Manage task queue |
| 170 | + Given "cache" list "tasks" has values: |
| 171 | + | process-order-1 | |
| 172 | + | process-order-2 | |
| 173 | + | send-email-3 | |
| 174 | + Then "cache" list "tasks" has "3" items |
| 175 | + And "cache" list "tasks" contains "process-order-1" |
| 176 | +``` |
| 177 | + |
| 178 | +### Set Operations |
| 179 | + |
| 180 | +```gherkin |
| 181 | +Scenario: Track unique visitors |
| 182 | + Given "cache" set "visitors" has members: |
| 183 | + | user-1 | |
| 184 | + | user-2 | |
| 185 | + | user-3 | |
| 186 | + Then "cache" set "visitors" has "3" members |
| 187 | + And "cache" set "visitors" contains "user-2" |
| 188 | +``` |
| 189 | + |
| 190 | +### Counter Operations |
| 191 | + |
| 192 | +```gherkin |
| 193 | +Scenario: Track page views |
| 194 | + Given "cache" key "pageviews" is "100" |
| 195 | + When "cache" key "pageviews" is incremented |
| 196 | + Then "cache" key "pageviews" has value "101" |
| 197 | + When "cache" key "pageviews" is incremented by "10" |
| 198 | + Then "cache" key "pageviews" has value "111" |
| 199 | +``` |
| 200 | + |
| 201 | +See [Redis Steps](../resources/redis.md) for the complete list of available steps. |
| 202 | + |
| 203 | +## Multiple Redis Databases |
| 204 | + |
| 205 | +You can configure multiple Redis resources pointing to different databases: |
| 206 | + |
| 207 | +```yaml |
| 208 | +resources: |
| 209 | + cache: |
| 210 | + type: redis |
| 211 | + container: redis |
| 212 | + options: |
| 213 | + db: 0 |
| 214 | + reset_strategy: flush |
| 215 | +
|
| 216 | + sessions: |
| 217 | + type: redis |
| 218 | + container: redis |
| 219 | + options: |
| 220 | + db: 1 |
| 221 | + reset_strategy: flush |
| 222 | +``` |
| 223 | + |
| 224 | +## Troubleshooting |
| 225 | + |
| 226 | +### Connection Refused |
| 227 | + |
| 228 | +If tests fail with "connection refused": |
| 229 | + |
| 230 | +1. Verify Redis container is running: `docker ps` |
| 231 | +2. Check port mapping: `docker port <container_id>` |
| 232 | +3. Ensure no firewall is blocking the connection |
| 233 | + |
| 234 | +### Authentication Failed |
| 235 | + |
| 236 | +If using password authentication: |
| 237 | + |
| 238 | +1. Verify password matches in container command and resource options |
| 239 | +2. Check for special characters that may need escaping |
| 240 | + |
| 241 | +### Keys Not Being Reset |
| 242 | + |
| 243 | +If keys persist between scenarios: |
| 244 | + |
| 245 | +1. Verify `reset_strategy` is set (defaults to `flush`) |
| 246 | +2. If using `pattern` strategy, verify `reset_pattern` matches your keys |
| 247 | +3. Check that `settings.reset.level` is set to `scenario` |
0 commit comments