2019-10-16 22:46:45 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
2021-04-01 18:44:18 +00:00
|
|
|
. "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
|
|
|
|
|
cacheutil "github.com/argoproj/argo-cd/v2/util/cache"
|
|
|
|
|
appstatecache "github.com/argoproj/argo-cd/v2/util/cache/appstate"
|
|
|
|
|
"github.com/argoproj/argo-cd/v2/util/oidc"
|
2019-10-16 22:46:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type fixtures struct {
|
|
|
|
|
*Cache
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newFixtures() *fixtures {
|
|
|
|
|
return &fixtures{NewCache(
|
|
|
|
|
appstatecache.NewCache(
|
|
|
|
|
cacheutil.NewCache(cacheutil.NewInMemoryCache(1*time.Hour)),
|
|
|
|
|
1*time.Minute,
|
|
|
|
|
),
|
|
|
|
|
1*time.Minute,
|
|
|
|
|
1*time.Minute,
|
2020-04-21 18:10:25 +00:00
|
|
|
1*time.Minute,
|
2019-10-16 22:46:45 +00:00
|
|
|
)}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCache_GetRepoConnectionState(t *testing.T) {
|
|
|
|
|
cache := newFixtures().Cache
|
|
|
|
|
// cache miss
|
|
|
|
|
_, err := cache.GetRepoConnectionState("my-repo")
|
|
|
|
|
assert.Equal(t, ErrCacheMiss, err)
|
|
|
|
|
// populate cache
|
|
|
|
|
err = cache.SetRepoConnectionState("my-repo", &ConnectionState{Status: "my-state"})
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
// cache miss
|
|
|
|
|
_, err = cache.GetRepoConnectionState("other-repo")
|
|
|
|
|
assert.Equal(t, ErrCacheMiss, err)
|
|
|
|
|
// cache hit
|
|
|
|
|
value, err := cache.GetRepoConnectionState("my-repo")
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
assert.Equal(t, ConnectionState{Status: "my-state"}, value)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCache_GetOIDCState(t *testing.T) {
|
|
|
|
|
cache := newFixtures().Cache
|
|
|
|
|
// cache miss
|
|
|
|
|
_, err := cache.GetOIDCState("my-key")
|
|
|
|
|
assert.Equal(t, ErrCacheMiss, err)
|
|
|
|
|
// populate cache
|
2020-04-21 18:10:25 +00:00
|
|
|
err = cache.SetOIDCState("my-key", &oidc.OIDCState{ReturnURL: "my-return-url"})
|
2019-10-16 22:46:45 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
//cache miss
|
|
|
|
|
_, err = cache.GetOIDCState("other-key")
|
|
|
|
|
assert.Equal(t, ErrCacheMiss, err)
|
|
|
|
|
// cache hit
|
|
|
|
|
value, err := cache.GetOIDCState("my-key")
|
|
|
|
|
assert.NoError(t, err)
|
2020-04-21 18:10:25 +00:00
|
|
|
assert.Equal(t, &oidc.OIDCState{ReturnURL: "my-return-url"}, value)
|
2019-10-16 22:46:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestAddCacheFlagsToCmd(t *testing.T) {
|
|
|
|
|
cache, err := AddCacheFlagsToCmd(&cobra.Command{})()
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
assert.Equal(t, 1*time.Hour, cache.connectionStatusCacheExpiration)
|
|
|
|
|
assert.Equal(t, 3*time.Minute, cache.oidcCacheExpiration)
|
|
|
|
|
}
|