From a3a4320aff280f2e4ce0f64ad873d594b5ec6dc8 Mon Sep 17 00:00:00 2001 From: Michelle Au Date: Tue, 26 Nov 2019 16:58:25 -0800 Subject: [PATCH] Wait for PV to be available before creating PVCs in volume binding test --- .../volumescheduling/volume_binding_test.go | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/test/integration/volumescheduling/volume_binding_test.go b/test/integration/volumescheduling/volume_binding_test.go index 874c9ad953e..10e9a5d38ef 100644 --- a/test/integration/volumescheduling/volume_binding_test.go +++ b/test/integration/volumescheduling/volume_binding_test.go @@ -215,6 +215,20 @@ func TestVolumeBinding(t *testing.T) { } } + // Wait for PVs to become available to avoid race condition in PV controller + // https://github.com/kubernetes/kubernetes/issues/85320 + for _, pvConfig := range test.pvs { + if err := waitForPVPhase(config.client, pvConfig.name, v1.VolumeAvailable); err != nil { + t.Fatalf("PersistentVolume %q failed to become available: %v", pvConfig.name, err) + } + } + + for _, pvConfig := range test.unboundPvs { + if err := waitForPVPhase(config.client, pvConfig.name, v1.VolumeAvailable); err != nil { + t.Fatalf("PersistentVolume %q failed to become available: %v", pvConfig.name, err) + } + } + // Create PVCs for _, pvcConfig := range test.pvcs { pvc := makePVC(pvcConfig.name, config.ns, &classes[pvcConfig.scName].Name, pvcConfig.preboundPV) @@ -1180,6 +1194,20 @@ func validatePVPhase(t *testing.T, client clientset.Interface, pvName string, ph } } +func waitForPVPhase(client clientset.Interface, pvName string, phase v1.PersistentVolumePhase) error { + return wait.PollImmediate(time.Second, 30*time.Second, func() (bool, error) { + pv, err := client.CoreV1().PersistentVolumes().Get(pvName, metav1.GetOptions{}) + if err != nil { + return false, err + } + + if pv.Status.Phase == phase { + return true, nil + } + return false, nil + }) +} + func waitForPVCBound(client clientset.Interface, pvc *v1.PersistentVolumeClaim) error { return wait.Poll(time.Second, 30*time.Second, func() (bool, error) { claim, err := client.CoreV1().PersistentVolumeClaims(pvc.Namespace).Get(pvc.Name, metav1.GetOptions{})