e2e.framework: Add helper to retry HTTP requests

Signed-off-by: Chris Crone <christopher.crone@docker.com>
This commit is contained in:
Chris Crone 2020-09-07 13:43:16 +02:00
parent 9545625131
commit 6f0f9e5600
1 changed files with 20 additions and 0 deletions

View File

@ -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
}