
This cleans up the framework.util.go file and moves various Expect* functions into their own file for better organization. Signed-off-by: alejandrox1 <alarcj137@gmail.com>
66 lines
2.4 KiB
Go
66 lines
2.4 KiB
Go
/*
|
|
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...)
|
|
}
|