Commit 0b6c028c authored by Yecheng Fu's avatar Yecheng Fu

Extract testing VolumeReactor into a separate package

parent 6d691c99
...@@ -22,6 +22,7 @@ import ( ...@@ -22,6 +22,7 @@ import (
"k8s.io/api/core/v1" "k8s.io/api/core/v1"
storage "k8s.io/api/storage/v1" storage "k8s.io/api/storage/v1"
pvtesting "k8s.io/kubernetes/pkg/controller/volume/persistentvolume/testing"
) )
// Test single call to syncVolume, expecting recycling to happen. // Test single call to syncVolume, expecting recycling to happen.
...@@ -91,11 +92,9 @@ func TestDeleteSync(t *testing.T) { ...@@ -91,11 +92,9 @@ func TestDeleteSync(t *testing.T) {
noclaims, noclaims,
noclaims, noclaims,
noevents, noerrors, noevents, noerrors,
wrapTestWithInjectedOperation(wrapTestWithReclaimCalls(operationDelete, []error{}, testSyncVolume), func(ctrl *PersistentVolumeController, reactor *volumeReactor) { 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.lock.Lock() reactor.DeleteVolume("volume8-6")
delete(reactor.volumes, "volume8-6")
reactor.lock.Unlock()
}), }),
}, },
{ {
...@@ -108,16 +107,12 @@ func TestDeleteSync(t *testing.T) { ...@@ -108,16 +107,12 @@ func TestDeleteSync(t *testing.T) {
noclaims, noclaims,
newClaimArray("claim8-7", "uid8-7", "10Gi", "volume8-7", v1.ClaimBound, nil), newClaimArray("claim8-7", "uid8-7", "10Gi", "volume8-7", v1.ClaimBound, nil),
noevents, noerrors, noevents, noerrors,
wrapTestWithInjectedOperation(wrapTestWithReclaimCalls(operationDelete, []error{}, testSyncVolume), func(ctrl *PersistentVolumeController, reactor *volumeReactor) { wrapTestWithInjectedOperation(wrapTestWithReclaimCalls(operationDelete, []error{}, testSyncVolume), func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor) {
reactor.lock.Lock()
defer reactor.lock.Unlock()
// 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)
reactor.claims[claim.Name] = claim reactor.AddClaimBoundToVolume(claim)
ctrl.claims.Add(claim) ctrl.claims.Add(claim)
volume := reactor.volumes["volume8-7"]
volume.Status.Phase = v1.VolumeBound
}), }),
}, },
{ {
...@@ -141,7 +136,7 @@ func TestDeleteSync(t *testing.T) { ...@@ -141,7 +136,7 @@ func TestDeleteSync(t *testing.T) {
noclaims, noclaims,
noclaims, noclaims,
noevents, noerrors, noevents, noerrors,
func(ctrl *PersistentVolumeController, reactor *volumeReactor, test controllerTest) error { func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor, test controllerTest) error {
// Inject external deleter annotation // Inject external deleter annotation
test.initialVolumes[0].Annotations[annDynamicallyProvisioned] = "external.io/test" test.initialVolumes[0].Annotations[annDynamicallyProvisioned] = "external.io/test"
test.expectedVolumes[0].Annotations[annDynamicallyProvisioned] = "external.io/test" test.expectedVolumes[0].Annotations[annDynamicallyProvisioned] = "external.io/test"
...@@ -184,7 +179,7 @@ func TestDeleteSync(t *testing.T) { ...@@ -184,7 +179,7 @@ func TestDeleteSync(t *testing.T) {
newClaimArray("claim8-12", "uid8-12", "10Gi", "volume8-12-2", v1.ClaimBound, nil), newClaimArray("claim8-12", "uid8-12", "10Gi", "volume8-12-2", v1.ClaimBound, nil),
newClaimArray("claim8-12", "uid8-12", "10Gi", "volume8-12-2", v1.ClaimBound, nil), newClaimArray("claim8-12", "uid8-12", "10Gi", "volume8-12-2", v1.ClaimBound, nil),
noevents, noerrors, noevents, noerrors,
func(ctrl *PersistentVolumeController, reactor *volumeReactor, test controllerTest) error { func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor, test controllerTest) error {
// Inject external deleter annotation // Inject external deleter annotation
test.initialVolumes[0].Annotations[annDynamicallyProvisioned] = "external.io/test" test.initialVolumes[0].Annotations[annDynamicallyProvisioned] = "external.io/test"
test.expectedVolumes[0].Annotations[annDynamicallyProvisioned] = "external.io/test" test.expectedVolumes[0].Annotations[annDynamicallyProvisioned] = "external.io/test"
......
...@@ -25,6 +25,7 @@ import ( ...@@ -25,6 +25,7 @@ import (
apierrs "k8s.io/apimachinery/pkg/api/errors" apierrs "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
api "k8s.io/kubernetes/pkg/apis/core" api "k8s.io/kubernetes/pkg/apis/core"
pvtesting "k8s.io/kubernetes/pkg/controller/volume/persistentvolume/testing"
) )
var class1Parameters = map[string]string{ var class1Parameters = map[string]string{
...@@ -191,13 +192,11 @@ func TestProvisionSync(t *testing.T) { ...@@ -191,13 +192,11 @@ func TestProvisionSync(t *testing.T) {
// 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, annStorageProvisioner), newClaimArray("claim11-7", "uid11-7", "1Gi", "", v1.ClaimPending, &classGold, annStorageProvisioner),
noevents, noerrors, noevents, noerrors,
wrapTestWithInjectedOperation(wrapTestWithProvisionCalls([]provisionCall{}, testSyncClaim), func(ctrl *PersistentVolumeController, reactor *volumeReactor) { 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.
reactor.lock.Lock()
volume := newVolume("pvc-uid11-7", "1Gi", "uid11-7", "claim11-7", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classGold, annBoundByController, annDynamicallyProvisioned) volume := newVolume("pvc-uid11-7", "1Gi", "uid11-7", "claim11-7", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classGold, annBoundByController, annDynamicallyProvisioned)
reactor.volumes[volume.Name] = volume reactor.AddVolume(volume)
reactor.lock.Unlock()
}), }),
}, },
{ {
...@@ -210,7 +209,7 @@ func TestProvisionSync(t *testing.T) { ...@@ -210,7 +209,7 @@ func TestProvisionSync(t *testing.T) {
// 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, annStorageProvisioner), newClaimArray("claim11-8", "uid11-8", "1Gi", "", v1.ClaimPending, &classGold, annStorageProvisioner),
[]string{"Normal ProvisioningSucceeded"}, []string{"Normal ProvisioningSucceeded"},
[]reactorError{ []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.
...@@ -227,7 +226,7 @@ func TestProvisionSync(t *testing.T) { ...@@ -227,7 +226,7 @@ func TestProvisionSync(t *testing.T) {
newClaimArray("claim11-9", "uid11-9", "1Gi", "", v1.ClaimPending, &classGold), newClaimArray("claim11-9", "uid11-9", "1Gi", "", v1.ClaimPending, &classGold),
newClaimArray("claim11-9", "uid11-9", "1Gi", "", v1.ClaimPending, &classGold, annStorageProvisioner), newClaimArray("claim11-9", "uid11-9", "1Gi", "", v1.ClaimPending, &classGold, annStorageProvisioner),
[]string{"Warning ProvisioningFailed"}, []string{"Warning ProvisioningFailed"},
[]reactorError{ []pvtesting.ReactorError{
// Inject error to five kubeclient.PersistentVolumes.Create() // Inject error to five kubeclient.PersistentVolumes.Create()
// calls // calls
{"create", "persistentvolumes", errors.New("Mock creation error1")}, {"create", "persistentvolumes", errors.New("Mock creation error1")},
...@@ -252,7 +251,7 @@ func TestProvisionSync(t *testing.T) { ...@@ -252,7 +251,7 @@ func TestProvisionSync(t *testing.T) {
newClaimArray("claim11-10", "uid11-10", "1Gi", "", v1.ClaimPending, &classGold), newClaimArray("claim11-10", "uid11-10", "1Gi", "", v1.ClaimPending, &classGold),
newClaimArray("claim11-10", "uid11-10", "1Gi", "", v1.ClaimPending, &classGold, annStorageProvisioner), newClaimArray("claim11-10", "uid11-10", "1Gi", "", v1.ClaimPending, &classGold, annStorageProvisioner),
[]string{"Warning ProvisioningFailed", "Warning ProvisioningCleanupFailed"}, []string{"Warning ProvisioningFailed", "Warning ProvisioningCleanupFailed"},
[]reactorError{ []pvtesting.ReactorError{
// Inject error to five kubeclient.PersistentVolumes.Create() // Inject error to five kubeclient.PersistentVolumes.Create()
// calls // calls
{"create", "persistentvolumes", errors.New("Mock creation error1")}, {"create", "persistentvolumes", errors.New("Mock creation error1")},
...@@ -273,7 +272,7 @@ func TestProvisionSync(t *testing.T) { ...@@ -273,7 +272,7 @@ func TestProvisionSync(t *testing.T) {
newClaimArray("claim11-11", "uid11-11", "1Gi", "", v1.ClaimPending, &classGold), newClaimArray("claim11-11", "uid11-11", "1Gi", "", v1.ClaimPending, &classGold),
newClaimArray("claim11-11", "uid11-11", "1Gi", "", v1.ClaimPending, &classGold, annStorageProvisioner), newClaimArray("claim11-11", "uid11-11", "1Gi", "", v1.ClaimPending, &classGold, annStorageProvisioner),
[]string{"Warning ProvisioningFailed", "Warning ProvisioningCleanupFailed"}, []string{"Warning ProvisioningFailed", "Warning ProvisioningCleanupFailed"},
[]reactorError{ []pvtesting.ReactorError{
// Inject error to five kubeclient.PersistentVolumes.Create() // Inject error to five kubeclient.PersistentVolumes.Create()
// calls // calls
{"create", "persistentvolumes", errors.New("Mock creation error1")}, {"create", "persistentvolumes", errors.New("Mock creation error1")},
...@@ -303,7 +302,7 @@ func TestProvisionSync(t *testing.T) { ...@@ -303,7 +302,7 @@ func TestProvisionSync(t *testing.T) {
newClaimArray("claim11-12", "uid11-12", "1Gi", "", v1.ClaimPending, &classGold), newClaimArray("claim11-12", "uid11-12", "1Gi", "", v1.ClaimPending, &classGold),
newClaimArray("claim11-12", "uid11-12", "1Gi", "", v1.ClaimPending, &classGold, annStorageProvisioner), newClaimArray("claim11-12", "uid11-12", "1Gi", "", v1.ClaimPending, &classGold, annStorageProvisioner),
[]string{"Warning ProvisioningFailed"}, []string{"Warning ProvisioningFailed"},
[]reactorError{ []pvtesting.ReactorError{
// Inject error to five kubeclient.PersistentVolumes.Create() // Inject error to five kubeclient.PersistentVolumes.Create()
// calls // calls
{"create", "persistentvolumes", errors.New("Mock creation error1")}, {"create", "persistentvolumes", errors.New("Mock creation error1")},
...@@ -397,7 +396,7 @@ func TestProvisionSync(t *testing.T) { ...@@ -397,7 +396,7 @@ func TestProvisionSync(t *testing.T) {
newClaimArray("claim11-19", "uid11-19", "1Gi", "", v1.ClaimPending, &classGold), newClaimArray("claim11-19", "uid11-19", "1Gi", "", v1.ClaimPending, &classGold),
newClaimArray("claim11-19", "uid11-19", "1Gi", "", v1.ClaimPending, &classGold, annStorageProvisioner), newClaimArray("claim11-19", "uid11-19", "1Gi", "", v1.ClaimPending, &classGold, annStorageProvisioner),
noevents, noevents,
[]reactorError{ []pvtesting.ReactorError{
// Inject errors to simulate crashed API server during // Inject errors to simulate crashed API server during
// kubeclient.PersistentVolumes.Create() // kubeclient.PersistentVolumes.Create()
{"create", "persistentvolumes", errors.New("Mock creation error1")}, {"create", "persistentvolumes", errors.New("Mock creation error1")},
......
...@@ -30,6 +30,7 @@ import ( ...@@ -30,6 +30,7 @@ import (
"k8s.io/client-go/tools/cache" "k8s.io/client-go/tools/cache"
"k8s.io/klog" "k8s.io/klog"
"k8s.io/kubernetes/pkg/controller" "k8s.io/kubernetes/pkg/controller"
pvtesting "k8s.io/kubernetes/pkg/controller/volume/persistentvolume/testing"
) )
var ( var (
...@@ -60,9 +61,9 @@ func TestControllerSync(t *testing.T) { ...@@ -60,9 +61,9 @@ func TestControllerSync(t *testing.T) {
newClaimArray("claim5-2", "uid5-2", "1Gi", "volume5-2", v1.ClaimBound, nil, annBoundByController, annBindCompleted), newClaimArray("claim5-2", "uid5-2", "1Gi", "volume5-2", v1.ClaimBound, nil, annBoundByController, annBindCompleted),
noevents, noerrors, noevents, noerrors,
// Custom test function that generates an add event // Custom test function that generates an add event
func(ctrl *PersistentVolumeController, reactor *volumeReactor, test controllerTest) error { 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
}, },
}, },
...@@ -75,10 +76,10 @@ func TestControllerSync(t *testing.T) { ...@@ -75,10 +76,10 @@ func TestControllerSync(t *testing.T) {
noclaims, noclaims,
noevents, noerrors, noevents, noerrors,
// Custom test function that generates a delete event // Custom test function that generates a delete event
func(ctrl *PersistentVolumeController, reactor *volumeReactor, test controllerTest) error { 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)
return nil return nil
}, },
}, },
...@@ -91,10 +92,10 @@ func TestControllerSync(t *testing.T) { ...@@ -91,10 +92,10 @@ func TestControllerSync(t *testing.T) {
newClaimArray("claim5-4", "uid5-4", "1Gi", "volume5-4", v1.ClaimLost, nil, annBoundByController, annBindCompleted), newClaimArray("claim5-4", "uid5-4", "1Gi", "volume5-4", v1.ClaimLost, nil, annBoundByController, annBindCompleted),
[]string{"Warning ClaimLost"}, noerrors, []string{"Warning ClaimLost"}, noerrors,
// Custom test function that generates a delete event // Custom test function that generates a delete event
func(ctrl *PersistentVolumeController, reactor *volumeReactor, test controllerTest) error { 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)
return nil return nil
}, },
}, },
...@@ -120,13 +121,13 @@ func TestControllerSync(t *testing.T) { ...@@ -120,13 +121,13 @@ func TestControllerSync(t *testing.T) {
reactor := newVolumeReactor(client, ctrl, fakeVolumeWatch, fakeClaimWatch, test.errors) reactor := newVolumeReactor(client, ctrl, fakeVolumeWatch, fakeClaimWatch, test.errors)
for _, claim := range test.initialClaims { for _, claim := range test.initialClaims {
reactor.claims[claim.Name] = claim reactor.AddClaim(claim)
go func(claim *v1.PersistentVolumeClaim) { go func(claim *v1.PersistentVolumeClaim) {
fakeClaimWatch.Add(claim) fakeClaimWatch.Add(claim)
}(claim) }(claim)
} }
for _, volume := range test.initialVolumes { for _, volume := range test.initialVolumes {
reactor.volumes[volume.Name] = volume reactor.AddVolume(volume)
go func(volume *v1.PersistentVolume) { go func(volume *v1.PersistentVolume) {
fakeVolumeWatch.Add(volume) fakeVolumeWatch.Add(volume)
}(volume) }(volume)
...@@ -148,7 +149,7 @@ func TestControllerSync(t *testing.T) { ...@@ -148,7 +149,7 @@ func TestControllerSync(t *testing.T) {
klog.V(4).Infof("controller synced, starting test") klog.V(4).Infof("controller synced, starting test")
// Call the tested function // Call the tested function
err = test.test(ctrl, reactor, test) err = test.test(ctrl, reactor.VolumeReactor, test)
if err != nil { if err != nil {
t.Errorf("Test %q initial test call failed: %v", test.name, err) t.Errorf("Test %q initial test call failed: %v", test.name, err)
} }
...@@ -162,7 +163,7 @@ func TestControllerSync(t *testing.T) { ...@@ -162,7 +163,7 @@ func TestControllerSync(t *testing.T) {
} }
close(stopCh) close(stopCh)
evaluateTestResults(ctrl, reactor, test, t) evaluateTestResults(ctrl, reactor.VolumeReactor, test, t)
} }
} }
......
...@@ -23,6 +23,7 @@ import ( ...@@ -23,6 +23,7 @@ import (
"k8s.io/api/core/v1" "k8s.io/api/core/v1"
storage "k8s.io/api/storage/v1" storage "k8s.io/api/storage/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
pvtesting "k8s.io/kubernetes/pkg/controller/volume/persistentvolume/testing"
) )
// Test single call to syncVolume, expecting recycling to happen. // Test single call to syncVolume, expecting recycling to happen.
...@@ -130,11 +131,9 @@ func TestRecycleSync(t *testing.T) { ...@@ -130,11 +131,9 @@ func TestRecycleSync(t *testing.T) {
noclaims, noclaims,
noclaims, noclaims,
noevents, noerrors, noevents, noerrors,
wrapTestWithInjectedOperation(wrapTestWithReclaimCalls(operationRecycle, []error{}, testSyncVolume), func(ctrl *PersistentVolumeController, reactor *volumeReactor) { 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.lock.Lock() reactor.DeleteVolume("volume6-6")
delete(reactor.volumes, "volume6-6")
reactor.lock.Unlock()
}), }),
}, },
{ {
...@@ -147,14 +146,9 @@ func TestRecycleSync(t *testing.T) { ...@@ -147,14 +146,9 @@ func TestRecycleSync(t *testing.T) {
noclaims, noclaims,
noclaims, noclaims,
noevents, noerrors, noevents, noerrors,
wrapTestWithInjectedOperation(wrapTestWithReclaimCalls(operationRecycle, []error{}, testSyncVolume), func(ctrl *PersistentVolumeController, reactor *volumeReactor) { 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.lock.Lock() reactor.MarkVolumeAvaiable("volume6-7")
volume := reactor.volumes["volume6-7"]
volume.Spec.ClaimRef = nil
volume.Status.Phase = v1.VolumeAvailable
volume.Annotations = nil
reactor.lock.Unlock()
}), }),
}, },
{ {
...@@ -164,17 +158,13 @@ func TestRecycleSync(t *testing.T) { ...@@ -164,17 +158,13 @@ func TestRecycleSync(t *testing.T) {
// user. // user.
"6-8 - prebound volume is deleted before recycling", "6-8 - prebound volume is deleted before recycling",
newVolumeArray("volume6-8", "1Gi", "uid6-8", "claim6-8", v1.VolumeBound, v1.PersistentVolumeReclaimRecycle, classEmpty), newVolumeArray("volume6-8", "1Gi", "uid6-8", "claim6-8", v1.VolumeBound, v1.PersistentVolumeReclaimRecycle, classEmpty),
newVolumeArray("volume6-8", "1Gi", "", "claim6-8", v1.VolumeAvailable, v1.PersistentVolumeReclaimRecycle, classEmpty), newVolumeArray("volume6-8", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRecycle, classEmpty),
noclaims, noclaims,
noclaims, noclaims,
noevents, noerrors, noevents, noerrors,
wrapTestWithInjectedOperation(wrapTestWithReclaimCalls(operationRecycle, []error{}, testSyncVolume), func(ctrl *PersistentVolumeController, reactor *volumeReactor) { 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.lock.Lock() reactor.MarkVolumeAvaiable("volume6-8")
volume := reactor.volumes["volume6-8"]
volume.Spec.ClaimRef.UID = ""
volume.Status.Phase = v1.VolumeAvailable
reactor.lock.Unlock()
}), }),
}, },
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment