github.com/argoproj/argo-cd@v1.8.7/controller/clusterinfoupdater_test.go (about)

     1  package controller
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
    10  	appsfake "github.com/argoproj/argo-cd/pkg/client/clientset/versioned/fake"
    11  	appinformers "github.com/argoproj/argo-cd/pkg/client/informers/externalversions/application/v1alpha1"
    12  	applisters "github.com/argoproj/argo-cd/pkg/client/listers/application/v1alpha1"
    13  	cacheutil "github.com/argoproj/argo-cd/util/cache"
    14  	"github.com/argoproj/argo-cd/util/cache/appstate"
    15  	"github.com/argoproj/argo-cd/util/db"
    16  	"github.com/argoproj/argo-cd/util/settings"
    17  
    18  	clustercache "github.com/argoproj/gitops-engine/pkg/cache"
    19  	"github.com/stretchr/testify/assert"
    20  	"k8s.io/client-go/kubernetes/fake"
    21  	"k8s.io/client-go/tools/cache"
    22  )
    23  
    24  // Expect cluster cache update is persisted in cluster secret
    25  func TestClusterSecretUpdater(t *testing.T) {
    26  	const fakeNamespace = "fake-ns"
    27  	const updatedK8sVersion = "1.0"
    28  	now := time.Now()
    29  
    30  	var tests = []struct {
    31  		LastCacheSyncTime *time.Time
    32  		SyncError         error
    33  		ExpectedStatus    v1alpha1.ConnectionStatus
    34  	}{
    35  		{nil, nil, v1alpha1.ConnectionStatusUnknown},
    36  		{&now, nil, v1alpha1.ConnectionStatusSuccessful},
    37  		{&now, fmt.Errorf("sync failed"), v1alpha1.ConnectionStatusFailed},
    38  	}
    39  
    40  	kubeclientset := fake.NewSimpleClientset()
    41  	appclientset := appsfake.NewSimpleClientset()
    42  	appInformer := appinformers.NewApplicationInformer(appclientset, "", time.Minute, cache.Indexers{})
    43  	settingsManager := settings.NewSettingsManager(context.Background(), kubeclientset, fakeNamespace)
    44  	argoDB := db.NewDB(fakeNamespace, settingsManager, kubeclientset)
    45  	ctx, cancel := context.WithCancel(context.Background())
    46  	defer cancel()
    47  
    48  	appCache := appstate.NewCache(cacheutil.NewCache(cacheutil.NewInMemoryCache(time.Minute)), time.Minute)
    49  	cluster, err := argoDB.CreateCluster(ctx, &v1alpha1.Cluster{Server: "http://minikube"})
    50  	assert.NoError(t, err, "Test prepare test data create cluster failed")
    51  
    52  	for _, test := range tests {
    53  		info := &clustercache.ClusterInfo{
    54  			Server:            cluster.Server,
    55  			K8SVersion:        updatedK8sVersion,
    56  			LastCacheSyncTime: test.LastCacheSyncTime,
    57  			SyncError:         test.SyncError,
    58  		}
    59  
    60  		lister := applisters.NewApplicationLister(appInformer.GetIndexer()).Applications(fakeNamespace)
    61  		updater := NewClusterInfoUpdater(nil, argoDB, lister, appCache, nil)
    62  
    63  		err = updater.updateClusterInfo(*cluster, info)
    64  		assert.NoError(t, err, "Invoking updateClusterInfo failed.")
    65  
    66  		var clusterInfo v1alpha1.ClusterInfo
    67  		err = appCache.GetClusterInfo(cluster.Server, &clusterInfo)
    68  		assert.NoError(t, err)
    69  		assert.Equal(t, updatedK8sVersion, clusterInfo.ServerVersion)
    70  		assert.Equal(t, test.ExpectedStatus, clusterInfo.ConnectionState.Status)
    71  	}
    72  }