diff --git a/test/e2e/framework/BUILD b/test/e2e/framework/BUILD index 350ac7aba9b..ada009b6e8d 100644 --- a/test/e2e/framework/BUILD +++ b/test/e2e/framework/BUILD @@ -6,6 +6,7 @@ go_library( "cleanup.go", "create.go", "exec_util.go", + "expect.go", "flake_reporting_util.go", "framework.go", "get-kubemark-resource-usage.go", diff --git a/test/e2e/framework/expect.go b/test/e2e/framework/expect.go new file mode 100644 index 00000000000..32335165498 --- /dev/null +++ b/test/e2e/framework/expect.go @@ -0,0 +1,65 @@ +/* +Copyright 2014 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 framework + +import ( + "runtime/debug" + + "github.com/onsi/gomega" +) + +// ExpectEqual expects the specified two are the same, otherwise an exception raises +func ExpectEqual(actual interface{}, extra interface{}, explain ...interface{}) { + gomega.ExpectWithOffset(1, actual).To(gomega.Equal(extra), explain...) +} + +// ExpectNotEqual expects the specified two are not the same, otherwise an exception raises +func ExpectNotEqual(actual interface{}, extra interface{}, explain ...interface{}) { + gomega.ExpectWithOffset(1, actual).NotTo(gomega.Equal(extra), explain...) +} + +// ExpectError expects an error happens, otherwise an exception raises +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...) +} + +// ExpectNoErrorWithOffset checks if "err" is set, and if so, fails assertion while logging the error at "offset" levels above its caller +// (for example, for call chain f -> g -> ExpectNoErrorWithOffset(1, ...) error would be logged for "f"). +func ExpectNoErrorWithOffset(offset int, err error, explain ...interface{}) { + gomega.ExpectWithOffset(1+offset, err).NotTo(gomega.HaveOccurred(), explain...) +} + +// ExpectNoErrorWithRetries checks if an error occurs with the given retry count. +func ExpectNoErrorWithRetries(fn func() error, maxRetries int, explain ...interface{}) { + var err error + for i := 0; i < maxRetries; i++ { + err = fn() + if err == nil { + return + } + Logf("(Attempt %d of %d) Unexpected error occurred: %v", i+1, maxRetries, err) + } + if err != nil { + debug.PrintStack() + } + gomega.ExpectWithOffset(1, err).NotTo(gomega.HaveOccurred(), explain...) +} diff --git a/test/e2e/framework/util.go b/test/e2e/framework/util.go index fa767dfb147..1b44f523ee2 100644 --- a/test/e2e/framework/util.go +++ b/test/e2e/framework/util.go @@ -32,7 +32,6 @@ import ( "path" "path/filepath" "regexp" - "runtime/debug" "sort" "strconv" "strings" @@ -937,48 +936,6 @@ func RandomSuffix() string { return strconv.Itoa(rand.Intn(10000)) } -// ExpectEqual expects the specified two are the same, otherwise an exception raises -func ExpectEqual(actual interface{}, extra interface{}, explain ...interface{}) { - gomega.ExpectWithOffset(1, actual).To(gomega.Equal(extra), explain...) -} - -// ExpectNotEqual expects the specified two are not the same, otherwise an exception raises -func ExpectNotEqual(actual interface{}, extra interface{}, explain ...interface{}) { - gomega.ExpectWithOffset(1, actual).NotTo(gomega.Equal(extra), explain...) -} - -// ExpectError expects an error happens, otherwise an exception raises -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...) -} - -// ExpectNoErrorWithOffset checks if "err" is set, and if so, fails assertion while logging the error at "offset" levels above its caller -// (for example, for call chain f -> g -> ExpectNoErrorWithOffset(1, ...) error would be logged for "f"). -func ExpectNoErrorWithOffset(offset int, err error, explain ...interface{}) { - gomega.ExpectWithOffset(1+offset, err).NotTo(gomega.HaveOccurred(), explain...) -} - -// ExpectNoErrorWithRetries checks if an error occurs with the given retry count. -func ExpectNoErrorWithRetries(fn func() error, maxRetries int, explain ...interface{}) { - var err error - for i := 0; i < maxRetries; i++ { - err = fn() - if err == nil { - return - } - Logf("(Attempt %d of %d) Unexpected error occurred: %v", i+1, maxRetries, err) - } - if err != nil { - debug.PrintStack() - } - gomega.ExpectWithOffset(1, err).NotTo(gomega.HaveOccurred(), explain...) -} - // Cleanup stops everything from filePath from namespace ns and checks if everything matching selectors from the given namespace is correctly stopped. func Cleanup(filePath, ns string, selectors ...string) { ginkgo.By("using delete to clean up resources")