...
1 package services
2
3 import (
4 b64 "encoding/base64"
5 "testing"
6
7 clusterApi "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/clients/generated/apis/container/v1beta1"
8 "github.com/go-test/deep"
9 "k8s.io/client-go/rest"
10 "k8s.io/client-go/tools/clientcmd/api"
11 "sigs.k8s.io/controller-runtime/pkg/client"
12
13 "edge-infra.dev/pkg/edge/api/types"
14 )
15
16 var config *rest.Config
17 var runtimeClient1 client.WithWatch
18 var varName = gkeClient{
19 KubeConfig: config,
20 RuntimeClient: runtimeClient1,
21 RuntimeClientMap: map[string]client.WithWatch{},
22 }
23
24 var testEndpoint = "test_endpoint"
25
26
27 func TestGetClientName(t *testing.T) {
28 cluster := types.GkeCluster{Name: "test_org_1", Banner: "test_name_1"}
29 result := varName.GetClientName(&cluster)
30 expected := "test_name_1-test_org_1"
31 if result != expected {
32 t.Fatalf(`result = %q want match for %q`, result, expected)
33 }
34 }
35
36
37 func TestGetClientNameEmptyString(t *testing.T) {
38 cluster := types.GkeCluster{Name: "", Banner: ""}
39 result := varName.GetClientName(&cluster)
40 expected := "-"
41 if result != expected {
42 t.Fatalf(`result = %q want match for %q`, result, expected)
43 }
44 }
45
46
47 func TestGetClientNameNil(t *testing.T) {
48 cluster := types.GkeCluster{}
49 result := varName.GetClientName(&cluster)
50 expected := "-"
51 if result != expected {
52 t.Fatalf(`result = %q want match for %q`, result, expected)
53 }
54 }
55
56
57
58 func TestGetConfigForClusterClusterCaCertificateNotReady(t *testing.T) {
59 singleResp := &clusterApi.ContainerCluster{}
60 config, err := getConfigForCluster(singleResp)
61 if config != nil || err.Error() != "ClusterCaCertificate not ready on kcc container cluster" {
62 t.Fatalf(`config = %v, err = %v want match for nil, "ClusterCaCertificate not ready on kcc container cluster"`, config, err)
63 }
64 }
65
66
67 func TestGetConfigForClusterHostNotReady(t *testing.T) {
68 singleResp := &clusterApi.ContainerCluster{}
69 val := "testClusterCaCertificate"
70 singleResp.Spec.MasterAuth = &clusterApi.ClusterMasterAuth{
71 ClusterCaCertificate: &val,
72 }
73 config, err := getConfigForCluster(singleResp)
74
75 if config != nil || err.Error() != "host not ready on kcc container cluster" {
76 t.Fatalf(`config = %v, err = %v want match for nil, "host not ready on kcc container cluster"`, config, err)
77 }
78 }
79
80
81 func TestGetConfigForClusterDecodeError(t *testing.T) {
82 singleResp := &clusterApi.ContainerCluster{}
83 val := "illegal val"
84 singleResp.Spec.MasterAuth = &clusterApi.ClusterMasterAuth{
85 ClusterCaCertificate: &val,
86 }
87 singleResp.Status.Endpoint = &testEndpoint
88 config, err := getConfigForCluster(singleResp)
89 _, expectedErr := b64.StdEncoding.DecodeString(*singleResp.Spec.MasterAuth.ClusterCaCertificate)
90 if err != expectedErr || config != nil {
91 t.Fatalf(`config = %v, err = %v want match for nil, %v`, config, err, expectedErr)
92 }
93 }
94
95
96 func TestGetConfigForCluster(t *testing.T) {
97 singleResp := &clusterApi.ContainerCluster{}
98 val := b64.StdEncoding.EncodeToString([]byte("test_ClusterCaCertificate"))
99 singleResp.Spec.MasterAuth = &clusterApi.ClusterMasterAuth{
100 ClusterCaCertificate: &val,
101 }
102 singleResp.Status.Endpoint = &testEndpoint
103 config, err := getConfigForCluster(singleResp)
104 expectedConfig := &rest.Config{
105 TLSClientConfig: rest.TLSClientConfig{
106 CAData: []byte("test_ClusterCaCertificate"),
107 },
108
109 Host: *singleResp.Status.Endpoint,
110 AuthProvider: &api.AuthProviderConfig{
111 Name: "gcp",
112 },
113 }
114
115 if diff := deep.Equal(config, expectedConfig); diff != nil {
116 t.Error(diff)
117 }
118
119 if err != nil {
120 t.Fatalf(`err = %v want match for %v`, err, nil)
121 }
122 }
123
View as plain text