Merge pull request #111690 from piroux/pv-controller-tests-structs-with-names

Pv controller tests structs with names
This commit is contained in:
Kubernetes Prow Robot 2022-08-23 17:17:26 -07:00 committed by GitHub
commit 9451747366
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 1091 additions and 894 deletions

File diff suppressed because it is too large Load Diff

View File

@ -40,66 +40,73 @@ func TestDeleteSync(t *testing.T) {
tests := []controllerTest{ tests := []controllerTest{
{ {
// delete volume bound by controller // delete volume bound by controller
"8-1 - successful delete", name: "8-1 - successful delete",
newVolumeArray("volume8-1", "1Gi", "uid8-1", "claim8-1", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty, volume.AnnBoundByController), initialVolumes: newVolumeArray("volume8-1", "1Gi", "uid8-1", "claim8-1", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty, volume.AnnBoundByController),
novolumes, expectedVolumes: novolumes,
noclaims, initialClaims: noclaims,
noclaims, expectedClaims: noclaims,
noevents, noerrors, expectedEvents: noevents,
errors: noerrors,
// Inject deleter into the controller and call syncVolume. The // Inject deleter into the controller and call syncVolume. The
// deleter simulates one delete() call that succeeds. // deleter simulates one delete() call that succeeds.
wrapTestWithReclaimCalls(operationDelete, []error{nil}, testSyncVolume), test: wrapTestWithReclaimCalls(operationDelete, []error{nil}, testSyncVolume),
}, },
{ {
// delete volume bound by user // delete volume bound by user
"8-2 - successful delete with prebound volume", name: "8-2 - successful delete with prebound volume",
newVolumeArray("volume8-2", "1Gi", "uid8-2", "claim8-2", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty), initialVolumes: newVolumeArray("volume8-2", "1Gi", "uid8-2", "claim8-2", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty),
novolumes, expectedVolumes: novolumes,
noclaims, initialClaims: noclaims,
noclaims, expectedClaims: noclaims,
noevents, noerrors, expectedEvents: noevents,
errors: noerrors,
// Inject deleter into the controller and call syncVolume. The // Inject deleter into the controller and call syncVolume. The
// deleter simulates one delete() call that succeeds. // deleter simulates one delete() call that succeeds.
wrapTestWithReclaimCalls(operationDelete, []error{nil}, testSyncVolume), test: wrapTestWithReclaimCalls(operationDelete, []error{nil}, testSyncVolume),
}, },
{ {
// delete failure - plugin not found // delete failure - plugin not found
"8-3 - plugin not found", name: "8-3 - plugin not found",
newVolumeArray("volume8-3", "1Gi", "uid8-3", "claim8-3", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty), initialVolumes: newVolumeArray("volume8-3", "1Gi", "uid8-3", "claim8-3", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty),
withMessage("error getting deleter volume plugin for volume \"volume8-3\": no volume plugin matched", newVolumeArray("volume8-3", "1Gi", "uid8-3", "claim8-3", v1.VolumeFailed, v1.PersistentVolumeReclaimDelete, classEmpty)), expectedVolumes: withMessage("error getting deleter volume plugin for volume \"volume8-3\": no volume plugin matched", newVolumeArray("volume8-3", "1Gi", "uid8-3", "claim8-3", v1.VolumeFailed, v1.PersistentVolumeReclaimDelete, classEmpty)),
noclaims, initialClaims: noclaims,
noclaims, expectedClaims: noclaims,
[]string{"Warning VolumeFailedDelete"}, noerrors, testSyncVolume, expectedEvents: []string{"Warning VolumeFailedDelete"},
errors: noerrors,
test: testSyncVolume,
}, },
{ {
// delete failure - newDeleter returns error // delete failure - newDeleter returns error
"8-4 - newDeleter returns error", name: "8-4 - newDeleter returns error",
newVolumeArray("volume8-4", "1Gi", "uid8-4", "claim8-4", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty), initialVolumes: newVolumeArray("volume8-4", "1Gi", "uid8-4", "claim8-4", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty),
withMessage("failed to create deleter for volume \"volume8-4\": Mock plugin error: no deleteCalls configured", newVolumeArray("volume8-4", "1Gi", "uid8-4", "claim8-4", v1.VolumeFailed, v1.PersistentVolumeReclaimDelete, classEmpty)), expectedVolumes: withMessage("failed to create deleter for volume \"volume8-4\": Mock plugin error: no deleteCalls configured", newVolumeArray("volume8-4", "1Gi", "uid8-4", "claim8-4", v1.VolumeFailed, v1.PersistentVolumeReclaimDelete, classEmpty)),
noclaims, initialClaims: noclaims,
noclaims, expectedClaims: noclaims,
[]string{"Warning VolumeFailedDelete"}, noerrors, expectedEvents: []string{"Warning VolumeFailedDelete"},
wrapTestWithReclaimCalls(operationDelete, []error{}, testSyncVolume), errors: noerrors,
test: wrapTestWithReclaimCalls(operationDelete, []error{}, testSyncVolume),
}, },
{ {
// delete failure - delete() returns error // delete failure - delete() returns error
"8-5 - delete returns error", name: "8-5 - delete returns error",
newVolumeArray("volume8-5", "1Gi", "uid8-5", "claim8-5", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty), initialVolumes: newVolumeArray("volume8-5", "1Gi", "uid8-5", "claim8-5", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty),
withMessage("Mock delete error", newVolumeArray("volume8-5", "1Gi", "uid8-5", "claim8-5", v1.VolumeFailed, v1.PersistentVolumeReclaimDelete, classEmpty)), expectedVolumes: withMessage("Mock delete error", newVolumeArray("volume8-5", "1Gi", "uid8-5", "claim8-5", v1.VolumeFailed, v1.PersistentVolumeReclaimDelete, classEmpty)),
noclaims, initialClaims: noclaims,
noclaims, expectedClaims: noclaims,
[]string{"Warning VolumeFailedDelete"}, noerrors, expectedEvents: []string{"Warning VolumeFailedDelete"},
wrapTestWithReclaimCalls(operationDelete, []error{errors.New("Mock delete error")}, testSyncVolume), errors: noerrors,
test: wrapTestWithReclaimCalls(operationDelete, []error{errors.New("Mock delete error")}, testSyncVolume),
}, },
{ {
// delete success(?) - volume is deleted before doDelete() starts // delete success(?) - volume is deleted before doDelete() starts
"8-6 - volume is deleted before deleting", name: "8-6 - volume is deleted before deleting",
newVolumeArray("volume8-6", "1Gi", "uid8-6", "claim8-6", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty), initialVolumes: newVolumeArray("volume8-6", "1Gi", "uid8-6", "claim8-6", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty),
novolumes, expectedVolumes: novolumes,
noclaims, initialClaims: noclaims,
noclaims, expectedClaims: noclaims,
noevents, noerrors, expectedEvents: noevents,
wrapTestWithInjectedOperation(wrapTestWithReclaimCalls(operationDelete, []error{}, testSyncVolume), func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor) { errors: noerrors,
test: wrapTestWithInjectedOperation(wrapTestWithReclaimCalls(operationDelete, []error{}, testSyncVolume), func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor) {
// Delete the volume before delete operation starts // Delete the volume before delete operation starts
reactor.DeleteVolume("volume8-6") reactor.DeleteVolume("volume8-6")
}), }),
@ -108,13 +115,14 @@ func TestDeleteSync(t *testing.T) {
// delete success(?) - volume is bound just at the time doDelete() // delete success(?) - volume is bound just at the time doDelete()
// starts. This simulates "volume no longer needs recycling, // starts. This simulates "volume no longer needs recycling,
// skipping". // skipping".
"8-7 - volume is bound before deleting", name: "8-7 - volume is bound before deleting",
newVolumeArray("volume8-7", "1Gi", "uid8-7", "claim8-7", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty, volume.AnnBoundByController), initialVolumes: newVolumeArray("volume8-7", "1Gi", "uid8-7", "claim8-7", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty, volume.AnnBoundByController),
newVolumeArray("volume8-7", "1Gi", "uid8-7", "claim8-7", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty, volume.AnnBoundByController), expectedVolumes: newVolumeArray("volume8-7", "1Gi", "uid8-7", "claim8-7", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty, volume.AnnBoundByController),
noclaims, initialClaims: noclaims,
newClaimArray("claim8-7", "uid8-7", "10Gi", "volume8-7", v1.ClaimBound, nil), expectedClaims: newClaimArray("claim8-7", "uid8-7", "10Gi", "volume8-7", v1.ClaimBound, nil),
noevents, noerrors, expectedEvents: noevents,
wrapTestWithInjectedOperation(wrapTestWithReclaimCalls(operationDelete, []error{}, testSyncVolume), func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor) { errors: noerrors,
test: wrapTestWithInjectedOperation(wrapTestWithReclaimCalls(operationDelete, []error{}, testSyncVolume), func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor) {
// Bind the volume to resurrected claim (this should never // Bind the volume to resurrected claim (this should never
// happen) // happen)
claim := newClaim("claim8-7", "uid8-7", "10Gi", "volume8-7", v1.ClaimBound, nil) claim := newClaim("claim8-7", "uid8-7", "10Gi", "volume8-7", v1.ClaimBound, nil)
@ -125,25 +133,27 @@ func TestDeleteSync(t *testing.T) {
{ {
// delete success - volume bound by user is deleted, while a new // delete success - volume bound by user is deleted, while a new
// claim is created with another UID. // claim is created with another UID.
"8-9 - prebound volume is deleted while the claim exists", name: "8-9 - prebound volume is deleted while the claim exists",
newVolumeArray("volume8-9", "1Gi", "uid8-9", "claim8-9", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty), initialVolumes: newVolumeArray("volume8-9", "1Gi", "uid8-9", "claim8-9", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty),
novolumes, expectedVolumes: novolumes,
newClaimArray("claim8-9", "uid8-9-x", "10Gi", "", v1.ClaimPending, nil), initialClaims: newClaimArray("claim8-9", "uid8-9-x", "10Gi", "", v1.ClaimPending, nil),
newClaimArray("claim8-9", "uid8-9-x", "10Gi", "", v1.ClaimPending, nil), expectedClaims: newClaimArray("claim8-9", "uid8-9-x", "10Gi", "", v1.ClaimPending, nil),
noevents, noerrors, expectedEvents: noevents,
errors: noerrors,
// Inject deleter into the controller and call syncVolume. The // Inject deleter into the controller and call syncVolume. The
// deleter simulates one delete() call that succeeds. // deleter simulates one delete() call that succeeds.
wrapTestWithReclaimCalls(operationDelete, []error{nil}, testSyncVolume), test: wrapTestWithReclaimCalls(operationDelete, []error{nil}, testSyncVolume),
}, },
{ {
// PV requires external deleter // PV requires external deleter
"8-10 - external deleter", name: "8-10 - external deleter",
[]*v1.PersistentVolume{newExternalProvisionedVolume("volume8-10", "1Gi", "uid10-1", "claim10-1", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty, gceDriver, nil, volume.AnnBoundByController)}, initialVolumes: []*v1.PersistentVolume{newExternalProvisionedVolume("volume8-10", "1Gi", "uid10-1", "claim10-1", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty, gceDriver, nil, volume.AnnBoundByController)},
[]*v1.PersistentVolume{newExternalProvisionedVolume("volume8-10", "1Gi", "uid10-1", "claim10-1", v1.VolumeReleased, v1.PersistentVolumeReclaimDelete, classEmpty, gceDriver, nil, volume.AnnBoundByController)}, expectedVolumes: []*v1.PersistentVolume{newExternalProvisionedVolume("volume8-10", "1Gi", "uid10-1", "claim10-1", v1.VolumeReleased, v1.PersistentVolumeReclaimDelete, classEmpty, gceDriver, nil, volume.AnnBoundByController)},
noclaims, initialClaims: noclaims,
noclaims, expectedClaims: noclaims,
noevents, noerrors, expectedEvents: noevents,
func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor, test controllerTest) error { errors: noerrors,
test: func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor, test controllerTest) error {
// Inject external deleter annotation // Inject external deleter annotation
test.initialVolumes[0].Annotations[volume.AnnDynamicallyProvisioned] = "external.io/test" test.initialVolumes[0].Annotations[volume.AnnDynamicallyProvisioned] = "external.io/test"
test.expectedVolumes[0].Annotations[volume.AnnDynamicallyProvisioned] = "external.io/test" test.expectedVolumes[0].Annotations[volume.AnnDynamicallyProvisioned] = "external.io/test"
@ -153,40 +163,42 @@ func TestDeleteSync(t *testing.T) {
{ {
// delete success - two PVs are provisioned for a single claim. // delete success - two PVs are provisioned for a single claim.
// One of the PVs is deleted. // One of the PVs is deleted.
"8-11 - two PVs provisioned for a single claim", name: "8-11 - two PVs provisioned for a single claim",
[]*v1.PersistentVolume{ initialVolumes: []*v1.PersistentVolume{
newVolume("volume8-11-1", "1Gi", "uid8-11", "claim8-11", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty, volume.AnnDynamicallyProvisioned), newVolume("volume8-11-1", "1Gi", "uid8-11", "claim8-11", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty, volume.AnnDynamicallyProvisioned),
newVolume("volume8-11-2", "1Gi", "uid8-11", "claim8-11", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty, volume.AnnDynamicallyProvisioned), newVolume("volume8-11-2", "1Gi", "uid8-11", "claim8-11", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty, volume.AnnDynamicallyProvisioned),
}, },
[]*v1.PersistentVolume{ expectedVolumes: []*v1.PersistentVolume{
newVolume("volume8-11-2", "1Gi", "uid8-11", "claim8-11", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty, volume.AnnDynamicallyProvisioned), newVolume("volume8-11-2", "1Gi", "uid8-11", "claim8-11", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty, volume.AnnDynamicallyProvisioned),
}, },
// the claim is bound to volume8-11-2 -> volume8-11-1 has lost the race and will be deleted // the claim is bound to volume8-11-2 -> volume8-11-1 has lost the race and will be deleted
newClaimArray("claim8-11", "uid8-11", "10Gi", "volume8-11-2", v1.ClaimBound, nil), initialClaims: newClaimArray("claim8-11", "uid8-11", "10Gi", "volume8-11-2", v1.ClaimBound, nil),
newClaimArray("claim8-11", "uid8-11", "10Gi", "volume8-11-2", v1.ClaimBound, nil), expectedClaims: newClaimArray("claim8-11", "uid8-11", "10Gi", "volume8-11-2", v1.ClaimBound, nil),
noevents, noerrors, expectedEvents: noevents,
errors: noerrors,
// Inject deleter into the controller and call syncVolume. The // Inject deleter into the controller and call syncVolume. The
// deleter simulates one delete() call that succeeds. // deleter simulates one delete() call that succeeds.
wrapTestWithReclaimCalls(operationDelete, []error{nil}, testSyncVolume), test: wrapTestWithReclaimCalls(operationDelete, []error{nil}, testSyncVolume),
}, },
{ {
// delete success - two PVs are externally provisioned for a single // delete success - two PVs are externally provisioned for a single
// claim. One of the PVs is marked as Released to be deleted by the // claim. One of the PVs is marked as Released to be deleted by the
// external provisioner. // external provisioner.
"8-12 - two PVs externally provisioned for a single claim", name: "8-12 - two PVs externally provisioned for a single claim",
[]*v1.PersistentVolume{ initialVolumes: []*v1.PersistentVolume{
newExternalProvisionedVolume("volume8-12-1", "1Gi", "uid8-12", "claim8-12", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty, gceDriver, nil, volume.AnnDynamicallyProvisioned), newExternalProvisionedVolume("volume8-12-1", "1Gi", "uid8-12", "claim8-12", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty, gceDriver, nil, volume.AnnDynamicallyProvisioned),
newExternalProvisionedVolume("volume8-12-2", "1Gi", "uid8-12", "claim8-12", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty, gceDriver, nil, volume.AnnDynamicallyProvisioned), newExternalProvisionedVolume("volume8-12-2", "1Gi", "uid8-12", "claim8-12", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty, gceDriver, nil, volume.AnnDynamicallyProvisioned),
}, },
[]*v1.PersistentVolume{ expectedVolumes: []*v1.PersistentVolume{
newExternalProvisionedVolume("volume8-12-1", "1Gi", "uid8-12", "claim8-12", v1.VolumeReleased, v1.PersistentVolumeReclaimDelete, classEmpty, gceDriver, nil, volume.AnnDynamicallyProvisioned), newExternalProvisionedVolume("volume8-12-1", "1Gi", "uid8-12", "claim8-12", v1.VolumeReleased, v1.PersistentVolumeReclaimDelete, classEmpty, gceDriver, nil, volume.AnnDynamicallyProvisioned),
newExternalProvisionedVolume("volume8-12-2", "1Gi", "uid8-12", "claim8-12", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty, gceDriver, nil, volume.AnnDynamicallyProvisioned), newExternalProvisionedVolume("volume8-12-2", "1Gi", "uid8-12", "claim8-12", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty, gceDriver, nil, volume.AnnDynamicallyProvisioned),
}, },
// the claim is bound to volume8-12-2 -> volume8-12-1 has lost the race and will be "Released" // the claim is bound to volume8-12-2 -> volume8-12-1 has lost the race and will be "Released"
newClaimArray("claim8-12", "uid8-12", "10Gi", "volume8-12-2", v1.ClaimBound, nil), initialClaims: newClaimArray("claim8-12", "uid8-12", "10Gi", "volume8-12-2", v1.ClaimBound, nil),
newClaimArray("claim8-12", "uid8-12", "10Gi", "volume8-12-2", v1.ClaimBound, nil), expectedClaims: newClaimArray("claim8-12", "uid8-12", "10Gi", "volume8-12-2", v1.ClaimBound, nil),
noevents, noerrors, expectedEvents: noevents,
func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor, test controllerTest) error { errors: noerrors,
test: func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor, test controllerTest) error {
// Inject external deleter annotation // Inject external deleter annotation
test.initialVolumes[0].Annotations[volume.AnnDynamicallyProvisioned] = "external.io/test" test.initialVolumes[0].Annotations[volume.AnnDynamicallyProvisioned] = "external.io/test"
test.expectedVolumes[0].Annotations[volume.AnnDynamicallyProvisioned] = "external.io/test" test.expectedVolumes[0].Annotations[volume.AnnDynamicallyProvisioned] = "external.io/test"
@ -195,13 +207,14 @@ func TestDeleteSync(t *testing.T) {
}, },
{ {
// delete success - volume has deletion timestamp before doDelete() starts // delete success - volume has deletion timestamp before doDelete() starts
"8-13 - volume has deletion timestamp and processed", name: "8-13 - volume has deletion timestamp and processed",
volumesWithFinalizers(withVolumeDeletionTimestamp(newVolumeArray("volume8-13", "1Gi", "uid8-13", "claim8-13", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty, volume.AnnBoundByController)), []string{volume.PVDeletionInTreeProtectionFinalizer}), initialVolumes: volumesWithFinalizers(withVolumeDeletionTimestamp(newVolumeArray("volume8-13", "1Gi", "uid8-13", "claim8-13", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty, volume.AnnBoundByController)), []string{volume.PVDeletionInTreeProtectionFinalizer}),
novolumes, expectedVolumes: novolumes,
noclaims, initialClaims: noclaims,
noclaims, expectedClaims: noclaims,
noevents, noerrors, expectedEvents: noevents,
wrapTestWithReclaimCalls(operationDelete, []error{nil}, testSyncVolume), errors: noerrors,
test: wrapTestWithReclaimCalls(operationDelete, []error{nil}, testSyncVolume),
}, },
} }
runSyncTests(t, tests, []*storage.StorageClass{}, []*v1.Pod{}) runSyncTests(t, tests, []*storage.StorageClass{}, []*v1.Pod{})
@ -227,13 +240,14 @@ func TestDeleteMultiSync(t *testing.T) {
{ {
// delete failure - delete returns error. The controller should // delete failure - delete returns error. The controller should
// try again. // try again.
"9-1 - delete returns error", name: "9-1 - delete returns error",
volumesWithFinalizers(newVolumeArray("volume9-1", "1Gi", "uid9-1", "claim9-1", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty), []string{volume.PVDeletionInTreeProtectionFinalizer}), initialVolumes: volumesWithFinalizers(newVolumeArray("volume9-1", "1Gi", "uid9-1", "claim9-1", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty), []string{volume.PVDeletionInTreeProtectionFinalizer}),
novolumes, expectedVolumes: novolumes,
noclaims, initialClaims: noclaims,
noclaims, expectedClaims: noclaims,
[]string{"Warning VolumeFailedDelete"}, noerrors, expectedEvents: []string{"Warning VolumeFailedDelete"},
wrapTestWithReclaimCalls(operationDelete, []error{errors.New("Mock delete error"), nil}, testSyncVolume), errors: noerrors,
test: wrapTestWithReclaimCalls(operationDelete, []error{errors.New("Mock delete error"), nil}, testSyncVolume),
}, },
} }

View File

@ -19,6 +19,7 @@ package persistentvolume
import ( import (
"context" "context"
"errors" "errors"
utilfeature "k8s.io/apiserver/pkg/util/feature" utilfeature "k8s.io/apiserver/pkg/util/feature"
featuregatetesting "k8s.io/component-base/featuregate/testing" featuregatetesting "k8s.io/component-base/featuregate/testing"
"k8s.io/kubernetes/pkg/features" "k8s.io/kubernetes/pkg/features"
@ -175,67 +176,74 @@ func TestProvisionSync(t *testing.T) {
tests := []controllerTest{ tests := []controllerTest{
{ {
// Provision a volume (with a default class) // Provision a volume (with a default class)
"11-1 - successful provision with storage class 1", name: "11-1 - successful provision with storage class 1",
novolumes, initialVolumes: novolumes,
volumesWithFinalizers(newVolumeArray("pvc-uid11-1", "1Gi", "uid11-1", "claim11-1", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classGold, volume.AnnBoundByController, volume.AnnDynamicallyProvisioned), []string{volume.PVDeletionInTreeProtectionFinalizer}), expectedVolumes: volumesWithFinalizers(newVolumeArray("pvc-uid11-1", "1Gi", "uid11-1", "claim11-1", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classGold, volume.AnnBoundByController, volume.AnnDynamicallyProvisioned), []string{volume.PVDeletionInTreeProtectionFinalizer}),
newClaimArray("claim11-1", "uid11-1", "1Gi", "", v1.ClaimPending, &classGold),
// Binding will be completed in the next syncClaim // Binding will be completed in the next syncClaim
newClaimArray("claim11-1", "uid11-1", "1Gi", "", v1.ClaimPending, &classGold, volume.AnnStorageProvisioner, volume.AnnBetaStorageProvisioner), initialClaims: newClaimArray("claim11-1", "uid11-1", "1Gi", "", v1.ClaimPending, &classGold),
[]string{"Normal ProvisioningSucceeded"}, noerrors, wrapTestWithProvisionCalls([]provisionCall{provision1Success}, testSyncClaim), expectedClaims: newClaimArray("claim11-1", "uid11-1", "1Gi", "", v1.ClaimPending, &classGold, volume.AnnStorageProvisioner, volume.AnnBetaStorageProvisioner),
expectedEvents: []string{"Normal ProvisioningSucceeded"},
errors: noerrors,
test: wrapTestWithProvisionCalls([]provisionCall{provision1Success}, testSyncClaim),
}, },
{ {
// Provision failure - plugin not found // Provision failure - plugin not found
"11-2 - plugin not found", name: "11-2 - plugin not found",
novolumes, initialVolumes: novolumes,
novolumes, expectedVolumes: novolumes,
newClaimArray("claim11-2", "uid11-2", "1Gi", "", v1.ClaimPending, &classGold), initialClaims: newClaimArray("claim11-2", "uid11-2", "1Gi", "", v1.ClaimPending, &classGold),
newClaimArray("claim11-2", "uid11-2", "1Gi", "", v1.ClaimPending, &classGold), expectedClaims: newClaimArray("claim11-2", "uid11-2", "1Gi", "", v1.ClaimPending, &classGold),
[]string{"Warning ProvisioningFailed"}, noerrors, expectedEvents: []string{"Warning ProvisioningFailed"},
testSyncClaim, errors: noerrors,
test: testSyncClaim,
}, },
{ {
// Provision failure - newProvisioner returns error // Provision failure - newProvisioner returns error
"11-3 - newProvisioner failure", name: "11-3 - newProvisioner failure",
novolumes, initialVolumes: novolumes,
novolumes, expectedVolumes: novolumes,
newClaimArray("claim11-3", "uid11-3", "1Gi", "", v1.ClaimPending, &classGold), initialClaims: newClaimArray("claim11-3", "uid11-3", "1Gi", "", v1.ClaimPending, &classGold),
newClaimArray("claim11-3", "uid11-3", "1Gi", "", v1.ClaimPending, &classGold, volume.AnnStorageProvisioner, volume.AnnBetaStorageProvisioner), expectedClaims: newClaimArray("claim11-3", "uid11-3", "1Gi", "", v1.ClaimPending, &classGold, volume.AnnStorageProvisioner, volume.AnnBetaStorageProvisioner),
[]string{"Warning ProvisioningFailed"}, noerrors, expectedEvents: []string{"Warning ProvisioningFailed"},
wrapTestWithProvisionCalls([]provisionCall{}, testSyncClaim), errors: noerrors,
test: wrapTestWithProvisionCalls([]provisionCall{}, testSyncClaim),
}, },
{ {
// Provision failure - Provision returns error // Provision failure - Provision returns error
"11-4 - provision failure", name: "11-4 - provision failure",
novolumes, initialVolumes: novolumes,
novolumes, expectedVolumes: novolumes,
newClaimArray("claim11-4", "uid11-4", "1Gi", "", v1.ClaimPending, &classGold), initialClaims: newClaimArray("claim11-4", "uid11-4", "1Gi", "", v1.ClaimPending, &classGold),
newClaimArray("claim11-4", "uid11-4", "1Gi", "", v1.ClaimPending, &classGold, volume.AnnStorageProvisioner, volume.AnnBetaStorageProvisioner), expectedClaims: newClaimArray("claim11-4", "uid11-4", "1Gi", "", v1.ClaimPending, &classGold, volume.AnnStorageProvisioner, volume.AnnBetaStorageProvisioner),
[]string{"Warning ProvisioningFailed"}, noerrors, expectedEvents: []string{"Warning ProvisioningFailed"},
wrapTestWithProvisionCalls([]provisionCall{provision1Error}, testSyncClaim), errors: noerrors,
test: wrapTestWithProvisionCalls([]provisionCall{provision1Error}, testSyncClaim),
}, },
{ {
// No provisioning if there is a matching volume available // No provisioning if there is a matching volume available
"11-6 - provisioning when there is a volume available", name: "11-6 - provisioning when there is a volume available",
newVolumeArray("volume11-6", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classGold), initialVolumes: newVolumeArray("volume11-6", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classGold),
newVolumeArray("volume11-6", "1Gi", "uid11-6", "claim11-6", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classGold, volume.AnnBoundByController), expectedVolumes: newVolumeArray("volume11-6", "1Gi", "uid11-6", "claim11-6", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classGold, volume.AnnBoundByController),
newClaimArray("claim11-6", "uid11-6", "1Gi", "", v1.ClaimPending, &classGold), initialClaims: newClaimArray("claim11-6", "uid11-6", "1Gi", "", v1.ClaimPending, &classGold),
newClaimArray("claim11-6", "uid11-6", "1Gi", "volume11-6", v1.ClaimBound, &classGold, volume.AnnBoundByController, volume.AnnBindCompleted), expectedClaims: newClaimArray("claim11-6", "uid11-6", "1Gi", "volume11-6", v1.ClaimBound, &classGold, volume.AnnBoundByController, volume.AnnBindCompleted),
noevents, noerrors, expectedEvents: noevents,
errors: noerrors,
// No provisioning plugin confingure - makes the test fail when // No provisioning plugin confingure - makes the test fail when
// the controller erroneously tries to provision something // the controller erroneously tries to provision something
wrapTestWithProvisionCalls([]provisionCall{provision1Success}, testSyncClaim), test: wrapTestWithProvisionCalls([]provisionCall{provision1Success}, testSyncClaim),
}, },
{ {
// Provision success? - claim is bound before provisioner creates // Provision success? - claim is bound before provisioner creates
// a volume. // a volume.
"11-7 - claim is bound before provisioning", name: "11-7 - claim is bound before provisioning",
novolumes, initialVolumes: novolumes,
newVolumeArray("pvc-uid11-7", "1Gi", "uid11-7", "claim11-7", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classGold, volume.AnnBoundByController, volume.AnnDynamicallyProvisioned), expectedVolumes: newVolumeArray("pvc-uid11-7", "1Gi", "uid11-7", "claim11-7", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classGold, volume.AnnBoundByController, volume.AnnDynamicallyProvisioned),
newClaimArray("claim11-7", "uid11-7", "1Gi", "", v1.ClaimPending, &classGold), initialClaims: newClaimArray("claim11-7", "uid11-7", "1Gi", "", v1.ClaimPending, &classGold),
// The claim would be bound in next syncClaim // The claim would be bound in next syncClaim
newClaimArray("claim11-7", "uid11-7", "1Gi", "", v1.ClaimPending, &classGold, volume.AnnStorageProvisioner, volume.AnnBetaStorageProvisioner), expectedClaims: newClaimArray("claim11-7", "uid11-7", "1Gi", "", v1.ClaimPending, &classGold, volume.AnnStorageProvisioner, volume.AnnBetaStorageProvisioner),
noevents, noerrors, expectedEvents: noevents,
wrapTestWithInjectedOperation(wrapTestWithProvisionCalls([]provisionCall{}, testSyncClaim), func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor) { errors: noerrors,
test: wrapTestWithInjectedOperation(wrapTestWithProvisionCalls([]provisionCall{}, testSyncClaim), func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor) {
// Create a volume before provisionClaimOperation starts. // Create a volume before provisionClaimOperation starts.
// This similates a parallel controller provisioning the volume. // This similates a parallel controller provisioning the volume.
volume := newVolume("pvc-uid11-7", "1Gi", "uid11-7", "claim11-7", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classGold, volume.AnnBoundByController, volume.AnnDynamicallyProvisioned) volume := newVolume("pvc-uid11-7", "1Gi", "uid11-7", "claim11-7", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classGold, volume.AnnBoundByController, volume.AnnDynamicallyProvisioned)
@ -245,31 +253,31 @@ func TestProvisionSync(t *testing.T) {
{ {
// Provision success - cannot save provisioned PV once, // Provision success - cannot save provisioned PV once,
// second retry succeeds // second retry succeeds
"11-8 - cannot save provisioned volume", name: "11-8 - cannot save provisioned volume",
novolumes, initialVolumes: novolumes,
volumesWithFinalizers(newVolumeArray("pvc-uid11-8", "1Gi", "uid11-8", "claim11-8", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classGold, volume.AnnBoundByController, volume.AnnDynamicallyProvisioned), []string{volume.PVDeletionInTreeProtectionFinalizer}), expectedVolumes: volumesWithFinalizers(newVolumeArray("pvc-uid11-8", "1Gi", "uid11-8", "claim11-8", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classGold, volume.AnnBoundByController, volume.AnnDynamicallyProvisioned), []string{volume.PVDeletionInTreeProtectionFinalizer}),
newClaimArray("claim11-8", "uid11-8", "1Gi", "", v1.ClaimPending, &classGold), initialClaims: newClaimArray("claim11-8", "uid11-8", "1Gi", "", v1.ClaimPending, &classGold),
// Binding will be completed in the next syncClaim // Binding will be completed in the next syncClaim
newClaimArray("claim11-8", "uid11-8", "1Gi", "", v1.ClaimPending, &classGold, volume.AnnStorageProvisioner, volume.AnnBetaStorageProvisioner), expectedClaims: newClaimArray("claim11-8", "uid11-8", "1Gi", "", v1.ClaimPending, &classGold, volume.AnnStorageProvisioner, volume.AnnBetaStorageProvisioner),
[]string{"Normal ProvisioningSucceeded"}, expectedEvents: []string{"Normal ProvisioningSucceeded"},
[]pvtesting.ReactorError{ errors: []pvtesting.ReactorError{
// Inject error to the first // Inject error to the first
// kubeclient.PersistentVolumes.Create() call. All other calls // kubeclient.PersistentVolumes.Create() call. All other calls
// will succeed. // will succeed.
{Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error")}, {Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error")},
}, },
wrapTestWithProvisionCalls([]provisionCall{provision1Success}, testSyncClaim), test: wrapTestWithProvisionCalls([]provisionCall{provision1Success}, testSyncClaim),
}, },
{ {
// Provision success? - cannot save provisioned PV five times, // Provision success? - cannot save provisioned PV five times,
// volume is deleted and delete succeeds // volume is deleted and delete succeeds
"11-9 - cannot save provisioned volume, delete succeeds", name: "11-9 - cannot save provisioned volume, delete succeeds",
novolumes, initialVolumes: novolumes,
novolumes, expectedVolumes: novolumes,
newClaimArray("claim11-9", "uid11-9", "1Gi", "", v1.ClaimPending, &classGold), initialClaims: newClaimArray("claim11-9", "uid11-9", "1Gi", "", v1.ClaimPending, &classGold),
newClaimArray("claim11-9", "uid11-9", "1Gi", "", v1.ClaimPending, &classGold, volume.AnnStorageProvisioner, volume.AnnBetaStorageProvisioner), expectedClaims: newClaimArray("claim11-9", "uid11-9", "1Gi", "", v1.ClaimPending, &classGold, volume.AnnStorageProvisioner, volume.AnnBetaStorageProvisioner),
[]string{"Warning ProvisioningFailed"}, expectedEvents: []string{"Warning ProvisioningFailed"},
[]pvtesting.ReactorError{ errors: []pvtesting.ReactorError{
// Inject error to five kubeclient.PersistentVolumes.Create() // Inject error to five kubeclient.PersistentVolumes.Create()
// calls // calls
{Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error1")}, {Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error1")},
@ -278,7 +286,7 @@ func TestProvisionSync(t *testing.T) {
{Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error4")}, {Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error4")},
{Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error5")}, {Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error5")},
}, },
wrapTestWithPluginCalls( test: wrapTestWithPluginCalls(
nil, // recycle calls nil, // recycle calls
[]error{nil}, // delete calls []error{nil}, // delete calls
[]provisionCall{provision1Success}, // provision calls []provisionCall{provision1Success}, // provision calls
@ -288,13 +296,13 @@ func TestProvisionSync(t *testing.T) {
{ {
// Provision failure - cannot save provisioned PV five times, // Provision failure - cannot save provisioned PV five times,
// volume delete failed - no plugin found // volume delete failed - no plugin found
"11-10 - cannot save provisioned volume, no delete plugin found", name: "11-10 - cannot save provisioned volume, no delete plugin found",
novolumes, initialVolumes: novolumes,
novolumes, expectedVolumes: novolumes,
newClaimArray("claim11-10", "uid11-10", "1Gi", "", v1.ClaimPending, &classGold), initialClaims: newClaimArray("claim11-10", "uid11-10", "1Gi", "", v1.ClaimPending, &classGold),
newClaimArray("claim11-10", "uid11-10", "1Gi", "", v1.ClaimPending, &classGold, volume.AnnStorageProvisioner, volume.AnnBetaStorageProvisioner), expectedClaims: newClaimArray("claim11-10", "uid11-10", "1Gi", "", v1.ClaimPending, &classGold, volume.AnnStorageProvisioner, volume.AnnBetaStorageProvisioner),
[]string{"Warning ProvisioningFailed", "Warning ProvisioningCleanupFailed"}, expectedEvents: []string{"Warning ProvisioningFailed", "Warning ProvisioningCleanupFailed"},
[]pvtesting.ReactorError{ errors: []pvtesting.ReactorError{
// Inject error to five kubeclient.PersistentVolumes.Create() // Inject error to five kubeclient.PersistentVolumes.Create()
// calls // calls
{Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error1")}, {Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error1")},
@ -304,18 +312,18 @@ func TestProvisionSync(t *testing.T) {
{Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error5")}, {Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error5")},
}, },
// No deleteCalls are configured, which results into no deleter plugin available for the volume // No deleteCalls are configured, which results into no deleter plugin available for the volume
wrapTestWithProvisionCalls([]provisionCall{provision1Success}, testSyncClaim), test: wrapTestWithProvisionCalls([]provisionCall{provision1Success}, testSyncClaim),
}, },
{ {
// Provision failure - cannot save provisioned PV five times, // Provision failure - cannot save provisioned PV five times,
// volume delete failed - deleter returns error five times // volume delete failed - deleter returns error five times
"11-11 - cannot save provisioned volume, deleter fails", name: "11-11 - cannot save provisioned volume, deleter fails",
novolumes, initialVolumes: novolumes,
novolumes, expectedVolumes: novolumes,
newClaimArray("claim11-11", "uid11-11", "1Gi", "", v1.ClaimPending, &classGold), initialClaims: newClaimArray("claim11-11", "uid11-11", "1Gi", "", v1.ClaimPending, &classGold),
newClaimArray("claim11-11", "uid11-11", "1Gi", "", v1.ClaimPending, &classGold, volume.AnnStorageProvisioner, volume.AnnBetaStorageProvisioner), expectedClaims: newClaimArray("claim11-11", "uid11-11", "1Gi", "", v1.ClaimPending, &classGold, volume.AnnStorageProvisioner, volume.AnnBetaStorageProvisioner),
[]string{"Warning ProvisioningFailed", "Warning ProvisioningCleanupFailed"}, expectedEvents: []string{"Warning ProvisioningFailed", "Warning ProvisioningCleanupFailed"},
[]pvtesting.ReactorError{ errors: []pvtesting.ReactorError{
// Inject error to five kubeclient.PersistentVolumes.Create() // Inject error to five kubeclient.PersistentVolumes.Create()
// calls // calls
{Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error1")}, {Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error1")},
@ -324,7 +332,7 @@ func TestProvisionSync(t *testing.T) {
{Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error4")}, {Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error4")},
{Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error5")}, {Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error5")},
}, },
wrapTestWithPluginCalls( test: wrapTestWithPluginCalls(
nil, // recycle calls nil, // recycle calls
[]error{ // delete calls []error{ // delete calls
errors.New("Mock deletion error1"), errors.New("Mock deletion error1"),
@ -339,13 +347,13 @@ func TestProvisionSync(t *testing.T) {
{ {
// Provision failure - cannot save provisioned PV five times, // Provision failure - cannot save provisioned PV five times,
// volume delete succeeds 2nd time // volume delete succeeds 2nd time
"11-12 - cannot save provisioned volume, delete succeeds 2nd time", name: "11-12 - cannot save provisioned volume, delete succeeds 2nd time",
novolumes, initialVolumes: novolumes,
novolumes, expectedVolumes: novolumes,
newClaimArray("claim11-12", "uid11-12", "1Gi", "", v1.ClaimPending, &classGold), initialClaims: newClaimArray("claim11-12", "uid11-12", "1Gi", "", v1.ClaimPending, &classGold),
newClaimArray("claim11-12", "uid11-12", "1Gi", "", v1.ClaimPending, &classGold, volume.AnnStorageProvisioner, volume.AnnBetaStorageProvisioner), expectedClaims: newClaimArray("claim11-12", "uid11-12", "1Gi", "", v1.ClaimPending, &classGold, volume.AnnStorageProvisioner, volume.AnnBetaStorageProvisioner),
[]string{"Warning ProvisioningFailed"}, expectedEvents: []string{"Warning ProvisioningFailed"},
[]pvtesting.ReactorError{ errors: []pvtesting.ReactorError{
// Inject error to five kubeclient.PersistentVolumes.Create() // Inject error to five kubeclient.PersistentVolumes.Create()
// calls // calls
{Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error1")}, {Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error1")},
@ -354,7 +362,7 @@ func TestProvisionSync(t *testing.T) {
{Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error4")}, {Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error4")},
{Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error5")}, {Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error5")},
}, },
wrapTestWithPluginCalls( test: wrapTestWithPluginCalls(
nil, // recycle calls nil, // recycle calls
[]error{ // delete calls []error{ // delete calls
errors.New("Mock deletion error1"), errors.New("Mock deletion error1"),
@ -366,62 +374,72 @@ func TestProvisionSync(t *testing.T) {
}, },
{ {
// Provision a volume (with non-default class) // Provision a volume (with non-default class)
"11-13 - successful provision with storage class 2", name: "11-13 - successful provision with storage class 2",
novolumes, initialVolumes: novolumes,
volumesWithFinalizers(newVolumeArray("pvc-uid11-13", "1Gi", "uid11-13", "claim11-13", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classSilver, volume.AnnBoundByController, volume.AnnDynamicallyProvisioned), []string{volume.PVDeletionInTreeProtectionFinalizer}), expectedVolumes: volumesWithFinalizers(newVolumeArray("pvc-uid11-13", "1Gi", "uid11-13", "claim11-13", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classSilver, volume.AnnBoundByController, volume.AnnDynamicallyProvisioned), []string{volume.PVDeletionInTreeProtectionFinalizer}),
newClaimArray("claim11-13", "uid11-13", "1Gi", "", v1.ClaimPending, &classSilver), initialClaims: newClaimArray("claim11-13", "uid11-13", "1Gi", "", v1.ClaimPending, &classSilver),
// Binding will be completed in the next syncClaim // Binding will be completed in the next syncClaim
newClaimArray("claim11-13", "uid11-13", "1Gi", "", v1.ClaimPending, &classSilver, volume.AnnStorageProvisioner, volume.AnnBetaStorageProvisioner), expectedClaims: newClaimArray("claim11-13", "uid11-13", "1Gi", "", v1.ClaimPending, &classSilver, volume.AnnStorageProvisioner, volume.AnnBetaStorageProvisioner),
[]string{"Normal ProvisioningSucceeded"}, noerrors, wrapTestWithProvisionCalls([]provisionCall{provision2Success}, testSyncClaim), expectedEvents: []string{"Normal ProvisioningSucceeded"},
errors: noerrors,
test: wrapTestWithProvisionCalls([]provisionCall{provision2Success}, testSyncClaim),
}, },
{ {
// Provision error - non existing class // Provision error - non existing class
"11-14 - fail due to non-existing class", name: "11-14 - fail due to non-existing class",
novolumes, initialVolumes: novolumes,
novolumes, expectedVolumes: novolumes,
newClaimArray("claim11-14", "uid11-14", "1Gi", "", v1.ClaimPending, &classNonExisting), initialClaims: newClaimArray("claim11-14", "uid11-14", "1Gi", "", v1.ClaimPending, &classNonExisting),
newClaimArray("claim11-14", "uid11-14", "1Gi", "", v1.ClaimPending, &classNonExisting), expectedClaims: newClaimArray("claim11-14", "uid11-14", "1Gi", "", v1.ClaimPending, &classNonExisting),
noevents, noerrors, wrapTestWithProvisionCalls([]provisionCall{}, testSyncClaim), expectedEvents: noevents,
errors: noerrors,
test: wrapTestWithProvisionCalls([]provisionCall{}, testSyncClaim),
}, },
{ {
// No provisioning with class="" // No provisioning with class=""
"11-15 - no provisioning with class=''", name: "11-15 - no provisioning with class=''",
novolumes, initialVolumes: novolumes,
novolumes, expectedVolumes: novolumes,
newClaimArray("claim11-15", "uid11-15", "1Gi", "", v1.ClaimPending, &classEmpty), initialClaims: newClaimArray("claim11-15", "uid11-15", "1Gi", "", v1.ClaimPending, &classEmpty),
newClaimArray("claim11-15", "uid11-15", "1Gi", "", v1.ClaimPending, &classEmpty), expectedClaims: newClaimArray("claim11-15", "uid11-15", "1Gi", "", v1.ClaimPending, &classEmpty),
noevents, noerrors, wrapTestWithProvisionCalls([]provisionCall{}, testSyncClaim), expectedEvents: noevents,
errors: noerrors,
test: wrapTestWithProvisionCalls([]provisionCall{}, testSyncClaim),
}, },
{ {
// No provisioning with class=nil // No provisioning with class=nil
"11-16 - no provisioning with class=nil", name: "11-16 - no provisioning with class=nil",
novolumes, initialVolumes: novolumes,
novolumes, expectedVolumes: novolumes,
newClaimArray("claim11-15", "uid11-15", "1Gi", "", v1.ClaimPending, nil), initialClaims: newClaimArray("claim11-15", "uid11-15", "1Gi", "", v1.ClaimPending, nil),
newClaimArray("claim11-15", "uid11-15", "1Gi", "", v1.ClaimPending, nil), expectedClaims: newClaimArray("claim11-15", "uid11-15", "1Gi", "", v1.ClaimPending, nil),
noevents, noerrors, wrapTestWithProvisionCalls([]provisionCall{}, testSyncClaim), expectedEvents: noevents,
errors: noerrors,
test: wrapTestWithProvisionCalls([]provisionCall{}, testSyncClaim),
}, },
{ {
// No provisioning + normal event with external provisioner // No provisioning + normal event with external provisioner
"11-17 - external provisioner", name: "11-17 - external provisioner",
novolumes, initialVolumes: novolumes,
novolumes, expectedVolumes: novolumes,
newClaimArray("claim11-17", "uid11-17", "1Gi", "", v1.ClaimPending, &classExternal), initialClaims: newClaimArray("claim11-17", "uid11-17", "1Gi", "", v1.ClaimPending, &classExternal),
claimWithAnnotation(volume.AnnBetaStorageProvisioner, "vendor.com/my-volume", expectedClaims: claimWithAnnotation(volume.AnnBetaStorageProvisioner, "vendor.com/my-volume",
claimWithAnnotation(volume.AnnStorageProvisioner, "vendor.com/my-volume", claimWithAnnotation(volume.AnnStorageProvisioner, "vendor.com/my-volume",
newClaimArray("claim11-17", "uid11-17", "1Gi", "", v1.ClaimPending, &classExternal))), newClaimArray("claim11-17", "uid11-17", "1Gi", "", v1.ClaimPending, &classExternal))),
[]string{"Normal ExternalProvisioning"}, expectedEvents: []string{"Normal ExternalProvisioning"},
noerrors, wrapTestWithProvisionCalls([]provisionCall{}, testSyncClaim), errors: noerrors,
test: wrapTestWithProvisionCalls([]provisionCall{}, testSyncClaim),
}, },
{ {
// No provisioning + warning event with unknown internal provisioner // No provisioning + warning event with unknown internal provisioner
"11-18 - unknown internal provisioner", name: "11-18 - unknown internal provisioner",
novolumes, initialVolumes: novolumes,
novolumes, expectedVolumes: novolumes,
newClaimArray("claim11-18", "uid11-18", "1Gi", "", v1.ClaimPending, &classUnknownInternal), initialClaims: newClaimArray("claim11-18", "uid11-18", "1Gi", "", v1.ClaimPending, &classUnknownInternal),
newClaimArray("claim11-18", "uid11-18", "1Gi", "", v1.ClaimPending, &classUnknownInternal), expectedClaims: newClaimArray("claim11-18", "uid11-18", "1Gi", "", v1.ClaimPending, &classUnknownInternal),
[]string{"Warning ProvisioningFailed"}, expectedEvents: []string{"Warning ProvisioningFailed"},
noerrors, wrapTestWithProvisionCalls([]provisionCall{}, testSyncClaim), errors: noerrors,
test: wrapTestWithProvisionCalls([]provisionCall{}, testSyncClaim),
}, },
{ {
// Provision success - first save of a PV to API server fails (API // Provision success - first save of a PV to API server fails (API
@ -455,22 +473,23 @@ func TestProvisionSync(t *testing.T) {
}, },
{ {
// No provisioning + warning event with unsupported storageClass.mountOptions // No provisioning + warning event with unsupported storageClass.mountOptions
"11-20 - unsupported storageClass.mountOptions", name: "11-20 - unsupported storageClass.mountOptions",
novolumes, initialVolumes: novolumes,
novolumes, expectedVolumes: novolumes,
newClaimArray("claim11-20", "uid11-20", "1Gi", "", v1.ClaimPending, &classUnsupportedMountOptions), initialClaims: newClaimArray("claim11-20", "uid11-20", "1Gi", "", v1.ClaimPending, &classUnsupportedMountOptions),
newClaimArray("claim11-20", "uid11-20", "1Gi", "", v1.ClaimPending, &classUnsupportedMountOptions, volume.AnnStorageProvisioner, volume.AnnBetaStorageProvisioner), expectedClaims: newClaimArray("claim11-20", "uid11-20", "1Gi", "", v1.ClaimPending, &classUnsupportedMountOptions, volume.AnnStorageProvisioner, volume.AnnBetaStorageProvisioner),
// Expect event to be prefixed with "Mount options" because saving PV will fail anyway // Expect event to be prefixed with "Mount options" because saving PV will fail anyway
[]string{"Warning ProvisioningFailed Mount options"}, expectedEvents: []string{"Warning ProvisioningFailed Mount options"},
noerrors, wrapTestWithProvisionCalls([]provisionCall{}, testSyncClaim), errors: noerrors,
test: wrapTestWithProvisionCalls([]provisionCall{}, testSyncClaim),
}, },
{ {
// No provisioning due to CSI migration + normal event with external provisioner // No provisioning due to CSI migration + normal event with external provisioner
"11-21 - external provisioner for CSI migration", name: "11-21 - external provisioner for CSI migration",
novolumes, initialVolumes: novolumes,
novolumes, expectedVolumes: novolumes,
newClaimArray("claim11-21", "uid11-21", "1Gi", "", v1.ClaimPending, &classGold), initialClaims: newClaimArray("claim11-21", "uid11-21", "1Gi", "", v1.ClaimPending, &classGold),
[]*v1.PersistentVolumeClaim{ expectedClaims: []*v1.PersistentVolumeClaim{
annotateClaim( annotateClaim(
newClaim("claim11-21", "uid11-21", "1Gi", "", v1.ClaimPending, &classGold), newClaim("claim11-21", "uid11-21", "1Gi", "", v1.ClaimPending, &classGold),
map[string]string{ map[string]string{
@ -479,19 +498,21 @@ func TestProvisionSync(t *testing.T) {
volume.AnnMigratedTo: "vendor.com/MockCSIDriver", volume.AnnMigratedTo: "vendor.com/MockCSIDriver",
}), }),
}, },
[]string{"Normal ExternalProvisioning"}, expectedEvents: []string{"Normal ExternalProvisioning"},
noerrors, wrapTestWithCSIMigrationProvisionCalls(testSyncClaim), errors: noerrors,
test: wrapTestWithCSIMigrationProvisionCalls(testSyncClaim),
}, },
{ {
// volume provisioned and available // volume provisioned and available
// in this case, NO normal event with external provisioner should be issued // in this case, NO normal event with external provisioner should be issued
"11-22 - external provisioner with volume available", name: "11-22 - external provisioner with volume available",
newVolumeArray("volume11-22", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classExternal), initialVolumes: newVolumeArray("volume11-22", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classExternal),
newVolumeArray("volume11-22", "1Gi", "uid11-22", "claim11-22", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classExternal, volume.AnnBoundByController), expectedVolumes: newVolumeArray("volume11-22", "1Gi", "uid11-22", "claim11-22", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classExternal, volume.AnnBoundByController),
newClaimArray("claim11-22", "uid11-22", "1Gi", "", v1.ClaimPending, &classExternal), initialClaims: newClaimArray("claim11-22", "uid11-22", "1Gi", "", v1.ClaimPending, &classExternal),
newClaimArray("claim11-22", "uid11-22", "1Gi", "volume11-22", v1.ClaimBound, &classExternal, volume.AnnBoundByController, volume.AnnBindCompleted), expectedClaims: newClaimArray("claim11-22", "uid11-22", "1Gi", "volume11-22", v1.ClaimBound, &classExternal, volume.AnnBoundByController, volume.AnnBindCompleted),
noevents, expectedEvents: noevents,
noerrors, wrapTestWithProvisionCalls([]provisionCall{}, testSyncClaim), errors: noerrors,
test: wrapTestWithProvisionCalls([]provisionCall{}, testSyncClaim),
}, },
{ {
// volume provision for PVC scheduled // volume provision for PVC scheduled
@ -517,28 +538,30 @@ func TestProvisionSync(t *testing.T) {
}, },
{ {
// volume provision for PVC that scheduled // volume provision for PVC that scheduled
"11-24 - skip finding PV and wait external provisioner for PVC annotated with AnnSelectedNode", name: "11-24 - skip finding PV and wait external provisioner for PVC annotated with AnnSelectedNode",
newVolumeArray("volume11-24", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimDelete, classExternalWait), initialVolumes: newVolumeArray("volume11-24", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimDelete, classExternalWait),
newVolumeArray("volume11-24", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimDelete, classExternalWait), expectedVolumes: newVolumeArray("volume11-24", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimDelete, classExternalWait),
claimWithAnnotation(volume.AnnSelectedNode, "node1", initialClaims: claimWithAnnotation(volume.AnnSelectedNode, "node1",
newClaimArray("claim11-24", "uid11-24", "1Gi", "", v1.ClaimPending, &classExternalWait)), newClaimArray("claim11-24", "uid11-24", "1Gi", "", v1.ClaimPending, &classExternalWait)),
claimWithAnnotation(volume.AnnBetaStorageProvisioner, "vendor.com/my-volume-wait", expectedClaims: claimWithAnnotation(volume.AnnBetaStorageProvisioner, "vendor.com/my-volume-wait",
claimWithAnnotation(volume.AnnStorageProvisioner, "vendor.com/my-volume-wait", claimWithAnnotation(volume.AnnStorageProvisioner, "vendor.com/my-volume-wait",
claimWithAnnotation(volume.AnnSelectedNode, "node1", claimWithAnnotation(volume.AnnSelectedNode, "node1",
newClaimArray("claim11-24", "uid11-24", "1Gi", "", v1.ClaimPending, &classExternalWait)))), newClaimArray("claim11-24", "uid11-24", "1Gi", "", v1.ClaimPending, &classExternalWait)))),
[]string{"Normal ExternalProvisioning"}, expectedEvents: []string{"Normal ExternalProvisioning"},
noerrors, testSyncClaim, errors: noerrors,
test: testSyncClaim,
}, },
{ {
// Provision a volume with a data source will fail // Provision a volume with a data source will fail
// for in-tree plugins // for in-tree plugins
"11-25 - failed in-tree provision with data source", name: "11-25 - failed in-tree provision with data source",
novolumes, initialVolumes: novolumes,
novolumes, expectedVolumes: novolumes,
claimWithDataSource("test-snap", "VolumeSnapshot", "snapshot.storage.k8s.io", newClaimArray("claim11-25", "uid11-25", "1Gi", "", v1.ClaimPending, &classGold)), initialClaims: claimWithDataSource("test-snap", "VolumeSnapshot", "snapshot.storage.k8s.io", newClaimArray("claim11-25", "uid11-25", "1Gi", "", v1.ClaimPending, &classGold)),
claimWithDataSource("test-snap", "VolumeSnapshot", "snapshot.storage.k8s.io", newClaimArray("claim11-25", "uid11-25", "1Gi", "", v1.ClaimPending, &classGold)), expectedClaims: claimWithDataSource("test-snap", "VolumeSnapshot", "snapshot.storage.k8s.io", newClaimArray("claim11-25", "uid11-25", "1Gi", "", v1.ClaimPending, &classGold)),
[]string{"Warning ProvisioningFailed"}, noerrors, expectedEvents: []string{"Warning ProvisioningFailed"},
testSyncClaim, errors: noerrors,
test: testSyncClaim,
}, },
{ {
// Provision a volume with a data source will proceed // Provision a volume with a data source will proceed
@ -551,7 +574,8 @@ func TestProvisionSync(t *testing.T) {
claimWithAnnotation(volume.AnnStorageProvisioner, "mydriver.csi.k8s.io", claimWithAnnotation(volume.AnnStorageProvisioner, "mydriver.csi.k8s.io",
claimWithDataSource("test-snap", "VolumeSnapshot", "snapshot.storage.k8s.io", newClaimArray("claim11-26", "uid11-26", "1Gi", "", v1.ClaimPending, &classCSI))), claimWithDataSource("test-snap", "VolumeSnapshot", "snapshot.storage.k8s.io", newClaimArray("claim11-26", "uid11-26", "1Gi", "", v1.ClaimPending, &classCSI))),
[]string{"Normal ExternalProvisioning"}, []string{"Normal ExternalProvisioning"},
noerrors, wrapTestWithProvisionCalls([]provisionCall{}, testSyncClaim), noerrors,
wrapTestWithProvisionCalls([]provisionCall{}, testSyncClaim),
}, },
} }
runSyncTests(t, tests, storageClasses, []*v1.Pod{}) runSyncTests(t, tests, storageClasses, []*v1.Pod{})
@ -576,25 +600,27 @@ func TestProvisionMultiSync(t *testing.T) {
tests := []controllerTest{ tests := []controllerTest{
{ {
// Provision a volume with binding // Provision a volume with binding
"12-1 - successful provision", name: "12-1 - successful provision",
novolumes, initialVolumes: novolumes,
newVolumeArray("pvc-uid12-1", "1Gi", "uid12-1", "claim12-1", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classGold, volume.AnnBoundByController, volume.AnnDynamicallyProvisioned), expectedVolumes: newVolumeArray("pvc-uid12-1", "1Gi", "uid12-1", "claim12-1", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classGold, volume.AnnBoundByController, volume.AnnDynamicallyProvisioned),
newClaimArray("claim12-1", "uid12-1", "1Gi", "", v1.ClaimPending, &classGold), initialClaims: newClaimArray("claim12-1", "uid12-1", "1Gi", "", v1.ClaimPending, &classGold),
newClaimArray("claim12-1", "uid12-1", "1Gi", "pvc-uid12-1", v1.ClaimBound, &classGold, volume.AnnBoundByController, volume.AnnBindCompleted, volume.AnnStorageProvisioner, volume.AnnBetaStorageProvisioner), expectedClaims: newClaimArray("claim12-1", "uid12-1", "1Gi", "pvc-uid12-1", v1.ClaimBound, &classGold, volume.AnnBoundByController, volume.AnnBindCompleted, volume.AnnStorageProvisioner, volume.AnnBetaStorageProvisioner),
noevents, noerrors, wrapTestWithProvisionCalls([]provisionCall{provision1Success}, testSyncClaim), expectedEvents: noevents,
errors: noerrors,
test: wrapTestWithProvisionCalls([]provisionCall{provision1Success}, testSyncClaim),
}, },
{ {
// provision a volume (external provisioner) and binding + normal event with external provisioner // provision a volume (external provisioner) and binding + normal event with external provisioner
"12-2 - external provisioner with volume provisioned success", name: "12-2 - external provisioner with volume provisioned success",
novolumes, initialVolumes: novolumes,
newVolumeArray("pvc-uid12-2", "1Gi", "uid12-2", "claim12-2", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classExternal, volume.AnnBoundByController), expectedVolumes: newVolumeArray("pvc-uid12-2", "1Gi", "uid12-2", "claim12-2", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classExternal, volume.AnnBoundByController),
newClaimArray("claim12-2", "uid12-2", "1Gi", "", v1.ClaimPending, &classExternal), initialClaims: newClaimArray("claim12-2", "uid12-2", "1Gi", "", v1.ClaimPending, &classExternal),
claimWithAnnotation(volume.AnnBetaStorageProvisioner, "vendor.com/my-volume", expectedClaims: claimWithAnnotation(volume.AnnBetaStorageProvisioner, "vendor.com/my-volume",
claimWithAnnotation(volume.AnnStorageProvisioner, "vendor.com/my-volume", claimWithAnnotation(volume.AnnStorageProvisioner, "vendor.com/my-volume",
newClaimArray("claim12-2", "uid12-2", "1Gi", "pvc-uid12-2", v1.ClaimBound, &classExternal, volume.AnnBoundByController, volume.AnnBindCompleted))), newClaimArray("claim12-2", "uid12-2", "1Gi", "pvc-uid12-2", v1.ClaimBound, &classExternal, volume.AnnBoundByController, volume.AnnBindCompleted))),
[]string{"Normal ExternalProvisioning"}, expectedEvents: []string{"Normal ExternalProvisioning"},
noerrors, errors: noerrors,
wrapTestWithInjectedOperation(wrapTestWithProvisionCalls([]provisionCall{}, testSyncClaim), func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor) { test: wrapTestWithInjectedOperation(wrapTestWithProvisionCalls([]provisionCall{}, testSyncClaim), func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor) {
// Create a volume before syncClaim tries to bind a PV to PVC // Create a volume before syncClaim tries to bind a PV to PVC
// This simulates external provisioner creating a volume while the controller // This simulates external provisioner creating a volume while the controller
// is waiting for a volume to bind to the existed claim // is waiting for a volume to bind to the existed claim
@ -611,29 +637,29 @@ func TestProvisionMultiSync(t *testing.T) {
}, },
{ {
// provision a volume (external provisioner) but binding will not happen + normal event with external provisioner // provision a volume (external provisioner) but binding will not happen + normal event with external provisioner
"12-3 - external provisioner with volume to be provisioned", name: "12-3 - external provisioner with volume to be provisioned",
novolumes, initialVolumes: novolumes,
novolumes, expectedVolumes: novolumes,
newClaimArray("claim12-3", "uid12-3", "1Gi", "", v1.ClaimPending, &classExternal), initialClaims: newClaimArray("claim12-3", "uid12-3", "1Gi", "", v1.ClaimPending, &classExternal),
claimWithAnnotation(volume.AnnBetaStorageProvisioner, "vendor.com/my-volume", expectedClaims: claimWithAnnotation(volume.AnnBetaStorageProvisioner, "vendor.com/my-volume",
claimWithAnnotation(volume.AnnStorageProvisioner, "vendor.com/my-volume", claimWithAnnotation(volume.AnnStorageProvisioner, "vendor.com/my-volume",
newClaimArray("claim12-3", "uid12-3", "1Gi", "", v1.ClaimPending, &classExternal))), newClaimArray("claim12-3", "uid12-3", "1Gi", "", v1.ClaimPending, &classExternal))),
[]string{"Normal ExternalProvisioning"}, expectedEvents: []string{"Normal ExternalProvisioning"},
noerrors, errors: noerrors,
wrapTestWithProvisionCalls([]provisionCall{provision1Success}, testSyncClaim), test: wrapTestWithProvisionCalls([]provisionCall{provision1Success}, testSyncClaim),
}, },
{ {
// provision a volume (external provisioner) and binding + normal event with external provisioner // provision a volume (external provisioner) and binding + normal event with external provisioner
"12-4 - external provisioner with volume provisioned/bound success", name: "12-4 - external provisioner with volume provisioned/bound success",
novolumes, initialVolumes: novolumes,
newVolumeArray("pvc-uid12-4", "1Gi", "uid12-4", "claim12-4", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classExternal, volume.AnnBoundByController), expectedVolumes: newVolumeArray("pvc-uid12-4", "1Gi", "uid12-4", "claim12-4", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classExternal, volume.AnnBoundByController),
newClaimArray("claim12-4", "uid12-4", "1Gi", "", v1.ClaimPending, &classExternal), initialClaims: newClaimArray("claim12-4", "uid12-4", "1Gi", "", v1.ClaimPending, &classExternal),
claimWithAnnotation(volume.AnnBetaStorageProvisioner, "vendor.com/my-volume", expectedClaims: claimWithAnnotation(volume.AnnBetaStorageProvisioner, "vendor.com/my-volume",
claimWithAnnotation(volume.AnnStorageProvisioner, "vendor.com/my-volume", claimWithAnnotation(volume.AnnStorageProvisioner, "vendor.com/my-volume",
newClaimArray("claim12-4", "uid12-4", "1Gi", "pvc-uid12-4", v1.ClaimBound, &classExternal, volume.AnnBoundByController, volume.AnnBindCompleted))), newClaimArray("claim12-4", "uid12-4", "1Gi", "pvc-uid12-4", v1.ClaimBound, &classExternal, volume.AnnBoundByController, volume.AnnBindCompleted))),
[]string{"Normal ExternalProvisioning"}, expectedEvents: []string{"Normal ExternalProvisioning"},
noerrors, errors: noerrors,
wrapTestWithInjectedOperation(wrapTestWithProvisionCalls([]provisionCall{}, testSyncClaim), func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor) { test: wrapTestWithInjectedOperation(wrapTestWithProvisionCalls([]provisionCall{}, testSyncClaim), func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor) {
// Create a volume before syncClaim tries to bind a PV to PVC // Create a volume before syncClaim tries to bind a PV to PVC
// This simulates external provisioner creating a volume while the controller // This simulates external provisioner creating a volume while the controller
// is waiting for a volume to bind to the existed claim // is waiting for a volume to bind to the existed claim

View File

@ -61,51 +61,55 @@ func TestControllerSync(t *testing.T) {
// sent to add/update/delete Claim/Volume as real controller would do. // sent to add/update/delete Claim/Volume as real controller would do.
{ {
// addClaim gets a new claim. Check it's bound to a volume. // addClaim gets a new claim. Check it's bound to a volume.
"5-2 - complete bind", name: "5-2 - complete bind",
newVolumeArray("volume5-2", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty), initialVolumes: newVolumeArray("volume5-2", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
newVolumeArray("volume5-2", "1Gi", "uid5-2", "claim5-2", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, volume.AnnBoundByController), expectedVolumes: newVolumeArray("volume5-2", "1Gi", "uid5-2", "claim5-2", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, volume.AnnBoundByController),
noclaims, /* added in testAddClaim5_2 */ initialClaims: noclaims, /* added in testAddClaim5_2 */
newClaimArray("claim5-2", "uid5-2", "1Gi", "volume5-2", v1.ClaimBound, nil, volume.AnnBoundByController, volume.AnnBindCompleted), expectedClaims: newClaimArray("claim5-2", "uid5-2", "1Gi", "volume5-2", v1.ClaimBound, nil, volume.AnnBoundByController, volume.AnnBindCompleted),
noevents, noerrors, expectedEvents: noevents,
errors: noerrors,
// Custom test function that generates an add event // Custom test function that generates an add event
func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor, test controllerTest) error { test: func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor, test controllerTest) error {
claim := newClaim("claim5-2", "uid5-2", "1Gi", "", v1.ClaimPending, nil) claim := newClaim("claim5-2", "uid5-2", "1Gi", "", v1.ClaimPending, nil)
reactor.AddClaimEvent(claim) reactor.AddClaimEvent(claim)
return nil return nil
}, },
}, },
{ {
"5-2-2 - complete bind when PV and PVC both exist", name: "5-2-2 - complete bind when PV and PVC both exist",
newVolumeArray("volume5-2", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty), initialVolumes: newVolumeArray("volume5-2", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
newVolumeArray("volume5-2", "1Gi", "uid5-2", "claim5-2", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, volume.AnnBoundByController), expectedVolumes: newVolumeArray("volume5-2", "1Gi", "uid5-2", "claim5-2", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, volume.AnnBoundByController),
newClaimArray("claim5-2", "uid5-2", "1Gi", "", v1.ClaimPending, nil), initialClaims: newClaimArray("claim5-2", "uid5-2", "1Gi", "", v1.ClaimPending, nil),
newClaimArray("claim5-2", "uid5-2", "1Gi", "volume5-2", v1.ClaimBound, nil, volume.AnnBoundByController, volume.AnnBindCompleted), expectedClaims: newClaimArray("claim5-2", "uid5-2", "1Gi", "volume5-2", v1.ClaimBound, nil, volume.AnnBoundByController, volume.AnnBindCompleted),
noevents, noerrors, expectedEvents: noevents,
func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor, test controllerTest) error { errors: noerrors,
test: func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor, test controllerTest) error {
return nil return nil
}, },
}, },
{ {
"5-2-3 - complete bind when PV and PVC both exist and PV has AnnPreResizeCapacity annotation", name: "5-2-3 - complete bind when PV and PVC both exist and PV has AnnPreResizeCapacity annotation",
volumesWithAnnotation(util.AnnPreResizeCapacity, "1Gi", newVolumeArray("volume5-2", "2Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty, volume.AnnBoundByController)), initialVolumes: volumesWithAnnotation(util.AnnPreResizeCapacity, "1Gi", newVolumeArray("volume5-2", "2Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty, volume.AnnBoundByController)),
volumesWithAnnotation(util.AnnPreResizeCapacity, "1Gi", newVolumeArray("volume5-2", "2Gi", "uid5-2", "claim5-2", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, volume.AnnBoundByController)), expectedVolumes: volumesWithAnnotation(util.AnnPreResizeCapacity, "1Gi", newVolumeArray("volume5-2", "2Gi", "uid5-2", "claim5-2", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, volume.AnnBoundByController)),
withExpectedCapacity("2Gi", newClaimArray("claim5-2", "uid5-2", "2Gi", "", v1.ClaimPending, nil)), initialClaims: withExpectedCapacity("2Gi", newClaimArray("claim5-2", "uid5-2", "2Gi", "", v1.ClaimPending, nil)),
withExpectedCapacity("1Gi", newClaimArray("claim5-2", "uid5-2", "2Gi", "volume5-2", v1.ClaimBound, nil, volume.AnnBoundByController, volume.AnnBindCompleted)), expectedClaims: withExpectedCapacity("1Gi", newClaimArray("claim5-2", "uid5-2", "2Gi", "volume5-2", v1.ClaimBound, nil, volume.AnnBoundByController, volume.AnnBindCompleted)),
noevents, noerrors, expectedEvents: noevents,
func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor, test controllerTest) error { errors: noerrors,
test: func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor, test controllerTest) error {
return nil return nil
}, },
}, },
{ {
// deleteClaim with a bound claim makes bound volume released. // deleteClaim with a bound claim makes bound volume released.
"5-3 - delete claim", name: "5-3 - delete claim",
newVolumeArray("volume5-3", "10Gi", "uid5-3", "claim5-3", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, volume.AnnBoundByController), initialVolumes: newVolumeArray("volume5-3", "10Gi", "uid5-3", "claim5-3", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, volume.AnnBoundByController),
newVolumeArray("volume5-3", "10Gi", "uid5-3", "claim5-3", v1.VolumeReleased, v1.PersistentVolumeReclaimRetain, classEmpty, volume.AnnBoundByController), expectedVolumes: newVolumeArray("volume5-3", "10Gi", "uid5-3", "claim5-3", v1.VolumeReleased, v1.PersistentVolumeReclaimRetain, classEmpty, volume.AnnBoundByController),
newClaimArray("claim5-3", "uid5-3", "1Gi", "volume5-3", v1.ClaimBound, nil, volume.AnnBoundByController, volume.AnnBindCompleted), initialClaims: newClaimArray("claim5-3", "uid5-3", "1Gi", "volume5-3", v1.ClaimBound, nil, volume.AnnBoundByController, volume.AnnBindCompleted),
noclaims, expectedClaims: noclaims,
noevents, noerrors, expectedEvents: noevents,
errors: noerrors,
// Custom test function that generates a delete event // Custom test function that generates a delete event
func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor, test controllerTest) error { test: func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor, test controllerTest) error {
obj := ctrl.claims.List()[0] obj := ctrl.claims.List()[0]
claim := obj.(*v1.PersistentVolumeClaim) claim := obj.(*v1.PersistentVolumeClaim)
reactor.DeleteClaimEvent(claim) reactor.DeleteClaimEvent(claim)
@ -114,14 +118,15 @@ func TestControllerSync(t *testing.T) {
}, },
{ {
// deleteVolume with a bound volume. Check the claim is Lost. // deleteVolume with a bound volume. Check the claim is Lost.
"5-4 - delete volume", name: "5-4 - delete volume",
newVolumeArray("volume5-4", "1Gi", "uid5-4", "claim5-4", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty), initialVolumes: newVolumeArray("volume5-4", "1Gi", "uid5-4", "claim5-4", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty),
novolumes, expectedVolumes: novolumes,
newClaimArray("claim5-4", "uid5-4", "1Gi", "volume5-4", v1.ClaimBound, nil, volume.AnnBoundByController, volume.AnnBindCompleted), initialClaims: newClaimArray("claim5-4", "uid5-4", "1Gi", "volume5-4", v1.ClaimBound, nil, volume.AnnBoundByController, volume.AnnBindCompleted),
newClaimArray("claim5-4", "uid5-4", "1Gi", "volume5-4", v1.ClaimLost, nil, volume.AnnBoundByController, volume.AnnBindCompleted), expectedClaims: newClaimArray("claim5-4", "uid5-4", "1Gi", "volume5-4", v1.ClaimLost, nil, volume.AnnBoundByController, volume.AnnBindCompleted),
[]string{"Warning ClaimLost"}, noerrors, expectedEvents: []string{"Warning ClaimLost"},
errors: noerrors,
// Custom test function that generates a delete event // Custom test function that generates a delete event
func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor, test controllerTest) error { test: func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor, test controllerTest) error {
obj := ctrl.volumes.store.List()[0] obj := ctrl.volumes.store.List()[0]
volume := obj.(*v1.PersistentVolume) volume := obj.(*v1.PersistentVolume)
reactor.DeleteVolumeEvent(volume) reactor.DeleteVolumeEvent(volume)
@ -131,18 +136,19 @@ func TestControllerSync(t *testing.T) {
{ {
// deleteClaim with a bound claim makes bound volume released with external deleter. // deleteClaim with a bound claim makes bound volume released with external deleter.
// delete the corresponding volume from apiserver, and report latency metric // delete the corresponding volume from apiserver, and report latency metric
"5-5 - delete claim and delete volume report metric", name: "5-5 - delete claim and delete volume report metric",
volumesWithAnnotation(volume.AnnDynamicallyProvisioned, "gcr.io/vendor-csi", initialVolumes: volumesWithAnnotation(volume.AnnDynamicallyProvisioned, "gcr.io/vendor-csi",
newVolumeArray("volume5-5", "10Gi", "uid5-5", "claim5-5", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classExternal, volume.AnnBoundByController)), newVolumeArray("volume5-5", "10Gi", "uid5-5", "claim5-5", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classExternal, volume.AnnBoundByController)),
novolumes, expectedVolumes: novolumes,
claimWithAnnotation(volume.AnnStorageProvisioner, "gcr.io/vendor-csi", initialClaims: claimWithAnnotation(volume.AnnStorageProvisioner, "gcr.io/vendor-csi",
newClaimArray("claim5-5", "uid5-5", "1Gi", "volume5-5", v1.ClaimBound, &classExternal, volume.AnnBoundByController, volume.AnnBindCompleted)), newClaimArray("claim5-5", "uid5-5", "1Gi", "volume5-5", v1.ClaimBound, &classExternal, volume.AnnBoundByController, volume.AnnBindCompleted)),
noclaims, expectedClaims: noclaims,
noevents, noerrors, expectedEvents: noevents,
errors: noerrors,
// Custom test function that generates a delete claim event which should have been caught by // Custom test function that generates a delete claim event which should have been caught by
// "deleteClaim" to remove the claim from controller's cache, after that, a volume deleted // "deleteClaim" to remove the claim from controller's cache, after that, a volume deleted
// event will be generated to trigger "deleteVolume" call for metric reporting // event will be generated to trigger "deleteVolume" call for metric reporting
func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor, test controllerTest) error { test: func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor, test controllerTest) error {
test.initialVolumes[0].Annotations[volume.AnnDynamicallyProvisioned] = "gcr.io/vendor-csi" test.initialVolumes[0].Annotations[volume.AnnDynamicallyProvisioned] = "gcr.io/vendor-csi"
obj := ctrl.claims.List()[0] obj := ctrl.claims.List()[0]
claim := obj.(*v1.PersistentVolumeClaim) claim := obj.(*v1.PersistentVolumeClaim)
@ -162,16 +168,17 @@ func TestControllerSync(t *testing.T) {
{ {
// deleteClaim with a bound claim makes bound volume released with external deleter pending // deleteClaim with a bound claim makes bound volume released with external deleter pending
// there should be an entry in operation timestamps cache in controller // there should be an entry in operation timestamps cache in controller
"5-6 - delete claim and waiting for external volume deletion", name: "5-6 - delete claim and waiting for external volume deletion",
volumesWithAnnotation(volume.AnnDynamicallyProvisioned, "gcr.io/vendor-csi", []*v1.PersistentVolume{newExternalProvisionedVolume("volume5-6", "10Gi", "uid5-6", "claim5-6", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classExternal, "fake.driver.csi", nil, volume.AnnBoundByController)}), initialVolumes: volumesWithAnnotation(volume.AnnDynamicallyProvisioned, "gcr.io/vendor-csi", []*v1.PersistentVolume{newExternalProvisionedVolume("volume5-6", "10Gi", "uid5-6", "claim5-6", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classExternal, "fake.driver.csi", nil, volume.AnnBoundByController)}),
volumesWithAnnotation(volume.AnnDynamicallyProvisioned, "gcr.io/vendor-csi", []*v1.PersistentVolume{newExternalProvisionedVolume("volume5-6", "10Gi", "uid5-6", "claim5-6", v1.VolumeReleased, v1.PersistentVolumeReclaimDelete, classExternal, "fake.driver.csi", nil, volume.AnnBoundByController)}), expectedVolumes: volumesWithAnnotation(volume.AnnDynamicallyProvisioned, "gcr.io/vendor-csi", []*v1.PersistentVolume{newExternalProvisionedVolume("volume5-6", "10Gi", "uid5-6", "claim5-6", v1.VolumeReleased, v1.PersistentVolumeReclaimDelete, classExternal, "fake.driver.csi", nil, volume.AnnBoundByController)}),
claimWithAnnotation(volume.AnnStorageProvisioner, "gcr.io/vendor-csi", initialClaims: claimWithAnnotation(volume.AnnStorageProvisioner, "gcr.io/vendor-csi",
newClaimArray("claim5-6", "uid5-6", "1Gi", "volume5-6", v1.ClaimBound, &classExternal, volume.AnnBoundByController, volume.AnnBindCompleted)), newClaimArray("claim5-6", "uid5-6", "1Gi", "volume5-6", v1.ClaimBound, &classExternal, volume.AnnBoundByController, volume.AnnBindCompleted)),
noclaims, expectedClaims: noclaims,
noevents, noerrors, expectedEvents: noevents,
errors: noerrors,
// Custom test function that generates a delete claim event which should have been caught by // Custom test function that generates a delete claim event which should have been caught by
// "deleteClaim" to remove the claim from controller's cache and mark bound volume to be released // "deleteClaim" to remove the claim from controller's cache and mark bound volume to be released
func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor, test controllerTest) error { test: func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor, test controllerTest) error {
// should have been provisioned by external provisioner // should have been provisioned by external provisioner
obj := ctrl.claims.List()[0] obj := ctrl.claims.List()[0]
claim := obj.(*v1.PersistentVolumeClaim) claim := obj.(*v1.PersistentVolumeClaim)
@ -197,17 +204,17 @@ func TestControllerSync(t *testing.T) {
{ {
// deleteVolume event issued before deleteClaim, no metric should have been reported // deleteVolume event issued before deleteClaim, no metric should have been reported
// and no delete operation start timestamp should be inserted into controller.operationTimestamps cache // and no delete operation start timestamp should be inserted into controller.operationTimestamps cache
"5-7 - delete volume event makes claim lost, delete claim event will not report metric", name: "5-7 - delete volume event makes claim lost, delete claim event will not report metric",
newVolumeArray("volume5-7", "10Gi", "uid5-7", "claim5-7", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classExternal, volume.AnnBoundByController, volume.AnnDynamicallyProvisioned), initialVolumes: newVolumeArray("volume5-7", "10Gi", "uid5-7", "claim5-7", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classExternal, volume.AnnBoundByController, volume.AnnDynamicallyProvisioned),
novolumes, expectedVolumes: novolumes,
claimWithAnnotation(volume.AnnStorageProvisioner, "gcr.io/vendor-csi", initialClaims: claimWithAnnotation(volume.AnnStorageProvisioner, "gcr.io/vendor-csi",
newClaimArray("claim5-7", "uid5-7", "1Gi", "volume5-7", v1.ClaimBound, &classExternal, volume.AnnBoundByController, volume.AnnBindCompleted)), newClaimArray("claim5-7", "uid5-7", "1Gi", "volume5-7", v1.ClaimBound, &classExternal, volume.AnnBoundByController, volume.AnnBindCompleted)),
noclaims, expectedClaims: noclaims,
[]string{"Warning ClaimLost"}, expectedEvents: []string{"Warning ClaimLost"},
noerrors, errors: noerrors,
// Custom test function that generates a delete claim event which should have been caught by // Custom test function that generates a delete claim event which should have been caught by
// "deleteClaim" to remove the claim from controller's cache and mark bound volume to be released // "deleteClaim" to remove the claim from controller's cache and mark bound volume to be released
func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor, test controllerTest) error { test: func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor, test controllerTest) error {
volume := ctrl.volumes.store.List()[0].(*v1.PersistentVolume) volume := ctrl.volumes.store.List()[0].(*v1.PersistentVolume)
reactor.DeleteVolumeEvent(volume) reactor.DeleteVolumeEvent(volume)
err := wait.Poll(10*time.Millisecond, wait.ForeverTestTimeout, func() (bool, error) { err := wait.Poll(10*time.Millisecond, wait.ForeverTestTimeout, func() (bool, error) {
@ -248,17 +255,17 @@ func TestControllerSync(t *testing.T) {
}, },
{ {
// delete a claim waiting for being bound cleans up provision(volume ref == "") entry from timestamp cache // delete a claim waiting for being bound cleans up provision(volume ref == "") entry from timestamp cache
"5-8 - delete claim cleans up operation timestamp cache for provision", name: "5-8 - delete claim cleans up operation timestamp cache for provision",
novolumes, initialVolumes: novolumes,
novolumes, expectedVolumes: novolumes,
claimWithAnnotation(volume.AnnStorageProvisioner, "gcr.io/vendor-csi", initialClaims: claimWithAnnotation(volume.AnnStorageProvisioner, "gcr.io/vendor-csi",
newClaimArray("claim5-8", "uid5-8", "1Gi", "", v1.ClaimPending, &classExternal)), newClaimArray("claim5-8", "uid5-8", "1Gi", "", v1.ClaimPending, &classExternal)),
noclaims, expectedClaims: noclaims,
[]string{"Normal ExternalProvisioning"}, expectedEvents: []string{"Normal ExternalProvisioning"},
noerrors, errors: noerrors,
// Custom test function that generates a delete claim event which should have been caught by // Custom test function that generates a delete claim event which should have been caught by
// "deleteClaim" to remove the claim from controller's cache and mark bound volume to be released // "deleteClaim" to remove the claim from controller's cache and mark bound volume to be released
func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor, test controllerTest) error { test: func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor, test controllerTest) error {
// wait until the provision timestamp has been inserted // wait until the provision timestamp has been inserted
err := wait.Poll(10*time.Millisecond, wait.ForeverTestTimeout, func() (bool, error) { err := wait.Poll(10*time.Millisecond, wait.ForeverTestTimeout, func() (bool, error) {
return ctrl.operationTimestamps.Has("default/claim5-8"), nil return ctrl.operationTimestamps.Has("default/claim5-8"), nil
@ -287,18 +294,18 @@ func TestControllerSync(t *testing.T) {
{ {
// Test that the finalizer gets removed if CSI migration is disabled. The in-tree finalizer is added // Test that the finalizer gets removed if CSI migration is disabled. The in-tree finalizer is added
// back on the PV since migration is disabled. // back on the PV since migration is disabled.
"5-9 - volume has its external PV deletion protection finalizer removed as CSI migration is disabled", name: "5-9 - volume has its external PV deletion protection finalizer removed as CSI migration is disabled",
volumesWithFinalizers( initialVolumes: volumesWithFinalizers(
volumesWithAnnotation(volume.AnnMigratedTo, "pd.csi.storage.gke.io", volumesWithAnnotation(volume.AnnMigratedTo, "pd.csi.storage.gke.io",
newVolumeArray("volume-5-9", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimDelete, classEmpty, volume.AnnDynamicallyProvisioned)), newVolumeArray("volume-5-9", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimDelete, classEmpty, volume.AnnDynamicallyProvisioned)),
[]string{volume.PVDeletionProtectionFinalizer}, []string{volume.PVDeletionProtectionFinalizer},
), ),
volumesWithFinalizers(newVolumeArray("volume-5-9", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimDelete, classEmpty, volume.AnnDynamicallyProvisioned), []string{volume.PVDeletionInTreeProtectionFinalizer}), expectedVolumes: volumesWithFinalizers(newVolumeArray("volume-5-9", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimDelete, classEmpty, volume.AnnDynamicallyProvisioned), []string{volume.PVDeletionInTreeProtectionFinalizer}),
noclaims, initialClaims: noclaims,
noclaims, expectedClaims: noclaims,
noevents, expectedEvents: noevents,
noerrors, errors: noerrors,
func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor, test controllerTest) error { test: func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor, test controllerTest) error {
return nil return nil
}, },
}, },

View File

@ -73,66 +73,73 @@ func TestRecycleSync(t *testing.T) {
tests := []controllerTest{ tests := []controllerTest{
{ {
// recycle volume bound by controller // recycle volume bound by controller
"6-1 - successful recycle", name: "6-1 - successful recycle",
newVolumeArray("volume6-1", "1Gi", "uid6-1", "claim6-1", v1.VolumeBound, v1.PersistentVolumeReclaimRecycle, classEmpty, volume.AnnBoundByController), initialVolumes: newVolumeArray("volume6-1", "1Gi", "uid6-1", "claim6-1", v1.VolumeBound, v1.PersistentVolumeReclaimRecycle, classEmpty, volume.AnnBoundByController),
newVolumeArray("volume6-1", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRecycle, classEmpty), expectedVolumes: newVolumeArray("volume6-1", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRecycle, classEmpty),
noclaims, initialClaims: noclaims,
noclaims, expectedClaims: noclaims,
noevents, noerrors, expectedEvents: noevents,
errors: noerrors,
// Inject recycler into the controller and call syncVolume. The // Inject recycler into the controller and call syncVolume. The
// recycler simulates one recycle() call that succeeds. // recycler simulates one recycle() call that succeeds.
wrapTestWithReclaimCalls(operationRecycle, []error{nil}, testSyncVolume), test: wrapTestWithReclaimCalls(operationRecycle, []error{nil}, testSyncVolume),
}, },
{ {
// recycle volume bound by user // recycle volume bound by user
"6-2 - successful recycle with prebound volume", name: "6-2 - successful recycle with prebound volume",
newVolumeArray("volume6-2", "1Gi", "uid6-2", "claim6-2", v1.VolumeBound, v1.PersistentVolumeReclaimRecycle, classEmpty), initialVolumes: newVolumeArray("volume6-2", "1Gi", "uid6-2", "claim6-2", v1.VolumeBound, v1.PersistentVolumeReclaimRecycle, classEmpty),
newVolumeArray("volume6-2", "1Gi", "", "claim6-2", v1.VolumeAvailable, v1.PersistentVolumeReclaimRecycle, classEmpty), expectedVolumes: newVolumeArray("volume6-2", "1Gi", "", "claim6-2", v1.VolumeAvailable, v1.PersistentVolumeReclaimRecycle, classEmpty),
noclaims, initialClaims: noclaims,
noclaims, expectedClaims: noclaims,
noevents, noerrors, expectedEvents: noevents,
errors: noerrors,
// Inject recycler into the controller and call syncVolume. The // Inject recycler into the controller and call syncVolume. The
// recycler simulates one recycle() call that succeeds. // recycler simulates one recycle() call that succeeds.
wrapTestWithReclaimCalls(operationRecycle, []error{nil}, testSyncVolume), test: wrapTestWithReclaimCalls(operationRecycle, []error{nil}, testSyncVolume),
}, },
{ {
// recycle failure - plugin not found // recycle failure - plugin not found
"6-3 - plugin not found", name: "6-3 - plugin not found",
newVolumeArray("volume6-3", "1Gi", "uid6-3", "claim6-3", v1.VolumeBound, v1.PersistentVolumeReclaimRecycle, classEmpty), initialVolumes: newVolumeArray("volume6-3", "1Gi", "uid6-3", "claim6-3", v1.VolumeBound, v1.PersistentVolumeReclaimRecycle, classEmpty),
withMessage("No recycler plugin found for the volume!", newVolumeArray("volume6-3", "1Gi", "uid6-3", "claim6-3", v1.VolumeFailed, v1.PersistentVolumeReclaimRecycle, classEmpty)), expectedVolumes: withMessage("No recycler plugin found for the volume!", newVolumeArray("volume6-3", "1Gi", "uid6-3", "claim6-3", v1.VolumeFailed, v1.PersistentVolumeReclaimRecycle, classEmpty)),
noclaims, initialClaims: noclaims,
noclaims, expectedClaims: noclaims,
[]string{"Warning VolumeFailedRecycle"}, noerrors, testSyncVolume, expectedEvents: []string{"Warning VolumeFailedRecycle"},
errors: noerrors,
test: testSyncVolume,
}, },
{ {
// recycle failure - Recycle returns error // recycle failure - Recycle returns error
"6-4 - newRecycler returns error", name: "6-4 - newRecycler returns error",
newVolumeArray("volume6-4", "1Gi", "uid6-4", "claim6-4", v1.VolumeBound, v1.PersistentVolumeReclaimRecycle, classEmpty), initialVolumes: newVolumeArray("volume6-4", "1Gi", "uid6-4", "claim6-4", v1.VolumeBound, v1.PersistentVolumeReclaimRecycle, classEmpty),
withMessage("Recycle failed: Mock plugin error: no recycleCalls configured", newVolumeArray("volume6-4", "1Gi", "uid6-4", "claim6-4", v1.VolumeFailed, v1.PersistentVolumeReclaimRecycle, classEmpty)), expectedVolumes: withMessage("Recycle failed: Mock plugin error: no recycleCalls configured", newVolumeArray("volume6-4", "1Gi", "uid6-4", "claim6-4", v1.VolumeFailed, v1.PersistentVolumeReclaimRecycle, classEmpty)),
noclaims, initialClaims: noclaims,
noclaims, expectedClaims: noclaims,
[]string{"Warning VolumeFailedRecycle"}, noerrors, expectedEvents: []string{"Warning VolumeFailedRecycle"},
wrapTestWithReclaimCalls(operationRecycle, []error{}, testSyncVolume), errors: noerrors,
test: wrapTestWithReclaimCalls(operationRecycle, []error{}, testSyncVolume),
}, },
{ {
// recycle failure - recycle returns error // recycle failure - recycle returns error
"6-5 - recycle returns error", name: "6-5 - recycle returns error",
newVolumeArray("volume6-5", "1Gi", "uid6-5", "claim6-5", v1.VolumeBound, v1.PersistentVolumeReclaimRecycle, classEmpty), initialVolumes: newVolumeArray("volume6-5", "1Gi", "uid6-5", "claim6-5", v1.VolumeBound, v1.PersistentVolumeReclaimRecycle, classEmpty),
withMessage("Recycle failed: Mock recycle error", newVolumeArray("volume6-5", "1Gi", "uid6-5", "claim6-5", v1.VolumeFailed, v1.PersistentVolumeReclaimRecycle, classEmpty)), expectedVolumes: withMessage("Recycle failed: Mock recycle error", newVolumeArray("volume6-5", "1Gi", "uid6-5", "claim6-5", v1.VolumeFailed, v1.PersistentVolumeReclaimRecycle, classEmpty)),
noclaims, initialClaims: noclaims,
noclaims, expectedClaims: noclaims,
[]string{"Warning VolumeFailedRecycle"}, noerrors, expectedEvents: []string{"Warning VolumeFailedRecycle"},
wrapTestWithReclaimCalls(operationRecycle, []error{errors.New("Mock recycle error")}, testSyncVolume), errors: noerrors,
test: wrapTestWithReclaimCalls(operationRecycle, []error{errors.New("Mock recycle error")}, testSyncVolume),
}, },
{ {
// recycle success(?) - volume is deleted before doRecycle() starts // recycle success(?) - volume is deleted before doRecycle() starts
"6-6 - volume is deleted before recycling", name: "6-6 - volume is deleted before recycling",
newVolumeArray("volume6-6", "1Gi", "uid6-6", "claim6-6", v1.VolumeBound, v1.PersistentVolumeReclaimRecycle, classEmpty), initialVolumes: newVolumeArray("volume6-6", "1Gi", "uid6-6", "claim6-6", v1.VolumeBound, v1.PersistentVolumeReclaimRecycle, classEmpty),
novolumes, expectedVolumes: novolumes,
noclaims, initialClaims: noclaims,
noclaims, expectedClaims: noclaims,
noevents, noerrors, expectedEvents: noevents,
wrapTestWithInjectedOperation(wrapTestWithReclaimCalls(operationRecycle, []error{}, testSyncVolume), func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor) { errors: noerrors,
test: wrapTestWithInjectedOperation(wrapTestWithReclaimCalls(operationRecycle, []error{}, testSyncVolume), func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor) {
// Delete the volume before recycle operation starts // Delete the volume before recycle operation starts
reactor.DeleteVolume("volume6-6") reactor.DeleteVolume("volume6-6")
}), }),
@ -141,13 +148,14 @@ func TestRecycleSync(t *testing.T) {
// recycle success(?) - volume is recycled by previous recycler just // recycle success(?) - volume is recycled by previous recycler just
// at the time new doRecycle() starts. This simulates "volume no // at the time new doRecycle() starts. This simulates "volume no
// longer needs recycling, skipping". // longer needs recycling, skipping".
"6-7 - volume is deleted before recycling", name: "6-7 - volume is deleted before recycling",
newVolumeArray("volume6-7", "1Gi", "uid6-7", "claim6-7", v1.VolumeBound, v1.PersistentVolumeReclaimRecycle, classEmpty, volume.AnnBoundByController), initialVolumes: newVolumeArray("volume6-7", "1Gi", "uid6-7", "claim6-7", v1.VolumeBound, v1.PersistentVolumeReclaimRecycle, classEmpty, volume.AnnBoundByController),
newVolumeArray("volume6-7", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRecycle, classEmpty), expectedVolumes: newVolumeArray("volume6-7", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRecycle, classEmpty),
noclaims, initialClaims: noclaims,
noclaims, expectedClaims: noclaims,
noevents, noerrors, expectedEvents: noevents,
wrapTestWithInjectedOperation(wrapTestWithReclaimCalls(operationRecycle, []error{}, testSyncVolume), func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor) { errors: noerrors,
test: wrapTestWithInjectedOperation(wrapTestWithReclaimCalls(operationRecycle, []error{}, testSyncVolume), func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor) {
// Mark the volume as Available before the recycler starts // Mark the volume as Available before the recycler starts
reactor.MarkVolumeAvailable("volume6-7") reactor.MarkVolumeAvailable("volume6-7")
}), }),
@ -157,13 +165,14 @@ func TestRecycleSync(t *testing.T) {
// recycler just at the time new doRecycle() starts. This simulates // recycler just at the time new doRecycle() starts. This simulates
// "volume no longer needs recycling, skipping" with volume bound by // "volume no longer needs recycling, skipping" with volume bound by
// user. // user.
"6-8 - prebound volume is deleted before recycling", name: "6-8 - prebound volume is deleted before recycling",
newVolumeArray("volume6-8", "1Gi", "uid6-8", "claim6-8", v1.VolumeBound, v1.PersistentVolumeReclaimRecycle, classEmpty), initialVolumes: newVolumeArray("volume6-8", "1Gi", "uid6-8", "claim6-8", v1.VolumeBound, v1.PersistentVolumeReclaimRecycle, classEmpty),
newVolumeArray("volume6-8", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRecycle, classEmpty), expectedVolumes: newVolumeArray("volume6-8", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRecycle, classEmpty),
noclaims, initialClaims: noclaims,
noclaims, expectedClaims: noclaims,
noevents, noerrors, expectedEvents: noevents,
wrapTestWithInjectedOperation(wrapTestWithReclaimCalls(operationRecycle, []error{}, testSyncVolume), func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor) { errors: noerrors,
test: wrapTestWithInjectedOperation(wrapTestWithReclaimCalls(operationRecycle, []error{}, testSyncVolume), func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor) {
// Mark the volume as Available before the recycler starts // Mark the volume as Available before the recycler starts
reactor.MarkVolumeAvailable("volume6-8") reactor.MarkVolumeAvailable("volume6-8")
}), }),
@ -171,64 +180,73 @@ func TestRecycleSync(t *testing.T) {
{ {
// recycle success - volume bound by user is recycled, while a new // recycle success - volume bound by user is recycled, while a new
// claim is created with another UID. // claim is created with another UID.
"6-9 - prebound volume is recycled while the claim exists", name: "6-9 - prebound volume is recycled while the claim exists",
newVolumeArray("volume6-9", "1Gi", "uid6-9", "claim6-9", v1.VolumeBound, v1.PersistentVolumeReclaimRecycle, classEmpty), initialVolumes: newVolumeArray("volume6-9", "1Gi", "uid6-9", "claim6-9", v1.VolumeBound, v1.PersistentVolumeReclaimRecycle, classEmpty),
newVolumeArray("volume6-9", "1Gi", "", "claim6-9", v1.VolumeAvailable, v1.PersistentVolumeReclaimRecycle, classEmpty), expectedVolumes: newVolumeArray("volume6-9", "1Gi", "", "claim6-9", v1.VolumeAvailable, v1.PersistentVolumeReclaimRecycle, classEmpty),
newClaimArray("claim6-9", "uid6-9-x", "10Gi", "", v1.ClaimPending, nil), initialClaims: newClaimArray("claim6-9", "uid6-9-x", "10Gi", "", v1.ClaimPending, nil),
newClaimArray("claim6-9", "uid6-9-x", "10Gi", "", v1.ClaimPending, nil), expectedClaims: newClaimArray("claim6-9", "uid6-9-x", "10Gi", "", v1.ClaimPending, nil),
noevents, noerrors, expectedEvents: noevents,
errors: noerrors,
// Inject recycler into the controller and call syncVolume. The // Inject recycler into the controller and call syncVolume. The
// recycler simulates one recycle() call that succeeds. // recycler simulates one recycle() call that succeeds.
wrapTestWithReclaimCalls(operationRecycle, []error{nil}, testSyncVolume), test: wrapTestWithReclaimCalls(operationRecycle, []error{nil}, testSyncVolume),
}, },
{ {
// volume has unknown reclaim policy - failure expected // volume has unknown reclaim policy - failure expected
"6-10 - unknown reclaim policy", name: "6-10 - unknown reclaim policy",
newVolumeArray("volume6-10", "1Gi", "uid6-10", "claim6-10", v1.VolumeBound, "Unknown", classEmpty), initialVolumes: newVolumeArray("volume6-10", "1Gi", "uid6-10", "claim6-10", v1.VolumeBound, "Unknown", classEmpty),
withMessage("Volume has unrecognized PersistentVolumeReclaimPolicy", newVolumeArray("volume6-10", "1Gi", "uid6-10", "claim6-10", v1.VolumeFailed, "Unknown", classEmpty)), expectedVolumes: withMessage("Volume has unrecognized PersistentVolumeReclaimPolicy", newVolumeArray("volume6-10", "1Gi", "uid6-10", "claim6-10", v1.VolumeFailed, "Unknown", classEmpty)),
noclaims, initialClaims: noclaims,
noclaims, expectedClaims: noclaims,
[]string{"Warning VolumeUnknownReclaimPolicy"}, noerrors, testSyncVolume, expectedEvents: []string{"Warning VolumeUnknownReclaimPolicy"},
errors: noerrors,
test: testSyncVolume,
}, },
{ {
// volume is used by a running pod - failure expected // volume is used by a running pod - failure expected
"6-11 - used by running pod", name: "6-11 - used by running pod",
newVolumeArray("volume6-11", "1Gi", "uid6-11", "runningClaim", v1.VolumeBound, v1.PersistentVolumeReclaimRecycle, classEmpty, volume.AnnBoundByController), initialVolumes: newVolumeArray("volume6-11", "1Gi", "uid6-11", "runningClaim", v1.VolumeBound, v1.PersistentVolumeReclaimRecycle, classEmpty, volume.AnnBoundByController),
newVolumeArray("volume6-11", "1Gi", "uid6-11", "runningClaim", v1.VolumeReleased, v1.PersistentVolumeReclaimRecycle, classEmpty, volume.AnnBoundByController), expectedVolumes: newVolumeArray("volume6-11", "1Gi", "uid6-11", "runningClaim", v1.VolumeReleased, v1.PersistentVolumeReclaimRecycle, classEmpty, volume.AnnBoundByController),
noclaims, initialClaims: noclaims,
noclaims, expectedClaims: noclaims,
[]string{"Normal VolumeFailedRecycle"}, noerrors, testSyncVolume, expectedEvents: []string{"Normal VolumeFailedRecycle"},
errors: noerrors,
test: testSyncVolume,
}, },
{ {
// volume is used by a pending pod - failure expected // volume is used by a pending pod - failure expected
"6-12 - used by pending pod", name: "6-12 - used by pending pod",
newVolumeArray("volume6-12", "1Gi", "uid6-12", "pendingClaim", v1.VolumeBound, v1.PersistentVolumeReclaimRecycle, classEmpty, volume.AnnBoundByController), initialVolumes: newVolumeArray("volume6-12", "1Gi", "uid6-12", "pendingClaim", v1.VolumeBound, v1.PersistentVolumeReclaimRecycle, classEmpty, volume.AnnBoundByController),
newVolumeArray("volume6-12", "1Gi", "uid6-12", "pendingClaim", v1.VolumeReleased, v1.PersistentVolumeReclaimRecycle, classEmpty, volume.AnnBoundByController), expectedVolumes: newVolumeArray("volume6-12", "1Gi", "uid6-12", "pendingClaim", v1.VolumeReleased, v1.PersistentVolumeReclaimRecycle, classEmpty, volume.AnnBoundByController),
noclaims, initialClaims: noclaims,
noclaims, expectedClaims: noclaims,
[]string{"Normal VolumeFailedRecycle"}, noerrors, testSyncVolume, expectedEvents: []string{"Normal VolumeFailedRecycle"},
errors: noerrors,
test: testSyncVolume,
}, },
{ {
// volume is used by a completed pod - recycle succeeds // volume is used by a completed pod - recycle succeeds
"6-13 - used by completed pod", name: "6-13 - used by completed pod",
newVolumeArray("volume6-13", "1Gi", "uid6-13", "completedClaim", v1.VolumeBound, v1.PersistentVolumeReclaimRecycle, classEmpty, volume.AnnBoundByController), initialVolumes: newVolumeArray("volume6-13", "1Gi", "uid6-13", "completedClaim", v1.VolumeBound, v1.PersistentVolumeReclaimRecycle, classEmpty, volume.AnnBoundByController),
newVolumeArray("volume6-13", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRecycle, classEmpty), expectedVolumes: newVolumeArray("volume6-13", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRecycle, classEmpty),
noclaims, initialClaims: noclaims,
noclaims, expectedClaims: noclaims,
noevents, noerrors, expectedEvents: noevents,
errors: noerrors,
// Inject recycler into the controller and call syncVolume. The // Inject recycler into the controller and call syncVolume. The
// recycler simulates one recycle() call that succeeds. // recycler simulates one recycle() call that succeeds.
wrapTestWithReclaimCalls(operationRecycle, []error{nil}, testSyncVolume), test: wrapTestWithReclaimCalls(operationRecycle, []error{nil}, testSyncVolume),
}, },
{ {
// volume is used by a completed pod, pod using claim with the same name bound to different pv is running, should recycle // volume is used by a completed pod, pod using claim with the same name bound to different pv is running, should recycle
"6-14 - seemingly used by running pod", name: "6-14 - seemingly used by running pod",
newVolumeArray("volume6-14", "1Gi", "uid6-14", "completedClaim", v1.VolumeBound, v1.PersistentVolumeReclaimRecycle, classEmpty, volume.AnnBoundByController), initialVolumes: newVolumeArray("volume6-14", "1Gi", "uid6-14", "completedClaim", v1.VolumeBound, v1.PersistentVolumeReclaimRecycle, classEmpty, volume.AnnBoundByController),
newVolumeArray("volume6-14", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRecycle, classEmpty), expectedVolumes: newVolumeArray("volume6-14", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRecycle, classEmpty),
newClaimArray("completedClaim", "uid6-14-x", "10Gi", "", v1.ClaimBound, nil), initialClaims: newClaimArray("completedClaim", "uid6-14-x", "10Gi", "", v1.ClaimBound, nil),
newClaimArray("completedClaim", "uid6-14-x", "10Gi", "", v1.ClaimBound, nil), expectedClaims: newClaimArray("completedClaim", "uid6-14-x", "10Gi", "", v1.ClaimBound, nil),
noevents, noerrors, expectedEvents: noevents,
wrapTestWithReclaimCalls(operationRecycle, []error{nil}, testSyncVolume), errors: noerrors,
test: wrapTestWithReclaimCalls(operationRecycle, []error{nil}, testSyncVolume),
}, },
} }
runSyncTests(t, tests, []*storage.StorageClass{}, pods) runSyncTests(t, tests, []*storage.StorageClass{}, pods)