From 6f0f9e5600a052294cdf1f69d5313d0ab2c272e0 Mon Sep 17 00:00:00 2001 From: Chris Crone Date: Mon, 7 Sep 2020 13:43:16 +0200 Subject: [PATCH] e2e.framework: Add helper to retry HTTP requests Signed-off-by: Chris Crone --- tests/framework/e2e.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/framework/e2e.go b/tests/framework/e2e.go index 8dc685e1c..d089461e3 100644 --- a/tests/framework/e2e.go +++ b/tests/framework/e2e.go @@ -22,12 +22,14 @@ import ( "errors" "fmt" "io/ioutil" + "net/http" "os" "os/exec" "path/filepath" "runtime" "strings" "testing" + "time" "gotest.tools/v3/assert" is "gotest.tools/v3/assert/cmp" @@ -192,3 +194,21 @@ func ParseContainerInspect(stdout string) (*containers.Container, error) { } return &res, nil } + +// HTTPGetWithRetry performs an HTTP GET on an `endpoint`. +// In the case of an error it retries the same request after a 5 second sleep, +// returning the error if count of `tries` is reached +func HTTPGetWithRetry(endpoint string, tries int) (*http.Response, error) { + var ( + r *http.Response + err error + ) + for t := 0; t < tries; t++ { + r, err = http.Get(endpoint) + if err == nil || t == tries-1 { + break + } + time.Sleep(5 * time.Second) + } + return r, err +}