1

I am testing creation of multiple AWS instances with proper subnet and rules by firing multiple IP addresses for testing AWS infrastructure using below terratest code but it is only taking first IP and hitting it 4 times. Once it gets time out message, it exits and doesn't go to next IP. I am expecting it should hit both the IP addresses coming from main.tf terraform file and hit the server. New to this so trying to understand what am I missing? If anyone could help me please.


list - ["52.205.87.202","54.89.179.76"] which is coming from main.tf

** Code :**

`func TestTerraformHelloWorldExample(t *testing.T) {

    // Construct the terraform options with default retryable errors to handle the most common
    // retryable errors in terraform testing.
    terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
        // Set the path to the Terraform code that will be tested.
        TerraformDir: "/Users/prernaprakash/Desktop/Terraform multiple/dev",
    })
    defer terraform.Destroy(t, terraformOptions)
    terraform.InitAndApply(t, terraformOptions)
    //defer terraform.Destory(t, terraformOptions)

    publicIp := terraform.OutputList(t, terraformOptions, "public_ipv4_address")
    fmt.Print(publicIp)
    //var index int = 0
    for i := 0; i < len(publicIp); i++ {
        

        url := fmt.Sprintf("http://%s:8080", publicIp[i])
        http_helper.HttpGetWithRetry(t, url, nil, 200, "I have made  a Terraform module", 2, 2*time.Second)
        fmt.Println(err)

    }
}`

**output**

http://52.205.87.202:8080TestTerraformHelloWorldExample 2023-02-07T20:43:25Z retry.go:91: HTTP GET to URL http://52.205.87.202:8080 TestTerraformHelloWorldExample 2023-02-07T20:43:25Z http_helper.go:59: Making an HTTP GET call to URL http://52.205.87.202:8080 TestTerraformHelloWorldExample 2023-02-07T20:43:35Z retry.go:103: HTTP GET to URL http://52.205.87.202:8080 returned an error: Get "http://52.205.87.202:8080": context deadline exceeded (Client.Timeout exceeded while awaiting headers). Sleeping for 2s and will try again. TestTerraformHelloWorldExample 2023-02-07T20:43:37Z retry.go:91: HTTP GET to URL http://52.205.87.202:8080 TestTerraformHelloWorldExample 2023-02-07T20:43:37Z http_helper.go:59: Making an HTTP GET call to URL http://52.205.87.202:8080 TestTerraformHelloWorldExample 2023-02-07T20:43:47Z retry.go:103: HTTP GET to URL http://52.205.87.202:8080 returned an error: Get "http://52.205.87.202:8080": context deadline exceeded (Client.Timeout exceeded while awaiting headers). Sleeping for 2s and will try again. TestTerraformHelloWorldExample 2023-02-07T20:43:49Z retry.go:91: HTTP GET to URL http://52.205.87.202:8080 TestTerraformHelloWorldExample 2023-02-07T20:43:49Z http_helper.go:59: Making an HTTP GET call to URL http://52.205.87.202:8080 TestTerraformHelloWorldExample 2023-02-07T20:43:59Z retry.go:103: HTTP GET to URL http://52.205.87.202:8080 returned an error: Get "http://52.205.87.202:8080": context deadline exceeded (Client.Timeout exceeded while awaiting headers). Sleeping for 2s and will try again.

did go through few stack overflow sites and couple of documents.
NIRAJ BAIS
  • 41
  • 4

1 Answers1

1

The reason for that is the HttpGetWithRetry method. As per Terratest comments:

HttpGetWithRetry will repeatedly perform an HTTP GET on the given URL until the given status code and body are returned or until max.

Ideally, you'll want to use something like HTTPDoE that will perform the given HTTP method on the given URL and return the HTTP status code, body, and any error, so you can save the status code and IP for each request into an array of maps.

Later on, you can verify this array to check if a status code is not 200 and print the results.

javierlga
  • 1,409
  • 9
  • 14