From 067febb23721b72f5b1bf75aee4b49bcfb570317 Mon Sep 17 00:00:00 2001 From: Eikykun Date: Fri, 8 Dec 2023 17:38:50 +0800 Subject: [PATCH] fix ut --- .../podtransitionrule_controller_suit_test.go | 10 +++++++--- .../podtransitionrule_controller_test.go | 4 ---- .../podtransitionrule/processor/rules/webhook_test.go | 8 +++++--- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/pkg/controllers/podtransitionrule/podtransitionrule_controller_suit_test.go b/pkg/controllers/podtransitionrule/podtransitionrule_controller_suit_test.go index 7d7bb1e1..e7b3f42f 100644 --- a/pkg/controllers/podtransitionrule/podtransitionrule_controller_suit_test.go +++ b/pkg/controllers/podtransitionrule/podtransitionrule_controller_suit_test.go @@ -30,15 +30,18 @@ import ( logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/log/zap" "sigs.k8s.io/controller-runtime/pkg/manager" + "sigs.k8s.io/controller-runtime/pkg/reconcile" appsv1alpha1 "kusionstack.io/operating/apis/apps/v1alpha1" "kusionstack.io/operating/pkg/utils/inject" ) var ( - env *envtest.Environment - mgr manager.Manager - c client.Client + env *envtest.Environment + mgr manager.Manager + c client.Client + request chan reconcile.Request + ctx context.Context cancel context.CancelFunc wg sync.WaitGroup @@ -77,6 +80,7 @@ func TestMain(m *testing.M) { }() <-time.After(time.Second * 5) code := m.Run() + Stop() env.Stop() os.Exit(code) } diff --git a/pkg/controllers/podtransitionrule/podtransitionrule_controller_test.go b/pkg/controllers/podtransitionrule/podtransitionrule_controller_test.go index 1a001e19..ee43b043 100644 --- a/pkg/controllers/podtransitionrule/podtransitionrule_controller_test.go +++ b/pkg/controllers/podtransitionrule/podtransitionrule_controller_test.go @@ -41,8 +41,6 @@ import ( func TestPodTransitionRule(t *testing.T) { g := gomega.NewGomegaWithT(t) - defer Stop() - <-time.After(1 * time.Second) var pods []*corev1.Pod pods = append(pods, genDefaultPod("default", "pod-test-1"), @@ -170,10 +168,8 @@ func TestPodTransitionRule(t *testing.T) { func TestWebhookRule(t *testing.T) { g := gomega.NewGomegaWithT(t) - stop, finish := RunHttpServer(handleHttpAlwaysSuccess, "8888") defer func() { - Stop() stop <- struct{}{} <-finish }() diff --git a/pkg/controllers/podtransitionrule/processor/rules/webhook_test.go b/pkg/controllers/podtransitionrule/processor/rules/webhook_test.go index c37f741a..496c5ecc 100644 --- a/pkg/controllers/podtransitionrule/processor/rules/webhook_test.go +++ b/pkg/controllers/podtransitionrule/processor/rules/webhook_test.go @@ -77,7 +77,7 @@ var ( } timeout = int64(60) interval = int64(5) - pollRS = &appsv1alpha1.PodTransitionRule{ + poRS = &appsv1alpha1.PodTransitionRule{ ObjectMeta: metav1.ObjectMeta{ Name: "podtransitionrule-poll", Namespace: "default", @@ -212,6 +212,7 @@ func TestWebhookPollFail(t *testing.T) { } subjects := sets.NewString("test-pod-a", "test-pod-b", "test-pod-c") g := gomega.NewGomegaWithT(t) + pollRS := poRS.DeepCopy() webhooks := GetWebhook(pollRS) g.Expect(len(webhooks)).Should(gomega.BeEquivalentTo(1)) web := webhooks[0] @@ -276,6 +277,7 @@ func TestWebhookPoll(t *testing.T) { } subjects := sets.NewString("test-pod-a", "test-pod-b", "test-pod-c") g := gomega.NewGomegaWithT(t) + pollRS := poRS.DeepCopy() webhooks := GetWebhook(pollRS) g.Expect(len(webhooks)).Should(gomega.BeEquivalentTo(1)) web := webhooks[0] @@ -302,7 +304,7 @@ func TestWebhookPoll(t *testing.T) { <-time.After(5 * time.Second) state = &appsv1alpha1.RuleState{Name: web.RuleName, WebhookStatus: res.RuleState.WebhookStatus} pollRS.Status.RuleStates = []*appsv1alpha1.RuleState{state} - webhooks = GetWebhook(pollRS) + webhooks = GetWebhook(pollRS.DeepCopy()) web = webhooks[0] res = web.Do(targets, subjects) rj, _ = json.Marshal(res) @@ -314,7 +316,7 @@ func TestWebhookPoll(t *testing.T) { <-time.After(5 * time.Second) state = &appsv1alpha1.RuleState{Name: web.RuleName, WebhookStatus: res.RuleState.WebhookStatus} pollRS.Status.RuleStates = []*appsv1alpha1.RuleState{state} - webhooks = GetWebhook(pollRS) + webhooks = GetWebhook(pollRS.DeepCopy()) web = webhooks[0] res = web.Do(targets, subjects) rj, _ = json.Marshal(res)