github.com/argoproj/argo-cd@v1.8.7/util/cache/appstate/cache_test.go (about) 1 package appstate 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/spf13/cobra" 8 "github.com/stretchr/testify/assert" 9 10 . "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1" 11 cacheutil "github.com/argoproj/argo-cd/util/cache" 12 ) 13 14 type fixtures struct { 15 *Cache 16 } 17 18 func newFixtures() *fixtures { 19 return &fixtures{NewCache( 20 cacheutil.NewCache(cacheutil.NewInMemoryCache(1*time.Hour)), 21 1*time.Minute, 22 )} 23 } 24 25 func TestCache_GetAppManagedResources(t *testing.T) { 26 cache := newFixtures().Cache 27 // cache miss 28 value := &[]*ResourceDiff{} 29 err := cache.GetAppManagedResources("my-appname", value) 30 assert.Equal(t, ErrCacheMiss, err) 31 // populate cache 32 err = cache.SetAppManagedResources("my-appname", []*ResourceDiff{{Name: "my-name"}}) 33 assert.NoError(t, err) 34 // cache miss 35 err = cache.GetAppManagedResources("other-appname", value) 36 assert.Equal(t, ErrCacheMiss, err) 37 // cache hit 38 err = cache.GetAppManagedResources("my-appname", value) 39 assert.NoError(t, err) 40 assert.Equal(t, &[]*ResourceDiff{{Name: "my-name"}}, value) 41 } 42 43 func TestCache_GetAppResourcesTree(t *testing.T) { 44 cache := newFixtures().Cache 45 // cache miss 46 value := &ApplicationTree{} 47 err := cache.GetAppResourcesTree("my-appname", value) 48 assert.Equal(t, ErrCacheMiss, err) 49 // populate cache 50 err = cache.SetAppResourcesTree("my-appname", &ApplicationTree{Nodes: []ResourceNode{{}}}) 51 assert.NoError(t, err) 52 // cache miss 53 err = cache.GetAppResourcesTree("other-appname", value) 54 assert.Equal(t, ErrCacheMiss, err) 55 // cache hit 56 err = cache.GetAppResourcesTree("my-appname", value) 57 assert.NoError(t, err) 58 assert.Equal(t, &ApplicationTree{Nodes: []ResourceNode{{}}}, value) 59 } 60 61 func TestAddCacheFlagsToCmd(t *testing.T) { 62 cache, err := AddCacheFlagsToCmd(&cobra.Command{})() 63 assert.NoError(t, err) 64 assert.Equal(t, 1*time.Hour, cache.appStateCacheExpiration) 65 }