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 18, 2023
1 parent 783bdaf commit 08da419
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25Kn
github.com/elazarl/goproxy v0.0.0-20170405201442-c4fc26588b6e/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc=
github.com/elazarl/goproxy v0.0.0-20180725130230-947c36da3153/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc=
github.com/emicklei/go-restful v0.0.0-20170410110728-ff4f55a20633/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs=
github.com/emicklei/go-restful v2.9.5+incompatible h1:spTtZBk5DYEvbxMVutUuTyh1Ao2r4iyvLdACqsl/Ljk=
github.com/emicklei/go-restful v2.9.5+incompatible/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs=
github.com/emicklei/go-restful/v3 v3.10.2 h1:hIovbnmBTLjHXkqEBUz3HGpXZdM7ZrE9fJIZIqlJLqE=
github.com/emicklei/go-restful/v3 v3.10.2/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
Expand Down
145 changes: 145 additions & 0 deletions pkg/clustermodule/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ import (
"testing"

"github.com/onsi/gomega"
"github.com/vmware/govmomi/simulator"
_ "github.com/vmware/govmomi/vapi/cluster/simulator"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"

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 +84,148 @@ 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 := initVCSimulator()
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())
})
}

func TestService_DoesExist(t *testing.T) {
t.Run("does exist test", func(t *testing.T) {
g := gomega.NewWithT(t)
simr, err := initVCSimulator()
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())
})
}

func TestService_Remove(t *testing.T) {
t.Run("remove cluster module", func(t *testing.T) {
g := gomega.NewWithT(t)
simr, err := initVCSimulator()
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())
err = svc.Remove(context.Background(), clusterCtx, moduleUUID)
g.Expect(err).ToNot(gomega.HaveOccurred())
})
}

func initVCSimulator() (*vcsim.Simulator, error) {
model := simulator.VPX()
simr, err := vcsim.NewBuilder().
WithModel(model).Build()
if err != nil {
return nil, err
}
return simr, nil
}

func machineDeployment(name, namespace, cluster string) *clusterv1.MachineDeployment {
Expand Down

0 comments on commit 08da419

Please sign in to comment.