Skip to content

Commit

Permalink
e2e_storage:stop using deprecated framework.ExpectError
Browse files Browse the repository at this point in the history
Signed-off-by: liyuerich <yue.li@daocloud.io>
  • Loading branch information
liyuerich committed Apr 22, 2024
1 parent 95a6f2e commit 014b23e
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 27 deletions.
9 changes: 0 additions & 9 deletions test/e2e/framework/expect.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,15 +293,6 @@ func (f *FailureError) backtrace() {
// }
var ErrFailure error = FailureError{}

// ExpectError expects an error happens, otherwise an exception raises
//
// Deprecated: use gomega.Expect().To(gomega.HaveOccurred()) or (better!) check
// specifically for the error that is expected with
// gomega.Expect().To(gomega.MatchError(gomega.ContainSubstring()))
func ExpectError(err error, explain ...interface{}) {
gomega.ExpectWithOffset(1, err).To(gomega.HaveOccurred(), explain...)
}

// ExpectNoError checks if "err" is set, and if so, fails assertion while logging the error.
func ExpectNoError(err error, explain ...interface{}) {
ExpectNoErrorWithOffset(1, err, explain...)
Expand Down
3 changes: 2 additions & 1 deletion test/e2e/storage/csi_mock/csi_attach_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"

v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -101,7 +102,7 @@ var _ = utils.SIGDescribe("CSI Mock volume attach", func() {
}
}
if test.disableAttach {
framework.ExpectError(err, "Unexpected VolumeAttachment found")
gomega.Expect(err).To(gomega.MatchError(apierrors.IsNotFound, "Unexpected VolumeAttachment found"))
}
})

Expand Down
4 changes: 3 additions & 1 deletion test/e2e/storage/csi_mock/csi_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"time"

"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -430,7 +432,7 @@ func deleteSnapshot(cs clientset.Interface, config *storageframework.PerTestConf

// check if the snapshot is deleted
_, err = dc.Resource(utils.SnapshotGVR).Get(context.TODO(), snapshot.GetName(), metav1.GetOptions{})
framework.ExpectError(err)
gomega.Expect(err).To(gomega.MatchError(apierrors.IsNotFound, "the snapshot is not deleted"))
}

