Skip to content

Commit

Permalink
Add unit test cases for ClusterModule
Browse files Browse the repository at this point in the history
  • Loading branch information
MrDXY committed Oct 25, 2023
1 parent 783bdaf commit 1759ccb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
47 changes: 47 additions & 0 deletions pkg/clustermodule/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

infrav1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/v1beta1"
"sigs.k8s.io/cluster-api-provider-vsphere/pkg/context/fake"
"sigs.k8s.io/cluster-api-provider-vsphere/test/helpers/vcsim"
)

func TestService_Create(t *testing.T) {
Expand Down Expand Up @@ -81,6 +82,52 @@ func TestService_Create(t *testing.T) {
g.Expect(moduleUUID).To(gomega.BeEmpty())
})
})

t.Run("creation is executed", func(t *testing.T) {
g := gomega.NewWithT(t)
simr, err := vcsim.NewBuilder().Build()
defer simr.Destroy()
g.Expect(err).ToNot(gomega.HaveOccurred())
md := machineDeployment("md", fake.Namespace, fake.Clusterv1a2Name)
md.Spec.Template.Spec.InfrastructureRef = corev1.ObjectReference{
Kind: "VSphereMachineTemplate",
Namespace: fake.Namespace,
Name: "blah-template",
}

machineTemplate := &infrav1.VSphereMachineTemplate{
TypeMeta: metav1.TypeMeta{Kind: "VSphereMachineTemplate"},
ObjectMeta: metav1.ObjectMeta{
Name: "blah-template",
Namespace: fake.Namespace,
},
Spec: infrav1.VSphereMachineTemplateSpec{
Template: infrav1.VSphereMachineTemplateResource{Spec: infrav1.VSphereMachineSpec{
VirtualMachineCloneSpec: infrav1.VirtualMachineCloneSpec{
Server: simr.ServerURL().Host,
Datacenter: "*",
ResourcePool: "/DC0/host/DC0_C0/Resources",
},
}},
},
}

controllerCtx := fake.NewControllerContext(fake.NewControllerManagerContext(md, machineTemplate))
clusterCtx := fake.NewClusterContext(controllerCtx)
clusterCtx.VSphereCluster.Spec.Server = simr.ServerURL().Host
controllerCtx.ControllerManagerContext.Username = simr.Username()
controllerCtx.ControllerManagerContext.Password = simr.Password()

svc := NewService(controllerCtx.ControllerManagerContext, controllerCtx.Client)
moduleUUID, err := svc.Create(context.Background(), clusterCtx, mdWrapper{md})
g.Expect(err).ToNot(gomega.HaveOccurred())
g.Expect(moduleUUID).NotTo(gomega.BeEmpty())
exists, err := svc.DoesExist(context.Background(), clusterCtx, mdWrapper{md}, moduleUUID)
g.Expect(exists).To(gomega.BeTrue())
g.Expect(err).NotTo(gomega.HaveOccurred())
err = svc.Remove(context.Background(), clusterCtx, moduleUUID)
g.Expect(err).ToNot(gomega.HaveOccurred())
})
}

func machineDeployment(name, namespace, cluster string) *clusterv1.MachineDeployment {
Expand Down
1 change: 1 addition & 0 deletions test/helpers/vcsim/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/onsi/gomega/gbytes"
"github.com/vmware/govmomi/simulator"
_ "github.com/vmware/govmomi/vapi/cluster/simulator"

Check warning on line 28 in test/helpers/vcsim/builder.go

View workflow job for this annotation

GitHub Actions / lint

blank-imports: a blank import should be only in a main or test package, or have a comment justifying it (revive)
)

type Builder struct {
Expand Down

0 comments on commit 1759ccb

Please sign in to comment.