Speed up pkg/volume/csi unit tests

This commit is contained in:
wzshiming 2021-02-04 18:55:29 +08:00 committed by Shiming Zhang
parent 2014d37e51
commit bc3d9252bc
3 changed files with 100 additions and 68 deletions

View File

@ -47,7 +47,10 @@ import (
volumetypes "k8s.io/kubernetes/pkg/volume/util/types"
)
const testWatchTimeout = 10 * time.Second
const (
testWatchTimeout = 10 * time.Second
testWatchFailTimeout = 2 * time.Second
)
var (
bFalse = false
@ -121,6 +124,7 @@ func TestAttacherAttach(t *testing.T) {
spec *volume.Spec
injectAttacherError bool
shouldFail bool
watchTimeout time.Duration
}{
{
name: "test ok 1",
@ -146,6 +150,7 @@ func TestAttacherAttach(t *testing.T) {
attachID: getAttachmentName("vol02", "driver02", "node02"),
spec: volume.NewSpecFromPersistentVolume(makeTestPV("pv01", 10, "driver02", "vol01"), false),
shouldFail: true,
watchTimeout: testWatchFailTimeout,
},
{
name: "mismatch driver",
@ -155,6 +160,7 @@ func TestAttacherAttach(t *testing.T) {
attachID: getAttachmentName("vol02", "driver02", "node02"),
spec: volume.NewSpecFromPersistentVolume(makeTestPV("pv01", 10, "driver01", "vol02"), false),
shouldFail: true,
watchTimeout: testWatchFailTimeout,
},
{
name: "mismatch node",
@ -164,6 +170,7 @@ func TestAttacherAttach(t *testing.T) {
attachID: getAttachmentName("vol02", "driver02", "node02"),
spec: volume.NewSpecFromPersistentVolume(makeTestPV("pv01", 10, "driver02", "vol02"), false),
shouldFail: true,
watchTimeout: testWatchFailTimeout,
},
{
name: "attacher error",
@ -207,7 +214,7 @@ func TestAttacherAttach(t *testing.T) {
t.Fatalf("failed to create new attacher: %v", err)
}
csiAttacher := getCsiAttacherFromVolumeAttacher(attacher)
csiAttacher := getCsiAttacherFromVolumeAttacher(attacher, tc.watchTimeout)
var wg sync.WaitGroup
wg.Add(1)
@ -251,6 +258,7 @@ func TestAttacherAttachWithInline(t *testing.T) {
spec *volume.Spec
injectAttacherError bool
shouldFail bool
watchTimeout time.Duration
}{
{
name: "test ok 1 with PV",
@ -293,7 +301,7 @@ func TestAttacherAttachWithInline(t *testing.T) {
if err != nil {
t.Fatalf("failed to create new attacher: %v", err)
}
csiAttacher := getCsiAttacherFromVolumeAttacher(attacher)
csiAttacher := getCsiAttacherFromVolumeAttacher(attacher, tc.watchTimeout)
var wg sync.WaitGroup
wg.Add(1)
@ -328,6 +336,7 @@ func TestAttacherWithCSIDriver(t *testing.T) {
name string
driver string
expectVolumeAttachment bool
watchTimeout time.Duration
}{
{
name: "CSIDriver not attachable",
@ -365,7 +374,7 @@ func TestAttacherWithCSIDriver(t *testing.T) {
if err != nil {
t.Fatalf("failed to create new attacher: %v", err)
}
csiAttacher := getCsiAttacherFromVolumeAttacher(attacher)
csiAttacher := getCsiAttacherFromVolumeAttacher(attacher, test.watchTimeout)
spec := volume.NewSpecFromPersistentVolume(makeTestPV("test-pv", 10, test.driver, "test-vol"), false)
pluginCanAttach, err := plug.CanAttach(spec)
@ -414,6 +423,7 @@ func TestAttacherWaitForVolumeAttachmentWithCSIDriver(t *testing.T) {
name string
driver string
expectError bool
watchTimeout time.Duration
}{
{
name: "CSIDriver not attachable -> success",
@ -457,7 +467,7 @@ func TestAttacherWaitForVolumeAttachmentWithCSIDriver(t *testing.T) {
if err != nil {
t.Fatalf("failed to create new attacher: %v", err)
}
csiAttacher := getCsiAttacherFromVolumeAttacher(attacher)
csiAttacher := getCsiAttacherFromVolumeAttacher(attacher, test.watchTimeout)
spec := volume.NewSpecFromPersistentVolume(makeTestPV("test-pv", 10, test.driver, "test-vol"), false)
pluginCanAttach, err := plug.CanAttach(spec)
@ -488,6 +498,7 @@ func TestAttacherWaitForAttach(t *testing.T) {
spec *volume.Spec
expectedAttachID string
expectError bool
watchTimeout time.Duration
}{
{
name: "successful attach",
@ -532,7 +543,7 @@ func TestAttacherWaitForAttach(t *testing.T) {
if err != nil {
t.Fatalf("failed to create new attacher: %v", err)
}
csiAttacher := getCsiAttacherFromVolumeAttacher(attacher)
csiAttacher := getCsiAttacherFromVolumeAttacher(attacher, test.watchTimeout)
if test.makeAttachment != nil {
attachment := test.makeAttachment()
@ -571,6 +582,7 @@ func TestAttacherWaitForAttachWithInline(t *testing.T) {
spec *volume.Spec
expectedAttachID string
expectError bool
watchTimeout time.Duration
}{
{
name: "successful attach with PV",
@ -615,7 +627,7 @@ func TestAttacherWaitForAttachWithInline(t *testing.T) {
if err != nil {
t.Fatalf("failed to create new attacher: %v", err)
}
csiAttacher := getCsiAttacherFromVolumeAttacher(attacher)
csiAttacher := getCsiAttacherFromVolumeAttacher(attacher, test.watchTimeout)
if test.makeAttachment != nil {
attachment := test.makeAttachment()
@ -653,6 +665,7 @@ func TestAttacherWaitForVolumeAttachment(t *testing.T) {
finalAttachErr *storage.VolumeError
timeout time.Duration
shouldFail bool
watchTimeout time.Duration
}{
{
name: "attach success at get",
@ -706,7 +719,7 @@ func TestAttacherWaitForVolumeAttachment(t *testing.T) {
if err != nil {
t.Fatalf("failed to create new attacher: %v", err)
}
csiAttacher := getCsiAttacherFromVolumeAttacher(attacher)
csiAttacher := getCsiAttacherFromVolumeAttacher(attacher, tc.watchTimeout)
t.Logf("running test: %v", tc.name)
pvName := fmt.Sprintf("test-pv-%d", i)
@ -763,10 +776,11 @@ func TestAttacherVolumesAreAttached(t *testing.T) {
testCases := []struct {
name string
attachedSpecs []attachedSpec
watchTimeout time.Duration
}{
{
"attach and detach",
[]attachedSpec{
name: "attach and detach",
attachedSpecs: []attachedSpec{
{"vol0", volume.NewSpecFromPersistentVolume(makeTestPV("pv0", 10, testDriver, "vol0"), false), true},
{"vol1", volume.NewSpecFromPersistentVolume(makeTestPV("pv1", 20, testDriver, "vol1"), false), true},
{"vol2", volume.NewSpecFromPersistentVolume(makeTestPV("pv2", 10, testDriver, "vol2"), false), false},
@ -775,23 +789,23 @@ func TestAttacherVolumesAreAttached(t *testing.T) {
},
},
{
"all detached",
[]attachedSpec{
name: "all detached",
attachedSpecs: []attachedSpec{
{"vol0", volume.NewSpecFromPersistentVolume(makeTestPV("pv0", 10, testDriver, "vol0"), false), false},
{"vol1", volume.NewSpecFromPersistentVolume(makeTestPV("pv1", 20, testDriver, "vol1"), false), false},
{"vol2", volume.NewSpecFromPersistentVolume(makeTestPV("pv2", 10, testDriver, "vol2"), false), false},
},
},
{
"all attached",
[]attachedSpec{
name: "all attached",
attachedSpecs: []attachedSpec{
{"vol0", volume.NewSpecFromPersistentVolume(makeTestPV("pv0", 10, testDriver, "vol0"), false), true},
{"vol1", volume.NewSpecFromPersistentVolume(makeTestPV("pv1", 20, testDriver, "vol1"), false), true},
},
},
{
"include non-attable",
[]attachedSpec{
name: "include non-attable",
attachedSpecs: []attachedSpec{
{"vol0", volume.NewSpecFromPersistentVolume(makeTestPV("pv0", 10, testDriver, "vol0"), false), true},
{"vol1", volume.NewSpecFromVolume(makeTestVol("pv1", testDriver)), false},
},
@ -807,7 +821,7 @@ func TestAttacherVolumesAreAttached(t *testing.T) {
if err != nil {
t.Fatalf("failed to create new attacher: %v", err)
}
csiAttacher := getCsiAttacherFromVolumeAttacher(attacher)
csiAttacher := getCsiAttacherFromVolumeAttacher(attacher, tc.watchTimeout)
nodeName := "fakeNode"
var specs []*volume.Spec
@ -856,10 +870,11 @@ func TestAttacherVolumesAreAttachedWithInline(t *testing.T) {
testCases := []struct {
name string
attachedSpecs []attachedSpec
watchTimeout time.Duration
}{
{
"attach and detach with volume sources",
[]attachedSpec{
name: "attach and detach with volume sources",
attachedSpecs: []attachedSpec{
{"vol0", volume.NewSpecFromPersistentVolume(makeTestPV("pv0", 10, testDriver, "vol0"), false), true},
{"vol1", volume.NewSpecFromVolume(makeTestVol("pv1", testDriver)), false},
{"vol2", volume.NewSpecFromPersistentVolume(makeTestPV("pv2", 10, testDriver, "vol2"), false), true},
@ -878,7 +893,7 @@ func TestAttacherVolumesAreAttachedWithInline(t *testing.T) {
if err != nil {
t.Fatalf("failed to create new attacher: %v", err)
}
csiAttacher := getCsiAttacherFromVolumeAttacher(attacher)
csiAttacher := getCsiAttacherFromVolumeAttacher(attacher, tc.watchTimeout)
nodeName := "fakeNode"
var specs []*volume.Spec
@ -925,6 +940,7 @@ func TestAttacherDetach(t *testing.T) {
attachID string
shouldFail bool
reactor func(action core.Action) (handled bool, ret runtime.Object, err error)
watchTimeout time.Duration
}{
{name: "normal test", volID: "vol-001", attachID: getAttachmentName("vol-001", testDriver, nodeName)},
{name: "normal test 2", volID: "vol-002", attachID: getAttachmentName("vol-002", testDriver, nodeName)},
@ -959,7 +975,7 @@ func TestAttacherDetach(t *testing.T) {
if err0 != nil {
t.Fatalf("failed to create new attacher: %v", err0)
}
csiAttacher := getCsiAttacherFromVolumeAttacher(attacher)
csiAttacher := getCsiAttacherFromVolumeAttacher(attacher, tc.watchTimeout)
pv := makeTestPV("test-pv", 10, testDriver, tc.volID)
spec := volume.NewSpecFromPersistentVolume(pv, pv.Spec.PersistentVolumeSource.CSI.ReadOnly)
@ -1004,7 +1020,7 @@ func TestAttacherGetDeviceMountPath(t *testing.T) {
if err0 != nil {
t.Fatalf("failed to create new attacher: %v", err0)
}
csiAttacher := getCsiAttacherFromVolumeAttacher(attacher)
csiAttacher := getCsiAttacherFromVolumeAttacher(attacher, testWatchTimeout)
pluginDir := csiAttacher.plugin.host.GetPluginDir(plug.GetPluginName())
@ -1067,6 +1083,7 @@ func TestAttacherMountDevice(t *testing.T) {
populateDeviceMountPath bool
exitError error
spec *volume.Spec
watchTimeout time.Duration
}{
{
testName: "normal PV",
@ -1187,7 +1204,7 @@ func TestAttacherMountDevice(t *testing.T) {
if err0 != nil {
t.Fatalf("failed to create new attacher: %v", err0)
}
csiAttacher := getCsiAttacherFromVolumeAttacher(attacher)
csiAttacher := getCsiAttacherFromVolumeAttacher(attacher, tc.watchTimeout)
csiAttacher.csiClient = setupClient(t, tc.stageUnstageSet)
if tc.deviceMountPath != "" {
@ -1309,6 +1326,7 @@ func TestAttacherMountDeviceWithInline(t *testing.T) {
stageUnstageSet bool
shouldFail bool
spec *volume.Spec
watchTimeout time.Duration
}{
{
testName: "normal PV",
@ -1387,7 +1405,7 @@ func TestAttacherMountDeviceWithInline(t *testing.T) {
if err0 != nil {
t.Fatalf("failed to create new attacher: %v", err0)
}
csiAttacher := getCsiAttacherFromVolumeAttacher(attacher)
csiAttacher := getCsiAttacherFromVolumeAttacher(attacher, tc.watchTimeout)
csiAttacher.csiClient = setupClient(t, tc.stageUnstageSet)
if tc.deviceMountPath != "" {
@ -1461,6 +1479,7 @@ func TestAttacherUnmountDevice(t *testing.T) {
createPV bool
stageUnstageSet bool
shouldFail bool
watchTimeout time.Duration
}{
{
testName: "normal, json file exists",
@ -1523,7 +1542,7 @@ func TestAttacherUnmountDevice(t *testing.T) {
if err0 != nil {
t.Fatalf("failed to create new attacher: %v", err0)
}
csiAttacher := getCsiAttacherFromVolumeAttacher(attacher)
csiAttacher := getCsiAttacherFromVolumeAttacher(attacher, tc.watchTimeout)
csiAttacher.csiClient = setupClient(t, tc.stageUnstageSet)
if tc.deviceMountPath != "" {
@ -1603,26 +1622,38 @@ func TestAttacherUnmountDevice(t *testing.T) {
}
}
func getCsiAttacherFromVolumeAttacher(attacher volume.Attacher) *csiAttacher {
func getCsiAttacherFromVolumeAttacher(attacher volume.Attacher, watchTimeout time.Duration) *csiAttacher {
if watchTimeout == 0 {
watchTimeout = testWatchTimeout
}
csiAttacher := attacher.(*csiAttacher)
csiAttacher.watchTimeout = testWatchTimeout
csiAttacher.watchTimeout = watchTimeout
return csiAttacher
}
func getCsiAttacherFromVolumeDetacher(detacher volume.Detacher) *csiAttacher {
func getCsiAttacherFromVolumeDetacher(detacher volume.Detacher, watchTimeout time.Duration) *csiAttacher {
if watchTimeout == 0 {
watchTimeout = testWatchTimeout
}
csiAttacher := detacher.(*csiAttacher)
csiAttacher.watchTimeout = testWatchTimeout
csiAttacher.watchTimeout = watchTimeout
return csiAttacher
}
func getCsiAttacherFromDeviceMounter(deviceMounter volume.DeviceMounter) *csiAttacher {
func getCsiAttacherFromDeviceMounter(deviceMounter volume.DeviceMounter, watchTimeout time.Duration) *csiAttacher {
if watchTimeout == 0 {
watchTimeout = testWatchTimeout
}
csiAttacher := deviceMounter.(*csiAttacher)
csiAttacher.watchTimeout = testWatchTimeout
csiAttacher.watchTimeout = watchTimeout
return csiAttacher
}
func getCsiAttacherFromDeviceUnmounter(deviceUnmounter volume.DeviceUnmounter) *csiAttacher {
func getCsiAttacherFromDeviceUnmounter(deviceUnmounter volume.DeviceUnmounter, watchTimeout time.Duration) *csiAttacher {
if watchTimeout == 0 {
watchTimeout = testWatchTimeout
}
csiAttacher := deviceUnmounter.(*csiAttacher)
csiAttacher.watchTimeout = testWatchTimeout
csiAttacher.watchTimeout = watchTimeout
return csiAttacher
}

View File

@ -910,7 +910,7 @@ func TestPluginNewAttacher(t *testing.T) {
t.Fatalf("failed to create new attacher: %v", err)
}
csiAttacher := getCsiAttacherFromVolumeAttacher(attacher)
csiAttacher := getCsiAttacherFromVolumeAttacher(attacher, testWatchTimeout)
if csiAttacher.plugin == nil {
t.Error("plugin not set for attacher")
}
@ -931,7 +931,7 @@ func TestPluginNewDetacher(t *testing.T) {
t.Fatalf("failed to create new detacher: %v", err)
}
csiDetacher := getCsiAttacherFromVolumeDetacher(detacher)
csiDetacher := getCsiAttacherFromVolumeDetacher(detacher, testWatchTimeout)
if csiDetacher.plugin == nil {
t.Error("plugin not set for detacher")
}

View File

@ -60,6 +60,7 @@ func TestCSI_VolumeAll(t *testing.T) {
shouldFail bool
disableFSGroupPolicyFeatureGate bool
driverSpec *storage.CSIDriverSpec
watchTimeout time.Duration
}{
{
name: "PersistentVolume",
@ -396,7 +397,7 @@ func TestCSI_VolumeAll(t *testing.T) {
}
if devMounter != nil {
csiDevMounter := getCsiAttacherFromDeviceMounter(devMounter)
csiDevMounter := getCsiAttacherFromDeviceMounter(devMounter, test.watchTimeout)
csiDevMounter.csiClient = csiClient
devMountPath, err := csiDevMounter.GetDeviceMountPath(volSpec)
if err != nil {
@ -559,8 +560,8 @@ func TestCSI_VolumeAll(t *testing.T) {
}
if devMounter != nil && devUnmounter != nil {
csiDevMounter := getCsiAttacherFromDeviceMounter(devMounter)
csiDevUnmounter := getCsiAttacherFromDeviceUnmounter(devUnmounter)
csiDevMounter := getCsiAttacherFromDeviceMounter(devMounter, test.watchTimeout)
csiDevUnmounter := getCsiAttacherFromDeviceUnmounter(devUnmounter, test.watchTimeout)
csiDevUnmounter.csiClient = csiClient
devMountPath, err := csiDevMounter.GetDeviceMountPath(volSpec)
@ -601,7 +602,7 @@ func TestCSI_VolumeAll(t *testing.T) {
if err != nil {
t.Fatal("csiTest.VolumeAll volumePlugin.GetVolumeName failed:", err)
}
csiDetacher := getCsiAttacherFromVolumeDetacher(volDetacher)
csiDetacher := getCsiAttacherFromVolumeDetacher(volDetacher, test.watchTimeout)
csiDetacher.csiClient = csiClient
if err := csiDetacher.Detach(volName, attachDetachVolumeHost.GetNodeName()); err != nil {
t.Fatal("csiTest.VolumeAll detacher.Detach failed:", err)