summaryrefslogtreecommitdiff
path: root/pkg/cache/memcache_test.go
blob: 71893de2835fe8635c0bdc49819b407cbaf8228a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package cache

import (
	"testing"
	"time"

	"github.com/argoproj-labs/argocd-image-updater/pkg/tag"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

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))
	})

	t.Run("Cache miss", func(t *testing.T) {
		mc := NewMemCache()
		newTag := tag.NewImageTag(imageTag, time.Unix(0, 0), "")
		mc.SetTag(imageName, newTag)
		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))
		mc.ClearCache()
		cachedTag, err = mc.GetTag(imageName, imageTag)
		require.NoError(t, err)
		require.Nil(t, cachedTag)
	})
}