github.com/argoproj/argo-cd@v1.8.7/util/db/cluster_test.go (about) 1 package db 2 3 import ( 4 "context" 5 "testing" 6 "time" 7 8 "github.com/stretchr/testify/assert" 9 v1 "k8s.io/api/core/v1" 10 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 11 "k8s.io/client-go/kubernetes/fake" 12 13 "github.com/argoproj/argo-cd/common" 14 "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1" 15 "github.com/argoproj/argo-cd/util/settings" 16 ) 17 18 const ( 19 fakeNamespace = "fake-ns" 20 ) 21 22 func Test_serverToSecretName(t *testing.T) { 23 name, err := serverToSecretName("http://foo") 24 assert.NoError(t, err) 25 assert.Equal(t, "cluster-foo-752281925", name) 26 } 27 28 func Test_secretToCluster(t *testing.T) { 29 secret := &v1.Secret{ 30 ObjectMeta: metav1.ObjectMeta{ 31 Name: "mycluster", 32 Namespace: fakeNamespace, 33 }, 34 Data: map[string][]byte{ 35 "name": []byte("test"), 36 "server": []byte("http://mycluster"), 37 "config": []byte("{\"username\":\"foo\"}"), 38 }, 39 } 40 cluster := secretToCluster(secret) 41 assert.Equal(t, *cluster, v1alpha1.Cluster{ 42 Name: "test", 43 Server: "http://mycluster", 44 Config: v1alpha1.ClusterConfig{ 45 Username: "foo", 46 }, 47 }) 48 } 49 50 func Test_secretToCluster_NoConfig(t *testing.T) { 51 secret := &v1.Secret{ 52 ObjectMeta: metav1.ObjectMeta{ 53 Name: "mycluster", 54 Namespace: fakeNamespace, 55 }, 56 Data: map[string][]byte{ 57 "name": []byte("test"), 58 "server": []byte("http://mycluster"), 59 }, 60 } 61 cluster := secretToCluster(secret) 62 assert.Equal(t, *cluster, v1alpha1.Cluster{ 63 Name: "test", 64 Server: "http://mycluster", 65 }) 66 } 67 68 func TestUpdateCluster(t *testing.T) { 69 kubeclientset := fake.NewSimpleClientset(&v1.Secret{ 70 ObjectMeta: metav1.ObjectMeta{ 71 Name: "mycluster", 72 Namespace: fakeNamespace, 73 Labels: map[string]string{ 74 common.LabelKeySecretType: common.LabelValueSecretTypeCluster, 75 }, 76 }, 77 Data: map[string][]byte{ 78 "server": []byte("http://mycluster"), 79 "config": []byte("{}"), 80 }, 81 }) 82 settingsManager := settings.NewSettingsManager(context.Background(), kubeclientset, fakeNamespace) 83 db := NewDB(fakeNamespace, settingsManager, kubeclientset) 84 requestedAt := metav1.Now() 85 _, err := db.UpdateCluster(context.Background(), &v1alpha1.Cluster{ 86 Name: "test", 87 Server: "http://mycluster", 88 RefreshRequestedAt: &requestedAt, 89 }) 90 if !assert.NoError(t, err) { 91 return 92 } 93 94 secret, err := kubeclientset.CoreV1().Secrets(fakeNamespace).Get(context.Background(), "mycluster", metav1.GetOptions{}) 95 if !assert.NoError(t, err) { 96 return 97 } 98 99 assert.Equal(t, secret.Annotations[common.AnnotationKeyRefresh], requestedAt.Format(time.RFC3339)) 100 } 101 102 func TestDeleteUnknownCluster(t *testing.T) { 103 kubeclientset := fake.NewSimpleClientset(&v1.Secret{ 104 ObjectMeta: metav1.ObjectMeta{ 105 Name: "mycluster", 106 Namespace: fakeNamespace, 107 Labels: map[string]string{ 108 common.LabelKeySecretType: common.LabelValueSecretTypeCluster, 109 }, 110 }, 111 Data: map[string][]byte{ 112 "server": []byte("http://mycluster"), 113 "name": []byte("mycluster"), 114 }, 115 }) 116 settingsManager := settings.NewSettingsManager(context.Background(), kubeclientset, fakeNamespace) 117 db := NewDB(fakeNamespace, settingsManager, kubeclientset) 118 assert.EqualError(t, db.DeleteCluster(context.Background(), "http://unknown"), `rpc error: code = NotFound desc = cluster "http://unknown" not found`) 119 } 120 121 func runWatchTest(t *testing.T, db ArgoDB, actions []func(old *v1alpha1.Cluster, new *v1alpha1.Cluster)) { 122 ctx, cancel := context.WithCancel(context.Background()) 123 defer cancel() 124 125 timeout := time.Second * 5 126 127 allDone := make(chan bool, 1) 128 129 doNext := func(old *v1alpha1.Cluster, new *v1alpha1.Cluster) { 130 if len(actions) == 0 { 131 assert.Fail(t, "Unexpected event") 132 } 133 next := actions[0] 134 next(old, new) 135 if t.Failed() { 136 allDone <- true 137 } 138 if len(actions) == 1 { 139 allDone <- true 140 } else { 141 actions = actions[1:] 142 } 143 } 144 145 go func() { 146 assert.NoError(t, db.WatchClusters(ctx, func(cluster *v1alpha1.Cluster) { 147 doNext(nil, cluster) 148 }, func(oldCluster *v1alpha1.Cluster, newCluster *v1alpha1.Cluster) { 149 doNext(oldCluster, newCluster) 150 }, func(clusterServer string) { 151 doNext(&v1alpha1.Cluster{Server: clusterServer}, nil) 152 })) 153 }() 154 155 select { 156 case <-allDone: 157 case <-time.After(timeout): 158 assert.Fail(t, "Failed due to timeout") 159 } 160 161 }