Skip to content

Commit

Permalink
Add UT for deploy.go
Browse files Browse the repository at this point in the history
Signed-off-by: rayywu <rayywu@tencent.com>
  • Loading branch information
realnumber666 committed Mar 26, 2023
1 parent 1d030d4 commit a3dfc45
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions pkg/karmadactl/cmdinit/kubernetes/deploy_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
package kubernetes

import (
"context"
"net"
"os"
"testing"
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"

clusterv1alpha1 "github.com/karmada-io/karmada/pkg/apis/cluster/v1alpha1"
"github.com/karmada-io/karmada/pkg/karmadactl/cmdinit/utils"
)

func newCluster(name string) *clusterv1alpha1.Cluster {
return &clusterv1alpha1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: clusterv1alpha1.ClusterSpec{},
Status: clusterv1alpha1.ClusterStatus{},
}
}

func Test_initializeDirectory(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -162,6 +177,76 @@ func TestCommandInitOption_genCerts(t *testing.T) {
}
}

func TestInitKarmadaAPIServer(t *testing.T) {
// Create a fake clientset
clientset := fake.NewSimpleClientset()

// Create a new CommandInitOption
initOption := &CommandInitOption{
KubeClientSet: clientset,
Namespace: "test-namespace",
}

// Call the function
err := initOption.initKarmadaAPIServer()

// Check if the function returned an error
if err != nil {
t.Errorf("initKarmadaAPIServer() returned an error: %v", err)
}

// Check if the etcd StatefulSet was created
_, err = clientset.AppsV1().StatefulSets(initOption.Namespace).Get(context.TODO(), etcdStatefulSetAndServiceName, metav1.GetOptions{})
if err != nil {
t.Errorf("initKarmadaAPIServer() failed to create etcd StatefulSet: %v", err)
}

// Check if the karmada APIServer Deployment was created
_, err = clientset.AppsV1().Deployments(initOption.Namespace).Get(context.TODO(), karmadaAPIServerDeploymentAndServiceName, metav1.GetOptions{})
if err != nil {
t.Errorf("initKarmadaAPIServer() failed to create karmada APIServer Deployment: %v", err)
}

// Check if the karmada aggregated apiserver Deployment was created
_, err = clientset.AppsV1().Deployments(initOption.Namespace).Get(context.TODO(), karmadaAggregatedAPIServerDeploymentAndServiceName, metav1.GetOptions{})
if err != nil {
t.Errorf("initKarmadaAPIServer() failed to create karmada aggregated apiserver Deployment: %v", err)
}
}

func TestCommandInitOption_initKarmadaComponent(t *testing.T) {
// Create a fake kube clientset
clientSet := fake.NewSimpleClientset()

// Create a new CommandInitOption
initOption := &CommandInitOption{
KubeClientSet: clientSet,
Namespace: "test-namespace",
}

// Call the function
err := initOption.initKarmadaComponent()

// Assert that no error was returned
if err != nil {
t.Errorf("initKarmadaComponent returned an unexpected error: %v", err)
}

// Assert that the expected deployments were created
expectedDeployments := []string{
kubeControllerManagerClusterRoleAndDeploymentAndServiceName,
schedulerDeploymentNameAndServiceAccountName,
controllerManagerDeploymentAndServiceName,
webhookDeploymentAndServiceAccountAndServiceName,
}
for _, deploymentName := range expectedDeployments {
_, err = clientSet.AppsV1().Deployments("test-namespace").Get(context.TODO(), deploymentName, metav1.GetOptions{})
if err != nil {
t.Errorf("Expected deployment %s was not created: %v", deploymentName, err)
}
}
}

func TestKubeRegistry(t *testing.T) {
tests := []struct {
name string
Expand Down

0 comments on commit a3dfc45

Please sign in to comment.