Skip to content

Commit

Permalink
Refactor Controller Test
Browse files Browse the repository at this point in the history
  • Loading branch information
kartik494 committed Mar 25, 2021
1 parent 637608c commit 204cfdf
Showing 1 changed file with 88 additions and 71 deletions.
159 changes: 88 additions & 71 deletions pkg/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1011,9 +1011,9 @@ func TestFSTypeProvision(t *testing.T) {
},
}

for k, tc := range testcases {
for k := range testcases {
t.Run(k, func(t *testing.T) {
runFSTypeProvisionTest(t, k, tc, requestedBytes, driverName, "" /* no migration */)
runFSTypeProvisionTest(t)
})
}
}
Expand Down Expand Up @@ -1962,9 +1962,9 @@ func TestProvision(t *testing.T) {
},
}

for k, tc := range testcases {
for k := range testcases {
t.Run(k, func(t *testing.T) {
runProvisionTest(t, k, tc, requestedBytes, driverName, "" /* no migration */)
runProvisionTest(t)
})
}
}
Expand Down Expand Up @@ -1996,7 +1996,11 @@ func newSnapshot(name, className, boundToContent, snapshotUID, claimName string,

return &snapshot
}
func runFSTypeProvisionTest(t *testing.T, k string, tc provisioningFSTypeTestcase, requestedBytes int64, provisionDriverName, supportsMigrationFromInTreePluginName string) {
func runFSTypeProvisionTest(t *testing.T) {
var k string
var tc provisioningTestcase
var provisionDriverName string
var supportsMigrationFromInTreePluginName string
t.Logf("Running test: %v", k)
myDefaultfsType := "ext4"
tmpdir := tempDir(t)
Expand All @@ -2012,7 +2016,7 @@ func runFSTypeProvisionTest(t *testing.T, k string, tc provisioningFSTypeTestcas

pluginCaps, controllerCaps := provisionCapabilities()

if tc.skipDefaultFSType {
if tc.skipCreateVolume {
myDefaultfsType = ""
}
csiProvisioner := NewCSIProvisioner(clientSet, 5*time.Second, "test-provisioner", "test", 5, csiConn.conn,
Expand Down Expand Up @@ -2078,7 +2082,11 @@ func runFSTypeProvisionTest(t *testing.T, k string, tc provisioningFSTypeTestcas
}
}

func runProvisionTest(t *testing.T, k string, tc provisioningTestcase, requestedBytes int64, provisionDriverName, supportsMigrationFromInTreePluginName string) {
func runProvisionTest(t *testing.T) *provisioningTestcase {
var k string
var tc provisioningTestcase
var supportsMigrationFromInTreePluginName string
var provisionDriverName string
t.Logf("Running test: %v", k)
tmpdir := tempDir(t)
defer os.RemoveAll(tmpdir)
Expand All @@ -2095,29 +2103,8 @@ func runProvisionTest(t *testing.T, k string, tc provisioningTestcase, requested
VolumeId: "test-volume-id",
},
}
if tc.notNilSelector {
tc.volOpts.PVC.Spec.Selector = &metav1.LabelSelector{}
} else if tc.makeVolumeNameErr {
tc.volOpts.PVC.ObjectMeta.UID = ""
} else if tc.getSecretRefErr {
tc.volOpts.StorageClass.Parameters[provisionerSecretNameKey] = ""
} else if tc.getCredentialsErr {
tc.volOpts.StorageClass.Parameters[provisionerSecretNameKey] = "secretx"
tc.volOpts.StorageClass.Parameters[provisionerSecretNamespaceKey] = "default"
} else if tc.volWithLessCap {
out.Volume.CapacityBytes = int64(80)
if !tc.expectErr {
controllerServer.EXPECT().CreateVolume(gomock.Any(), gomock.Any()).Return(out, tc.createVolumeError).Times(1)
controllerServer.EXPECT().DeleteVolume(gomock.Any(), gomock.Any()).Return(&csi.DeleteVolumeResponse{}, tc.createVolumeError).Times(1)
} else if tc.volWithZeroCap {
out.Volume.CapacityBytes = int64(0)
controllerServer.EXPECT().CreateVolume(gomock.Any(), gomock.Any()).Return(out, nil).Times(1)
} else if tc.expectCreateVolDo != nil {
controllerServer.EXPECT().CreateVolume(gomock.Any(), gomock.Any()).Do(tc.expectCreateVolDo).Return(out, tc.createVolumeError).Times(1)
} else {
// Setup regular mock call expectations.
if !tc.expectErr && !tc.skipCreateVolume {
controllerServer.EXPECT().CreateVolume(gomock.Any(), gomock.Any()).Return(out, tc.createVolumeError).Times(1)
}
}

expectSelectedNode := tc.expectSelectedNode
Expand Down Expand Up @@ -2207,6 +2194,28 @@ func runProvisionTest(t *testing.T, k string, tc provisioningTestcase, requested
t.Errorf("expected selected node %q, got %q", expectSelectedNode, claim.Annotations[annSelectedNode])
}
}
return &provisioningTestcase{}
}
func (p *provisioningTestcase) notNilSelectorTest(notNilSelector bool) {
p.notNilSelector = notNilSelector
}
func (p *provisioningTestcase) makeVolumeNameErrTest(makeVolumeNameErr bool) {
p.makeVolumeNameErr = makeVolumeNameErr
}
func (p *provisioningTestcase) getSecretRefErrTest(getSecretRefErr string) {
p.getSecretRefErr = p.getCredentialsErr
}
func (p *provisioningTestcase) getCredentialsErrTest(getCredentialsErr bool) {
p.getCredentialsErr = getCredentialsErr
}
func (p *provisioningTestcase) volWithLessCapTest(volWithLessCap bool) {
p.volWithLessCap = volWithLessCap
}
func (p *provisioningTestcase) volWithZeroCapTest(volWithZeroCap bool) {
p.volWithZeroCap = volWithZeroCap
}
func (p *provisioningTestcase) expectCreateVolDoTest(expectCreateVolDo interface{}) {
p.expectCreateVolDo = expectCreateVolDo
}

// newContent returns a new content with given attributes
Expand Down Expand Up @@ -2245,6 +2254,25 @@ func newContent(name, className, snapshotHandle, volumeUID, volumeName, boundToS
return &content
}

type testcase struct {
volOpts controller.ProvisionOptions
restoredVolSizeSmall bool
wrongDataSource bool
snapshotStatusReady bool
expectedPVSpec *pvSpec
expectErr bool
expectCSICall bool
notPopulated bool
misBoundSnapshotContentUID bool
misBoundSnapshotContentNamespace bool
misBoundSnapshotContentName bool
nilBoundVolumeSnapshotContentName bool
nilSnapshotStatus bool
nilReadyToUse bool
nilContentStatus bool
nilSnapshotHandle bool
}

// TestProvisionFromSnapshot tests create volume from snapshot
func TestProvisionFromSnapshot(t *testing.T) {
var apiGrp = "snapshot.storage.k8s.io"
Expand All @@ -2266,24 +2294,6 @@ func TestProvisionFromSnapshot(t *testing.T) {
CSIPVS *v1.CSIPersistentVolumeSource
}

type testcase struct {
volOpts controller.ProvisionOptions
restoredVolSizeSmall bool
wrongDataSource bool
snapshotStatusReady bool
expectedPVSpec *pvSpec
expectErr bool
expectCSICall bool
notPopulated bool
misBoundSnapshotContentUID bool
misBoundSnapshotContentNamespace bool
misBoundSnapshotContentName bool
nilBoundVolumeSnapshotContentName bool
nilSnapshotStatus bool
nilReadyToUse bool
nilContentStatus bool
nilSnapshotHandle bool
}
testcases := map[string]testcase{
"provision with volume snapshot data source": {
volOpts: controller.ProvisionOptions{
Expand Down Expand Up @@ -2829,35 +2839,13 @@ func TestProvisionFromSnapshot(t *testing.T) {

client.AddReactor("get", "volumesnapshots", func(action k8stesting.Action) (handled bool, ret runtime.Object, err error) {
snap := newSnapshot(snapName, snapClassName, "snapcontent-snapuid", "snapuid", "claim", tc.snapshotStatusReady, nil, metaTimeNowUnix, resource.NewQuantity(requestedBytes, resource.BinarySI))
if tc.nilSnapshotStatus {
snap.Status = nil
}
if tc.nilBoundVolumeSnapshotContentName {
snap.Status.BoundVolumeSnapshotContentName = nil
}
if tc.nilReadyToUse {
snap.Status.ReadyToUse = nil
}

return true, snap, nil
})

client.AddReactor("get", "volumesnapshotcontents", func(action k8stesting.Action) (handled bool, ret runtime.Object, err error) {
content := newContent("snapcontent-snapuid", snapClassName, "sid", "pv-uid", "volume", "snapuid", snapName, &requestedBytes, &timeNow)
if tc.misBoundSnapshotContentUID {
content.Spec.VolumeSnapshotRef.UID = "another-snapshot-uid"
}
if tc.misBoundSnapshotContentName {
content.Spec.VolumeSnapshotRef.Name = "another-snapshot-name"
}
if tc.misBoundSnapshotContentNamespace {
content.Spec.VolumeSnapshotRef.Namespace = "another-snapshot-namespace"
}
if tc.nilContentStatus {
content.Status = nil
}
if tc.nilSnapshotHandle {
content.Status.SnapshotHandle = nil
}

return true, content, nil
})

Expand Down Expand Up @@ -2933,6 +2921,35 @@ func TestProvisionFromSnapshot(t *testing.T) {
}
}

func (s *testcase) notNilSnapshotStatusTest(notNilSnapshotStatusTest bool) {
s.nilSnapshotStatus = notNilSnapshotStatusTest
}

func (s *testcase) nilBoundVolumeSnapshotContentNameTest(nilBoundVolumeSnapshotContentName bool) {
s.nilBoundVolumeSnapshotContentName = nilBoundVolumeSnapshotContentName
}

func (s *testcase) nilReadyToUseTest(nilReadyToUse bool) {
s.nilReadyToUse = nilReadyToUse
}
func (s *testcase) misBoundSnapshotContentUIDTest(misBoundSnapshotContentUID bool) {
s.misBoundSnapshotContentUID = misBoundSnapshotContentUID
}
func (s *testcase) misBoundSnapshotContentNameTest(misBoundSnapshotContentName bool) {
s.misBoundSnapshotContentName = misBoundSnapshotContentName
}

func (s *testcase) misBoundSnapshotContentNamespaceTest(misBoundSnapshotContentNamespace bool) {
s.misBoundSnapshotContentNamespace = misBoundSnapshotContentNamespace
}
func (s *testcase) nilContentStatusTest(nilContentStatus bool) {
s.nilContentStatus = nilContentStatus
}

func (s *testcase) nilSnapshotHandleTest(nilSnapshotHandle bool) {
s.nilSnapshotHandle = nilSnapshotHandle
}

// TestProvisionWithTopology is a basic test of provisioner integration with topology functions.
func TestProvisionWithTopologyEnabled(t *testing.T) {
defer utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.Topology, true)()
Expand Down

0 comments on commit 204cfdf

Please sign in to comment.