🍱 add test for rotation

This commit is contained in:
maxlerebourg
2026-07-26 03:04:42 +02:00
parent 0344e82223
commit 75227becca
3 changed files with 13 additions and 43 deletions
+12 -14
View File
@@ -67,23 +67,21 @@ func (rc *redisCache) nextReader() *simpleredis.SimpleRedis {
func (rc *redisCache) get(key string) (string, error) { func (rc *redisCache) get(key string) (string, error) {
value, err := rc.nextReader().Get(key) value, err := rc.nextReader().Get(key)
if err == nil { if err != nil {
valueString := string(value) switch err.Error() {
if len(valueString) > 0 { case simpleredis.RedisMiss:
return valueString, nil return "", errors.New(CacheMiss)
case simpleredis.RedisUnreachable:
return "", errors.New(CacheUnreachable)
default:
return "", err
} }
// Reachable and no error, but nothing stored: treat as a miss. This
// also keeps err non-nil for the switch below, which would otherwise
// panic on err.Error().
return "", errors.New(CacheMiss)
} }
switch err.Error() { valueString := string(value)
case simpleredis.RedisMiss: if len(valueString) > 0 {
return "", errors.New(CacheMiss) return valueString, nil
case simpleredis.RedisUnreachable:
return "", errors.New(CacheUnreachable)
} }
return "", err return "", errors.New(CacheMiss)
} }
func (rc *redisCache) set(key, value string, duration int64) { func (rc *redisCache) set(key, value string, duration int64) {
+1 -1
View File
@@ -25,6 +25,6 @@ http:
redisCacheHost: "@@REDIS_HOST@@" redisCacheHost: "@@REDIS_HOST@@"
redisCacheReadHosts: redisCacheReadHosts:
- "@@REDIS_READ_HOST@@" - "@@REDIS_READ_HOST@@"
- "@@REDIS_READ_HOST@@" - "@@REDIS_HOST@@"
forwardedHeadersTrustedIps: forwardedHeadersTrustedIps:
- "127.0.0.1/32" - "127.0.0.1/32"
-28
View File
@@ -1,28 +0,0 @@
#!/usr/bin/env bash
set -euo pipefail
HERE="$(cd "$(dirname "$0")" && pwd)"
# shellcheck source=../../lib/common.sh
source "$HERE/../../lib/common.sh"
SCENARIO=redis
# Redis cache check: reads are offloaded to a replica (redisCacheReadHosts)
# while writes go to the primary (redisCacheHost). The replica mock returns "f"
# (not banned) for 1.2.3.4 and "t" (banned) for 1.2.3.5; the primary mock always
# misses. All other IPs miss on the replica and fall through to the LAPI (no
# decision → allowed). Because the verdicts live only on the replica, the banned
# IP being blocked proves the plugin reads decisions from the replica, not the
# primary.
body() {
echo "[$SCENARIO] cached clean IP must pass"
assert_status "http://127.0.0.1:${WEB_PORT}/foo" 200 -H "X-Forwarded-For: 1.2.3.4"
echo "[$SCENARIO] cached banned IP must be blocked"
assert_status "http://127.0.0.1:${WEB_PORT}/foo" 403 -H "X-Forwarded-For: 1.2.3.5"
echo "[$SCENARIO] unknown IP (redis miss) must fall through to LAPI and pass"
assert_status "http://127.0.0.1:${WEB_PORT}/foo" 200 -H "X-Forwarded-For: 1.2.3.6"
}
run_scenario "$SCENARIO" "$HERE" body