diff --git a/go.sum b/go.sum index 48c3fbae7b..bab19e70e5 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/pkg/clustermodule/service_test.go b/pkg/clustermodule/service_test.go index 8fdaaa1792..14642f5a43 100644 --- a/pkg/clustermodule/service_test.go +++ b/pkg/clustermodule/service_test.go @@ -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) { @@ -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 {