Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backwards Incompatible] Use options classes for HttpHelper methods #1055

Closed
wants to merge 10 commits into from
2 changes: 1 addition & 1 deletion modules/docker/stop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand Down
7 changes: 5 additions & 2 deletions modules/helm/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 5 additions & 2 deletions modules/helm/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion modules/http-helper/continuous.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}:
Expand Down
2 changes: 1 addition & 1 deletion modules/http-helper/dummy_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
150 changes: 83 additions & 67 deletions modules/http-helper/http_helper.go

Large diffs are not rendered by default.

20 changes: 8 additions & 12 deletions modules/http-helper/http_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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")
Expand All @@ -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)
}

Expand All @@ -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")
Expand Down
7 changes: 5 additions & 2 deletions modules/k8s/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 10 additions & 4 deletions modules/k8s/tunnel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions modules/terraform/plan_struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand Down
7 changes: 5 additions & 2 deletions test/azure/terraform_azure_aks_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 5 additions & 2 deletions test/helm_basic_example_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 5 additions & 2 deletions test/kubernetes_basic_example_service_check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion test/kubernetes_hello_world_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion test/packer_docker_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion test/terraform_aws_hello_world_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion test/terraform_http_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion test/terraform_packer_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions test/terraform_redeploy_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
})
})
Expand Down