github.com/argoproj/argo-cd@v1.8.7/server/cache/cache_test.go (about)

     1  package cache
     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  	appstatecache "github.com/argoproj/argo-cd/util/cache/appstate"
    13  	"github.com/argoproj/argo-cd/util/oidc"
    14  )
    15  
    16  type fixtures struct {
    17  	*Cache
    18  }
    19  
    20  func newFixtures() *fixtures {
    21  	return &fixtures{NewCache(
    22  		appstatecache.NewCache(
    23  			cacheutil.NewCache(cacheutil.NewInMemoryCache(1*time.Hour)),
    24  			1*time.Minute,
    25  		),
    26  		1*time.Minute,
    27  		1*time.Minute,
    28  		1*time.Minute,
    29  	)}
    30  }
    31  
    32  func TestCache_GetRepoConnectionState(t *testing.T) {
    33  	cache := newFixtures().Cache
    34  	// cache miss
    35  	_, err := cache.GetRepoConnectionState("my-repo")
    36  	assert.Equal(t, ErrCacheMiss, err)
    37  	// populate cache
    38  	err = cache.SetRepoConnectionState("my-repo", &ConnectionState{Status: "my-state"})
    39  	assert.NoError(t, err)
    40  	// cache miss
    41  	_, err = cache.GetRepoConnectionState("other-repo")
    42  	assert.Equal(t, ErrCacheMiss, err)
    43  	// cache hit
    44  	value, err := cache.GetRepoConnectionState("my-repo")
    45  	assert.NoError(t, err)
    46  	assert.Equal(t, ConnectionState{Status: "my-state"}, value)
    47  }
    48  
    49  func TestCache_GetOIDCState(t *testing.T) {
    50  	cache := newFixtures().Cache
    51  	// cache miss
    52  	_, err := cache.GetOIDCState("my-key")
    53  	assert.Equal(t, ErrCacheMiss, err)
    54  	// populate cache
    55  	err = cache.SetOIDCState("my-key", &oidc.OIDCState{ReturnURL: "my-return-url"})
    56  	assert.NoError(t, err)
    57  	//cache miss
    58  	_, err = cache.GetOIDCState("other-key")
    59  	assert.Equal(t, ErrCacheMiss, err)
    60  	// cache hit
    61  	value, err := cache.GetOIDCState("my-key")
    62  	assert.NoError(t, err)
    63  	assert.Equal(t, &oidc.OIDCState{ReturnURL: "my-return-url"}, value)
    64  }
    65  
    66  func TestAddCacheFlagsToCmd(t *testing.T) {
    67  	cache, err := AddCacheFlagsToCmd(&cobra.Command{})()
    68  	assert.NoError(t, err)
    69  	assert.Equal(t, 1*time.Hour, cache.connectionStatusCacheExpiration)
    70  	assert.Equal(t, 3*time.Minute, cache.oidcCacheExpiration)
    71  }