|
| 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 e2e |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + "path/filepath" |
| 24 | + |
| 25 | + . "github.com/onsi/ginkgo" |
| 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 | +// TwoClustersSpec implements a test that verifies two clusters can co-exist. |
| 35 | +func TwoClustersSpec(ctx context.Context, inputGetter func() CommonSpecInput) { |
| 36 | + |
| 37 | + const specName = "two-clusters" |
| 38 | + |
| 39 | + var ( |
| 40 | + input CommonSpecInput |
| 41 | + namespace1 *corev1.Namespace |
| 42 | + namespace2 *corev1.Namespace |
| 43 | + cancelWatches1 context.CancelFunc |
| 44 | + cancelWatches2 context.CancelFunc |
| 45 | + clusterResources1 *clusterctl.ApplyClusterTemplateAndWaitResult |
| 46 | + clusterResources2 *clusterctl.ApplyClusterTemplateAndWaitResult |
| 47 | + ) |
| 48 | + |
| 49 | + createCluster := func(flavor string, namespace *corev1.Namespace, resources *clusterctl.ApplyClusterTemplateAndWaitResult) { |
| 50 | + clusterctl.ApplyClusterTemplateAndWait(ctx, clusterctl.ApplyClusterTemplateAndWaitInput{ |
| 51 | + ClusterProxy: input.BootstrapClusterProxy, |
| 52 | + CNIManifestPath: input.E2EConfig.GetVariable(CNIPath), |
| 53 | + ConfigCluster: clusterctl.ConfigClusterInput{ |
| 54 | + LogFolder: filepath.Join(input.ArtifactFolder, "clusters", input.BootstrapClusterProxy.GetName()), |
| 55 | + ClusterctlConfigPath: input.ClusterctlConfigPath, |
| 56 | + KubeconfigPath: input.BootstrapClusterProxy.GetKubeconfigPath(), |
| 57 | + InfrastructureProvider: clusterctl.DefaultInfrastructureProvider, |
| 58 | + Flavor: flavor, |
| 59 | + Namespace: namespace.Name, |
| 60 | + ClusterName: fmt.Sprintf("%s-%s", specName, util.RandomString(6)), |
| 61 | + KubernetesVersion: input.E2EConfig.GetVariable(KubernetesVersion), |
| 62 | + ControlPlaneMachineCount: pointer.Int64Ptr(1), |
| 63 | + WorkerMachineCount: pointer.Int64Ptr(1), |
| 64 | + }, |
| 65 | + WaitForClusterIntervals: input.E2EConfig.GetIntervals(specName, "wait-cluster"), |
| 66 | + WaitForControlPlaneIntervals: input.E2EConfig.GetIntervals(specName, "wait-control-plane"), |
| 67 | + WaitForMachineDeployments: input.E2EConfig.GetIntervals(specName, "wait-worker-nodes"), |
| 68 | + }, resources) |
| 69 | + } |
| 70 | + |
| 71 | + BeforeEach(func() { |
| 72 | + Expect(ctx).NotTo(BeNil(), "ctx is required for %s spec", specName) |
| 73 | + input = inputGetter() |
| 74 | + Expect(input.E2EConfig).ToNot(BeNil(), "Invalid argument. input.E2EConfig can't be nil when calling %s spec", specName) |
| 75 | + Expect(input.ClusterctlConfigPath).To(BeAnExistingFile(), "Invalid argument. input.ClusterctlConfigPath must be an existing file when calling %s spec", specName) |
| 76 | + Expect(input.BootstrapClusterProxy).ToNot(BeNil(), "Invalid argument. input.BootstrapClusterProxy can't be nil when calling %s spec", specName) |
| 77 | + Expect(os.MkdirAll(input.ArtifactFolder, 0750)).To(Succeed(), "Invalid argument. input.ArtifactFolder can't be created for %s spec", specName) |
| 78 | + Expect(input.E2EConfig.Variables).To(HaveKey(KubernetesVersion)) |
| 79 | + Expect(input.E2EConfig.Variables).To(HaveValidVersion(input.E2EConfig.GetVariable(KubernetesVersion))) |
| 80 | + |
| 81 | + // Set up namespaces to host objects for this spec and create watchers for the namespace events. |
| 82 | + namespace1, cancelWatches1 = setupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder) |
| 83 | + namespace2, cancelWatches2 = setupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder) |
| 84 | + clusterResources1 = new(clusterctl.ApplyClusterTemplateAndWaitResult) |
| 85 | + clusterResources2 = new(clusterctl.ApplyClusterTemplateAndWaitResult) |
| 86 | + }) |
| 87 | + |
| 88 | + It("should successfully add and remove a second cluster without breaking the first cluster", func() { |
| 89 | + mgmtClient := input.BootstrapClusterProxy.GetClient() |
| 90 | + |
| 91 | + By("Create the first cluster and verify that it's ready") |
| 92 | + createCluster(clusterctl.DefaultFlavor, namespace1, clusterResources1) |
| 93 | + Expect(IsClusterReady(ctx, mgmtClient, clusterResources1.Cluster)).To(BeTrue()) |
| 94 | + |
| 95 | + By("Create the second cluster and verify that it's ready") |
| 96 | + createCluster("second-cluster", namespace2, clusterResources2) |
| 97 | + Expect(IsClusterReady(ctx, mgmtClient, clusterResources2.Cluster)).To(BeTrue()) |
| 98 | + |
| 99 | + By("Delete the second cluster") |
| 100 | + dumpSpecResourcesAndCleanup(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, namespace2, |
| 101 | + cancelWatches2, clusterResources2.Cluster, input.E2EConfig.GetIntervals, false) |
| 102 | + |
| 103 | + By("Verify the second cluster is gone") |
| 104 | + Expect(ClusterExists(ctx, mgmtClient, clusterResources2.Cluster)).To(BeFalse()) |
| 105 | + |
| 106 | + By("Verify the first cluster is still ready") |
| 107 | + Expect(IsClusterReady(ctx, mgmtClient, clusterResources1.Cluster)).To(BeTrue()) |
| 108 | + |
| 109 | + By("PASSED!") |
| 110 | + }) |
| 111 | + |
| 112 | + AfterEach(func() { |
| 113 | + // Dumps all the resources in the spec namespace, then cleanups the cluster object and the spec namespace itself. |
| 114 | + dumpSpecResourcesAndCleanup(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, namespace1, cancelWatches1, clusterResources1.Cluster, input.E2EConfig.GetIntervals, input.SkipCleanup) |
| 115 | + if clusterResources2.Cluster != nil && ClusterExists(ctx, input.BootstrapClusterProxy.GetClient(), clusterResources2.Cluster) { |
| 116 | + dumpSpecResourcesAndCleanup(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, namespace2, cancelWatches2, clusterResources2.Cluster, input.E2EConfig.GetIntervals, input.SkipCleanup) |
| 117 | + } |
| 118 | + }) |
| 119 | +} |
0 commit comments