Skip to content

Commit 5edb9d0

Browse files
authored
tests: add unit tests for pkg/cache (argoproj-labs#770)
Signed-off-by: Cheng Fang <[email protected]>
1 parent 3e7dc57 commit 5edb9d0

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

pkg/cache/memcache_test.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import (
44
"testing"
55
"time"
66

7-
"github.com/argoproj-labs/argocd-image-updater/pkg/tag"
8-
7+
memcache "github.com/patrickmn/go-cache"
98
"github.com/stretchr/testify/assert"
109
"github.com/stretchr/testify/require"
10+
11+
"github.com/argoproj-labs/argocd-image-updater/pkg/tag"
1112
)
1213

1314
func Test_MemCache(t *testing.T) {
@@ -22,12 +23,14 @@ func Test_MemCache(t *testing.T) {
2223
require.NotNil(t, cachedTag)
2324
assert.Equal(t, imageTag, cachedTag.TagName)
2425
assert.True(t, mc.HasTag(imageName, imageTag))
26+
assert.Equal(t, 1, mc.NumEntries())
2527
})
2628

2729
t.Run("Cache miss", func(t *testing.T) {
2830
mc := NewMemCache()
2931
newTag := tag.NewImageTag(imageTag, time.Unix(0, 0), "")
3032
mc.SetTag(imageName, newTag)
33+
assert.Equal(t, 1, mc.NumEntries())
3134
cachedTag, err := mc.GetTag(imageName, "v1.0.1")
3235
require.NoError(t, err)
3336
require.Nil(t, cachedTag)
@@ -43,9 +46,25 @@ func Test_MemCache(t *testing.T) {
4346
require.NotNil(t, cachedTag)
4447
assert.Equal(t, imageTag, cachedTag.TagName)
4548
assert.True(t, mc.HasTag(imageName, imageTag))
49+
assert.Equal(t, 1, mc.NumEntries())
4650
mc.ClearCache()
51+
assert.Equal(t, 0, mc.NumEntries())
4752
cachedTag, err = mc.GetTag(imageName, imageTag)
4853
require.NoError(t, err)
4954
require.Nil(t, cachedTag)
5055
})
56+
t.Run("Image Cache Key", func(t *testing.T) {
57+
mc := MemCache{
58+
cache: memcache.New(0, 0),
59+
}
60+
application := "application1"
61+
key := imageCacheKey(imageName)
62+
mc.SetImage(imageName, application)
63+
app, b := mc.cache.Get(key)
64+
assert.True(t, b)
65+
assert.Equal(t, application, app)
66+
assert.Equal(t, 1, mc.NumEntries())
67+
mc.ClearCache()
68+
assert.Equal(t, 0, mc.NumEntries())
69+
})
5170
}

0 commit comments

Comments
 (0)