From 1eee8ff3da4d9380e691dc50563e466614d38988 Mon Sep 17 00:00:00 2001 From: carlory Date: Tue, 10 Sep 2024 13:02:19 +0800 Subject: [PATCH] fix lint --- .../volume/persistent_volumes_test.go | 53 ++++++++++-------- test/integration/volume/util_test.go | 54 ------------------- 2 files changed, 32 insertions(+), 75 deletions(-) delete mode 100644 test/integration/volume/util_test.go diff --git a/test/integration/volume/persistent_volumes_test.go b/test/integration/volume/persistent_volumes_test.go index 09512359f28..eed2c652628 100644 --- a/test/integration/volume/persistent_volumes_test.go +++ b/test/integration/volume/persistent_volumes_test.go @@ -286,7 +286,9 @@ func TestPersistentVolumeBindRace(t *testing.T) { waitForPersistentVolumePhase(testClient, pv.Name, watchPV, v1.VolumeBound) klog.V(2).Infof("TestPersistentVolumeBindRace pv bound") - waitForAnyPersistentVolumeClaimPhase(watchPVC, v1.ClaimBound) + if err := waitForAnyPersistentVolumeClaimPhase(watchPVC, v1.ClaimBound); err != nil { + t.Fatalf("Unexpected error waiting for any pvc to be bound: %v", err) + } klog.V(2).Infof("TestPersistentVolumeBindRace pvc bound") pv, err = testClient.CoreV1().PersistentVolumes().Get(context.TODO(), pv.Name, metav1.GetOptions{}) @@ -873,10 +875,11 @@ func TestPersistentVolumeMultiPVsPVCs(t *testing.T) { }() // wait until the binder pairs all claims - for i := 0; i < objCount; i++ { - waitForAnyPersistentVolumeClaimPhase(watchPVC, v1.ClaimBound) - klog.V(1).Infof("%d claims bound", i+1) + err := waitForSomePersistentVolumeClaimPhase(tCtx, testClient, namespaceName, objCount, watchPVC, v1.ClaimBound) + if err != nil { + t.Fatalf("Failed to wait for all claims to be bound: %v", err) } + // wait until the binder pairs all volumes for i := 0; i < objCount; i++ { waitForPersistentVolumePhase(testClient, pvs[i].Name, watchPV, v1.VolumeBound) @@ -954,7 +957,9 @@ func TestPersistentVolumeControllerStartup(t *testing.T) { // Drain watchPVC with all events generated by the PVC until it's bound // We don't want to catch "PVC created with Status.Phase == Pending" // later in this test. - waitForAnyPersistentVolumeClaimPhase(watchPVC, v1.ClaimBound) + if err := waitForAnyPersistentVolumeClaimPhase(watchPVC, v1.ClaimBound); err != nil { + t.Fatalf("Unexpected error waiting for any pvc to be bound: %v", err) + } pv := createPV(pvName, "/tmp/foo"+strconv.Itoa(i), "1G", []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce}, v1.PersistentVolumeReclaimRetain) @@ -1091,22 +1096,7 @@ func TestPersistentVolumeProvisionMultiPVCs(t *testing.T) { }() // Wait until the controller provisions and binds all of them - err := wait.ExponentialBackoffWithContext(tCtx, retry.DefaultBackoff, func(ctx context.Context) (bool, error) { - for i := 0; i < objCount; i++ { - waitErr := waitForAnyPersistentVolumeClaimPhase(watchPVC, v1.ClaimBound) - if waitErr != nil { - newWatchPVC, err := testClient.CoreV1().PersistentVolumeClaims(namespaceName).Watch(ctx, metav1.ListOptions{}) - if err != nil { - return false, err - } - watchPVC.Stop() - watchPVC = newWatchPVC - return false, waitErr - } - klog.V(1).Infof("%d claims bound", i+1) - } - return true, nil - }) + err := waitForSomePersistentVolumeClaimPhase(tCtx, testClient, namespaceName, objCount, watchPVC, v1.ClaimBound) if err != nil { t.Fatalf("Failed to wait for all claims to be bound: %v", err) } @@ -1477,6 +1467,27 @@ func waitForAnyPersistentVolumePhase(w watch.Interface, phase v1.PersistentVolum } } +func waitForSomePersistentVolumeClaimPhase(ctx context.Context, testClient clientset.Interface, namespace string, objCount int, watchPVC watch.Interface, phase v1.PersistentVolumeClaimPhase) error { + return wait.ExponentialBackoffWithContext(ctx, retry.DefaultBackoff, func(ctx context.Context) (bool, error) { + for i := 0; i < objCount; i++ { + waitErr := waitForAnyPersistentVolumeClaimPhase(watchPVC, v1.ClaimBound) + if waitErr != nil { + klog.Errorf("Failed to wait for a claim (%d/%d) to be bound: %v", i+1, objCount, waitErr) + klog.Info("Recreating a watch for claims and re-checking the count of bound claims") + newWatchPVC, err := testClient.CoreV1().PersistentVolumeClaims(namespace).Watch(ctx, metav1.ListOptions{}) + if err != nil { + return false, err + } + watchPVC.Stop() + watchPVC = newWatchPVC + return false, waitErr + } + klog.V(1).Infof("%d claims bound", i+1) + } + return true, nil + }) +} + func waitForAnyPersistentVolumeClaimPhase(w watch.Interface, phase v1.PersistentVolumeClaimPhase) error { for { event, ok := <-w.ResultChan() diff --git a/test/integration/volume/util_test.go b/test/integration/volume/util_test.go deleted file mode 100644 index a6bd6457603..00000000000 --- a/test/integration/volume/util_test.go +++ /dev/null @@ -1,54 +0,0 @@ -/* -Copyright 2024 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package volume - -import ( - "encoding/json" - "os" - "path/filepath" - - "k8s.io/klog/v2" -) - -func reportToArtifacts(filename string, obj any) { - if os.Getenv("ARTIFACTS") == "" { - return - } - path := filepath.Join(os.Getenv("ARTIFACTS"), filename) - file, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) - if err != nil { - klog.Error("Error opening file:", err) - return - } - defer func() { - if err := file.Close(); err != nil { - klog.Error("Error closing file:", err) - } - }() - - content, err := json.Marshal(obj) - if err != nil { - klog.Error("Error marshalling to json:", err) - return - } - content = append(content, '\n') - _, err = file.Write(content) - if err != nil { - klog.Error("Error writing to file:", err) - return - } -}