Skip to content

Commit

Permalink
Unit tests for v1alpha1 and refactor too
Browse files Browse the repository at this point in the history
  • Loading branch information
ammujumdar-bcom committed Oct 8, 2024
1 parent 76bf14c commit cdb36ef
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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() {
Expand Down Expand Up @@ -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))
})
})
}
})

})

}
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
})
})
}
})

})
}

0 comments on commit cdb36ef

Please sign in to comment.