package services import ( b64 "encoding/base64" "testing" clusterApi "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/clients/generated/apis/container/v1beta1" "github.com/go-test/deep" "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd/api" "sigs.k8s.io/controller-runtime/pkg/client" "edge-infra.dev/pkg/edge/api/types" ) var config *rest.Config var runtimeClient1 client.WithWatch var varName = gkeClient{ KubeConfig: config, RuntimeClient: runtimeClient1, RuntimeClientMap: map[string]client.WithWatch{}, } var testEndpoint = "test_endpoint" // Unit tests for GetClientName func TestGetClientName(t *testing.T) { cluster := types.GkeCluster{Name: "test_org_1", Banner: "test_name_1"} result := varName.GetClientName(&cluster) expected := "test_name_1-test_org_1" if result != expected { t.Fatalf(`result = %q want match for %q`, result, expected) } } // testcase: name and organization are empty strings func TestGetClientNameEmptyString(t *testing.T) { cluster := types.GkeCluster{Name: "", Banner: ""} result := varName.GetClientName(&cluster) expected := "-" if result != expected { t.Fatalf(`result = %q want match for %q`, result, expected) } } // testcase: name and organization are nil func TestGetClientNameNil(t *testing.T) { cluster := types.GkeCluster{} result := varName.GetClientName(&cluster) expected := "-" if result != expected { t.Fatalf(`result = %q want match for %q`, result, expected) } } // Unit tests for getConfigForCluster // testcase: get error "ClusterCaCertificate not ready on kcc container cluster." func TestGetConfigForClusterClusterCaCertificateNotReady(t *testing.T) { singleResp := &clusterApi.ContainerCluster{} config, err := getConfigForCluster(singleResp) if config != nil || err.Error() != "ClusterCaCertificate not ready on kcc container cluster" { t.Fatalf(`config = %v, err = %v want match for nil, "ClusterCaCertificate not ready on kcc container cluster"`, config, err) } } // testcase: get error "host not ready on kcc container cluster." func TestGetConfigForClusterHostNotReady(t *testing.T) { singleResp := &clusterApi.ContainerCluster{} val := "testClusterCaCertificate" singleResp.Spec.MasterAuth = &clusterApi.ClusterMasterAuth{ ClusterCaCertificate: &val, } config, err := getConfigForCluster(singleResp) if config != nil || err.Error() != "host not ready on kcc container cluster" { t.Fatalf(`config = %v, err = %v want match for nil, "host not ready on kcc container cluster"`, config, err) } } // testcase: get decode error func TestGetConfigForClusterDecodeError(t *testing.T) { singleResp := &clusterApi.ContainerCluster{} val := "illegal val" singleResp.Spec.MasterAuth = &clusterApi.ClusterMasterAuth{ ClusterCaCertificate: &val, } singleResp.Status.Endpoint = &testEndpoint config, err := getConfigForCluster(singleResp) _, expectedErr := b64.StdEncoding.DecodeString(*singleResp.Spec.MasterAuth.ClusterCaCertificate) if err != expectedErr || config != nil { t.Fatalf(`config = %v, err = %v want match for nil, %v`, config, err, expectedErr) } } // testcase: get config func TestGetConfigForCluster(t *testing.T) { singleResp := &clusterApi.ContainerCluster{} val := b64.StdEncoding.EncodeToString([]byte("test_ClusterCaCertificate")) singleResp.Spec.MasterAuth = &clusterApi.ClusterMasterAuth{ ClusterCaCertificate: &val, } singleResp.Status.Endpoint = &testEndpoint config, err := getConfigForCluster(singleResp) expectedConfig := &rest.Config{ TLSClientConfig: rest.TLSClientConfig{ CAData: []byte("test_ClusterCaCertificate"), }, Host: *singleResp.Status.Endpoint, AuthProvider: &api.AuthProviderConfig{ Name: "gcp", }, } if diff := deep.Equal(config, expectedConfig); diff != nil { t.Error(diff) } if err != nil { t.Fatalf(`err = %v want match for %v`, err, nil) } }