Merge pull request #84410 from jfbai/move-funcs-of-expect-to-common

move funcs of expect.go to e2e/common
This commit is contained in:
Kubernetes Prow Robot 2019-10-29 19:54:52 -07:00 committed by GitHub
commit e86878436f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 20 deletions

View File

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

View File

@ -17,8 +17,6 @@ limitations under the License.
package framework package framework
import ( import (
"runtime/debug"
"github.com/onsi/gomega" "github.com/onsi/gomega"
) )
@ -47,19 +45,3 @@ func ExpectNoError(err error, explain ...interface{}) {
func ExpectNoErrorWithOffset(offset int, err error, explain ...interface{}) { func ExpectNoErrorWithOffset(offset int, err error, explain ...interface{}) {
gomega.ExpectWithOffset(1+offset, err).NotTo(gomega.HaveOccurred(), explain...) 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. // 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. // expectNoError checks if "err" is set, and if so, fails assertion while logging the error.
func expectNoError(err error, explain ...interface{}) { func expectNoError(err error, explain ...interface{}) {
expectNoErrorWithOffset(1, err, explain...) expectNoErrorWithOffset(1, err, explain...)