github.com/argoproj/argo-cd/v3@v3.2.1/cmd/argocd/commands/admin/cluster_test.go (about) 1 package admin 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1" 8 fakeapps "github.com/argoproj/argo-cd/v3/pkg/client/clientset/versioned/fake" 9 cacheutil "github.com/argoproj/argo-cd/v3/util/cache" 10 "github.com/argoproj/argo-cd/v3/util/cache/appstate" 11 12 "github.com/stretchr/testify/assert" 13 "github.com/stretchr/testify/require" 14 corev1 "k8s.io/api/core/v1" 15 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 16 "k8s.io/client-go/kubernetes/fake" 17 "k8s.io/utils/ptr" 18 ) 19 20 func Test_loadClusters(t *testing.T) { 21 argoCDCM := &corev1.ConfigMap{ 22 ObjectMeta: metav1.ObjectMeta{ 23 Name: "argocd-cm", 24 Namespace: "argocd", 25 Labels: map[string]string{ 26 "app.kubernetes.io/part-of": "argocd", 27 }, 28 }, 29 Data: map[string]string{}, 30 } 31 argoCDSecret := &corev1.Secret{ 32 ObjectMeta: metav1.ObjectMeta{ 33 Name: "argocd-secret", 34 Namespace: "argocd", 35 }, 36 Data: map[string][]byte{ 37 "server.secretkey": []byte("test"), 38 }, 39 } 40 app := &v1alpha1.Application{ 41 ObjectMeta: metav1.ObjectMeta{ 42 Name: "test", 43 Namespace: "argocd", 44 }, 45 Spec: v1alpha1.ApplicationSpec{ 46 Project: "default", 47 Destination: v1alpha1.ApplicationDestination{ 48 Server: "https://kubernetes.default.svc", 49 Namespace: "test", 50 }, 51 }, 52 } 53 ctx := t.Context() 54 kubeClient := fake.NewClientset(argoCDCM, argoCDSecret) 55 appClient := fakeapps.NewSimpleClientset(app) 56 cacheSrc := func() (*appstate.Cache, error) { 57 return appstate.NewCache(cacheutil.NewCache(cacheutil.NewInMemoryCache(time.Minute)), time.Minute), nil 58 } 59 clusters, err := loadClusters(ctx, kubeClient, appClient, 3, "", "argocd", false, cacheSrc, 0, "", "", "") 60 require.NoError(t, err) 61 for i := range clusters { 62 // This changes, nil it to avoid testing it. 63 //nolint:staticcheck 64 clusters[i].ConnectionState.ModifiedAt = nil 65 } 66 67 expected := []ClusterWithInfo{{ 68 Cluster: v1alpha1.Cluster{ 69 ID: "", 70 Server: "https://kubernetes.default.svc", 71 Name: "in-cluster", 72 ConnectionState: v1alpha1.ConnectionState{ 73 Status: "Successful", 74 }, 75 ServerVersion: ".", 76 Shard: ptr.To(int64(0)), 77 }, 78 Namespaces: []string{"test"}, 79 }} 80 assert.Equal(t, expected, clusters) 81 }