Commit ec3655a1 authored by Patrick Ohly's avatar Patrick Ohly

e2e/storage: re-initialize all local variables

There is a risk that the init function does not reset one of the local variables that was set by a previous test. To avoid this, all variables set by init are now in a struct which gets cleaned completely first.
parent e79cd9ef
...@@ -80,15 +80,19 @@ func (p *provisioningTestSuite) getTestSuiteInfo() TestSuiteInfo { ...@@ -80,15 +80,19 @@ func (p *provisioningTestSuite) getTestSuiteInfo() TestSuiteInfo {
} }
func (p *provisioningTestSuite) defineTests(driver TestDriver, pattern testpatterns.TestPattern) { func (p *provisioningTestSuite) defineTests(driver TestDriver, pattern testpatterns.TestPattern) {
var ( type local struct {
dInfo = driver.GetDriverInfo()
dDriver DynamicPVTestDriver
config *PerTestConfig config *PerTestConfig
testCleanup func() testCleanup func()
testCase *StorageClassTest
cs clientset.Interface testCase *StorageClassTest
pvc *v1.PersistentVolumeClaim cs clientset.Interface
sc *storage.StorageClass pvc *v1.PersistentVolumeClaim
sc *storage.StorageClass
}
var (
dInfo = driver.GetDriverInfo()
dDriver DynamicPVTestDriver
l local
) )
BeforeEach(func() { BeforeEach(func() {
...@@ -110,30 +114,32 @@ func (p *provisioningTestSuite) defineTests(driver TestDriver, pattern testpatte ...@@ -110,30 +114,32 @@ func (p *provisioningTestSuite) defineTests(driver TestDriver, pattern testpatte
f := framework.NewDefaultFramework("provisioning") f := framework.NewDefaultFramework("provisioning")
init := func() { init := func() {
l = local{}
// Now do the more expensive test initialization. // Now do the more expensive test initialization.
config, testCleanup = driver.PrepareTest(f) l.config, l.testCleanup = driver.PrepareTest(f)
cs = config.Framework.ClientSet l.cs = l.config.Framework.ClientSet
claimSize := dDriver.GetClaimSize() claimSize := dDriver.GetClaimSize()
sc = dDriver.GetDynamicProvisionStorageClass(config, "") l.sc = dDriver.GetDynamicProvisionStorageClass(l.config, "")
if sc == nil { if l.sc == nil {
framework.Skipf("Driver %q does not define Dynamic Provision StorageClass - skipping", dInfo.Name) framework.Skipf("Driver %q does not define Dynamic Provision StorageClass - skipping", dInfo.Name)
} }
pvc = getClaim(claimSize, config.Framework.Namespace.Name) l.pvc = getClaim(claimSize, l.config.Framework.Namespace.Name)
pvc.Spec.StorageClassName = &sc.Name l.pvc.Spec.StorageClassName = &l.sc.Name
framework.Logf("In creating storage class object and pvc object for driver - sc: %v, pvc: %v", sc, pvc) framework.Logf("In creating storage class object and pvc object for driver - sc: %v, pvc: %v", l.sc, l.pvc)
testCase = &StorageClassTest{ l.testCase = &StorageClassTest{
Client: config.Framework.ClientSet, Client: l.config.Framework.ClientSet,
Claim: pvc, Claim: l.pvc,
Class: sc, Class: l.sc,
ClaimSize: claimSize, ClaimSize: claimSize,
ExpectedSize: claimSize, ExpectedSize: claimSize,
} }
} }
cleanup := func() { cleanup := func() {
if testCleanup != nil { if l.testCleanup != nil {
testCleanup() l.testCleanup()
testCleanup = nil l.testCleanup = nil
} }
} }
...@@ -141,7 +147,7 @@ func (p *provisioningTestSuite) defineTests(driver TestDriver, pattern testpatte ...@@ -141,7 +147,7 @@ func (p *provisioningTestSuite) defineTests(driver TestDriver, pattern testpatte
init() init()
defer cleanup() defer cleanup()
testCase.TestDynamicProvisioning() l.testCase.TestDynamicProvisioning()
}) })
It("should provision storage with mount options", func() { It("should provision storage with mount options", func() {
...@@ -152,8 +158,8 @@ func (p *provisioningTestSuite) defineTests(driver TestDriver, pattern testpatte ...@@ -152,8 +158,8 @@ func (p *provisioningTestSuite) defineTests(driver TestDriver, pattern testpatte
init() init()
defer cleanup() defer cleanup()
testCase.Class.MountOptions = dInfo.SupportedMountOption.Union(dInfo.RequiredMountOption).List() l.testCase.Class.MountOptions = dInfo.SupportedMountOption.Union(dInfo.RequiredMountOption).List()
testCase.TestDynamicProvisioning() l.testCase.TestDynamicProvisioning()
}) })
It("should access volume from different nodes", func() { It("should access volume from different nodes", func() {
...@@ -164,19 +170,19 @@ func (p *provisioningTestSuite) defineTests(driver TestDriver, pattern testpatte ...@@ -164,19 +170,19 @@ func (p *provisioningTestSuite) defineTests(driver TestDriver, pattern testpatte
// locked onto a single node, then the driver is // locked onto a single node, then the driver is
// usable on all of them *and* supports accessing a volume // usable on all of them *and* supports accessing a volume
// from any node. // from any node.
if config.ClientNodeName != "" { if l.config.ClientNodeName != "" {
framework.Skipf("Driver %q only supports testing on one node - skipping", dInfo.Name) framework.Skipf("Driver %q only supports testing on one node - skipping", dInfo.Name)
} }
// Ensure that we actually have more than one node. // Ensure that we actually have more than one node.
nodes := framework.GetReadySchedulableNodesOrDie(cs) nodes := framework.GetReadySchedulableNodesOrDie(l.cs)
if len(nodes.Items) <= 1 { if len(nodes.Items) <= 1 {
framework.Skipf("need more than one node - skipping") framework.Skipf("need more than one node - skipping")
} }
testCase.PvCheck = func(claim *v1.PersistentVolumeClaim, volume *v1.PersistentVolume) { l.testCase.PvCheck = func(claim *v1.PersistentVolumeClaim, volume *v1.PersistentVolume) {
PVMultiNodeCheck(cs, claim, volume, NodeSelection{Name: config.ClientNodeName}) PVMultiNodeCheck(l.cs, claim, volume, NodeSelection{Name: l.config.ClientNodeName})
} }
testCase.TestDynamicProvisioning() l.testCase.TestDynamicProvisioning()
}) })
It("should create and delete block persistent volumes", func() { It("should create and delete block persistent volumes", func() {
...@@ -188,9 +194,9 @@ func (p *provisioningTestSuite) defineTests(driver TestDriver, pattern testpatte ...@@ -188,9 +194,9 @@ func (p *provisioningTestSuite) defineTests(driver TestDriver, pattern testpatte
defer cleanup() defer cleanup()
block := v1.PersistentVolumeBlock block := v1.PersistentVolumeBlock
testCase.VolumeMode = &block l.testCase.VolumeMode = &block
pvc.Spec.VolumeMode = &block l.pvc.Spec.VolumeMode = &block
testCase.TestDynamicProvisioning() l.testCase.TestDynamicProvisioning()
}) })
It("should provision storage with snapshot data source [Feature:VolumeSnapshotDataSource]", func() { It("should provision storage with snapshot data source [Feature:VolumeSnapshotDataSource]", func() {
...@@ -206,18 +212,18 @@ func (p *provisioningTestSuite) defineTests(driver TestDriver, pattern testpatte ...@@ -206,18 +212,18 @@ func (p *provisioningTestSuite) defineTests(driver TestDriver, pattern testpatte
init() init()
defer cleanup() defer cleanup()
dc := config.Framework.DynamicClient dc := l.config.Framework.DynamicClient
vsc := sDriver.GetSnapshotClass(config) vsc := sDriver.GetSnapshotClass(l.config)
dataSource, cleanupFunc := prepareDataSourceForProvisioning(NodeSelection{Name: config.ClientNodeName}, cs, dc, pvc, sc, vsc) dataSource, cleanupFunc := prepareDataSourceForProvisioning(NodeSelection{Name: l.config.ClientNodeName}, l.cs, dc, l.pvc, l.sc, vsc)
defer cleanupFunc() defer cleanupFunc()
pvc.Spec.DataSource = dataSource l.pvc.Spec.DataSource = dataSource
testCase.PvCheck = func(claim *v1.PersistentVolumeClaim, volume *v1.PersistentVolume) { l.testCase.PvCheck = func(claim *v1.PersistentVolumeClaim, volume *v1.PersistentVolume) {
By("checking whether the created volume has the pre-populated data") By("checking whether the created volume has the pre-populated data")
command := fmt.Sprintf("grep '%s' /mnt/test/initialData", claim.Namespace) command := fmt.Sprintf("grep '%s' /mnt/test/initialData", claim.Namespace)
RunInPodWithVolume(cs, claim.Namespace, claim.Name, "pvc-snapshot-tester", command, NodeSelection{Name: config.ClientNodeName}) RunInPodWithVolume(l.cs, claim.Namespace, claim.Name, "pvc-snapshot-tester", command, NodeSelection{Name: l.config.ClientNodeName})
} }
testCase.TestDynamicProvisioning() l.testCase.TestDynamicProvisioning()
}) })
It("should allow concurrent writes on the same node", func() { It("should allow concurrent writes on the same node", func() {
...@@ -228,7 +234,7 @@ func (p *provisioningTestSuite) defineTests(driver TestDriver, pattern testpatte ...@@ -228,7 +234,7 @@ func (p *provisioningTestSuite) defineTests(driver TestDriver, pattern testpatte
init() init()
defer cleanup() defer cleanup()
testCase.PvCheck = func(claim *v1.PersistentVolumeClaim, volume *v1.PersistentVolume) { l.testCase.PvCheck = func(claim *v1.PersistentVolumeClaim, volume *v1.PersistentVolume) {
// We start two pods concurrently on the same node, // We start two pods concurrently on the same node,
// using the same PVC. Both wait for other to create a // using the same PVC. Both wait for other to create a
// file before returning. The pods are forced onto the // file before returning. The pods are forced onto the
...@@ -241,7 +247,7 @@ func (p *provisioningTestSuite) defineTests(driver TestDriver, pattern testpatte ...@@ -241,7 +247,7 @@ func (p *provisioningTestSuite) defineTests(driver TestDriver, pattern testpatte
defer GinkgoRecover() defer GinkgoRecover()
defer wg.Done() defer wg.Done()
node := NodeSelection{ node := NodeSelection{
Name: config.ClientNodeName, Name: l.config.ClientNodeName,
} }
if podName == secondPodName { if podName == secondPodName {
node.Affinity = &v1.Affinity{ node.Affinity = &v1.Affinity{
...@@ -259,13 +265,13 @@ func (p *provisioningTestSuite) defineTests(driver TestDriver, pattern testpatte ...@@ -259,13 +265,13 @@ func (p *provisioningTestSuite) defineTests(driver TestDriver, pattern testpatte
}, },
} }
} }
RunInPodWithVolume(cs, claim.Namespace, claim.Name, podName, command, node) RunInPodWithVolume(l.cs, claim.Namespace, claim.Name, podName, command, node)
} }
go run(firstPodName, "touch /mnt/test/first && while ! [ -f /mnt/test/second ]; do sleep 1; done") go run(firstPodName, "touch /mnt/test/first && while ! [ -f /mnt/test/second ]; do sleep 1; done")
go run(secondPodName, "touch /mnt/test/second && while ! [ -f /mnt/test/first ]; do sleep 1; done") go run(secondPodName, "touch /mnt/test/second && while ! [ -f /mnt/test/first ]; do sleep 1; done")
wg.Wait() wg.Wait()
} }
testCase.TestDynamicProvisioning() l.testCase.TestDynamicProvisioning()
}) })
} }
......
...@@ -75,11 +75,15 @@ func (t *volumeIOTestSuite) getTestSuiteInfo() TestSuiteInfo { ...@@ -75,11 +75,15 @@ func (t *volumeIOTestSuite) getTestSuiteInfo() TestSuiteInfo {
} }
func (t *volumeIOTestSuite) defineTests(driver TestDriver, pattern testpatterns.TestPattern) { func (t *volumeIOTestSuite) defineTests(driver TestDriver, pattern testpatterns.TestPattern) {
var ( type local struct {
dInfo = driver.GetDriverInfo()
config *PerTestConfig config *PerTestConfig
testCleanup func() testCleanup func()
resource *genericVolumeTestResource
resource *genericVolumeTestResource
}
var (
dInfo = driver.GetDriverInfo()
l local
) )
// No preconditions to test. Normally they would be in a BeforeEach here. // No preconditions to test. Normally they would be in a BeforeEach here.
...@@ -91,23 +95,25 @@ func (t *volumeIOTestSuite) defineTests(driver TestDriver, pattern testpatterns. ...@@ -91,23 +95,25 @@ func (t *volumeIOTestSuite) defineTests(driver TestDriver, pattern testpatterns.
f := framework.NewDefaultFramework("volumeio") f := framework.NewDefaultFramework("volumeio")
init := func() { init := func() {
l = local{}
// Now do the more expensive test initialization. // Now do the more expensive test initialization.
config, testCleanup = driver.PrepareTest(f) l.config, l.testCleanup = driver.PrepareTest(f)
resource = createGenericVolumeTestResource(driver, config, pattern) l.resource = createGenericVolumeTestResource(driver, l.config, pattern)
if resource.volSource == nil { if l.resource.volSource == nil {
framework.Skipf("Driver %q does not define volumeSource - skipping", dInfo.Name) framework.Skipf("Driver %q does not define volumeSource - skipping", dInfo.Name)
} }
} }
cleanup := func() { cleanup := func() {
if resource != nil { if l.resource != nil {
resource.cleanupResource() l.resource.cleanupResource()
resource = nil l.resource = nil
} }
if testCleanup != nil { if l.testCleanup != nil {
testCleanup() l.testCleanup()
testCleanup = nil l.testCleanup = nil
} }
} }
...@@ -126,7 +132,7 @@ func (t *volumeIOTestSuite) defineTests(driver TestDriver, pattern testpatterns. ...@@ -126,7 +132,7 @@ func (t *volumeIOTestSuite) defineTests(driver TestDriver, pattern testpatterns.
podSec := v1.PodSecurityContext{ podSec := v1.PodSecurityContext{
FSGroup: fsGroup, FSGroup: fsGroup,
} }
err := testVolumeIO(f, cs, convertTestConfig(config), *resource.volSource, &podSec, testFile, fileSizes) err := testVolumeIO(f, cs, convertTestConfig(l.config), *l.resource.volSource, &podSec, testFile, fileSizes)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
}) })
} }
......
...@@ -90,12 +90,14 @@ func skipExecTest(driver TestDriver) { ...@@ -90,12 +90,14 @@ func skipExecTest(driver TestDriver) {
} }
func (t *volumesTestSuite) defineTests(driver TestDriver, pattern testpatterns.TestPattern) { func (t *volumesTestSuite) defineTests(driver TestDriver, pattern testpatterns.TestPattern) {
var ( type local struct {
dInfo = driver.GetDriverInfo()
config *PerTestConfig config *PerTestConfig
testCleanup func() testCleanup func()
resource *genericVolumeTestResource
) resource *genericVolumeTestResource
}
var dInfo = driver.GetDriverInfo()
var l local
// No preconditions to test. Normally they would be in a BeforeEach here. // No preconditions to test. Normally they would be in a BeforeEach here.
...@@ -106,23 +108,25 @@ func (t *volumesTestSuite) defineTests(driver TestDriver, pattern testpatterns.T ...@@ -106,23 +108,25 @@ func (t *volumesTestSuite) defineTests(driver TestDriver, pattern testpatterns.T
f := framework.NewDefaultFramework("volumeio") f := framework.NewDefaultFramework("volumeio")
init := func() { init := func() {
l = local{}
// Now do the more expensive test initialization. // Now do the more expensive test initialization.
config, testCleanup = driver.PrepareTest(f) l.config, l.testCleanup = driver.PrepareTest(f)
resource = createGenericVolumeTestResource(driver, config, pattern) l.resource = createGenericVolumeTestResource(driver, l.config, pattern)
if resource.volSource == nil { if l.resource.volSource == nil {
framework.Skipf("Driver %q does not define volumeSource - skipping", dInfo.Name) framework.Skipf("Driver %q does not define volumeSource - skipping", dInfo.Name)
} }
} }
cleanup := func() { cleanup := func() {
if resource != nil { if l.resource != nil {
resource.cleanupResource() l.resource.cleanupResource()
resource = nil l.resource = nil
} }
if testCleanup != nil { if l.testCleanup != nil {
testCleanup() l.testCleanup()
testCleanup = nil l.testCleanup = nil
} }
} }
...@@ -130,20 +134,20 @@ func (t *volumesTestSuite) defineTests(driver TestDriver, pattern testpatterns.T ...@@ -130,20 +134,20 @@ func (t *volumesTestSuite) defineTests(driver TestDriver, pattern testpatterns.T
skipPersistenceTest(driver) skipPersistenceTest(driver)
init() init()
defer func() { defer func() {
framework.VolumeTestCleanup(f, convertTestConfig(config)) framework.VolumeTestCleanup(f, convertTestConfig(l.config))
cleanup() cleanup()
}() }()
tests := []framework.VolumeTest{ tests := []framework.VolumeTest{
{ {
Volume: *resource.volSource, Volume: *l.resource.volSource,
File: "index.html", File: "index.html",
// Must match content // Must match content
ExpectedContent: fmt.Sprintf("Hello from %s from namespace %s", ExpectedContent: fmt.Sprintf("Hello from %s from namespace %s",
dInfo.Name, f.Namespace.Name), dInfo.Name, f.Namespace.Name),
}, },
} }
config := convertTestConfig(config) config := convertTestConfig(l.config)
framework.InjectHtml(f.ClientSet, config, tests[0].Volume, tests[0].ExpectedContent) framework.InjectHtml(f.ClientSet, config, tests[0].Volume, tests[0].ExpectedContent)
var fsGroup *int64 var fsGroup *int64
if dInfo.Capabilities[CapFsGroup] { if dInfo.Capabilities[CapFsGroup] {
...@@ -158,7 +162,7 @@ func (t *volumesTestSuite) defineTests(driver TestDriver, pattern testpatterns.T ...@@ -158,7 +162,7 @@ func (t *volumesTestSuite) defineTests(driver TestDriver, pattern testpatterns.T
init() init()
defer cleanup() defer cleanup()
testScriptInPod(f, resource.volType, resource.volSource, config.ClientNodeSelector) testScriptInPod(f, l.resource.volType, l.resource.volSource, l.config.ClientNodeSelector)
}) })
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment