|
| 1 | +/* |
| 2 | +Copyright 2020 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 | +// This suite tests volumes under stress conditions |
| 18 | + |
| 19 | +package testsuites |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "sync" |
| 24 | + |
| 25 | + "github.com/onsi/ginkgo" |
| 26 | + |
| 27 | + v1 "k8s.io/api/core/v1" |
| 28 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 29 | + errors "k8s.io/apimachinery/pkg/util/errors" |
| 30 | + clientset "k8s.io/client-go/kubernetes" |
| 31 | + "k8s.io/kubernetes/test/e2e/framework" |
| 32 | + e2epod "k8s.io/kubernetes/test/e2e/framework/pod" |
| 33 | + e2epv "k8s.io/kubernetes/test/e2e/framework/pv" |
| 34 | + e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper" |
| 35 | + "k8s.io/kubernetes/test/e2e/storage/testpatterns" |
| 36 | +) |
| 37 | + |
| 38 | +type stressTestSuite struct { |
| 39 | + tsInfo TestSuiteInfo |
| 40 | +} |
| 41 | + |
| 42 | +type stressTest struct { |
| 43 | + config *PerTestConfig |
| 44 | + driverCleanup func() |
| 45 | + |
| 46 | + intreeOps opCounts |
| 47 | + migratedOps opCounts |
| 48 | + |
| 49 | + resources []*VolumeResource |
| 50 | + pods []*v1.Pod |
| 51 | + // stop and wait for any async routines |
| 52 | + wg sync.WaitGroup |
| 53 | + stopChs []chan struct{} |
| 54 | + |
| 55 | + testOptions StressTestOptions |
| 56 | +} |
| 57 | + |
| 58 | +var _ TestSuite = &stressTestSuite{} |
| 59 | + |
| 60 | +// InitStressTestSuite returns stressTestSuite that implements TestSuite interface |
| 61 | +func InitStressTestSuite() TestSuite { |
| 62 | + return &stressTestSuite{ |
| 63 | + tsInfo: TestSuiteInfo{ |
| 64 | + Name: "stress", |
| 65 | + TestPatterns: []testpatterns.TestPattern{ |
| 66 | + testpatterns.DefaultFsDynamicPV, |
| 67 | + testpatterns.BlockVolModeDynamicPV, |
| 68 | + }, |
| 69 | + }, |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func (t *stressTestSuite) GetTestSuiteInfo() TestSuiteInfo { |
| 74 | + return t.tsInfo |
| 75 | +} |
| 76 | + |
| 77 | +func (t *stressTestSuite) SkipRedundantSuite(driver TestDriver, pattern testpatterns.TestPattern) { |
| 78 | +} |
| 79 | + |
| 80 | +func (t *stressTestSuite) DefineTests(driver TestDriver, pattern testpatterns.TestPattern) { |
| 81 | + var ( |
| 82 | + dInfo = driver.GetDriverInfo() |
| 83 | + cs clientset.Interface |
| 84 | + ) |
| 85 | + |
| 86 | + ginkgo.BeforeEach(func() { |
| 87 | + // Check preconditions. |
| 88 | + if dInfo.StressTestOptions == nil { |
| 89 | + e2eskipper.Skipf("Driver %s doesn't specify stress test options -- skipping", dInfo.Name) |
| 90 | + } |
| 91 | + |
| 92 | + if _, ok := driver.(DynamicPVTestDriver); !ok { |
| 93 | + e2eskipper.Skipf("Driver %s doesn't implement DynamicPVTestDriver -- skipping", dInfo.Name) |
| 94 | + } |
| 95 | + |
| 96 | + if !driver.GetDriverInfo().Capabilities[CapBlock] && pattern.VolMode == v1.PersistentVolumeBlock { |
| 97 | + e2eskipper.Skipf("Driver %q does not support block volume mode - skipping", dInfo.Name) |
| 98 | + } |
| 99 | + }) |
| 100 | + |
| 101 | + // This intentionally comes after checking the preconditions because it |
| 102 | + // registers its own BeforeEach which creates the namespace. Beware that it |
| 103 | + // also registers an AfterEach which renders f unusable. Any code using |
| 104 | + // f must run inside an It or Context callback. |
| 105 | + f := framework.NewDefaultFramework("stress") |
| 106 | + |
| 107 | + init := func() *stressTest { |
| 108 | + cs = f.ClientSet |
| 109 | + l := &stressTest{} |
| 110 | + |
| 111 | + // Now do the more expensive test initialization. |
| 112 | + l.config, l.driverCleanup = driver.PrepareTest(f) |
| 113 | + l.intreeOps, l.migratedOps = getMigrationVolumeOpCounts(f.ClientSet, dInfo.InTreePluginName) |
| 114 | + l.resources = []*VolumeResource{} |
| 115 | + l.pods = []*v1.Pod{} |
| 116 | + l.stopChs = []chan struct{}{} |
| 117 | + l.testOptions = *dInfo.StressTestOptions |
| 118 | + |
| 119 | + return l |
| 120 | + } |
| 121 | + |
| 122 | + cleanup := func(l *stressTest) { |
| 123 | + var errs []error |
| 124 | + |
| 125 | + framework.Logf("Stopping and waiting for all test routines to finish") |
| 126 | + for _, stopCh := range l.stopChs { |
| 127 | + close(stopCh) |
| 128 | + } |
| 129 | + l.wg.Wait() |
| 130 | + |
| 131 | + for _, pod := range l.pods { |
| 132 | + framework.Logf("Deleting pod %v", pod.Name) |
| 133 | + err := e2epod.DeletePodWithWait(cs, pod) |
| 134 | + errs = append(errs, err) |
| 135 | + } |
| 136 | + |
| 137 | + for _, resource := range l.resources { |
| 138 | + errs = append(errs, resource.CleanupResource()) |
| 139 | + } |
| 140 | + |
| 141 | + errs = append(errs, tryFunc(l.driverCleanup)) |
| 142 | + framework.ExpectNoError(errors.NewAggregate(errs), "while cleaning up resource") |
| 143 | + validateMigrationVolumeOpCounts(f.ClientSet, dInfo.InTreePluginName, l.intreeOps, l.migratedOps) |
| 144 | + } |
| 145 | + |
| 146 | + ginkgo.It("multiple pods should access different volumes repeatedly [Slow] [Serial]", func() { |
| 147 | + var err error |
| 148 | + |
| 149 | + l := init() |
| 150 | + defer func() { |
| 151 | + cleanup(l) |
| 152 | + }() |
| 153 | + |
| 154 | + for i := 0; i < l.testOptions.NumPods; i++ { |
| 155 | + framework.Logf("Creating resources for pod %v/%v", i, l.testOptions.NumPods-1) |
| 156 | + r := CreateVolumeResource(driver, l.config, pattern, t.GetTestSuiteInfo().SupportedSizeRange) |
| 157 | + l.resources = append(l.resources, r) |
| 158 | + podConfig := e2epod.Config{ |
| 159 | + NS: f.Namespace.Name, |
| 160 | + PVCs: []*v1.PersistentVolumeClaim{r.Pvc}, |
| 161 | + SeLinuxLabel: e2epv.SELinuxLabel, |
| 162 | + } |
| 163 | + pod, err := e2epod.MakeSecPod(&podConfig) |
| 164 | + framework.ExpectNoError(err) |
| 165 | + |
| 166 | + l.pods = append(l.pods, pod) |
| 167 | + l.stopChs = append(l.stopChs, make(chan struct{})) |
| 168 | + } |
| 169 | + |
| 170 | + // Restart pod repeatedly |
| 171 | + for i := 0; i < l.testOptions.NumPods; i++ { |
| 172 | + podIndex := i |
| 173 | + l.wg.Add(1) |
| 174 | + go func() { |
| 175 | + defer ginkgo.GinkgoRecover() |
| 176 | + defer l.wg.Done() |
| 177 | + for j := 0; j < l.testOptions.NumRestarts; j++ { |
| 178 | + select { |
| 179 | + case <-l.stopChs[podIndex]: |
| 180 | + return |
| 181 | + default: |
| 182 | + pod := l.pods[podIndex] |
| 183 | + framework.Logf("Pod %v, Iteration %v/%v", podIndex, j, l.testOptions.NumRestarts-1) |
| 184 | + _, err = cs.CoreV1().Pods(pod.Namespace).Create(context.TODO(), pod, metav1.CreateOptions{}) |
| 185 | + framework.ExpectNoError(err) |
| 186 | + |
| 187 | + err = e2epod.WaitForPodRunningInNamespace(cs, pod) |
| 188 | + framework.ExpectNoError(err) |
| 189 | + |
| 190 | + // TODO: write data per pod and validate it everytime |
| 191 | + |
| 192 | + err = e2epod.DeletePodWithWait(f.ClientSet, pod) |
| 193 | + framework.ExpectNoError(err) |
| 194 | + } |
| 195 | + } |
| 196 | + }() |
| 197 | + } |
| 198 | + |
| 199 | + l.wg.Wait() |
| 200 | + }) |
| 201 | +} |
0 commit comments