Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UT for deploy.go #3332

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions pkg/karmadactl/cmdinit/kubernetes/deploy_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package kubernetes

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

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

"github.com/karmada-io/karmada/pkg/karmadactl/cmdinit/utils"
)

Expand Down Expand Up @@ -162,6 +166,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