|
1 | 1 | package memberwatch
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "encoding/base64" |
4 | 5 | "encoding/json"
|
5 | 6 | "testing"
|
6 | 7 |
|
7 | 8 | "github.com/stretchr/testify/assert"
|
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + "sigs.k8s.io/controller-runtime/pkg/cluster" |
8 | 11 |
|
9 | 12 | "github.com/10gen/ops-manager-kubernetes/api/v1/mdb"
|
| 13 | + mc "github.com/10gen/ops-manager-kubernetes/pkg/multicluster" |
10 | 14 | "github.com/10gen/ops-manager-kubernetes/pkg/multicluster/failedcluster"
|
11 | 15 | )
|
12 | 16 |
|
@@ -147,3 +151,192 @@ func TestShouldAddFailedClusterAnnotation(t *testing.T) {
|
147 | 151 | assert.Equal(t, shouldAddFailedClusterAnnotation(tt.annotations, tt.clusterName), tt.out)
|
148 | 152 | }
|
149 | 153 | }
|
| 154 | + |
| 155 | +func TestGetClusterCredentials(t *testing.T) { |
| 156 | + validCertContent := "valid-cert" |
| 157 | + validCert := base64.StdEncoding.EncodeToString([]byte(validCertContent)) |
| 158 | + invalidCert := "invalid-base64!!!" |
| 159 | + clusterName := "cluster1" |
| 160 | + userToken := "abc123" |
| 161 | + mockUserItemList := []mc.KubeConfigUserItem{ |
| 162 | + {Name: "user1", User: mc.KubeConfigUser{Token: userToken}}, |
| 163 | + } |
| 164 | + mockKubeContext := mc.KubeConfigContextItem{ |
| 165 | + Name: "context1", |
| 166 | + Context: mc.KubeConfigContext{ |
| 167 | + Cluster: clusterName, |
| 168 | + User: "user1", |
| 169 | + }, |
| 170 | + } |
| 171 | + kubeconfigServerURL := "https://example.com" |
| 172 | + mockKubeConfig := mc.KubeConfigFile{ |
| 173 | + Clusters: []mc.KubeConfigClusterItem{ |
| 174 | + { |
| 175 | + Name: clusterName, |
| 176 | + Cluster: mc.KubeConfigCluster{ |
| 177 | + Server: kubeconfigServerURL, |
| 178 | + CertificateAuthority: validCert, |
| 179 | + }, |
| 180 | + }, |
| 181 | + }, |
| 182 | + Users: mockUserItemList, |
| 183 | + } |
| 184 | + |
| 185 | + tests := []struct { |
| 186 | + name string |
| 187 | + clustersMap map[string]cluster.Cluster // Using as a set; the value is not used. |
| 188 | + kubeConfig mc.KubeConfigFile |
| 189 | + kubeContext mc.KubeConfigContextItem |
| 190 | + wantErr bool |
| 191 | + errContains string |
| 192 | + expectedServer string |
| 193 | + expectedToken string |
| 194 | + expectedCA []byte |
| 195 | + }{ |
| 196 | + { |
| 197 | + name: "Cluster not in clustersMap", |
| 198 | + clustersMap: map[string]cluster.Cluster{}, // Empty map; cluster1 is missing. |
| 199 | + kubeConfig: mockKubeConfig, |
| 200 | + kubeContext: mockKubeContext, |
| 201 | + wantErr: true, |
| 202 | + errContains: "cluster cluster1 not found in clustersMap", |
| 203 | + }, |
| 204 | + { |
| 205 | + name: "Cluster missing in kubeConfig.Clusters", |
| 206 | + clustersMap: map[string]cluster.Cluster{ |
| 207 | + clusterName: nil, |
| 208 | + }, |
| 209 | + kubeConfig: mc.KubeConfigFile{ |
| 210 | + Clusters: []mc.KubeConfigClusterItem{}, // No cluster defined. |
| 211 | + Users: mockUserItemList, |
| 212 | + }, |
| 213 | + kubeContext: mockKubeContext, |
| 214 | + wantErr: true, |
| 215 | + errContains: "failed to get cluster with clustername: cluster1", |
| 216 | + }, |
| 217 | + { |
| 218 | + name: "Invalid certificate authority", |
| 219 | + clustersMap: map[string]cluster.Cluster{ |
| 220 | + clusterName: nil, |
| 221 | + }, |
| 222 | + kubeConfig: mc.KubeConfigFile{ |
| 223 | + Clusters: []mc.KubeConfigClusterItem{ |
| 224 | + { |
| 225 | + Name: clusterName, |
| 226 | + Cluster: mc.KubeConfigCluster{ |
| 227 | + Server: kubeconfigServerURL, |
| 228 | + CertificateAuthority: invalidCert, // The kubeConfig has an invalid CA |
| 229 | + }, |
| 230 | + }, |
| 231 | + }, |
| 232 | + Users: mockUserItemList, |
| 233 | + }, |
| 234 | + kubeContext: mockKubeContext, |
| 235 | + wantErr: true, |
| 236 | + errContains: "failed to decode certificate for cluster: cluster1", |
| 237 | + }, |
| 238 | + { |
| 239 | + name: "User not found", |
| 240 | + clustersMap: map[string]cluster.Cluster{ |
| 241 | + clusterName: nil, |
| 242 | + }, |
| 243 | + kubeConfig: mc.KubeConfigFile{ |
| 244 | + Clusters: []mc.KubeConfigClusterItem{ |
| 245 | + { |
| 246 | + Name: clusterName, |
| 247 | + Cluster: mc.KubeConfigCluster{ |
| 248 | + Server: kubeconfigServerURL, |
| 249 | + CertificateAuthority: validCert, |
| 250 | + }, |
| 251 | + }, |
| 252 | + }, |
| 253 | + Users: []mc.KubeConfigUserItem{}, // No users defined. |
| 254 | + }, |
| 255 | + kubeContext: mc.KubeConfigContextItem{ |
| 256 | + Name: "context1", |
| 257 | + Context: mc.KubeConfigContext{ |
| 258 | + Cluster: clusterName, |
| 259 | + User: "user1", // User is not present. |
| 260 | + }, |
| 261 | + }, |
| 262 | + wantErr: true, |
| 263 | + errContains: "failed to get user with name: user1", |
| 264 | + }, |
| 265 | + { |
| 266 | + name: "Successful extraction", |
| 267 | + clustersMap: map[string]cluster.Cluster{ |
| 268 | + clusterName: nil, |
| 269 | + }, |
| 270 | + kubeConfig: mockKubeConfig, |
| 271 | + kubeContext: mockKubeContext, |
| 272 | + wantErr: false, |
| 273 | + expectedServer: kubeconfigServerURL, |
| 274 | + expectedToken: userToken, |
| 275 | + expectedCA: []byte(validCertContent), |
| 276 | + }, |
| 277 | + } |
| 278 | + |
| 279 | + for _, tc := range tests { |
| 280 | + t.Run(tc.name, func(t *testing.T) { |
| 281 | + creds, err := getClusterCredentials(tc.clustersMap, tc.kubeConfig, tc.kubeContext) |
| 282 | + if tc.wantErr { |
| 283 | + assert.ErrorContains(t, err, tc.errContains) |
| 284 | + } else { |
| 285 | + require.NoError(t, err) |
| 286 | + assert.Equal(t, tc.expectedServer, creds.Server) |
| 287 | + assert.Equal(t, tc.expectedToken, creds.Token) |
| 288 | + assert.Equal(t, tc.expectedCA, creds.CertificateAuthority) |
| 289 | + } |
| 290 | + }) |
| 291 | + } |
| 292 | +} |
| 293 | + |
| 294 | +func TestGetUserFromContext(t *testing.T) { |
| 295 | + tests := []struct { |
| 296 | + name string |
| 297 | + userName string |
| 298 | + users []mc.KubeConfigUserItem |
| 299 | + expectedUser *mc.KubeConfigUser |
| 300 | + }{ |
| 301 | + { |
| 302 | + name: "User exists", |
| 303 | + userName: "alice", |
| 304 | + users: []mc.KubeConfigUserItem{ |
| 305 | + {Name: "alice", User: mc.KubeConfigUser{Token: "alice-token"}}, |
| 306 | + {Name: "bob", User: mc.KubeConfigUser{Token: "bob-token"}}, |
| 307 | + }, |
| 308 | + expectedUser: &mc.KubeConfigUser{Token: "alice-token"}, |
| 309 | + }, |
| 310 | + { |
| 311 | + name: "User does not exist", |
| 312 | + userName: "charlie", |
| 313 | + users: []mc.KubeConfigUserItem{ |
| 314 | + {Name: "alice", User: mc.KubeConfigUser{Token: "alice-token"}}, |
| 315 | + {Name: "bob", User: mc.KubeConfigUser{Token: "bob-token"}}, |
| 316 | + }, |
| 317 | + expectedUser: nil, |
| 318 | + }, |
| 319 | + { |
| 320 | + name: "Empty users slice", |
| 321 | + userName: "alice", |
| 322 | + users: []mc.KubeConfigUserItem{}, |
| 323 | + expectedUser: nil, |
| 324 | + }, |
| 325 | + { |
| 326 | + name: "Multiple users with same name, returns first match", |
| 327 | + userName: "duplicated", |
| 328 | + users: []mc.KubeConfigUserItem{ |
| 329 | + {Name: "duplicated", User: mc.KubeConfigUser{Token: "first-token"}}, |
| 330 | + {Name: "duplicated", User: mc.KubeConfigUser{Token: "second-token"}}, |
| 331 | + }, |
| 332 | + expectedUser: &mc.KubeConfigUser{Token: "first-token"}, |
| 333 | + }, |
| 334 | + } |
| 335 | + |
| 336 | + for _, tc := range tests { |
| 337 | + t.Run(tc.name, func(t *testing.T) { |
| 338 | + user := getUserFromContext(tc.userName, tc.users) |
| 339 | + assert.Equal(t, tc.expectedUser, user) |
| 340 | + }) |
| 341 | + } |
| 342 | +} |
0 commit comments