Repository: kubean-io/kubean
Pull request #976 (2023-09-25 08:57:12)

add unit-test
<!--  

Thanks for sending a pull request!  Here are some tips for you:

* make sure your commit is signed off

-->

#### What type of PR is this?

<!--
Add one of the following kinds:
/kind bug
/kind cleanup
/kind documentation
/kind enhancement
/kind feature

Optionally add one or more of the following kinds if applicable:
/kind api-change
/kind deprecation
/kind failing-test
/kind flake
/kind design
/kind regression
-->

#### What this PR does / why we need it:

#### Which issue(s) this PR fixes:
<!--
*Automatically closes linked issue when PR is merged.
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
_If PR is about `failing-tests or flakes`, please post the related issues/tests in a comment and do not use `Fixes`_*
-->
Fixes #

#### Special notes for your reviewer:


tu1h: /lgtm

Patch:
diff --git a/pkg/webhooks/clusterops/clusterops_webhook.go b/pkg/webhooks/clusterops/clusterops_webhook.go
index fc10fedff..d512ec11e 100644
--- a/pkg/webhooks/clusterops/clusterops_webhook.go
+++ b/pkg/webhooks/clusterops/clusterops_webhook.go
@@ -279,13 +279,15 @@ func PrepareWebHookHTTPSServer(KubeanClusterOpsSet clusterOperationClientSet.Int
 	return server
 }
 
-func StartWebHookHTTPSServer(server *http.Server) {
+func StartWebHookHTTPSServer(server *http.Server) error {
 	certPath := filepath.Join(certsDir, certFile)
 	keyPath := filepath.Join(certsDir, certKey)
 	klog.Warning("start https server for webhook")
 	if err := server.ListenAndServeTLS(certPath, keyPath); err != nil {
 		klog.ErrorS(err, "start https server for webhook but failed")
+		return err
 	}
+	return nil
 }
 
 func UpdateClusterOperationWebhook(clientSet kubernetes.Interface) error {


Test patch:
diff --git a/pkg/util/cluster_test.go b/pkg/util/cluster_test.go
index 9343c3b2d..cc0663b85 100644
--- a/pkg/util/cluster_test.go
+++ b/pkg/util/cluster_test.go
@@ -4,13 +4,18 @@
 package util
 
 import (
+	"context"
 	"os"
 	"path/filepath"
 	"testing"
 
+	corev1 "k8s.io/api/core/v1"
+	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 	"k8s.io/apimachinery/pkg/runtime"
 	"k8s.io/apimachinery/pkg/runtime/schema"
+	clientsetfake "k8s.io/client-go/kubernetes/fake"
 
+	"github.com/kubean-io/kubean-api/apis"
 	clusterv1alpha1 "github.com/kubean-io/kubean-api/apis/cluster/v1alpha1"
 	clusteroperationv1alpha1 "github.com/kubean-io/kubean-api/apis/clusteroperation/v1alpha1"
 	localartifactsetv1alpha1 "github.com/kubean-io/kubean-api/apis/localartifactset/v1alpha1"
@@ -115,3 +120,54 @@ func TestGetCurrentNS(t *testing.T) {
 		})
 	}
 }
+
+func TestGetCurrentRunningPodName(t *testing.T) {
+	os.Setenv("HOSTNAME", "abc")
+	if GetCurrentRunningPodName() != "abc" {
+		t.Fatal()
+	}
+}
+
+func TestUpdateOwnReference(t *testing.T) {
+	tests := []struct {
+		name string
+		args func() bool
+		want bool
+	}{
+		{
+			name: "not found",
+			args: func() bool {
+				fakeClient := clientsetfake.NewSimpleClientset()
+				configMapList := []*apis.ConfigMapRef{{Name: "cm1", NameSpace: "abc"}}
+				secretList := []*apis.SecretRef{{Name: "secret1", NameSpace: "abc"}}
+				return UpdateOwnReference(fakeClient, configMapList, secretList, metav1.OwnerReference{}) == nil
+			},
+			want: true,
+		},
+		{
+			name: "record exists",
+			args: func() bool {
+				fakeClient := clientsetfake.NewSimpleClientset()
+				fakeClient.CoreV1().ConfigMaps("abc").Create(context.Background(), &corev1.ConfigMap{ObjectMeta: metav1.ObjectMeta{
+					Name:      "cm1",
+					Namespace: "abc",
+				}}, metav1.CreateOptions{})
+				fakeClient.CoreV1().Secrets("abc").Create(context.Background(), &corev1.Secret{ObjectMeta: metav1.ObjectMeta{
+					Name:      "secret1",
+					Namespace: "abc",
+				}}, metav1.CreateOptions{})
+				configMapList := []*apis.ConfigMapRef{{Name: "cm1", NameSpace: "abc"}}
+				secretList := []*apis.SecretRef{{Name: "secret1", NameSpace: "abc"}}
+				return UpdateOwnReference(fakeClient, configMapList, secretList, metav1.OwnerReference{}) == nil
+			},
+			want: true,
+		},
+	}
+	for _, test := range tests {
+		t.Run(test.name, func(t *testing.T) {
+			if test.args() != test.want {
+				t.Fatal()
+			}
+		})
+	}
+}
diff --git a/pkg/webhooks/clusterops/clusterops_webhook_test.go b/pkg/webhooks/clusterops/clusterops_webhook_test.go
index e663b207c..3b832df0c 100644
--- a/pkg/webhooks/clusterops/clusterops_webhook_test.go
+++ b/pkg/webhooks/clusterops/clusterops_webhook_test.go
@@ -20,6 +20,7 @@ import (
 	corev1 "k8s.io/api/core/v1"
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 	"k8s.io/apimachinery/pkg/runtime"
+	"k8s.io/apimachinery/pkg/util/errors"
 	"k8s.io/apiserver/pkg/endpoints/responsewriter"
 	clientsetfake "k8s.io/client-go/kubernetes/fake"
 	"k8s.io/client-go/rest"
@@ -66,6 +67,24 @@ func TestCreateHTTPSCAFiles(t *testing.T) {
 	if !util.IsExist(filepath.Join(certsDir, certKey)) {
 		t.Fatal()
 	}
+
+	mux := http.NewServeMux()
+	mux.Handle("/ping", PingHandler{})
+	server := &http.Server{
+		Addr:    ":10443",
+		Handler: mux,
+	}
+	go func() {
+		time.Sleep(time.Second * 2)
+		server.Close()
+	}()
+	err := errors.AggregateGoroutines(func() error {
+		return StartWebHookHTTPSServer(server)
+	})
+	if err != nil && !strings.Contains(err.Error(), "tls: failed to find any PEM data") {
+		// other err
+		t.Fatal(err)
+	}
 }
 
 func TestPrepareWebHookHTTPSServer(t *testing.T) {