type snapshotMetricsTestConfig struct {
Expand Down
6 changes: 3 additions & 3 deletions test/e2e/storage/csi_mock/csi_volume_expansion.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
csipbv1 "github.com/container-storage-interface/spec/lib/go/csi"
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -151,11 +152,10 @@ var _ = utils.SIGDescribe("CSI Mock volume expansion", func() {
framework.Failf("error updating pvc size %q", pvc.Name)
}
if test.expectFailure {
err = testsuites.WaitForResizingCondition(ctx, pvc, m.cs, csiResizingConditionWait)
framework.ExpectError(err, "unexpected resizing condition on PVC")
gomega.Consistently(ctx, framework.GetObject(m.cs.CoreV1().PersistentVolumeClaims(pvc.Namespace).Get, pvc.Name, metav1.GetOptions{})).WithTimeout(csiResizingConditionWait).
ShouldNot(gomega.HaveField("Status.Conditions", gomega.ContainElement(gomega.HaveField("Type", gomega.Equal("PersistentVolumeClaimResizing")))), "unexpected resizing condition on PVC")
return
}

ginkgo.By("Waiting for persistent volume resize to finish")
err = testsuites.WaitForControllerVolumeResize(ctx, pvc, m.cs, csiResizeWaitPeriod)
framework.ExpectNoError(err, "While waiting for CSI PV resize to finish")
Expand Down
6 changes: 3 additions & 3 deletions test/e2e/storage/persistent_volumes-local.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,9 @@ var _ = utils.SIGDescribe("PersistentVolumes-local", func() {
ginkgo.By("Creating local PVC and PV")
createLocalPVCsPVs(ctx, config, []*localTestVolume{testVol}, immediateMode)
pod, err := createLocalPod(ctx, config, testVol, nil)
framework.ExpectError(err)
gomega.Expect(err).To(gomega.MatchError(gomega.ContainSubstring("is not Running")))
err = e2epod.WaitTimeoutForPodRunningInNamespace(ctx, config.client, pod.Name, pod.Namespace, f.Timeouts.PodStart)
framework.ExpectError(err)
gomega.Expect(err).To(gomega.MatchError(framework.ErrFailure))
cleanupLocalPVCsPVs(ctx, config, []*localTestVolume{testVol})
})

Expand All @@ -349,7 +349,7 @@ var _ = utils.SIGDescribe("PersistentVolumes-local", func() {
framework.ExpectNoError(err)

err = e2epod.WaitTimeoutForPodRunningInNamespace(ctx, config.client, pod.Name, pod.Namespace, f.Timeouts.PodStart)
framework.ExpectError(err)
gomega.Expect(err).To(gomega.MatchError(framework.ErrFailure))

cleanupLocalVolumes(ctx, config, []*localTestVolume{testVol})
})
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/storage/testsuites/provisioning.go
Original file line number Diff line number Diff line change
Expand Up @@ -1004,7 +1004,7 @@ func (t StorageClassTest) TestBindingWaitForFirstConsumerMultiPVC(ctx context.Co
// Wait for ClaimProvisionTimeout (across all PVCs in parallel) and make sure the phase did not become Bound i.e. the Wait errors out
ginkgo.By("checking the claims are in pending state")
err = e2epv.WaitForPersistentVolumeClaimsPhase(ctx, v1.ClaimBound, t.Client, namespace, claimNames, 2*time.Second /* Poll */, t.Timeouts.ClaimProvisionShort, true)
framework.ExpectError(err)
gomega.Expect(err).To(gomega.MatchError(gomega.ContainSubstring("not all in phase Bound")))
verifyPVCsPending(ctx, t.Client, createdClaims)

ginkgo.By("creating a pod referring to the claims")
Expand Down
6 changes: 3 additions & 3 deletions test/e2e/storage/testsuites/snapshottable.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,12 +410,12 @@ func deleteVolumeSnapshot(ctx context.Context, f *framework.Framework, dc dynami
switch pattern.SnapshotDeletionPolicy {
case storageframework.DeleteSnapshot:
ginkgo.By("checking the SnapshotContent has been deleted")
err = storageutils.WaitForGVRDeletion(ctx, dc, storageutils.SnapshotContentGVR, vscontent.GetName(), framework.Poll, f.Timeouts.SnapshotDelete)
err = storageutils.EnsureGVRDeletion(ctx, dc, storageutils.SnapshotContentGVR, vscontent.GetName(), framework.Poll, f.Timeouts.SnapshotDelete, "")
framework.ExpectNoError(err)
case storageframework.RetainSnapshot:
ginkgo.By("checking the SnapshotContent has not been deleted")
err = storageutils.WaitForGVRDeletion(ctx, dc, storageutils.SnapshotContentGVR, vscontent.GetName(), 1*time.Second /* poll */, 30*time.Second /* timeout */)
framework.ExpectError(err)
err = storageutils.EnsureNoGVRDeletion(ctx, dc, storageutils.SnapshotContentGVR, vscontent.GetName(), 1*time.Second /* poll */, 30*time.Second /* timeout */, "")
framework.ExpectNoError(err)
}
}

Expand Down
23 changes: 21 additions & 2 deletions test/e2e/storage/testsuites/volume_expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/onsi/gomega"

v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/errors"
Expand Down Expand Up @@ -157,6 +158,24 @@ func (v *volumeExpandTestSuite) DefineTests(driver storageframework.TestDriver,
ginkgo.DeferCleanup(cleanup)

var err error
// create Pod with pvc
ginkgo.By("Creating a pod with PVC")
podConfig := e2epod.Config{
NS: f.Namespace.Name,
PVCs: []*v1.PersistentVolumeClaim{l.resource.Pvc},
SeLinuxLabel: e2epod.GetLinuxLabel(),
NodeSelection: l.config.ClientNodeSelection,
ImageID: e2epod.GetDefaultTestImageID(),
}
l.pod, err = e2epod.CreateSecPodWithNodeSelection(ctx, f.ClientSet, &podConfig, f.Timeouts.PodStart)
ginkgo.DeferCleanup(e2epod.DeletePodWithWait, f.ClientSet, l.pod)
framework.ExpectNoError(err, "While creating pods for expanding")

// Waiting for pod to run
ginkgo.By("Waiting for pod to run")
err = e2epod.WaitTimeoutForPodRunningInNamespace(ctx, f.ClientSet, l.pod.Name, l.pod.Namespace, f.Timeouts.PodStart)
framework.ExpectNoError(err)

gomega.Expect(l.resource.Sc.AllowVolumeExpansion).NotTo(gomega.BeNil())
allowVolumeExpansion := *l.resource.Sc.AllowVolumeExpansion
gomega.Expect(allowVolumeExpansion).To(gomega.BeFalse())
Expand All @@ -166,7 +185,7 @@ func (v *volumeExpandTestSuite) DefineTests(driver storageframework.TestDriver,
newSize.Add(resource.MustParse("1Gi"))
framework.Logf("currentPvcSize %v, newSize %v", currentPvcSize, newSize)
_, err = ExpandPVCSize(ctx, l.resource.Pvc, newSize, f.ClientSet)
framework.ExpectError(err, "While updating non-expandable PVC")
gomega.Expect(err).To(gomega.MatchError(apierrors.IsForbidden, "While updating non-expandable PVC"))
})
} else {
ginkgo.It("Verify if offline PVC expansion works", func(ctx context.Context) {
Expand Down Expand Up @@ -316,7 +335,7 @@ func ExpandPVCSize(ctx context.Context, origPVC *v1.PersistentVolumeClaim, size
return true, nil
})
if wait.Interrupted(waitErr) {
return nil, fmt.Errorf("timed out attempting to update PVC size. last update error: %v", lastUpdateError)
return nil, fmt.Errorf("timed out attempting to update PVC size. last update error: %w", lastUpdateError)
}
if waitErr != nil {
return nil, fmt.Errorf("failed to expand PVC size (check logs for error): %v", waitErr)
Expand Down
34 changes: 34 additions & 0 deletions test/e2e/storage/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,40 @@ func WaitForGVRDeletion(ctx context.Context, c dynamic.Interface, gvr schema.Gro
return fmt.Errorf("%s %s is not deleted within %v", gvr.Resource, objectName, timeout)
}

// EnsureGVRDeletion checks that no object as defined by the group/version/kind and name is ever found during the given time period
func EnsureGVRDeletion(ctx context.Context, c dynamic.Interface, gvr schema.GroupVersionResource, objectName string, poll, timeout time.Duration, namespace string) error {
var resourceClient dynamic.ResourceInterface
if namespace != "" {
resourceClient = c.Resource(gvr).Namespace(namespace)
} else {
resourceClient = c.Resource(gvr)
}

err := framework.Gomega().Eventually(ctx, func(ctx context.Context) error {
_, err := resourceClient.Get(ctx, objectName, metav1.GetOptions{})
return err
}).WithTimeout(timeout).WithPolling(poll).Should(gomega.MatchError(apierrors.IsNotFound, fmt.Sprintf("failed to delete %s %s", gvr, objectName)))
return err
}

// EnsureNoGVRDeletion checks that an object as defined by the group/version/kind and name has not been deleted during the given time period
func EnsureNoGVRDeletion(ctx context.Context, c dynamic.Interface, gvr schema.GroupVersionResource, objectName string, poll, timeout time.Duration, namespace string) error {
var resourceClient dynamic.ResourceInterface
if namespace != "" {
resourceClient = c.Resource(gvr).Namespace(namespace)
} else {
resourceClient = c.Resource(gvr)
}
err := framework.Gomega().Consistently(ctx, func(ctx context.Context) error {
_, err := resourceClient.Get(ctx, objectName, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("failed to get %s %s: %w", gvr.Resource, objectName, err)
}
return nil
}).WithTimeout(timeout).WithPolling(poll).Should(gomega.Succeed())
return err
}

// WaitForNamespacedGVRDeletion waits until a namespaced object has been deleted
func WaitForNamespacedGVRDeletion(ctx context.Context, c dynamic.Interface, gvr schema.GroupVersionResource, ns, objectName string, poll, timeout time.Duration) error {
framework.Logf("Waiting up to %v for %s %s to be deleted", timeout, gvr.Resource, objectName)
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/storage/volume_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ var _ = utils.SIGDescribe(framework.WithSerial(), "Volume metrics", func() {
framework.ExpectNoError(err, "failed to create Pod %s/%s", pod.Namespace, pod.Name)

err = e2epod.WaitTimeoutForPodRunningInNamespace(ctx, c, pod.Name, pod.Namespace, f.Timeouts.PodStart)
framework.ExpectError(err)
gomega.Expect(err).To(gomega.MatchError(framework.ErrFailure))

framework.Logf("Deleting pod %q/%q", pod.Namespace, pod.Name)
framework.ExpectNoError(e2epod.DeletePodWithWait(ctx, c, pod))
Expand Down
6 changes: 3 additions & 3 deletions test/e2e/storage/volume_provisioning.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ var _ = utils.SIGDescribe("Dynamic Provisioning", func() {

// The claim should timeout phase:Pending
err = e2epv.WaitForPersistentVolumeClaimPhase(ctx, v1.ClaimBound, c, ns, claim.Name, 2*time.Second, framework.ClaimProvisionShortTimeout)
framework.ExpectError(err)
gomega.Expect(err).To(gomega.MatchError(gomega.ContainSubstring("not all in phase Bound")))
framework.Logf(err.Error())
claim, err = c.CoreV1().PersistentVolumeClaims(ns).Get(ctx, claim.Name, metav1.GetOptions{})
framework.ExpectNoError(err)
Expand Down Expand Up @@ -618,7 +618,7 @@ var _ = utils.SIGDescribe("Dynamic Provisioning", func() {

// The claim should timeout phase:Pending
err = e2epv.WaitForPersistentVolumeClaimPhase(ctx, v1.ClaimBound, c, ns, claim.Name, 2*time.Second, framework.ClaimProvisionShortTimeout)
framework.ExpectError(err)
gomega.Expect(err).To(gomega.MatchError(gomega.ContainSubstring("not all in phase Bound")))
framework.Logf(err.Error())
claim, err = c.CoreV1().PersistentVolumeClaims(ns).Get(ctx, claim.Name, metav1.GetOptions{})
framework.ExpectNoError(err)
Expand Down Expand Up @@ -747,7 +747,7 @@ func waitForProvisionedVolumesDeleted(ctx context.Context, c clientset.Interface
return true, nil // No PVs remain
})
if err != nil {
return remainingPVs, fmt.Errorf("Error waiting for PVs to be deleted: %w", err)
return remainingPVs, fmt.Errorf("error waiting for PVs to be deleted: %w", err)
}
return nil, nil
}
Expand Down

0 comments on commit 014b23e

Please sign in to comment.