github.com/argoproj/argo-cd/v3@v3.2.1/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  	"github.com/stretchr/testify/require"
    10  
    11  	. "github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1"
    12  	cacheutil "github.com/argoproj/argo-cd/v3/util/cache"
    13  	appstatecache "github.com/argoproj/argo-cd/v3/util/cache/appstate"
    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  	)}
    29  }
    30  
    31  func TestCache_GetRepoConnectionState(t *testing.T) {
    32  	cache := newFixtures().Cache
    33  	// cache miss
    34  	_, err := cache.GetRepoConnectionState("my-repo", "")
    35  	assert.Equal(t, ErrCacheMiss, err)
    36  	// populate cache
    37  	err = cache.SetRepoConnectionState("my-repo", "", &ConnectionState{Status: "my-state"})
    38  	require.NoError(t, err)
    39  	// cache miss
    40  	_, err = cache.GetRepoConnectionState("my-repo", "some-project")
    41  	assert.Equal(t, ErrCacheMiss, err)
    42  	// populate cache
    43  	err = cache.SetRepoConnectionState("my-repo", "some-project", &ConnectionState{Status: "my-project-state"})
    44  	require.NoError(t, err)
    45  	// cache hit
    46  	value, err := cache.GetRepoConnectionState("my-repo", "")
    47  	require.NoError(t, err)
    48  	assert.Equal(t, ConnectionState{Status: "my-state"}, value)
    49  	// cache hit
    50  	value, err = cache.GetRepoConnectionState("my-repo", "some-project")
    51  	require.NoError(t, err)
    52  	assert.Equal(t, ConnectionState{Status: "my-project-state"}, value)
    53  }
    54  
    55  func TestAddCacheFlagsToCmd(t *testing.T) {
    56  	cache, err := AddCacheFlagsToCmd(&cobra.Command{})()
    57  	require.NoError(t, err)
    58  	assert.Equal(t, 1*time.Hour, cache.connectionStatusCacheExpiration)
    59  	assert.Equal(t, 3*time.Minute, cache.oidcCacheExpiration)
    60  }