@@ -0,0 +1,159 @@
1+package redis_test
2+
3+import (
4+ "context"
5+ "encoding/base64"
6+ "time"
7+
8+ "github.com/alicebob/miniredis/v2"
9+ "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
10+ "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/encryption"
11+ "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/sessions/redis"
12+ . "github.com/onsi/ginkgo"
13+ . "github.com/onsi/gomega"
14+)
15+
16+var _ = Describe("Redis Client Tests", func() {
17+ Context("with basic client", func() {
18+ RunClientTests(func(mr *miniredis.Miniredis) options.RedisStoreOptions {
19+ return options.RedisStoreOptions{
20+ ConnectionURL: "redis://" + mr.Addr(),
21+ }
22+ })
23+ })
24+
25+ Context("with cluster client", func() {
26+ RunClientTests(func(mr *miniredis.Miniredis) options.RedisStoreOptions {
27+ return options.RedisStoreOptions{
28+ ClusterConnectionURLs: []string{"redis://" + mr.Addr()},
29+ UseCluster: true,
30+ }
31+ })
32+ })
33+})
34+
35+type getOptsFunc func(mr *miniredis.Miniredis) options.RedisStoreOptions
36+
37+func RunClientTests(getOptsFunc getOptsFunc) {
38+ var mr *miniredis.Miniredis
39+ var client redis.Client
40+ var err error
41+ var key string
42+ var ctx context.Context
43+
44+ BeforeEach(func() {
45+ mr, err = miniredis.Run()
46+ Expect(err).ToNot(HaveOccurred())
47+
48+ client, err = redis.NewRedisClient(getOptsFunc(mr))
49+ Expect(err).ToNot(HaveOccurred())
50+
51+ nonce, err := encryption.Nonce(32)
52+ Expect(err).ToNot(HaveOccurred())
53+ key = base64.RawURLEncoding.EncodeToString(nonce)
54+
55+ ctx = context.Background()
56+ })
57+
58+ AfterEach(func() {
59+ if mr != nil {
60+ mr.Close()
61+ mr = nil
62+ }
63+ })
64+
65+ Context("when Get is called", func() {
66+ expectedValue := []byte("value")
67+
68+ BeforeEach(func() {
69+ client.Set(context.Background(), key, expectedValue, time.Duration(1*time.Minute))
70+ })
71+
72+ It("returns the saved value", func() {
73+ value, err := client.Get(ctx, key)
74+ Expect(err).ToNot(HaveOccurred())
75+ Expect(value).To(Equal(value))
76+ })
77+
78+ It("does not return expired values", func() {
79+ mr.FastForward(5 * time.Minute)
80+
81+ _, err = client.Get(ctx, key)
82+ Expect(err).To(HaveOccurred())
83+ })
84+
85+ It("returns an error if value does not exist", func() {
86+ _, err = client.Get(ctx, "does-not-exists")
87+ Expect(err).To(HaveOccurred())
88+ })
89+ })
90+
91+ Context("using Lock", func() {
92+ It("maintains the lock", func() {
93+ lock := client.Lock(key)
94+
95+ err = lock.Obtain(ctx, 1*time.Minute)
96+ Expect(err).ToNot(HaveOccurred())
97+
98+ isLocked, err := lock.Peek(ctx)
99+ Expect(err).ToNot(HaveOccurred())
100+ Expect(isLocked).To(BeTrue())
101+
102+ err = lock.Release(ctx)
103+ Expect(err).ToNot(HaveOccurred())
104+ })
105+
106+ It("reflects non-locked instance", func() {
107+ lock := client.Lock(key)
108+
109+ isLocked, err := lock.Peek(ctx)
110+ Expect(err).ToNot(HaveOccurred())
111+ Expect(isLocked).To(BeFalse())
112+ })
113+ })
114+
115+ Context("when Set is called", func() {
116+ expectedValue := []byte("value")
117+
118+ It("sets the expected value", func() {
119+ err = client.Set(ctx, key, expectedValue, 1*time.Minute)
120+ Expect(err).ToNot(HaveOccurred())
121+
122+ value, err := client.Get(ctx, key)
123+ Expect(value).To(Equal(expectedValue))
124+ Expect(err).ToNot(HaveOccurred())
125+ })
126+ })
127+
128+ Context("when Del is called", func() {
129+ It("does not return an error when key exists", func() {
130+ err = client.Set(ctx, key, []byte("dummy"), 1*time.Minute)
131+ Expect(err).ToNot(HaveOccurred())
132+
133+ err = client.Del(ctx, key)
134+ Expect(err).ToNot(HaveOccurred())
135+
136+ _, err = client.Get(ctx, key)
137+ Expect(err).To(HaveOccurred())
138+ })
139+ })
140+
141+ Context("when Ping is called", func() {
142+ Context("when redis is up", func() {
143+ It("does not return an error", func() {
144+ err = client.Ping(ctx)
145+ Expect(err).ToNot(HaveOccurred())
146+ })
147+ })
148+
149+ Context("when redis is down", func() {
150+ It("returns an error", func() {
151+ mr.Close()
152+ mr = nil
153+
154+ err = client.Ping(ctx)
155+ Expect(err).To(HaveOccurred())
156+ })
157+ })
158+ })
159+}