|
| 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 e2e |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + "path/filepath" |
| 24 | + "runtime" |
| 25 | + "time" |
| 26 | + |
| 27 | + . "github.com/onsi/ginkgo" |
| 28 | + . "github.com/onsi/gomega" |
| 29 | + corev1 "k8s.io/api/core/v1" |
| 30 | + "k8s.io/utils/pointer" |
| 31 | + "sigs.k8s.io/cluster-api-provider-cloudstack-staging/test/e2e/helpers" |
| 32 | + "sigs.k8s.io/cluster-api-provider-cloudstack-staging/test/e2e/toxiproxy" |
| 33 | + "sigs.k8s.io/cluster-api/test/framework/clusterctl" |
| 34 | + "sigs.k8s.io/cluster-api/util" |
| 35 | +) |
| 36 | + |
| 37 | +// NetworkInterruptionToxiSpec implements a test that verifies that an app deployed to the workload cluster works. |
| 38 | +func NetworkInterruptionToxiSpec(ctx context.Context, inputGetter func() CommonSpecInput) { |
| 39 | + var ( |
| 40 | + specName = "network-interruption-toxi" |
| 41 | + input CommonSpecInput |
| 42 | + namespace *corev1.Namespace |
| 43 | + cancelWatches context.CancelFunc |
| 44 | + clusterResources *clusterctl.ApplyClusterTemplateAndWaitResult |
| 45 | + cloudStackToxiProxyContext *toxiproxy.Context |
| 46 | + clusterName = fmt.Sprintf("%s-%s", specName, util.RandomString(6)) |
| 47 | + networkInterruptorShutdownChannel = make(chan bool, 2) |
| 48 | + ) |
| 49 | + |
| 50 | + BeforeEach(func() { |
| 51 | + // ToxiProxy running in a docker container requires docker host networking, only available in linux. |
| 52 | + Expect(runtime.GOOS).To(Equal("linux")) |
| 53 | + |
| 54 | + Expect(ctx).NotTo(BeNil(), "ctx is required for %s spec", specName) |
| 55 | + input = inputGetter() |
| 56 | + Expect(input.E2EConfig).ToNot(BeNil(), "Invalid argument. input.E2EConfig can't be nil when calling %s spec", specName) |
| 57 | + Expect(input.ClusterctlConfigPath).To(BeAnExistingFile(), "Invalid argument. input.ClusterctlConfigPath must be an existing file when calling %s spec", specName) |
| 58 | + Expect(input.BootstrapClusterProxy).ToNot(BeNil(), "Invalid argument. input.BootstrapClusterProxy can't be nil when calling %s spec", specName) |
| 59 | + Expect(os.MkdirAll(input.ArtifactFolder, 0750)).To(Succeed(), "Invalid argument. input.ArtifactFolder can't be created for %s spec", specName) |
| 60 | + Expect(input.E2EConfig.Variables).To(HaveKey(KubernetesVersion)) |
| 61 | + |
| 62 | + // Set up a toxiProxy for CloudStack |
| 63 | + cloudStackToxiProxyContext = toxiproxy.SetupForToxiProxyTestingACS( |
| 64 | + ctx, |
| 65 | + clusterName, |
| 66 | + input.BootstrapClusterProxy, |
| 67 | + input.E2EConfig, |
| 68 | + input.ClusterctlConfigPath, |
| 69 | + ) |
| 70 | + |
| 71 | + // Set up a Namespace to host objects for this spec and create a watcher for the namespace events. |
| 72 | + namespace, cancelWatches = setupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder) |
| 73 | + clusterResources = new(clusterctl.ApplyClusterTemplateAndWaitResult) |
| 74 | + |
| 75 | + }) |
| 76 | + |
| 77 | + It("Should be able to create a cluster despite a network interruption during that process", func() { |
| 78 | + By("Creating a workload cluster") |
| 79 | + |
| 80 | + flavor := clusterctl.DefaultFlavor |
| 81 | + if input.Flavor != nil { |
| 82 | + flavor = *input.Flavor |
| 83 | + } |
| 84 | + namespace := namespace.Name |
| 85 | + |
| 86 | + // While I'd prefer to closely synchronize the network interruption (ToxiProxy disable) to a particular point in the cluster provisioning |
| 87 | + // process, doing so for this asynchronously running process would be harder and more impactful than I can tackle right now. So I'm going |
| 88 | + // to give CAPC a short period to get started with the provisioning, and then interrupt the network for a fixed time, and then restore it. |
| 89 | + // CAPC should tolerate this and ultimately succeed. |
| 90 | + // To do this while ApplyClusterTemplateAndWait() is waiting, I'm going to use a concurrent goroutine and an interruptible version of sleep |
| 91 | + // so it can be shut down cleanly. |
| 92 | + go networkInterruptor(cloudStackToxiProxyContext, networkInterruptorShutdownChannel) |
| 93 | + |
| 94 | + clusterctl.ApplyClusterTemplateAndWait(ctx, clusterctl.ApplyClusterTemplateAndWaitInput{ |
| 95 | + ClusterProxy: input.BootstrapClusterProxy, |
| 96 | + CNIManifestPath: input.E2EConfig.GetVariable(CNIPath), |
| 97 | + ConfigCluster: clusterctl.ConfigClusterInput{ |
| 98 | + LogFolder: filepath.Join(input.ArtifactFolder, "clusters", input.BootstrapClusterProxy.GetName()), |
| 99 | + ClusterctlConfigPath: cloudStackToxiProxyContext.ConfigPath, |
| 100 | + KubeconfigPath: input.BootstrapClusterProxy.GetKubeconfigPath(), |
| 101 | + InfrastructureProvider: clusterctl.DefaultInfrastructureProvider, |
| 102 | + Flavor: flavor, |
| 103 | + Namespace: namespace, |
| 104 | + ClusterName: clusterName, |
| 105 | + KubernetesVersion: input.E2EConfig.GetVariable(KubernetesVersion), |
| 106 | + ControlPlaneMachineCount: pointer.Int64Ptr(1), |
| 107 | + WorkerMachineCount: pointer.Int64Ptr(2), |
| 108 | + }, |
| 109 | + WaitForClusterIntervals: input.E2EConfig.GetIntervals(specName, "wait-cluster"), |
| 110 | + WaitForControlPlaneIntervals: input.E2EConfig.GetIntervals(specName, "wait-control-plane"), |
| 111 | + WaitForMachineDeployments: input.E2EConfig.GetIntervals(specName, "wait-worker-nodes"), |
| 112 | + }, clusterResources) |
| 113 | + |
| 114 | + By("PASSED!") |
| 115 | + }) |
| 116 | + |
| 117 | + AfterEach(func() { |
| 118 | + // Stop the networkInterruptor (in case it's still running because tests failed before it completed) |
| 119 | + networkInterruptorShutdownChannel <- true |
| 120 | + |
| 121 | + // Dumps all the resources in the spec namespace, then cleanups the cluster object and the spec namespace itself. |
| 122 | + dumpSpecResourcesAndCleanup(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, namespace, cancelWatches, clusterResources.Cluster, input.E2EConfig.GetIntervals, input.SkipCleanup) |
| 123 | + |
| 124 | + // Tear down the ToxiProxies |
| 125 | + toxiproxy.TearDownToxiProxyACS(ctx, input.BootstrapClusterProxy, cloudStackToxiProxyContext) |
| 126 | + }) |
| 127 | +} |
| 128 | + |
| 129 | +func networkInterruptor(toxiProxyContext *toxiproxy.Context, shutdownChannel chan bool) { |
| 130 | + for { |
| 131 | + // Wait for ApplyClusterTemplateAndWait() to make some progress |
| 132 | + helpers.InterruptibleSleep(15*time.Second, time.Second, shutdownChannel) |
| 133 | + |
| 134 | + // Disable communications to ACS |
| 135 | + toxiProxyContext.Disable() |
| 136 | + |
| 137 | + // Leave the network disabled for some period of time |
| 138 | + helpers.InterruptibleSleep(30*time.Second, time.Second, shutdownChannel) |
| 139 | + |
| 140 | + // Restore communications to ACS |
| 141 | + toxiProxyContext.Enable() |
| 142 | + } |
| 143 | +} |
0 commit comments