From cdb36ef7350662b24eefe42dcf4dfc29995bbb05 Mon Sep 17 00:00:00 2001 From: ammujumdar Date: Tue, 8 Oct 2024 14:51:56 -0700 Subject: [PATCH] Unit tests for v1alpha1 and refactor too --- .../v1alpha1/webconsolerequest_unit_test.go | 82 +++++++++++++- .../v1alpha2/webconsolerequest_unit_test.go | 105 ++++++++---------- 2 files changed, 124 insertions(+), 63 deletions(-) diff --git a/controllers/virtualmachinewebconsolerequest/v1alpha1/webconsolerequest_unit_test.go b/controllers/virtualmachinewebconsolerequest/v1alpha1/webconsolerequest_unit_test.go index 31509ba44..6bc84b514 100644 --- a/controllers/virtualmachinewebconsolerequest/v1alpha1/webconsolerequest_unit_test.go +++ b/controllers/virtualmachinewebconsolerequest/v1alpha1/webconsolerequest_unit_test.go @@ -17,9 +17,12 @@ import ( vmopv1a1 "github.com/vmware-tanzu/vm-operator/api/v1alpha1" vmopv1 "github.com/vmware-tanzu/vm-operator/api/v1alpha3" webconsolerequest "github.com/vmware-tanzu/vm-operator/controllers/virtualmachinewebconsolerequest/v1alpha1" + "github.com/vmware-tanzu/vm-operator/external/appplatform/api/vmw_v1alpha1" + pkgcfg "github.com/vmware-tanzu/vm-operator/pkg/config" "github.com/vmware-tanzu/vm-operator/pkg/constants/testlabels" pkgctx "github.com/vmware-tanzu/vm-operator/pkg/context" providerfake "github.com/vmware-tanzu/vm-operator/pkg/providers/fake" + "github.com/vmware-tanzu/vm-operator/pkg/webconsoleurl" "github.com/vmware-tanzu/vm-operator/test/builder" ) @@ -42,11 +45,12 @@ func unitTestsReconcile() { ctx *builder.UnitTestContextForController fakeVMProvider *providerfake.VMProvider - reconciler *webconsolerequest.Reconciler - wcrCtx *pkgctx.WebConsoleRequestContext - wcr *vmopv1a1.WebConsoleRequest - vm *vmopv1a1.VirtualMachine - proxySvc *corev1.Service + reconciler *webconsolerequest.Reconciler + wcrCtx *pkgctx.WebConsoleRequestContext + wcr *vmopv1a1.WebConsoleRequest + vm *vmopv1a1.VirtualMachine + proxySvc *corev1.Service + proxySvcDNS *vmw_v1alpha1.SupervisorProperties ) BeforeEach(func() { @@ -141,5 +145,73 @@ func unitTestsReconcile() { // Checking the label key only because UID will not be set to a resource during unit test. Expect(wcrCtx.WebConsoleRequest.Labels).To(HaveKey(webconsolerequest.UUIDLabelKey)) }) + + When("SimplifiedEnablement Feature is true", func() { + JustBeforeEach(func() { + reconciler = webconsolerequest.NewReconciler( + pkgcfg.UpdateContext( + ctx, + func(config *pkgcfg.Config) { + config.Features.SimplifiedEnablement = true + }, + ), + ctx.Client, + ctx.Logger, + ctx.Recorder, + ctx.VMProvider, + ) + }) + + scenarios := []struct { + name string + apiServerDNSName []string + expectedProxy string + }{ + { + name: "API Server DNS Name is set", + apiServerDNSName: []string{"domain-1.test"}, + expectedProxy: "domain-1.test", + }, + { + name: "API Server DNS Name is not set", + apiServerDNSName: []string{}, + expectedProxy: "dummy-proxy-ip", + }, + } + + for _, scenario := range scenarios { + When(scenario.name, func() { + JustBeforeEach(func() { + proxySvcDNS = &vmw_v1alpha1.SupervisorProperties{ + ObjectMeta: metav1.ObjectMeta{ + Name: webconsoleurl.SupervisorServiceObjName, + Namespace: webconsoleurl.SupervisorServiceObjNamespace, + }, + Spec: vmw_v1alpha1.SupervisorPropertiesSpec{ + APIServerDNSNames: scenario.apiServerDNSName, + }, + } + initObjects = append(initObjects, proxySvcDNS) + ctx = suite.NewUnitTestContextForController(initObjects...) + reconciler = webconsolerequest.NewReconciler( + ctx, + ctx.Client, + ctx.Logger, + ctx.Recorder, + ctx.VMProvider, + ) + }) + + It("returns success and sets ProxyAddr", func() { + err := reconciler.ReconcileNormal(wcrCtx) + Expect(err).ToNot(HaveOccurred()) + + Expect(wcrCtx.WebConsoleRequest.Status.ProxyAddr).To(Equal(scenario.expectedProxy)) + }) + }) + } + }) + }) + } diff --git a/controllers/virtualmachinewebconsolerequest/v1alpha2/webconsolerequest_unit_test.go b/controllers/virtualmachinewebconsolerequest/v1alpha2/webconsolerequest_unit_test.go index 42533fe8c..9eca700a9 100644 --- a/controllers/virtualmachinewebconsolerequest/v1alpha2/webconsolerequest_unit_test.go +++ b/controllers/virtualmachinewebconsolerequest/v1alpha2/webconsolerequest_unit_test.go @@ -152,65 +152,54 @@ func unitTestsReconcile() { ) }) - When("API Server DNS Name is set", func() { - JustBeforeEach(func() { - proxySvcDNS = &vmw_v1alpha1.SupervisorProperties{ - ObjectMeta: metav1.ObjectMeta{ - Name: webconsoleurl.SupervisorServiceObjName, - Namespace: webconsoleurl.SupervisorServiceObjNamespace, - }, - Spec: vmw_v1alpha1.SupervisorPropertiesSpec{ - APIServerDNSNames: []string{"domain-1.test"}, - }, - } - initObjects = append(initObjects, proxySvcDNS) - ctx = suite.NewUnitTestContextForController(initObjects...) - reconciler = webconsolerequest.NewReconciler( - ctx, - ctx.Client, - ctx.Logger, - ctx.Recorder, - ctx.VMProvider, - ) - }) - - It("returns success and sets ProxyAddr to API Server DNS Name", func() { - err := reconciler.ReconcileNormal(wcrCtx) - Expect(err).ToNot(HaveOccurred()) - - Expect(wcrCtx.WebConsoleRequest.Status.ProxyAddr).To(Equal("domain-1.test")) - }) - }) - - When("API Server DNS Name is not set", func() { - JustBeforeEach(func() { - proxySvcDNS = &vmw_v1alpha1.SupervisorProperties{ - ObjectMeta: metav1.ObjectMeta{ - Name: webconsoleurl.SupervisorServiceObjName, - Namespace: webconsoleurl.SupervisorServiceObjNamespace, - }, - Spec: vmw_v1alpha1.SupervisorPropertiesSpec{ - APIServerDNSNames: []string{}, - }, - } - initObjects = append(initObjects, proxySvcDNS) - ctx = suite.NewUnitTestContextForController(initObjects...) - reconciler = webconsolerequest.NewReconciler( - ctx, - ctx.Client, - ctx.Logger, - ctx.Recorder, - ctx.VMProvider, - ) - }) - It("returns success and sets ProxyAddr to virtual IP", func() { - err := reconciler.ReconcileNormal(wcrCtx) - Expect(err).ToNot(HaveOccurred()) - - Expect(wcrCtx.WebConsoleRequest.Status.ProxyAddr).To(Equal("dummy-proxy-ip")) + scenarios := []struct { + name string + apiServerDNSName []string + expectedProxy string + }{ + { + name: "API Server DNS Name is set", + apiServerDNSName: []string{"domain-1.test"}, + expectedProxy: "domain-1.test", + }, + { + name: "API Server DNS Name is not set", + apiServerDNSName: []string{}, + expectedProxy: "dummy-proxy-ip", + }, + } + + for _, scenario := range scenarios { + When(scenario.name, func() { + JustBeforeEach(func() { + proxySvcDNS = &vmw_v1alpha1.SupervisorProperties{ + ObjectMeta: metav1.ObjectMeta{ + Name: webconsoleurl.SupervisorServiceObjName, + Namespace: webconsoleurl.SupervisorServiceObjNamespace, + }, + Spec: vmw_v1alpha1.SupervisorPropertiesSpec{ + APIServerDNSNames: scenario.apiServerDNSName, + }, + } + initObjects = append(initObjects, proxySvcDNS) + ctx = suite.NewUnitTestContextForController(initObjects...) + reconciler = webconsolerequest.NewReconciler( + ctx, + ctx.Client, + ctx.Logger, + ctx.Recorder, + ctx.VMProvider, + ) + }) + + It("returns success and sets ProxyAddr", func() { + err := reconciler.ReconcileNormal(wcrCtx) + Expect(err).ToNot(HaveOccurred()) + + Expect(wcrCtx.WebConsoleRequest.Status.ProxyAddr).To(Equal(scenario.expectedProxy)) + }) }) - }) + } }) - }) }