diff options
| author | Cheng Fang <cfang@redhat.com> | 2024-07-09 08:23:25 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-09 08:23:25 -0400 |
| commit | 5edb9d003efdb54501d4e6f31f877670e39d06d6 (patch) | |
| tree | 861af9fad117c04220126208343536ee8ffc8a87 /pkg | |
| parent | 3e7dc57bc8805c7da5176283fbab985ed9acab61 (diff) | |
tests: add unit tests for pkg/cache (#770)
Signed-off-by: Cheng Fang <cfang@redhat.com>
Diffstat (limited to 'pkg')
| -rw-r--r-- | pkg/cache/memcache_test.go | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/pkg/cache/memcache_test.go b/pkg/cache/memcache_test.go index 71893de..8fcb47b 100644 --- a/pkg/cache/memcache_test.go +++ b/pkg/cache/memcache_test.go @@ -4,10 +4,11 @@ import ( "testing" "time" - "github.com/argoproj-labs/argocd-image-updater/pkg/tag" - + memcache "github.com/patrickmn/go-cache" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + + "github.com/argoproj-labs/argocd-image-updater/pkg/tag" ) func Test_MemCache(t *testing.T) { @@ -22,12 +23,14 @@ func Test_MemCache(t *testing.T) { require.NotNil(t, cachedTag) assert.Equal(t, imageTag, cachedTag.TagName) assert.True(t, mc.HasTag(imageName, imageTag)) + assert.Equal(t, 1, mc.NumEntries()) }) t.Run("Cache miss", func(t *testing.T) { mc := NewMemCache() newTag := tag.NewImageTag(imageTag, time.Unix(0, 0), "") mc.SetTag(imageName, newTag) + assert.Equal(t, 1, mc.NumEntries()) cachedTag, err := mc.GetTag(imageName, "v1.0.1") require.NoError(t, err) require.Nil(t, cachedTag) @@ -43,9 +46,25 @@ func Test_MemCache(t *testing.T) { require.NotNil(t, cachedTag) assert.Equal(t, imageTag, cachedTag.TagName) assert.True(t, mc.HasTag(imageName, imageTag)) + assert.Equal(t, 1, mc.NumEntries()) mc.ClearCache() + assert.Equal(t, 0, mc.NumEntries()) cachedTag, err = mc.GetTag(imageName, imageTag) require.NoError(t, err) require.Nil(t, cachedTag) }) + t.Run("Image Cache Key", func(t *testing.T) { + mc := MemCache{ + cache: memcache.New(0, 0), + } + application := "application1" + key := imageCacheKey(imageName) + mc.SetImage(imageName, application) + app, b := mc.cache.Get(key) + assert.True(t, b) + assert.Equal(t, application, app) + assert.Equal(t, 1, mc.NumEntries()) + mc.ClearCache() + assert.Equal(t, 0, mc.NumEntries()) + }) } |
