github.com/argoproj/argo-cd/v3@v3.2.1/cmd/util/cluster_test.go (about) 1 package util 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 "github.com/stretchr/testify/require" 8 corev1 "k8s.io/api/core/v1" 9 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 10 "k8s.io/apimachinery/pkg/runtime" 11 "k8s.io/client-go/kubernetes/fake" 12 "k8s.io/client-go/rest" 13 clientcmdapiv1 "k8s.io/client-go/tools/clientcmd/api/v1" 14 "sigs.k8s.io/yaml" 15 16 "github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1" 17 ) 18 19 func Test_newCluster(t *testing.T) { 20 labels := map[string]string{"key1": "val1"} 21 annotations := map[string]string{"key2": "val2"} 22 clusterWithData := NewCluster("test-cluster", []string{"test-namespace"}, false, &rest.Config{ 23 TLSClientConfig: rest.TLSClientConfig{ 24 Insecure: false, 25 ServerName: "test-endpoint.example.com", 26 CAData: []byte("test-ca-data"), 27 CertData: []byte("test-cert-data"), 28 KeyData: []byte("test-key-data"), 29 }, 30 Host: "test-endpoint.example.com", 31 }, 32 "test-bearer-token", 33 &v1alpha1.AWSAuthConfig{}, 34 &v1alpha1.ExecProviderConfig{}, labels, annotations) 35 36 assert.Equal(t, "test-cert-data", string(clusterWithData.Config.CertData)) 37 assert.Equal(t, "test-key-data", string(clusterWithData.Config.KeyData)) 38 assert.Empty(t, clusterWithData.Config.BearerToken) 39 assert.Equal(t, labels, clusterWithData.Labels) 40 assert.Equal(t, annotations, clusterWithData.Annotations) 41 assert.False(t, clusterWithData.Config.DisableCompression) 42 43 clusterWithFiles := NewCluster("test-cluster", []string{"test-namespace"}, false, &rest.Config{ 44 TLSClientConfig: rest.TLSClientConfig{ 45 Insecure: false, 46 ServerName: "test-endpoint.example.com", 47 CAData: []byte("test-ca-data"), 48 CertFile: "./testdata/test.cert.pem", 49 KeyFile: "./testdata/test.key.pem", 50 }, 51 Host: "test-endpoint.example.com", 52 }, 53 "test-bearer-token", 54 &v1alpha1.AWSAuthConfig{}, 55 &v1alpha1.ExecProviderConfig{}, labels, nil) 56 57 assert.Contains(t, string(clusterWithFiles.Config.CertData), "test-cert-data") 58 assert.Contains(t, string(clusterWithFiles.Config.KeyData), "test-key-data") 59 assert.Empty(t, clusterWithFiles.Config.BearerToken) 60 assert.Equal(t, labels, clusterWithFiles.Labels) 61 assert.Nil(t, clusterWithFiles.Annotations) 62 63 clusterWithBearerToken := NewCluster("test-cluster", []string{"test-namespace"}, false, &rest.Config{ 64 TLSClientConfig: rest.TLSClientConfig{ 65 Insecure: false, 66 ServerName: "test-endpoint.example.com", 67 CAData: []byte("test-ca-data"), 68 }, 69 Host: "test-endpoint.example.com", 70 }, 71 "test-bearer-token", 72 &v1alpha1.AWSAuthConfig{}, 73 &v1alpha1.ExecProviderConfig{}, nil, nil) 74 75 assert.Equal(t, "test-bearer-token", clusterWithBearerToken.Config.BearerToken) 76 assert.Nil(t, clusterWithBearerToken.Labels) 77 assert.Nil(t, clusterWithBearerToken.Annotations) 78 79 clusterWithDisableCompression := NewCluster("test-cluster", []string{"test-namespace"}, false, &rest.Config{ 80 TLSClientConfig: rest.TLSClientConfig{ 81 Insecure: false, 82 ServerName: "test-endpoint.example.com", 83 CAData: []byte("test-ca-data"), 84 }, 85 DisableCompression: true, 86 Host: "test-endpoint.example.com", 87 }, "test-bearer-token", 88 &v1alpha1.AWSAuthConfig{}, 89 &v1alpha1.ExecProviderConfig{}, labels, annotations) 90 91 assert.True(t, clusterWithDisableCompression.Config.DisableCompression) 92 } 93 94 func TestGetKubePublicEndpoint(t *testing.T) { 95 cases := []struct { 96 name string 97 clusterInfo *corev1.ConfigMap 98 expectedEndpoint string 99 expectedCAData []byte 100 expectError bool 101 }{ 102 { 103 name: "has public endpoint and certificate authority data", 104 clusterInfo: &corev1.ConfigMap{ 105 ObjectMeta: metav1.ObjectMeta{ 106 Namespace: "kube-public", 107 Name: "cluster-info", 108 }, 109 Data: map[string]string{ 110 "kubeconfig": kubeconfigFixture("https://test-cluster:6443", []byte("test-ca-data")), 111 }, 112 }, 113 expectedEndpoint: "https://test-cluster:6443", 114 expectedCAData: []byte("test-ca-data"), 115 }, 116 { 117 name: "has public endpoint", 118 clusterInfo: &corev1.ConfigMap{ 119 ObjectMeta: metav1.ObjectMeta{ 120 Namespace: "kube-public", 121 Name: "cluster-info", 122 }, 123 Data: map[string]string{ 124 "kubeconfig": kubeconfigFixture("https://test-cluster:6443", nil), 125 }, 126 }, 127 expectedEndpoint: "https://test-cluster:6443", 128 expectedCAData: nil, 129 }, 130 { 131 name: "no cluster-info", 132 expectError: true, 133 }, 134 { 135 name: "no kubeconfig in cluster-info", 136 clusterInfo: &corev1.ConfigMap{ 137 ObjectMeta: metav1.ObjectMeta{ 138 Namespace: "kube-public", 139 Name: "cluster-info", 140 }, 141 Data: map[string]string{ 142 "argo": "the project, not the movie", 143 }, 144 }, 145 expectError: true, 146 }, 147 { 148 name: "no clusters in cluster-info kubeconfig", 149 clusterInfo: &corev1.ConfigMap{ 150 ObjectMeta: metav1.ObjectMeta{ 151 Namespace: "kube-public", 152 Name: "cluster-info", 153 }, 154 Data: map[string]string{ 155 "kubeconfig": kubeconfigFixture("", nil), 156 }, 157 }, 158 expectError: true, 159 }, 160 { 161 name: "can't parse kubeconfig", 162 clusterInfo: &corev1.ConfigMap{ 163 ObjectMeta: metav1.ObjectMeta{ 164 Namespace: "kube-public", 165 Name: "cluster-info", 166 }, 167 Data: map[string]string{ 168 "kubeconfig": "this is not valid YAML", 169 }, 170 }, 171 expectError: true, 172 }, 173 } 174 175 for _, tc := range cases { 176 t.Run(tc.name, func(t *testing.T) { 177 objects := []runtime.Object{} 178 if tc.clusterInfo != nil { 179 objects = append(objects, tc.clusterInfo) 180 } 181 clientset := fake.NewClientset(objects...) 182 endpoint, caData, err := GetKubePublicEndpoint(clientset) 183 if tc.expectError { 184 require.Error(t, err) 185 } else { 186 require.NoError(t, err) 187 } 188 require.Equalf(t, tc.expectedEndpoint, endpoint, "expected endpoint %s, got %s", tc.expectedEndpoint, endpoint) 189 require.Equalf(t, tc.expectedCAData, caData, "expected caData %s, got %s", tc.expectedCAData, caData) 190 }) 191 } 192 } 193 194 func kubeconfigFixture(endpoint string, certificateAuthorityData []byte) string { 195 kubeconfig := &clientcmdapiv1.Config{} 196 if endpoint != "" { 197 kubeconfig.Clusters = []clientcmdapiv1.NamedCluster{ 198 { 199 Name: "test-kube", 200 Cluster: clientcmdapiv1.Cluster{ 201 Server: endpoint, 202 CertificateAuthorityData: certificateAuthorityData, 203 }, 204 }, 205 } 206 } 207 configYAML, err := yaml.Marshal(kubeconfig) 208 if err != nil { 209 return "" 210 } 211 return string(configYAML) 212 }