|
| 1 | +/* |
| 2 | +Copyright 2023 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 e2e |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + "path/filepath" |
| 24 | + |
| 25 | + . "github.com/onsi/ginkgo/v2" |
| 26 | + . "github.com/onsi/gomega" |
| 27 | + corev1 "k8s.io/api/core/v1" |
| 28 | + "k8s.io/utils/pointer" |
| 29 | + |
| 30 | + "sigs.k8s.io/cluster-api/test/framework/clusterctl" |
| 31 | + "sigs.k8s.io/cluster-api/util" |
| 32 | +) |
| 33 | + |
| 34 | +// VPCNetworkSpec implements a test that verifies cluster creation in a VPC network. |
| 35 | +func VPCNetworkSpec(ctx context.Context, inputGetter func() CommonSpecInput) { |
| 36 | + var ( |
| 37 | + specName = "vpc-network" |
| 38 | + input CommonSpecInput |
| 39 | + namespace *corev1.Namespace |
| 40 | + cancelWatches context.CancelFunc |
| 41 | + clusterResources *clusterctl.ApplyClusterTemplateAndWaitResult |
| 42 | + vpcName = "vpc-for-e2e-1" |
| 43 | + ) |
| 44 | + |
| 45 | + BeforeEach(func() { |
| 46 | + Expect(ctx).NotTo(BeNil(), "ctx is required for %s spec", specName) |
| 47 | + input = inputGetter() |
| 48 | + Expect(input.E2EConfig).ToNot(BeNil(), "Invalid argument. input.E2EConfig can't be nil when calling %s spec", specName) |
| 49 | + Expect(input.ClusterctlConfigPath).To(BeAnExistingFile(), "Invalid argument. input.ClusterctlConfigPath must be an existing file when calling %s spec", specName) |
| 50 | + Expect(input.BootstrapClusterProxy).ToNot(BeNil(), "Invalid argument. input.BootstrapClusterProxy can't be nil when calling %s spec", specName) |
| 51 | + Expect(os.MkdirAll(input.ArtifactFolder, 0750)).To(Succeed(), "Invalid argument. input.ArtifactFolder can't be created for %s spec", specName) |
| 52 | + Expect(input.E2EConfig.Variables).To(HaveKey(KubernetesVersion)) |
| 53 | + Expect(input.E2EConfig.Variables).To(HaveValidVersion(input.E2EConfig.GetVariable(KubernetesVersion))) |
| 54 | + |
| 55 | + // Setup a Namespace where to host objects for this spec and create a watcher for the namespace events. |
| 56 | + namespace, cancelWatches = setupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder) |
| 57 | + clusterResources = new(clusterctl.ApplyClusterTemplateAndWaitResult) |
| 58 | + }) |
| 59 | + |
| 60 | + It("Should successfully create a cluster in a VPC network", func() { |
| 61 | + By("Creating a workload cluster in a VPC network") |
| 62 | + |
| 63 | + clusterctl.ApplyClusterTemplateAndWait(ctx, clusterctl.ApplyClusterTemplateAndWaitInput{ |
| 64 | + ClusterProxy: input.BootstrapClusterProxy, |
| 65 | + CNIManifestPath: input.E2EConfig.GetVariable(CNIPath), |
| 66 | + ConfigCluster: clusterctl.ConfigClusterInput{ |
| 67 | + LogFolder: filepath.Join(input.ArtifactFolder, "clusters", input.BootstrapClusterProxy.GetName()), |
| 68 | + ClusterctlConfigPath: input.ClusterctlConfigPath, |
| 69 | + KubeconfigPath: input.BootstrapClusterProxy.GetKubeconfigPath(), |
| 70 | + InfrastructureProvider: clusterctl.DefaultInfrastructureProvider, |
| 71 | + Flavor: specName, |
| 72 | + Namespace: namespace.Name, |
| 73 | + ClusterName: fmt.Sprintf("%s-%s", specName, util.RandomString(6)), |
| 74 | + KubernetesVersion: input.E2EConfig.GetVariable(KubernetesVersion), |
| 75 | + ControlPlaneMachineCount: pointer.Int64Ptr(3), |
| 76 | + WorkerMachineCount: pointer.Int64Ptr(2), |
| 77 | + }, |
| 78 | + WaitForClusterIntervals: input.E2EConfig.GetIntervals(specName, "wait-cluster"), |
| 79 | + WaitForControlPlaneIntervals: input.E2EConfig.GetIntervals(specName, "wait-control-plane"), |
| 80 | + WaitForMachineDeployments: input.E2EConfig.GetIntervals(specName, "wait-worker-nodes"), |
| 81 | + }, clusterResources) |
| 82 | + |
| 83 | + csClient := CreateCloudStackClient(ctx, input.BootstrapClusterProxy.GetKubeconfigPath()) |
| 84 | + |
| 85 | + exists, err := CheckVPCExists(csClient, vpcName) |
| 86 | + Expect(err).To(BeNil()) |
| 87 | + Expect(exists).To(BeTrue()) |
| 88 | + |
| 89 | + By("PASSED!") |
| 90 | + }) |
| 91 | + |
| 92 | + AfterEach(func() { |
| 93 | + // Dumps all the resources in the spec namespace, then cleanups the cluster object and the spec namespace itself. |
| 94 | + dumpSpecResourcesAndCleanup(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, namespace, cancelWatches, clusterResources.Cluster, input.E2EConfig.GetIntervals, input.SkipCleanup) |
| 95 | + }) |
| 96 | +} |
0 commit comments