|
| 1 | +/* |
| 2 | +Copyright 2021 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 controllers |
| 18 | + |
| 19 | +import ( |
| 20 | + goctx "context" |
| 21 | + "time" |
| 22 | + |
| 23 | + . "github.com/onsi/ginkgo" |
| 24 | + . "github.com/onsi/gomega" |
| 25 | + |
| 26 | + corev1 "k8s.io/api/core/v1" |
| 27 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 28 | + clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3" |
| 29 | + "sigs.k8s.io/cluster-api/util/patch" |
| 30 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 31 | + |
| 32 | + infrav1 "sigs.k8s.io/cluster-api-provider-vsphere/api/v1alpha3" |
| 33 | +) |
| 34 | + |
| 35 | +const ( |
| 36 | + timeout = time.Second * 30 |
| 37 | +) |
| 38 | + |
| 39 | +var _ = Describe("ClusterReconciler", func() { |
| 40 | + BeforeEach(func() {}) |
| 41 | + AfterEach(func() {}) |
| 42 | + |
| 43 | + Context("Reconcile an VSphereCluster", func() { |
| 44 | + It("should create a cluster", func() { |
| 45 | + ctx := goctx.Background() |
| 46 | + |
| 47 | + capiCluster := &clusterv1.Cluster{ |
| 48 | + ObjectMeta: metav1.ObjectMeta{ |
| 49 | + GenerateName: "test1-", |
| 50 | + Namespace: "default", |
| 51 | + }, |
| 52 | + Spec: clusterv1.ClusterSpec{ |
| 53 | + InfrastructureRef: &corev1.ObjectReference{ |
| 54 | + APIVersion: "infrastructure.cluster.x-k8s.io/v1alpha4", |
| 55 | + Kind: "VsphereCluster", |
| 56 | + Name: "vsphere-test1", |
| 57 | + }, |
| 58 | + }, |
| 59 | + } |
| 60 | + // Create the CAPI cluster (owner) object |
| 61 | + Expect(testEnv.Create(ctx, capiCluster)).To(Succeed()) |
| 62 | + |
| 63 | + instance := &infrav1.VSphereCluster{ |
| 64 | + ObjectMeta: metav1.ObjectMeta{ |
| 65 | + Name: "vsphere-test1", |
| 66 | + Namespace: "default", |
| 67 | + }, |
| 68 | + Spec: infrav1.VSphereClusterSpec{}, |
| 69 | + } |
| 70 | + |
| 71 | + // Create the VSphereCluster object |
| 72 | + Expect(testEnv.Create(ctx, instance)).To(Succeed()) |
| 73 | + key := client.ObjectKey{Namespace: instance.Namespace, Name: instance.Name} |
| 74 | + defer func() { |
| 75 | + err := testEnv.Delete(ctx, instance) |
| 76 | + Expect(err).NotTo(HaveOccurred()) |
| 77 | + }() |
| 78 | + |
| 79 | + // Make sure the VSphereCluster exists. |
| 80 | + Eventually(func() bool { |
| 81 | + err := testEnv.Get(ctx, key, instance) |
| 82 | + return err == nil |
| 83 | + }, timeout).Should(BeTrue()) |
| 84 | + |
| 85 | + By("setting the OwnerRef on the VSphereCluster") |
| 86 | + Eventually(func() bool { |
| 87 | + ph, err := patch.NewHelper(instance, testEnv) |
| 88 | + Expect(err).ShouldNot(HaveOccurred()) |
| 89 | + instance.OwnerReferences = append(instance.OwnerReferences, metav1.OwnerReference{Kind: "Cluster", APIVersion: clusterv1.GroupVersion.String(), Name: capiCluster.Name, UID: "blah"}) |
| 90 | + Expect(ph.Patch(ctx, instance, patch.WithStatusObservedGeneration{})).ShouldNot(HaveOccurred()) |
| 91 | + return true |
| 92 | + }, timeout).Should(BeTrue()) |
| 93 | + |
| 94 | + Eventually(func() bool { |
| 95 | + if err := testEnv.Get(ctx, key, instance); err != nil { |
| 96 | + return false |
| 97 | + } |
| 98 | + return len(instance.Finalizers) > 0 |
| 99 | + }, timeout).Should(BeTrue()) |
| 100 | + }) |
| 101 | + }) |
| 102 | +}) |
0 commit comments