@@ -0,0 +1,159 @@
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"
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(),
25+ Context("with cluster client", func() {
26+ RunClientTests(func(mr *miniredis.Miniredis) options.RedisStoreOptions {
27+ return options.RedisStoreOptions{
28+ ClusterConnectionURLs: []string{"redis://" + mr.Addr()},
35+type getOptsFunc func(mr *miniredis.Miniredis) options.RedisStoreOptions
37+func RunClientTests(getOptsFunc getOptsFunc) {
38+ var mr *miniredis.Miniredis
39+ var client redis.Client
42+ var ctx context.Context
45+ mr, err = miniredis.Run()
46+ Expect(err).ToNot(HaveOccurred())
48+ client, err = redis.NewRedisClient(getOptsFunc(mr))
49+ Expect(err).ToNot(HaveOccurred())
51+ nonce, err := encryption.Nonce(32)
52+ Expect(err).ToNot(HaveOccurred())
53+ key = base64.RawURLEncoding.EncodeToString(nonce)
55+ ctx = context.Background()
65+ Context("when Get is called", func() {
66+ expectedValue := []byte("value")
69+ client.Set(context.Background(), key, expectedValue, time.Duration(1*time.Minute))
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))
78+ It("does not return expired values", func() {
79+ mr.FastForward(5 * time.Minute)
81+ _, err = client.Get(ctx, key)
82+ Expect(err).To(HaveOccurred())
85+ It("returns an error if value does not exist", func() {
86+ _, err = client.Get(ctx, "does-not-exists")
87+ Expect(err).To(HaveOccurred())
91+ Context("using Lock", func() {
92+ It("maintains the lock", func() {
93+ lock := client.Lock(key)
95+ err = lock.Obtain(ctx, 1*time.Minute)
96+ Expect(err).ToNot(HaveOccurred())
98+ isLocked, err := lock.Peek(ctx)
99+ Expect(err).ToNot(HaveOccurred())
100+ Expect(isLocked).To(BeTrue())
102+ err = lock.Release(ctx)
103+ Expect(err).ToNot(HaveOccurred())
106+ It("reflects non-locked instance", func() {
107+ lock := client.Lock(key)
109+ isLocked, err := lock.Peek(ctx)
110+ Expect(err).ToNot(HaveOccurred())
111+ Expect(isLocked).To(BeFalse())
115+ Context("when Set is called", func() {
116+ expectedValue := []byte("value")
118+ It("sets the expected value", func() {
119+ err = client.Set(ctx, key, expectedValue, 1*time.Minute)
120+ Expect(err).ToNot(HaveOccurred())
122+ value, err := client.Get(ctx, key)
123+ Expect(value).To(Equal(expectedValue))
124+ Expect(err).ToNot(HaveOccurred())
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())
133+ err = client.Del(ctx, key)
134+ Expect(err).ToNot(HaveOccurred())
136+ _, err = client.Get(ctx, key)
137+ Expect(err).To(HaveOccurred())
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())
149+ Context("when redis is down", func() {
150+ It("returns an error", func() {
154+ err = client.Ping(ctx)
155+ Expect(err).To(HaveOccurred())