move funcs of expect.go to e2e/common

This commit is contained in:
Jianfei Bai 2019-10-27 11:54:32 +08:00
parent ba4adeff20
commit 7401f2fe9b
3 changed files with 18 additions and 20 deletions

View File

@ -20,6 +20,7 @@ import (
"bytes"
"fmt"
"io"
"runtime/debug"
"strconv"
"strings"
"time"
@ -158,6 +159,22 @@ func getRestartDelay(podClient *framework.PodClient, podName string, containerNa
return 0, fmt.Errorf("timeout getting pod restart delay")
}
// 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
}
framework.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...)
}
var _ = framework.KubeDescribe("Pods", func() {
f := framework.NewDefaultFramework("pods")
var podClient *framework.PodClient
@ -526,7 +543,7 @@ var _ = framework.KubeDescribe("Pods", func() {
"FOOSERVICE_PORT_8765_TCP=",
"FOOSERVICE_PORT_8765_TCP_ADDR=",
}
framework.ExpectNoErrorWithRetries(func() error {
expectNoErrorWithRetries(func() error {
return f.MatchContainerOutput(pod, containerName, expectedVars, gomega.ContainSubstring)
}, maxRetries, "Container should have service environment variables set")
})

View File

@ -17,8 +17,6 @@ limitations under the License.
package framework
import (
"runtime/debug"
"github.com/onsi/gomega"
)
@ -47,19 +45,3 @@ func ExpectNoError(err error, explain ...interface{}) {
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...)
}

View File

@ -42,7 +42,6 @@ import (
)
// TODO: Move to its own subpkg.
// expectNoErrorWithRetries to their own subpackages within framework.
// 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...)