Files
go-redis/universal_test.go
d11bf62381 chore(tests): run the Redis Enterprise suite against an external cluster (#3905)
* test: run the Redis Enterprise suite against an external cluster

Read the target database connection (host, port, username, password, TLS)
from REDIS_ENDPOINTS_CONFIG_PATH when RE_CLUSTER=true, and apply it to the
clients the suite builds (redisOptions, the per-suite setupRedisClient
closures, the RediSearch constructions, and the universal-client spec).
RE_DB_NAME selects a named database; when the env var is unset behaviour is
unchanged (localhost:6379, no auth).

Label specs that cannot meaningfully run against a remote, authenticated,
managed Redis Enterprise deployment NonRedisEnterprise, each with an
explanatory comment (client-internal/local-only, OSS-vs-Enterprise
behaviour, or impractical over network latency).

* test: skip the local-TLS tests when running against Redis Enterprise

TestTLSStandalone and TestTLSRedissURL are plain Go tests, so the
NonRedisEnterprise Ginkgo label filter cannot exclude them, and they
t.Fatalf when the docker-compose TLS server (localhost:6666) is absent.
Skip them explicitly on RE_CLUSTER=true runs; local runs keep failing
loudly when the TLS server is expected but missing.

* test: harden Redis Enterprise endpoint resolution

Fall through to the endpoint URLs when a raw endpoint carries no DNS
name instead of producing a ":port" address that dials localhost, and
share the relaxed timeout values between the Options and
UniversalOptions overlays so the two cannot drift.

* test: run the Auth spec against password-protected servers

A failed AUTH is reported as "ERR AUTH ..." by a passwordless OSS
server but as "WRONGPASS ..." by a password-protected one (e.g. Redis
Enterprise), so accept either and drop the NonRedisEnterprise label.

The two failing AUTH commands now go through separate pipelines: on a
password-protected Redis 8.x a second failed AUTH in the same pipeline
is throttled by the server's brute-force protection and never answered,
which stalls the read until the client times out.

* fix(test): bracket IPv6 endpoints with net.JoinHostPort

url.Hostname() strips the brackets from an IPv6 literal, so joining
host and port with "%s:%d" produced an undialable "::1:6379".
JoinHostPort restores the bracketed form; add a regression test
pinning the bracketed Addr.

---------

Co-authored-by: kiryazovi-redis <ivaylo.kiryazov@redis.com>
Co-authored-by: ofekshenawa <104765379+ofekshenawa@users.noreply.github.com>
2026-07-29 13:36:52 +03:00

155 lines
5.3 KiB
Go

package redis_test
import (
"fmt"
. "github.com/bsm/ginkgo/v2"
. "github.com/bsm/gomega"
"github.com/redis/go-redis/v9"
)
var _ = Describe("UniversalClient", Serial, func() {
var client redis.UniversalClient
AfterEach(func() {
if client != nil {
Expect(client.Close()).To(Succeed())
}
})
It("should connect to failover servers", Label("NonRedisEnterprise"), func() {
client = redis.NewUniversalClient(&redis.UniversalOptions{
MasterName: sentinelName,
Addrs: sentinelAddrs,
})
Expect(client.Ping(ctx).Err()).NotTo(HaveOccurred())
})
It("should connect to failover cluster", Label("NonRedisEnterprise"), func() {
client = redis.NewUniversalClient(&redis.UniversalOptions{
MasterName: sentinelName,
RouteRandomly: true,
Addrs: sentinelAddrs,
})
_, ok := client.(*redis.ClusterClient)
Expect(ok).To(BeTrue(), "expected a ClusterClient")
})
It("should connect to simple servers", func() {
opt := &redis.UniversalOptions{
Addrs: []string{redisAddr},
}
applyREConnectionUniversal(opt)
client = redis.NewUniversalClient(opt)
Expect(client.Ping(ctx).Err()).NotTo(HaveOccurred())
})
It("should connect to clusters", Label("NonRedisEnterprise"), func() {
client = redis.NewUniversalClient(&redis.UniversalOptions{
Addrs: cluster.addrs(),
})
Expect(client.Ping(ctx).Err()).NotTo(HaveOccurred())
})
It("connect to clusters with UniversalClient and UnstableResp3", Label("NonRedisEnterprise"), func() {
client = redis.NewUniversalClient(&redis.UniversalOptions{
Addrs: cluster.addrs(),
Protocol: 3,
UnstableResp3: true,
})
Expect(client.Ping(ctx).Err()).NotTo(HaveOccurred())
a := func() { client.FTInfo(ctx, "all").Result() }
Expect(a).ToNot(Panic())
})
It("connect to clusters with ClusterClient and UnstableResp3", Label("NonRedisEnterprise"), func() {
client = redis.NewClusterClient(&redis.ClusterOptions{
Addrs: cluster.addrs(),
Protocol: 3,
UnstableResp3: true,
})
Expect(client.Ping(ctx).Err()).NotTo(HaveOccurred())
a := func() { client.FTInfo(ctx, "all").Result() }
Expect(a).ToNot(Panic())
})
It("should connect to failover servers on slaves when readonly Options is ok", Label("NonRedisEnterprise"), func() {
// Two failure modes combine to make this spec flaky under random
// spec ordering when any prior spec triggered a failover:
//
// 1. FailoverClient(ReadOnly:true) routes via RandomReplicaAddr,
// which silently falls back to the master when Sentinel
// reports zero usable replicas (post-failover replicas often
// linger as "disconnected" for several seconds). Observed
// symptom: ROLE returns "master".
//
// 2. The SET assertion is similarly bypassed by go-redis's
// retry-on-READONLY logic: SET against a replica returns
// READONLY, shouldRetry returns true, the conn is recycled,
// and the next dial goes through masterReplicaDialer again
// — which can fall back to master via the same path as (1).
// The SET then succeeds against the master. Observed symptom:
// ROLE returns "slave" but SET unexpectedly succeeds.
//
// Defenses:
//
// - Disable client retries (MaxRetries: -1) so a READONLY reply
// surfaces as the first/only SET error instead of triggering
// a fresh dial that may land on the master.
//
// - Run the full assertion (connect → ROLE → SET) atomically
// inside a bounded Eventually. If any step lands on the
// master (because RandomReplicaAddr fell back), close and
// retry with a fresh client so RandomReplicaAddr is
// re-evaluated against current Sentinel state.
Eventually(func() error {
if client != nil {
_ = client.Close()
}
client = redis.NewUniversalClient(&redis.UniversalOptions{
MasterName: sentinelName,
Addrs: sentinelAddrs,
ReadOnly: true,
MaxRetries: -1,
})
if err := client.Ping(ctx).Err(); err != nil {
return fmt.Errorf("ping: %w", err)
}
role, err := client.Do(ctx, "ROLE").Result()
if err != nil {
return fmt.Errorf("ROLE: %w", err)
}
roleSlice, ok := role.([]interface{})
if !ok || len(roleSlice) == 0 {
return fmt.Errorf("ROLE returned unexpected shape: %v", role)
}
firstRole, _ := roleSlice[0].(string)
if firstRole != "slave" {
return fmt.Errorf("ROLE = %q, want %q (RandomReplicaAddr fell back to master)", firstRole, "slave")
}
if setErr := client.Set(ctx, "somekey", "somevalue", 0).Err(); setErr == nil {
return fmt.Errorf("SET on replica unexpectedly succeeded (likely landed on master)")
}
return nil
}, "30s", "500ms").Should(Succeed())
})
It("should connect to clusters if IsClusterMode is set even if only a single address is provided", Label("NonRedisEnterprise"), func() {
client = redis.NewUniversalClient(&redis.UniversalOptions{
Addrs: []string{cluster.addrs()[0]},
IsClusterMode: true,
})
_, ok := client.(*redis.ClusterClient)
Expect(ok).To(BeTrue(), "expected a ClusterClient")
})
It("should return all slots after instantiating UniversalClient with IsClusterMode", Label("NonRedisEnterprise"), func() {
client = redis.NewUniversalClient(&redis.UniversalOptions{
Addrs: []string{cluster.addrs()[0]},
IsClusterMode: true,
})
Expect(client.ClusterSlots(ctx).Val()).To(HaveLen(3))
})
})