From a8605a02f9628b0c3726ec5fb80f2a67ea5244c2 Mon Sep 17 00:00:00 2001 From: Zheng Xi Zhou Date: Thu, 6 May 2021 22:48:19 +0800 Subject: [PATCH] Fix unstability of ApplicationContext UT (#1609) * Fix unstablity of ApplicationContext UT * fix import order * fix compatibility issue --- .../applicationcontext_controller_test.go | 36 +++++++++---------- .../v1alpha2/applicationcontext/suit_test.go | 5 ++- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/pkg/controller/core.oam.dev/v1alpha2/applicationcontext/applicationcontext_controller_test.go b/pkg/controller/core.oam.dev/v1alpha2/applicationcontext/applicationcontext_controller_test.go index 0a4e2e7b4..c7f74de9d 100644 --- a/pkg/controller/core.oam.dev/v1alpha2/applicationcontext/applicationcontext_controller_test.go +++ b/pkg/controller/core.oam.dev/v1alpha2/applicationcontext/applicationcontext_controller_test.go @@ -19,19 +19,20 @@ package applicationcontext import ( "context" - "time" + "github.com/crossplane/crossplane-runtime/pkg/logging" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" v1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" + ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/reconcile" "github.com/oam-dev/kubevela/apis/core.oam.dev/v1alpha2" - "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1" + core_oam_dev "github.com/oam-dev/kubevela/pkg/controller/core.oam.dev" "github.com/oam-dev/kubevela/pkg/oam/util" ) @@ -39,11 +40,17 @@ var _ = Describe("Test ApplicationContext Controller", func() { ctx := context.Background() It("Applying ApplicationContext", func() { - Context("appContext doesn't exist", func() { + Context("appContext not found", func() { + By("reconciling") + req := reconcile.Request{NamespacedName: client.ObjectKey{Name: "not-existed-appContext", Namespace: "ns1"}} + reconcileRetry(&r, req) + }) + + Context("appContext rollingStatus is completed", func() { By("apply an ApplicationContext") var ( appContextName = "app1" - appRevisionName = "xxx-v1" + appRevisionName = "app1-v1" componentName = "comp1" ns = "default" appContext = v1alpha2.ApplicationContext{ @@ -135,7 +142,7 @@ var _ = Describe("Test ApplicationContext Controller", func() { Namespace: ns, }, Spec: v1alpha2.ApplicationConfigurationSpec{ - Components: []v1alpha2.ApplicationConfigurationComponent{{ComponentName: "comp1"}}}, + Components: []v1alpha2.ApplicationConfigurationComponent{{ComponentName: componentName}}}, } appRevision := v1alpha2.ApplicationRevision{ @@ -149,19 +156,12 @@ var _ = Describe("Test ApplicationContext Controller", func() { }, } Expect(k8sClient.Create(ctx, &appRevision)).Should(Succeed()) - Eventually(func() int { - By("Reconcile") - reconcileRetry(&r, req) - if err := k8sClient.Get(ctx, client.ObjectKey{Namespace: ns, Name: appRevisionName}, &v1beta1.ApplicationRevision{}); err != nil { - return 0 - } - return 1 - }, 5*time.Second, time.Second).Should(Equal(1)) - - By("check application context") - var ac v1alpha2.ApplicationContext - Expect(k8sClient.Get(ctx, client.ObjectKey{Namespace: ns, Name: appContextName}, &ac)).Should(BeNil()) - Expect(string(ac.Status.ConditionedStatus.Conditions[0].Status)).Should(Equal("False")) + reconcileRetry(&r, req) }) }) + + It("Testing Setup", func() { + logr := ctrl.Log.WithName("ApplicationContext") + Expect(Setup(mgr, core_oam_dev.Args{}, logging.NewLogrLogger(logr).WithValues("suitTest", "Setup"))).Should(BeNil()) + }) }) diff --git a/pkg/controller/core.oam.dev/v1alpha2/applicationcontext/suit_test.go b/pkg/controller/core.oam.dev/v1alpha2/applicationcontext/suit_test.go index 7b0cfb181..8a56806c1 100644 --- a/pkg/controller/core.oam.dev/v1alpha2/applicationcontext/suit_test.go +++ b/pkg/controller/core.oam.dev/v1alpha2/applicationcontext/suit_test.go @@ -23,6 +23,8 @@ import ( "testing" "time" + "sigs.k8s.io/controller-runtime/pkg/manager" + "github.com/crossplane/crossplane-runtime/pkg/event" "github.com/crossplane/crossplane-runtime/pkg/logging" . "github.com/onsi/ginkgo" @@ -47,6 +49,7 @@ var testEnv *envtest.Environment var controllerDone chan struct{} var r Reconciler var defRevisionLimit = 5 +var mgr manager.Manager func TestComponentDefinition(t *testing.T) { RegisterFailHandler(Fail) @@ -77,7 +80,7 @@ var _ = BeforeSuite(func(done Done) { Expect(k8sClient).ToNot(BeNil()) By("Starting the controller in the background") - mgr, err := ctrl.NewManager(cfg, ctrl.Options{ + mgr, err = ctrl.NewManager(cfg, ctrl.Options{ Scheme: scheme.Scheme, MetricsBindAddress: "0", Port: 48082,