Skip to content

Commit

Permalink
test: unit test with dummy redis server
Browse files Browse the repository at this point in the history
  • Loading branch information
clambin committed Sep 1, 2024
1 parent d0c52e8 commit 55c571b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 86 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/clambin/traefik-simple-auth
go 1.23.0

require (
github.com/alicebob/miniredis/v2 v2.33.0
github.com/bradfitz/gomemcache v0.0.0-20230905024940-24af94b03874
github.com/clambin/go-common/cache v0.7.0
github.com/clambin/go-common/http v0.5.2
Expand All @@ -17,6 +18,7 @@ require (
)

require (
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand All @@ -32,6 +34,7 @@ require (
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/yuin/gopher-lua v1.1.1 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/sys v0.22.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a h1:HbKu58rmZpUGpz5+4FfNmIU+FmZg2P3Xaj2v2bfNWmk=
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a/go.mod h1:SGnFV6hVsYE877CKEZ6tDNTjaSXYUk6QqoIK6PrAtcc=
github.com/alicebob/miniredis/v2 v2.33.0 h1:uvTF0EDeu9RLnUEG27Db5I68ESoIxTiXbNUiji6lZrA=
github.com/alicebob/miniredis/v2 v2.33.0/go.mod h1:MhP4a3EU7aENRi9aO+tHfTBZicLqQevyi/DJpoj6mi0=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bradfitz/gomemcache v0.0.0-20230905024940-24af94b03874 h1:N7oVaKyGp8bttX0bfZGmcGkjz7DLQXhAn3DNd3T0ous=
Expand Down Expand Up @@ -62,6 +66,8 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M=
github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
Expand Down
111 changes: 25 additions & 86 deletions pkg/state/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,97 +3,66 @@ package state
import (
"context"
"errors"
"github.com/alicebob/miniredis/v2"
"github.com/bradfitz/gomemcache/memcache"
gcc "github.com/clambin/go-common/cache"
"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/assert"
"testing"
"time"
)

func Test_newCache(t *testing.T) {
func Test_cache(t *testing.T) {
rd := miniredis.RunT(t)
tests := []struct {
name string
cfg Configuration
shouldPanic bool
name string
cfg Configuration
panics bool
}{
{
name: "empty",
shouldPanic: true,
name: "invalid",
cfg: Configuration{CacheType: "invalid"},
panics: true,
},
{
name: "invalid",
cfg: Configuration{CacheType: "invalid"},
shouldPanic: true,
},
{
name: "memory",
name: "local cache",
cfg: Configuration{CacheType: "memory"},
},
{
name: "redis",
cfg: Configuration{CacheType: "redis"},
},
{
name: "memcached",
cfg: Configuration{CacheType: "memcached"},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.shouldPanic {
assert.Panics(t, func() { newCache[string](tt.cfg) })
return
}
c := newCache[string](tt.cfg)
assert.NotNil(t, c)
})
}
}

func TestCache(t *testing.T) {
tests := []struct {
name string
cache cache[string]
}{
{
name: "local cache",
cache: localCache[string]{
values: gcc.New[string, string](0, 0),
},
},
{
name: "memcached",
cache: memcachedCache[string]{
Client: &fakeMemcachedClient{c: localCache[string]{values: gcc.New[string, string](0, 0)}},
},
},
{
name: "redis",
cache: redisCache[string]{
Client: &fakeRedisClient{c: localCache[string]{values: gcc.New[string, string](0, 0)}},
},
cfg: Configuration{CacheType: "redis", RedisConfiguration: RedisConfiguration{Addr: rd.Addr()}},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if tt.panics {
assert.Panics(t, func() { newCache[string](tt.cfg) })
return
}

c := newCache[string](tt.cfg)
if tt.cfg.CacheType == "memcached" {
c = memcachedCache[string]{Client: &fakeMemcachedClient{c: localCache[string]{values: gcc.New[string, string](0, 0)}}}
}

ctx := context.Background()
count, err := tt.cache.Len(ctx)
count, err := c.Len(ctx)
assert.NoError(t, err)
assert.Zero(t, count)

assert.NoError(t, tt.cache.Add(ctx, "key", "value", time.Hour))
value, err := tt.cache.GetDel(ctx, "key")
assert.NoError(t, c.Add(ctx, "key", "value", time.Hour))
value, err := c.GetDel(ctx, "key")
assert.NoError(t, err)
assert.Equal(t, "value", value)
_, err = tt.cache.GetDel(ctx, "key")
_, err = c.GetDel(ctx, "key")
assert.ErrorIs(t, err, ErrNotFound)

assert.NoError(t, tt.cache.Ping(ctx))
assert.NoError(t, c.Ping(ctx))
})
}
}
Expand Down Expand Up @@ -145,33 +114,3 @@ func (f *fakeMemcachedClient) Delete(key string) error {
f.c.values.Remove(key)
return nil
}

var _ redisClient = fakeRedisClient{}

type fakeRedisClient struct {
c localCache[string]
}

func (f fakeRedisClient) Ping(ctx context.Context) *redis.StatusCmd {
cmd := redis.NewStatusCmd(ctx)
cmd.SetErr(nil)
return cmd
}

func (f fakeRedisClient) Set(ctx context.Context, key string, value any, ttl time.Duration) *redis.StatusCmd {
err := f.c.Add(ctx, key, string(value.([]byte)), ttl)
cmd := redis.NewStatusCmd(ctx)
cmd.SetErr(err)
return cmd
}

func (f fakeRedisClient) GetDel(ctx context.Context, key string) *redis.StringCmd {
val, err := f.c.GetDel(ctx, key)
if errors.Is(err, ErrNotFound) {
err = redis.Nil
}
cmd := redis.NewStringCmd(ctx)
cmd.SetErr(err)
cmd.SetVal(val)
return cmd
}

0 comments on commit 55c571b

Please # to comment.