Fix data race in volume controller unit test.

Reactor must be locked when fiddling with reactor.volumes and reactor.claims.
Therefore add new functions to add/delete volume/claim with sending an event.
This commit is contained in:
Jan Safranek
2016-06-01 08:35:33 +02:00
parent c0dfccc388
commit 2d43e4549e
2 changed files with 59 additions and 19 deletions

View File

@@ -25,7 +25,6 @@ import (
"k8s.io/kubernetes/pkg/client/cache"
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
"k8s.io/kubernetes/pkg/controller/framework"
"k8s.io/kubernetes/pkg/conversion"
)
// Test the real controller methods (add/update/delete claim/volume) with
@@ -52,8 +51,7 @@ func TestControllerSync(t *testing.T) {
// Custom test function that generates an add event
func(ctrl *PersistentVolumeController, reactor *volumeReactor, test controllerTest) error {
claim := newClaim("claim5-2", "uid5-2", "1Gi", "", api.ClaimPending)
reactor.claims[claim.Name] = claim
reactor.claimSource.Add(claim)
reactor.addClaimEvent(claim)
return nil
},
},
@@ -69,14 +67,7 @@ func TestControllerSync(t *testing.T) {
func(ctrl *PersistentVolumeController, reactor *volumeReactor, test controllerTest) error {
obj := ctrl.claims.List()[0]
claim := obj.(*api.PersistentVolumeClaim)
// Remove the claim from list of resulting claims.
delete(reactor.claims, claim.Name)
// Poke the controller with deletion event. Cloned claim is
// needed to prevent races (and we would get a clone from etcd
// too).
clone, _ := conversion.NewCloner().DeepCopy(claim)
claimClone := clone.(*api.PersistentVolumeClaim)
reactor.claimSource.Delete(claimClone)
reactor.deleteClaimEvent(claim)
return nil
},
},
@@ -92,14 +83,7 @@ func TestControllerSync(t *testing.T) {
func(ctrl *PersistentVolumeController, reactor *volumeReactor, test controllerTest) error {
obj := ctrl.volumes.store.List()[0]
volume := obj.(*api.PersistentVolume)
// Remove the volume from list of resulting volumes.
delete(reactor.volumes, volume.Name)
// Poke the controller with deletion event. Cloned volume is
// needed to prevent races (and we would get a clone from etcd
// too).
clone, _ := conversion.NewCloner().DeepCopy(volume)
volumeClone := clone.(*api.PersistentVolume)
reactor.volumeSource.Delete(volumeClone)
reactor.deleteVolumeEvent(volume)
return nil
},
},