diff options
| author | Ishita Sequeira <46771830+ishitasequeira@users.noreply.github.com> | 2024-11-13 08:57:55 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-11-13 08:57:55 -0500 |
| commit | cde58c56ff0981df884433eeea76d747f4b809de (patch) | |
| tree | 6fb745e6995e3bc3fe87be90524a4d39ebe3531a /registry-scanner/pkg/cache/memcache_test.go | |
| parent | 1d04e9aa3e237530336bc0693eaaa7f6de7b66e1 (diff) | |
refactor: Add registry pkg registry scanner (#933)
Signed-off-by: Ishita Sequeira <ishiseq29@gmail.com>
Diffstat (limited to 'registry-scanner/pkg/cache/memcache_test.go')
| -rw-r--r-- | registry-scanner/pkg/cache/memcache_test.go | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/registry-scanner/pkg/cache/memcache_test.go b/registry-scanner/pkg/cache/memcache_test.go new file mode 100644 index 0000000..8fcb47b --- /dev/null +++ b/registry-scanner/pkg/cache/memcache_test.go @@ -0,0 +1,70 @@ +package cache + +import ( + "testing" + "time" + + 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) { + imageName := "foo/bar" + imageTag := "v1.0.0" + t.Run("Cache hit", func(t *testing.T) { + mc := NewMemCache() + newTag := tag.NewImageTag(imageTag, time.Unix(0, 0), "") + mc.SetTag(imageName, newTag) + cachedTag, err := mc.GetTag(imageName, imageTag) + require.NoError(t, err) + 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) + assert.False(t, mc.HasTag(imageName, "v1.0.1")) + }) + + t.Run("Cache clear", func(t *testing.T) { + mc := NewMemCache() + newTag := tag.NewImageTag(imageTag, time.Unix(0, 0), "") + mc.SetTag(imageName, newTag) + cachedTag, err := mc.GetTag(imageName, imageTag) + require.NoError(t, err) + 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()) + }) +} |
