@@ -28,6 +28,23 @@ func Test_onClusterAdded(t *testing.T) {
2828 m .onClusterAdded (s )
2929 assert .Len (t , m .clusters , 1 )
3030 })
31+ t .Run ("Secret is malformed" , func (t * testing.T ) {
32+ m , err := NewManager (context .TODO (), "argocd" , "" , "" , "" , kube .NewFakeKubeClient ("argocd" ))
33+ require .NoError (t , err )
34+ s := & v1.Secret {
35+ ObjectMeta : metav1.ObjectMeta {
36+ Labels : map [string ]string {
37+ LabelKeyClusterAgentMapping : "agent" ,
38+ common .LabelKeySecretType : common .LabelValueSecretTypeCluster ,
39+ },
40+ },
41+ Data : map [string ][]byte {
42+ "config" : []byte ("invalid json" ),
43+ },
44+ }
45+ m .onClusterAdded (s )
46+ assert .Len (t , m .clusters , 0 )
47+ })
3148 t .Run ("Secret is missing one or more labels" , func (t * testing.T ) {
3249 m , err := NewManager (context .TODO (), "argocd" , "" , "" , "" , kube .NewFakeKubeClient ("argocd" ))
3350 require .NoError (t , err )
@@ -91,6 +108,50 @@ func Test_onClusterUpdated(t *testing.T) {
91108 assert .Nil (t , m .mapping ("agent1" ))
92109 assert .NotNil (t , m .mapping ("agent2" ))
93110 })
111+ t .Run ("Secret is malformed" , func (t * testing.T ) {
112+ old := & v1.Secret {
113+ ObjectMeta : metav1.ObjectMeta {
114+ Labels : map [string ]string {
115+ LabelKeyClusterAgentMapping : "agent1" ,
116+ common .LabelKeySecretType : common .LabelValueSecretTypeCluster ,
117+ },
118+ Name : "cluster1" ,
119+ Namespace : "argocd" ,
120+ },
121+ Data : map [string ][]byte {
122+ "name" : []byte ("cluster1" ),
123+ },
124+ }
125+ new := & v1.Secret {
126+ ObjectMeta : metav1.ObjectMeta {
127+ Labels : map [string ]string {
128+ LabelKeyClusterAgentMapping : "agent1" ,
129+ common .LabelKeySecretType : common .LabelValueSecretTypeCluster ,
130+ },
131+ Name : "cluster1" ,
132+ Namespace : "argocd" ,
133+ },
134+ Data : map [string ][]byte {
135+ "name" : []byte ("cluster2" ),
136+ },
137+ }
138+ m , err := NewManager (context .TODO (), "argocd" , "" , "" , "" , kube .NewFakeKubeClient ("argocd" ))
139+ require .NoError (t , err )
140+ m .mapCluster ("agent1" , & v1alpha1.Cluster {Name : "cluster1" })
141+ assert .NotNil (t , m .mapping ("agent1" ))
142+ // First (successful) update will change the cluster name to "cluster2"
143+ m .onClusterUpdated (old , new )
144+ assert .NotNil (t , m .mapping ("agent1" ))
145+ assert .Equal (t , "cluster2" , m .mapping ("agent1" ).Name )
146+ old = new .DeepCopy ()
147+ // Inject some invalid data to the new secret to trigger an error
148+ new .Data ["name" ] = []byte ("cluster3" )
149+ new .Data ["config" ] = []byte ("invalid json" )
150+ // Second (unsuccessful) update will not change the cluster name
151+ m .onClusterUpdated (old , new )
152+ assert .NotNil (t , m .mapping ("agent1" ))
153+ assert .Equal (t , "cluster2" , m .mapping ("agent1" ).Name )
154+ })
94155
95156 t .Run ("Can't remap to an agent that has a mapping already" , func (t * testing.T ) {
96157 old := & v1.Secret {
0 commit comments