mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-26 21:17:23 +00:00
Merge pull request #83510 from alejandrox1/framework-err-handling
Refactored e2e-test-framework util.go
This commit is contained in:
commit
53d500eca3
@ -6,6 +6,7 @@ go_library(
|
|||||||
"cleanup.go",
|
"cleanup.go",
|
||||||
"create.go",
|
"create.go",
|
||||||
"exec_util.go",
|
"exec_util.go",
|
||||||
|
"expect.go",
|
||||||
"flake_reporting_util.go",
|
"flake_reporting_util.go",
|
||||||
"framework.go",
|
"framework.go",
|
||||||
"get-kubemark-resource-usage.go",
|
"get-kubemark-resource-usage.go",
|
||||||
|
65
test/e2e/framework/expect.go
Normal file
65
test/e2e/framework/expect.go
Normal file
@ -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...)
|
||||||
|
}
|
@ -32,7 +32,6 @@ import (
|
|||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"runtime/debug"
|
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -937,48 +936,6 @@ func RandomSuffix() string {
|
|||||||
return strconv.Itoa(rand.Intn(10000))
|
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.
|
// 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) {
|
func Cleanup(filePath, ns string, selectors ...string) {
|
||||||
ginkgo.By("using delete to clean up resources")
|
ginkgo.By("using delete to clean up resources")
|
||||||
|
Loading…
Reference in New Issue
Block a user