From 436544488b865ef801b1875b40895e1ee30b0fea Mon Sep 17 00:00:00 2001 From: Kenichi Omichi Date: Wed, 20 Feb 2019 19:22:16 +0000 Subject: [PATCH] Add ExpectError() to e2e test framework There is a lot of gomega.Expect(err).To(gomega.HaveOccurred()) callers which expect an error happens in e2e tests. However these test code seems confusing because the code readers need to take care of To() or NotTo() on each test scenario. This adds ExpectError() for more readable test code. In addition, this applies ExpectError() to e2e provisioning.go as a sample. --- test/e2e/framework/util.go | 5 +++++ test/e2e/storage/testsuites/provisioning.go | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/test/e2e/framework/util.go b/test/e2e/framework/util.go index fd264d662c1..36c64c45733 100644 --- a/test/e2e/framework/util.go +++ b/test/e2e/framework/util.go @@ -2038,6 +2038,11 @@ func RandomSuffix() string { return strconv.Itoa(r.Int() % 10000) } +// ExpectError expects an error happens, otherwise an exception raises +func ExpectError(err error, explain ...interface{}) { + gomega.Expect(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...) diff --git a/test/e2e/storage/testsuites/provisioning.go b/test/e2e/storage/testsuites/provisioning.go index 61e5d53415e..9d76110f6bf 100644 --- a/test/e2e/storage/testsuites/provisioning.go +++ b/test/e2e/storage/testsuites/provisioning.go @@ -484,7 +484,7 @@ func (t StorageClassTest) TestBindingWaitForFirstConsumerMultiPVC(claims []*v1.P // Wait for ClaimProvisionTimeout (across all PVCs in parallel) and make sure the phase did not become Bound i.e. the Wait errors out By("checking the claims are in pending state") err = framework.WaitForPersistentVolumeClaimsPhase(v1.ClaimBound, t.Client, namespace, claimNames, 2*time.Second /* Poll */, framework.ClaimProvisionShortTimeout, true) - Expect(err).To(HaveOccurred()) + framework.ExpectError(err) verifyPVCsPending(t.Client, createdClaims) By("creating a pod referring to the claims")