diff --git a/modules/docker/stop_test.go b/modules/docker/stop_test.go index f789fd1a8..a977d8be4 100644 --- a/modules/docker/stop_test.go +++ b/modules/docker/stop_test.go @@ -36,7 +36,7 @@ func TestStop(t *testing.T) { Run(t, "nginx:1.17-alpine", runOpts) // verify nginx is running - http_helper.HttpGetWithRetryWithCustomValidation(t, testURL, &tls.Config{}, 60, 2*time.Second, verifyNginxIsUp) + http_helper.HttpGetWithRetryWithCustomValidation(t, &http_helper.HttpGetOptions{testURL, &tls.Config{}, 10}, 60, 2*time.Second, verifyNginxIsUp) // try to stop it now out := Stop(t, []string{name}, &StopOptions{}) diff --git a/modules/helm/install_test.go b/modules/helm/install_test.go index a67a5c787..2f84d4724 100644 --- a/modules/helm/install_test.go +++ b/modules/helm/install_test.go @@ -83,8 +83,11 @@ func TestRemoteChartInstall(t *testing.T) { http_helper.HttpGetWithRetryWithCustomValidation( t, - fmt.Sprintf("http://%s", endpoint), - &tlsConfig, + &http_helper.HttpGetOptions{ + fmt.Sprintf("http://%s", endpoint), + &tlsConfig, + 10, + }, 30, 10*time.Second, func(statusCode int, body string) bool { diff --git a/modules/helm/upgrade_test.go b/modules/helm/upgrade_test.go index 1f55a73ec..6cba5c5ed 100644 --- a/modules/helm/upgrade_test.go +++ b/modules/helm/upgrade_test.go @@ -82,8 +82,11 @@ func TestRemoteChartInstallUpgradeRollback(t *testing.T) { http_helper.HttpGetWithRetryWithCustomValidation( t, - fmt.Sprintf("http://%s", endpoint), - &tlsConfig, + &http_helper.HttpGetOptions{ + fmt.Sprintf("http://%s", endpoint), + &tlsConfig, + 10, + }, 30, 10*time.Second, func(statusCode int, body string) bool { diff --git a/modules/http-helper/continuous.go b/modules/http-helper/continuous.go index 1af3cd76b..ab71cbd7a 100644 --- a/modules/http-helper/continuous.go +++ b/modules/http-helper/continuous.go @@ -36,7 +36,7 @@ func ContinuouslyCheckUrl( logger.Logf(t, "Got signal to stop downtime checks for URL %s.\n", url) return case <-time.After(sleepBetweenChecks): - statusCode, body, err := HttpGetE(t, url, &tls.Config{}) + statusCode, body, err := HttpGetE(t, &HttpGetOptions{url, &tls.Config{}, 10}) // Non-blocking send, defaulting to logging a warning if there is no channel reader select { case responses <- GetResponse{StatusCode: statusCode, Body: body}: diff --git a/modules/http-helper/dummy_server_test.go b/modules/http-helper/dummy_server_test.go index 817480df8..726de8a5e 100644 --- a/modules/http-helper/dummy_server_test.go +++ b/modules/http-helper/dummy_server_test.go @@ -21,7 +21,7 @@ func TestRunDummyServer(t *testing.T) { defer shutDownServer(t, listener) url := fmt.Sprintf("http://localhost:%d", port) - HttpGetWithValidation(t, url, &tls.Config{}, 200, text) + HttpGetWithValidation(t, &HttpGetOptions{url, &tls.Config{}, 10}, 200, text) } func TestContinuouslyCheck(t *testing.T) { diff --git a/modules/http-helper/http_helper.go b/modules/http-helper/http_helper.go index 444087a3f..0c4f11375 100644 --- a/modules/http-helper/http_helper.go +++ b/modules/http-helper/http_helper.go @@ -16,10 +16,25 @@ import ( "github.com/gruntwork-io/terratest/modules/testing" ) +type HttpGetOptions struct { + Url string + TlsConfig *tls.Config + Timeout int +} + +type HttpDoOptions struct { + Method string + Url string + Body []byte + Headers map[string]string + TlsConfig *tls.Config + Timeout int +} + // HttpGet performs an HTTP GET, with an optional pointer to a custom TLS configuration, on the given URL and // return the HTTP status code and body. If there's any error, fail the test. -func HttpGet(t testing.TestingT, url string, tlsConfig *tls.Config) (int, string) { - statusCode, body, err := HttpGetE(t, url, tlsConfig) +func HttpGet(t testing.TestingT, options *HttpGetOptions) (int, string) { + statusCode, body, err := HttpGetE(t, options) if err != nil { t.Fatal(err) } @@ -28,21 +43,26 @@ func HttpGet(t testing.TestingT, url string, tlsConfig *tls.Config) (int, string // HttpGetE performs an HTTP GET, with an optional pointer to a custom TLS configuration, on the given URL and // return the HTTP status code, body, and any error. -func HttpGetE(t testing.TestingT, url string, tlsConfig *tls.Config) (int, string, error) { - logger.Logf(t, "Making an HTTP GET call to URL %s", url) +func HttpGetE(t testing.TestingT, options *HttpGetOptions) (int, string, error) { + logger.Logf(t, "Making an HTTP GET call to URL %s", options.Url) // Set HTTP client transport config tr := http.DefaultTransport.(*http.Transport).Clone() - tr.TLSClientConfig = tlsConfig + tr.TLSClientConfig = options.TlsConfig + + timeout := 10 + if options.Timeout > 0 { + timeout = options.Timeout + } client := http.Client{ // By default, Go does not impose a timeout, so an HTTP connection attempt can hang for a LONG time. - Timeout: 10 * time.Second, + Timeout: time.Duration(timeout) * time.Second, // Include the previously created transport config Transport: tr, } - resp, err := client.Get(url) + resp, err := client.Get(options.Url) if err != nil { return -1, "", err } @@ -59,8 +79,8 @@ func HttpGetE(t testing.TestingT, url string, tlsConfig *tls.Config) (int, strin // HttpGetWithValidation performs an HTTP GET on the given URL and verify that you get back the expected status code and body. If either // doesn't match, fail the test. -func HttpGetWithValidation(t testing.TestingT, url string, tlsConfig *tls.Config, expectedStatusCode int, expectedBody string) { - err := HttpGetWithValidationE(t, url, tlsConfig, expectedStatusCode, expectedBody) +func HttpGetWithValidation(t testing.TestingT, options *HttpGetOptions, expectedStatusCode int, expectedBody string) { + err := HttpGetWithValidationE(t, options, expectedStatusCode, expectedBody) if err != nil { t.Fatal(err) } @@ -68,30 +88,30 @@ func HttpGetWithValidation(t testing.TestingT, url string, tlsConfig *tls.Config // HttpGetWithValidationE performs an HTTP GET on the given URL and verify that you get back the expected status code and body. If either // doesn't match, return an error. -func HttpGetWithValidationE(t testing.TestingT, url string, tlsConfig *tls.Config, expectedStatusCode int, expectedBody string) error { - return HttpGetWithCustomValidationE(t, url, tlsConfig, func(statusCode int, body string) bool { +func HttpGetWithValidationE(t testing.TestingT, options *HttpGetOptions, expectedStatusCode int, expectedBody string) error { + return HttpGetWithCustomValidationE(t, options, func(statusCode int, body string) bool { return statusCode == expectedStatusCode && body == expectedBody }) } // HttpGetWithCustomValidation performs an HTTP GET on the given URL and validate the returned status code and body using the given function. -func HttpGetWithCustomValidation(t testing.TestingT, url string, tlsConfig *tls.Config, validateResponse func(int, string) bool) { - err := HttpGetWithCustomValidationE(t, url, tlsConfig, validateResponse) +func HttpGetWithCustomValidation(t testing.TestingT, options *HttpGetOptions, validateResponse func(int, string) bool) { + err := HttpGetWithCustomValidationE(t, options, validateResponse) if err != nil { t.Fatal(err) } } // HttpGetWithCustomValidationE performs an HTTP GET on the given URL and validate the returned status code and body using the given function. -func HttpGetWithCustomValidationE(t testing.TestingT, url string, tlsConfig *tls.Config, validateResponse func(int, string) bool) error { - statusCode, body, err := HttpGetE(t, url, tlsConfig) +func HttpGetWithCustomValidationE(t testing.TestingT, options *HttpGetOptions, validateResponse func(int, string) bool) error { + statusCode, body, err := HttpGetE(t, options) if err != nil { return err } if !validateResponse(statusCode, body) { - return ValidationFunctionFailed{Url: url, Status: statusCode, Body: body} + return ValidationFunctionFailed{Url: options.Url, Status: statusCode, Body: body} } return nil @@ -99,8 +119,8 @@ func HttpGetWithCustomValidationE(t testing.TestingT, url string, tlsConfig *tls // HttpGetWithRetry repeatedly performs an HTTP GET on the given URL until the given status code and body are returned or until max // retries has been exceeded. -func HttpGetWithRetry(t testing.TestingT, url string, tlsConfig *tls.Config, expectedStatus int, expectedBody string, retries int, sleepBetweenRetries time.Duration) { - err := HttpGetWithRetryE(t, url, tlsConfig, expectedStatus, expectedBody, retries, sleepBetweenRetries) +func HttpGetWithRetry(t testing.TestingT, options *HttpGetOptions, expectedStatus int, expectedBody string, retries int, sleepBetweenRetries time.Duration) { + err := HttpGetWithRetryE(t, options, expectedStatus, expectedBody, retries, sleepBetweenRetries) if err != nil { t.Fatal(err) } @@ -108,9 +128,9 @@ func HttpGetWithRetry(t testing.TestingT, url string, tlsConfig *tls.Config, exp // HttpGetWithRetryE repeatedly performs an HTTP GET on the given URL until the given status code and body are returned or until max // retries has been exceeded. -func HttpGetWithRetryE(t testing.TestingT, url string, tlsConfig *tls.Config, expectedStatus int, expectedBody string, retries int, sleepBetweenRetries time.Duration) error { - _, err := retry.DoWithRetryE(t, fmt.Sprintf("HTTP GET to URL %s", url), retries, sleepBetweenRetries, func() (string, error) { - return "", HttpGetWithValidationE(t, url, tlsConfig, expectedStatus, expectedBody) +func HttpGetWithRetryE(t testing.TestingT, options *HttpGetOptions, expectedStatus int, expectedBody string, retries int, sleepBetweenRetries time.Duration) error { + _, err := retry.DoWithRetryE(t, fmt.Sprintf("HTTP GET to URL %s", options.Url), retries, sleepBetweenRetries, func() (string, error) { + return "", HttpGetWithValidationE(t, options, expectedStatus, expectedBody) }) return err @@ -118,8 +138,8 @@ func HttpGetWithRetryE(t testing.TestingT, url string, tlsConfig *tls.Config, ex // HttpGetWithRetryWithCustomValidation repeatedly performs an HTTP GET on the given URL until the given validation function returns true or max retries // has been exceeded. -func HttpGetWithRetryWithCustomValidation(t testing.TestingT, url string, tlsConfig *tls.Config, retries int, sleepBetweenRetries time.Duration, validateResponse func(int, string) bool) { - err := HttpGetWithRetryWithCustomValidationE(t, url, tlsConfig, retries, sleepBetweenRetries, validateResponse) +func HttpGetWithRetryWithCustomValidation(t testing.TestingT, options *HttpGetOptions, retries int, sleepBetweenRetries time.Duration, validateResponse func(int, string) bool) { + err := HttpGetWithRetryWithCustomValidationE(t, options, retries, sleepBetweenRetries, validateResponse) if err != nil { t.Fatal(err) } @@ -127,9 +147,9 @@ func HttpGetWithRetryWithCustomValidation(t testing.TestingT, url string, tlsCon // HttpGetWithRetryWithCustomValidationE repeatedly performs an HTTP GET on the given URL until the given validation function returns true or max retries // has been exceeded. -func HttpGetWithRetryWithCustomValidationE(t testing.TestingT, url string, tlsConfig *tls.Config, retries int, sleepBetweenRetries time.Duration, validateResponse func(int, string) bool) error { - _, err := retry.DoWithRetryE(t, fmt.Sprintf("HTTP GET to URL %s", url), retries, sleepBetweenRetries, func() (string, error) { - return "", HttpGetWithCustomValidationE(t, url, tlsConfig, validateResponse) +func HttpGetWithRetryWithCustomValidationE(t testing.TestingT, options *HttpGetOptions, retries int, sleepBetweenRetries time.Duration, validateResponse func(int, string) bool) error { + _, err := retry.DoWithRetryE(t, fmt.Sprintf("HTTP GET to URL %s", options.Url), retries, sleepBetweenRetries, func() (string, error) { + return "", HttpGetWithCustomValidationE(t, options, validateResponse) }) return err @@ -138,10 +158,9 @@ func HttpGetWithRetryWithCustomValidationE(t testing.TestingT, url string, tlsCo // HTTPDo performs the given HTTP method on the given URL and return the HTTP status code and body. // If there's any error, fail the test. func HTTPDo( - t testing.TestingT, method string, url string, body io.Reader, - headers map[string]string, tlsConfig *tls.Config, + t testing.TestingT, options *HttpDoOptions, ) (int, string) { - statusCode, respBody, err := HTTPDoE(t, method, url, body, headers, tlsConfig) + statusCode, respBody, err := HTTPDoE(t, options) if err != nil { t.Fatal(err) } @@ -150,22 +169,26 @@ func HTTPDo( // HTTPDoE performs the given HTTP method on the given URL and return the HTTP status code, body, and any error. func HTTPDoE( - t testing.TestingT, method string, url string, body io.Reader, - headers map[string]string, tlsConfig *tls.Config, + t testing.TestingT, options *HttpDoOptions, ) (int, string, error) { - logger.Logf(t, "Making an HTTP %s call to URL %s", method, url) + logger.Logf(t, "Making an HTTP %s call to URL %s", options.Method, options.Url) tr := &http.Transport{ - TLSClientConfig: tlsConfig, + TLSClientConfig: options.TlsConfig, + } + + timeout := 10 + if options.Timeout > 0 { + timeout = options.Timeout } client := http.Client{ // By default, Go does not impose a timeout, so an HTTP connection attempt can hang for a LONG time. - Timeout: 10 * time.Second, + Timeout: time.Duration(timeout) * time.Second, Transport: tr, } - req := newRequest(method, url, body, headers) + req := newRequest(options.Method, options.Url, bytes.NewReader(options.Body), options.Headers) resp, err := client.Do(req) if err != nil { return -1, "", err @@ -185,12 +208,10 @@ func HTTPDoE( // returned or until max retries has been exceeded. // The function compares the expected status code against the received one and fails if they don't match. func HTTPDoWithRetry( - t testing.TestingT, method string, url string, - body []byte, headers map[string]string, expectedStatus int, - retries int, sleepBetweenRetries time.Duration, tlsConfig *tls.Config, + t testing.TestingT, options *HttpDoOptions, expectedStatus int, + retries int, sleepBetweenRetries time.Duration, ) string { - out, err := HTTPDoWithRetryE(t, method, url, body, - headers, expectedStatus, retries, sleepBetweenRetries, tlsConfig) + out, err := HTTPDoWithRetryE(t, options, expectedStatus, retries, sleepBetweenRetries) if err != nil { t.Fatal(err) } @@ -201,21 +222,19 @@ func HTTPDoWithRetry( // returned or until max retries has been exceeded. // The function compares the expected status code against the received one and fails if they don't match. func HTTPDoWithRetryE( - t testing.TestingT, method string, url string, - body []byte, headers map[string]string, expectedStatus int, - retries int, sleepBetweenRetries time.Duration, tlsConfig *tls.Config, + t testing.TestingT, options *HttpDoOptions, expectedStatus int, + retries int, sleepBetweenRetries time.Duration, ) (string, error) { out, err := retry.DoWithRetryE( - t, fmt.Sprintf("HTTP %s to URL %s", method, url), retries, + t, fmt.Sprintf("HTTP %s to URL %s", options.Method, options.Url), retries, sleepBetweenRetries, func() (string, error) { - bodyReader := bytes.NewReader(body) - statusCode, out, err := HTTPDoE(t, method, url, bodyReader, headers, tlsConfig) + statusCode, out, err := HTTPDoE(t, options) if err != nil { return "", err } logger.Logf(t, "output: %v", out) if statusCode != expectedStatus { - return "", ValidationFunctionFailed{Url: url, Status: statusCode} + return "", ValidationFunctionFailed{Url: options.Url, Status: statusCode} } return out, nil }) @@ -226,11 +245,10 @@ func HTTPDoWithRetryE( // HTTPDoWithValidationRetry repeatedly performs the given HTTP method on the given URL until the given status code and // body are returned or until max retries has been exceeded. func HTTPDoWithValidationRetry( - t testing.TestingT, method string, url string, - body []byte, headers map[string]string, expectedStatus int, - expectedBody string, retries int, sleepBetweenRetries time.Duration, tlsConfig *tls.Config, + t testing.TestingT, options *HttpDoOptions, expectedStatus int, + expectedBody string, retries int, sleepBetweenRetries time.Duration, ) { - err := HTTPDoWithValidationRetryE(t, method, url, body, headers, expectedStatus, expectedBody, retries, sleepBetweenRetries, tlsConfig) + err := HTTPDoWithValidationRetryE(t, options, expectedStatus, expectedBody, retries, sleepBetweenRetries) if err != nil { t.Fatal(err) } @@ -239,14 +257,12 @@ func HTTPDoWithValidationRetry( // HTTPDoWithValidationRetryE repeatedly performs the given HTTP method on the given URL until the given status code and // body are returned or until max retries has been exceeded. func HTTPDoWithValidationRetryE( - t testing.TestingT, method string, url string, - body []byte, headers map[string]string, expectedStatus int, - expectedBody string, retries int, sleepBetweenRetries time.Duration, tlsConfig *tls.Config, + t testing.TestingT, options *HttpDoOptions, expectedStatus int, + expectedBody string, retries int, sleepBetweenRetries time.Duration, ) error { - _, err := retry.DoWithRetryE(t, fmt.Sprintf("HTTP %s to URL %s", method, url), retries, + _, err := retry.DoWithRetryE(t, fmt.Sprintf("HTTP %s to URL %s", options.Method, options.Url), retries, sleepBetweenRetries, func() (string, error) { - bodyReader := bytes.NewReader(body) - return "", HTTPDoWithValidationE(t, method, url, bodyReader, headers, expectedStatus, expectedBody, tlsConfig) + return "", HTTPDoWithValidationE(t, options, expectedStatus, expectedBody) }) return err @@ -254,8 +270,8 @@ func HTTPDoWithValidationRetryE( // HTTPDoWithValidation performs the given HTTP method on the given URL and verify that you get back the expected status // code and body. If either doesn't match, fail the test. -func HTTPDoWithValidation(t testing.TestingT, method string, url string, body io.Reader, headers map[string]string, expectedStatusCode int, expectedBody string, tlsConfig *tls.Config) { - err := HTTPDoWithValidationE(t, method, url, body, headers, expectedStatusCode, expectedBody, tlsConfig) +func HTTPDoWithValidation(t testing.TestingT, options *HttpDoOptions, expectedStatusCode int, expectedBody string) { + err := HTTPDoWithValidationE(t, options, expectedStatusCode, expectedBody) if err != nil { t.Fatal(err) } @@ -263,16 +279,16 @@ func HTTPDoWithValidation(t testing.TestingT, method string, url string, body io // HTTPDoWithValidationE performs the given HTTP method on the given URL and verify that you get back the expected status // code and body. If either doesn't match, return an error. -func HTTPDoWithValidationE(t testing.TestingT, method string, url string, body io.Reader, headers map[string]string, expectedStatusCode int, expectedBody string, tlsConfig *tls.Config) error { - return HTTPDoWithCustomValidationE(t, method, url, body, headers, func(statusCode int, body string) bool { +func HTTPDoWithValidationE(t testing.TestingT, options *HttpDoOptions, expectedStatusCode int, expectedBody string) error { + return HTTPDoWithCustomValidationE(t, options, func(statusCode int, body string) bool { return statusCode == expectedStatusCode && body == expectedBody - }, tlsConfig) + }) } // HTTPDoWithCustomValidation performs the given HTTP method on the given URL and validate the returned status code and // body using the given function. -func HTTPDoWithCustomValidation(t testing.TestingT, method string, url string, body io.Reader, headers map[string]string, validateResponse func(int, string) bool, tlsConfig *tls.Config) { - err := HTTPDoWithCustomValidationE(t, method, url, body, headers, validateResponse, tlsConfig) +func HTTPDoWithCustomValidation(t testing.TestingT, options *HttpDoOptions, validateResponse func(int, string) bool) { + err := HTTPDoWithCustomValidationE(t, options, validateResponse) if err != nil { t.Fatal(err) } @@ -280,15 +296,15 @@ func HTTPDoWithCustomValidation(t testing.TestingT, method string, url string, b // HTTPDoWithCustomValidationE performs the given HTTP method on the given URL and validate the returned status code and // body using the given function. -func HTTPDoWithCustomValidationE(t testing.TestingT, method string, url string, body io.Reader, headers map[string]string, validateResponse func(int, string) bool, tlsConfig *tls.Config) error { - statusCode, respBody, err := HTTPDoE(t, method, url, body, headers, tlsConfig) +func HTTPDoWithCustomValidationE(t testing.TestingT, options *HttpDoOptions, validateResponse func(int, string) bool) error { + statusCode, respBody, err := HTTPDoE(t, options) if err != nil { return err } if !validateResponse(statusCode, respBody) { - return ValidationFunctionFailed{Url: url, Status: statusCode, Body: respBody} + return ValidationFunctionFailed{Url: options.Url, Status: statusCode, Body: respBody} } return nil diff --git a/modules/http-helper/http_helper_test.go b/modules/http-helper/http_helper_test.go index 9df2c212c..11aa2ccb6 100644 --- a/modules/http-helper/http_helper_test.go +++ b/modules/http-helper/http_helper_test.go @@ -25,8 +25,7 @@ func TestOkBody(t *testing.T) { defer ts.Close() url := ts.URL expectedBody := "Hello, Terratest!" - body := bytes.NewReader([]byte(expectedBody)) - statusCode, respBody := HTTPDo(t, "POST", url, body, nil, nil) + statusCode, respBody := HTTPDo(t, &HttpDoOptions{"POST", url, []byte(expectedBody), nil, nil, 10}) expectedCode := 200 if statusCode != expectedCode { @@ -43,8 +42,7 @@ func TestHTTPDoWithValidation(t *testing.T) { defer ts.Close() url := ts.URL expectedBody := "Hello, Terratest!" - body := bytes.NewReader([]byte(expectedBody)) - HTTPDoWithValidation(t, "POST", url, body, nil, 200, expectedBody, nil) + HTTPDoWithValidation(t, &HttpDoOptions{"POST", url, []byte(expectedBody), nil, nil, 10}, 200, expectedBody) } func TestHTTPDoWithCustomValidation(t *testing.T) { @@ -53,13 +51,12 @@ func TestHTTPDoWithCustomValidation(t *testing.T) { defer ts.Close() url := ts.URL expectedBody := "Hello, Terratest!" - body := bytes.NewReader([]byte(expectedBody)) customValidation := func(statusCode int, response string) bool { return statusCode == 200 && response == expectedBody } - HTTPDoWithCustomValidation(t, "POST", url, body, nil, customValidation, nil) + HTTPDoWithCustomValidation(t, &HttpDoOptions{"POST", url, []byte(expectedBody), nil, nil, 10}, customValidation) } func TestOkHeaders(t *testing.T) { @@ -68,7 +65,7 @@ func TestOkHeaders(t *testing.T) { defer ts.Close() url := ts.URL headers := map[string]string{"Authorization": "Bearer 1a2b3c99ff"} - statusCode, respBody := HTTPDo(t, "POST", url, nil, headers, nil) + statusCode, respBody := HTTPDo(t, &HttpDoOptions{"POST", url, nil, headers, nil, 10}) expectedCode := 200 if statusCode != expectedCode { @@ -85,7 +82,7 @@ func TestWrongStatus(t *testing.T) { ts := getTestServerForFunction(wrongStatusHandler) defer ts.Close() url := ts.URL - statusCode, _ := HTTPDo(t, "POST", url, nil, nil, nil) + statusCode, _ := HTTPDo(t, &HttpDoOptions{"POST", url, nil, nil, nil, 10}) expectedCode := 500 if statusCode != expectedCode { @@ -98,7 +95,7 @@ func TestRequestTimeout(t *testing.T) { ts := getTestServerForFunction(sleepingHandler) defer ts.Close() url := ts.URL - _, _, err := HTTPDoE(t, "DELETE", url, nil, nil, nil) + _, _, err := HTTPDoE(t, &HttpDoOptions{"DELETE", url, nil, nil, nil, 10}) if err == nil { t.Error("handler didn't return a timeout error") @@ -113,10 +110,9 @@ func TestOkWithRetry(t *testing.T) { ts := getTestServerForFunction(retryHandler) defer ts.Close() body := "TEST_CONTENT" - bodyBytes := []byte(body) url := ts.URL counter = 3 - response := HTTPDoWithRetry(t, "POST", url, bodyBytes, nil, 200, 10, time.Second, nil) + response := HTTPDoWithRetry(t, &HttpDoOptions{"POST", url, []byte(body), nil, nil, 10}, 200, 10, time.Second) require.Equal(t, body, response) } @@ -126,7 +122,7 @@ func TestErrorWithRetry(t *testing.T) { defer ts.Close() failCounter = 3 url := ts.URL - _, err := HTTPDoWithRetryE(t, "POST", url, nil, nil, 200, 2, time.Second, nil) + _, err := HTTPDoWithRetryE(t, &HttpDoOptions{"POST", url, nil, nil, nil, 10}, 200, 2, time.Second) if err == nil { t.Error("handler didn't return a retry error") diff --git a/modules/k8s/service_test.go b/modules/k8s/service_test.go index b1ca63ebf..b433142c9 100644 --- a/modules/k8s/service_test.go +++ b/modules/k8s/service_test.go @@ -91,8 +91,11 @@ func TestGetServiceEndpointEReturnsAccessibleEndpointForNodePort(t *testing.T) { // Test up to 5 minutes http_helper.HttpGetWithRetryWithCustomValidation( t, - fmt.Sprintf("http://%s", endpoint), - &tlsConfig, + &http_helper.HttpGetOptions{ + fmt.Sprintf("http://%s", endpoint), + &tlsConfig, + 10, + }, 30, 10*time.Second, func(statusCode int, body string) bool { diff --git a/modules/k8s/tunnel_test.go b/modules/k8s/tunnel_test.go index ef872ba68..ee74654d4 100644 --- a/modules/k8s/tunnel_test.go +++ b/modules/k8s/tunnel_test.go @@ -40,8 +40,11 @@ func TestTunnelOpensAPortForwardTunnelToPod(t *testing.T) { // Try to access the nginx service on the local port, retrying until we get a good response for up to 5 minutes http_helper.HttpGetWithRetryWithCustomValidation( t, - fmt.Sprintf("http://%s", tunnel.Endpoint()), - &tlsConfig, + &http_helper.HttpGetOptions{ + fmt.Sprintf("http://%s", tunnel.Endpoint()), + &tlsConfig, + 10, + }, 60, 5*time.Second, verifyNginxWelcomePage, @@ -70,8 +73,11 @@ func TestTunnelOpensAPortForwardTunnelToService(t *testing.T) { // Try to access the nginx service on the local port, retrying until we get a good response for up to 5 minutes http_helper.HttpGetWithRetryWithCustomValidation( t, - fmt.Sprintf("http://%s", tunnel.Endpoint()), - &tlsConfig, + &http_helper.HttpGetOptions{ + fmt.Sprintf("http://%s", tunnel.Endpoint()), + &tlsConfig, + 10, + }, 60, 5*time.Second, verifyNginxWelcomePage, diff --git a/modules/terraform/plan_struct_test.go b/modules/terraform/plan_struct_test.go index b1e5c3ed8..ddff63653 100644 --- a/modules/terraform/plan_struct_test.go +++ b/modules/terraform/plan_struct_test.go @@ -22,7 +22,7 @@ func TestPlannedValuesMapWithBasicJson(t *testing.T) { t.Parallel() // Retrieve test data from the terraform-json project. - _, jsonData := http_helper.HttpGet(t, basicJsonUrl, nil) + _, jsonData := http_helper.HttpGet(t, &http_helper.HttpGetOptions{basicJsonUrl, nil, 10}) plan, err := parsePlanJson(jsonData) require.NoError(t, err) @@ -47,7 +47,7 @@ func TestPlannedValuesMapWithDeepModuleJson(t *testing.T) { t.Parallel() // Retrieve test data from the terraform-json project. - _, jsonData := http_helper.HttpGet(t, deepModuleJsonUrl, nil) + _, jsonData := http_helper.HttpGet(t, &http_helper.HttpGetOptions{deepModuleJsonUrl, nil, 10}) plan, err := parsePlanJson(jsonData) require.NoError(t, err) @@ -63,7 +63,7 @@ func TestResourceChangesJson(t *testing.T) { t.Parallel() // Retrieve test data from the terraform-json project. - _, jsonData := http_helper.HttpGet(t, changesJsonUrl, nil) + _, jsonData := http_helper.HttpGet(t, &http_helper.HttpGetOptions{changesJsonUrl, nil, 10}) plan, err := parsePlanJson(jsonData) require.NoError(t, err) diff --git a/test/azure/terraform_azure_aks_example_test.go b/test/azure/terraform_azure_aks_example_test.go index 6c526f02f..6bd910d3b 100644 --- a/test/azure/terraform_azure_aks_example_test.go +++ b/test/azure/terraform_azure_aks_example_test.go @@ -89,8 +89,11 @@ func TestTerraformAzureAKSExample(t *testing.T) { // response. http_helper.HttpGetWithRetryWithCustomValidation( t, - fmt.Sprintf("http://%s", endpoint), - &tlsConfig, + &http_helper.HttpGetOptions{ + fmt.Sprintf("http://%s", endpoint), + &tlsConfig, + 10, + }, 30, 10*time.Second, func(statusCode int, body string) bool { diff --git a/test/helm_basic_example_integration_test.go b/test/helm_basic_example_integration_test.go index 59f3e9bbf..cf5e3d589 100644 --- a/test/helm_basic_example_integration_test.go +++ b/test/helm_basic_example_integration_test.go @@ -91,8 +91,11 @@ func TestHelmBasicExampleDeployment(t *testing.T) { // response. http_helper.HttpGetWithRetryWithCustomValidation( t, - fmt.Sprintf("http://%s", endpoint), - &tlsConfig, + &http_helper.HttpGetOptions{ + fmt.Sprintf("http://%s", endpoint), + &tlsConfig, + 10, + }, 30, 10*time.Second, func(statusCode int, body string) bool { diff --git a/test/kubernetes_basic_example_service_check_test.go b/test/kubernetes_basic_example_service_check_test.go index 6c4519a12..d62076165 100644 --- a/test/kubernetes_basic_example_service_check_test.go +++ b/test/kubernetes_basic_example_service_check_test.go @@ -65,8 +65,11 @@ func TestKubernetesBasicExampleServiceCheck(t *testing.T) { // response. http_helper.HttpGetWithRetryWithCustomValidation( t, - fmt.Sprintf("http://%s", endpoint), - &tlsConfig, + &http_helper.HttpGetOptions{ + fmt.Sprintf("http://%s", endpoint), + &tlsConfig, + 10, + }, 30, 10*time.Second, func(statusCode int, body string) bool { diff --git a/test/kubernetes_hello_world_example_test.go b/test/kubernetes_hello_world_example_test.go index 32b023fd2..25ae51d43 100644 --- a/test/kubernetes_hello_world_example_test.go +++ b/test/kubernetes_hello_world_example_test.go @@ -33,5 +33,5 @@ func TestKubernetesHelloWorldExample(t *testing.T) { url := fmt.Sprintf("http://%s", k8s.GetServiceEndpoint(t, options, service, 5000)) // website::tag::5:: Make an HTTP request to the URL and make sure it returns a 200 OK with the body "Hello, World". - http_helper.HttpGetWithRetry(t, url, nil, 200, "Hello world!", 30, 3*time.Second) + http_helper.HttpGetWithRetry(t, &http_helper.HttpGetOptions{url, nil, 10}, 200, "Hello world!", 30, 3*time.Second) } diff --git a/test/packer_docker_example_test.go b/test/packer_docker_example_test.go index f038d753a..0d2f567e5 100644 --- a/test/packer_docker_example_test.go +++ b/test/packer_docker_example_test.go @@ -65,5 +65,5 @@ func TestPackerDockerExampleLocal(t *testing.T) { tlsConfig := tls.Config{} // website::tag::5::Verify that we get back a 200 OK with the expected text - http_helper.HttpGetWithRetry(t, url, &tlsConfig, 200, expectedServerText, maxRetries, timeBetweenRetries) + http_helper.HttpGetWithRetry(t, &http_helper.HttpGetOptions{url, &tlsConfig, 10}, 200, expectedServerText, maxRetries, timeBetweenRetries) } diff --git a/test/terraform_aws_hello_world_example_test.go b/test/terraform_aws_hello_world_example_test.go index dcf79b1d1..4fe83b33e 100644 --- a/test/terraform_aws_hello_world_example_test.go +++ b/test/terraform_aws_hello_world_example_test.go @@ -31,5 +31,5 @@ func TestTerraformAwsHelloWorldExample(t *testing.T) { // website::tag::5:: Make an HTTP request to the instance and make sure we get back a 200 OK with the body "Hello, World!" url := fmt.Sprintf("http://%s:8080", publicIp) - http_helper.HttpGetWithRetry(t, url, nil, 200, "Hello, World!", 30, 5*time.Second) + http_helper.HttpGetWithRetry(t, &http_helper.HttpGetOptions{url, nil, 10}, 200, "Hello, World!", 30, 5*time.Second) } diff --git a/test/terraform_http_example_test.go b/test/terraform_http_example_test.go index 2e7fd6fe2..53279a4e1 100644 --- a/test/terraform_http_example_test.go +++ b/test/terraform_http_example_test.go @@ -65,5 +65,5 @@ func TestTerraformHttpExample(t *testing.T) { timeBetweenRetries := 5 * time.Second // Verify that we get back a 200 OK with the expected instanceText - http_helper.HttpGetWithRetry(t, instanceURL, &tlsConfig, 200, instanceText, maxRetries, timeBetweenRetries) + http_helper.HttpGetWithRetry(t, &http_helper.HttpGetOptions{instanceURL, &tlsConfig, 10}, 200, instanceText, maxRetries, timeBetweenRetries) } diff --git a/test/terraform_packer_example_test.go b/test/terraform_packer_example_test.go index 74bea07a7..2bbaab5d0 100644 --- a/test/terraform_packer_example_test.go +++ b/test/terraform_packer_example_test.go @@ -187,5 +187,5 @@ func validateInstanceRunningWebServer(t *testing.T, workingDir string) { timeBetweenRetries := 5 * time.Second // Verify that we get back a 200 OK with the expected instanceText - http_helper.HttpGetWithRetry(t, instanceURL, &tlsConfig, 200, instanceText, maxRetries, timeBetweenRetries) + http_helper.HttpGetWithRetry(t, &http_helper.HttpGetOptions{instanceURL, &tlsConfig, 10}, 200, instanceText, maxRetries, timeBetweenRetries) } diff --git a/test/terraform_redeploy_example_test.go b/test/terraform_redeploy_example_test.go index 01e6b4933..b41f0bf65 100644 --- a/test/terraform_redeploy_example_test.go +++ b/test/terraform_redeploy_example_test.go @@ -140,7 +140,7 @@ func validateAsgRunningWebServer(t *testing.T, awsRegion string, workingDir stri // Verify that we get back a 200 OK with the expectedText // It can take a few minutes for the ALB to boot up, so retry a few times - http_helper.HttpGetWithRetry(t, url, &tlsConfig, 200, expectedText, maxRetries, timeBetweenRetries) + http_helper.HttpGetWithRetry(t, &http_helper.HttpGetOptions{url, &tlsConfig, 10}, 200, expectedText, maxRetries, timeBetweenRetries) } // Validate we can deploy an update to the ASG with zero downtime for users accessing the ALB @@ -166,7 +166,7 @@ func validateAsgRedeploy(t *testing.T, workingDir string) { // Check once per second that the ELB returns a proper response to make sure there is no downtime during deployment elbChecks := retry.DoInBackgroundUntilStopped(t, fmt.Sprintf("Check URL %s", url), 1*time.Second, func() { - http_helper.HttpGetWithCustomValidation(t, url, &tlsConfig, func(statusCode int, body string) bool { + http_helper.HttpGetWithCustomValidation(t, &http_helper.HttpGetOptions{url, &tlsConfig, 10}, func(statusCode int, body string) bool { return statusCode == 200 && (body == originalText || body == newText) }) })