|
| 1 | +/* |
| 2 | +Copyright 2022 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package clustermodule |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/onsi/gomega" |
| 24 | + corev1 "k8s.io/api/core/v1" |
| 25 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | + clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" |
| 27 | + |
| 28 | + infrav1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/v1beta1" |
| 29 | + "sigs.k8s.io/cluster-api-provider-vsphere/pkg/context/fake" |
| 30 | +) |
| 31 | + |
| 32 | +func TestService_Create(t *testing.T) { |
| 33 | + svc := NewService() |
| 34 | + |
| 35 | + t.Run("creation is skipped", func(t *testing.T) { |
| 36 | + t.Run("when wrapper points to template != VSphereMachineTemplate", func(t *testing.T) { |
| 37 | + md := machineDeployment("md", fake.Namespace, fake.Clusterv1a2Name) |
| 38 | + md.Spec.Template.Spec.InfrastructureRef = corev1.ObjectReference{ |
| 39 | + Kind: "NonVSphereMachineTemplate", |
| 40 | + Namespace: fake.Namespace, |
| 41 | + Name: "blah", |
| 42 | + } |
| 43 | + |
| 44 | + g := gomega.NewWithT(t) |
| 45 | + controllerCtx := fake.NewControllerContext(fake.NewControllerManagerContext(md)) |
| 46 | + ctx := fake.NewClusterContext(controllerCtx) |
| 47 | + |
| 48 | + moduleUUID, err := svc.Create(ctx, mdWrapper{md}) |
| 49 | + g.Expect(err).ToNot(gomega.HaveOccurred()) |
| 50 | + g.Expect(moduleUUID).To(gomega.BeEmpty()) |
| 51 | + }) |
| 52 | + |
| 53 | + t.Run("when template uses a different vCenter URL", func(t *testing.T) { |
| 54 | + md := machineDeployment("md", fake.Namespace, fake.Clusterv1a2Name) |
| 55 | + md.Spec.Template.Spec.InfrastructureRef = corev1.ObjectReference{ |
| 56 | + Kind: "VSphereMachineTemplate", |
| 57 | + Namespace: fake.Namespace, |
| 58 | + Name: "blah-template", |
| 59 | + } |
| 60 | + |
| 61 | + machineTemplate := &infrav1.VSphereMachineTemplate{ |
| 62 | + TypeMeta: metav1.TypeMeta{Kind: "VSphereMachineTemplate"}, |
| 63 | + ObjectMeta: metav1.ObjectMeta{ |
| 64 | + Name: "blah-template", |
| 65 | + Namespace: fake.Namespace, |
| 66 | + }, |
| 67 | + Spec: infrav1.VSphereMachineTemplateSpec{ |
| 68 | + Template: infrav1.VSphereMachineTemplateResource{Spec: infrav1.VSphereMachineSpec{ |
| 69 | + VirtualMachineCloneSpec: infrav1.VirtualMachineCloneSpec{Server: fmt.Sprintf("not.%s", fake.VCenterURL)}, |
| 70 | + }}, |
| 71 | + }, |
| 72 | + } |
| 73 | + |
| 74 | + g := gomega.NewWithT(t) |
| 75 | + controllerCtx := fake.NewControllerContext(fake.NewControllerManagerContext(md, machineTemplate)) |
| 76 | + ctx := fake.NewClusterContext(controllerCtx) |
| 77 | + |
| 78 | + moduleUUID, err := svc.Create(ctx, mdWrapper{md}) |
| 79 | + g.Expect(err).ToNot(gomega.HaveOccurred()) |
| 80 | + g.Expect(moduleUUID).To(gomega.BeEmpty()) |
| 81 | + }) |
| 82 | + }) |
| 83 | +} |
| 84 | + |
| 85 | +func machineDeployment(name, namespace, cluster string) *clusterv1.MachineDeployment { |
| 86 | + return &clusterv1.MachineDeployment{ |
| 87 | + TypeMeta: metav1.TypeMeta{ |
| 88 | + APIVersion: clusterv1.GroupVersion.String(), |
| 89 | + Kind: "MachineDeployment", |
| 90 | + }, |
| 91 | + ObjectMeta: metav1.ObjectMeta{ |
| 92 | + Name: name, |
| 93 | + Namespace: namespace, |
| 94 | + Labels: map[string]string{clusterv1.ClusterLabelName: cluster}, |
| 95 | + }, |
| 96 | + } |
| 97 | +} |
0 commit comments