diff --git a/test/fixtures/e2eapi.go b/test/fixtures/e2eapi.go index 3f0d9d9646..12406f4478 100644 --- a/test/fixtures/e2eapi.go +++ b/test/fixtures/e2eapi.go @@ -23,6 +23,9 @@ import ( "log" "net/http" "strings" + "time" + + "k8s.io/apimachinery/pkg/util/wait" ) func InvokeE2EAPI(format string, args ...interface{}) string { @@ -51,7 +54,27 @@ func InvokeE2EAPI(format string, args ...interface{}) string { func InvokeE2EAPIPOST(format string, body string, args ...interface{}) string { url := "http://127.0.0.1:8378" + fmt.Sprintf(format, args...) - resp, err := http.Post(url, "application/json", strings.NewReader(body)) + + var err error + var resp *http.Response + // Invoking POST can fail due to "500 Internal Server Error". It's because the server is still booting up and not ready to serve requests. + // To prevent such issue, we apply retry strategy. + // 3 attempts with 5 second fixed wait time are tested sufficient for it. + var retryBackOff = wait.Backoff{ + Factor: 1, + Jitter: 0, + Steps: 3, + Duration: time.Second * 5, + } + _ = wait.ExponentialBackoff(retryBackOff, func() (done bool, err error) { + resp, err = http.Post(url, "application/json", strings.NewReader(body)) + if err == nil && resp.StatusCode < 300 { + return true, nil + } + fmt.Printf("Got error %v, response %v, retrying.\n", err, *resp) + return false, nil + }) + if err != nil { panic(err) }