英文:
is there a way to fire multiple IP addresses using http_helper.HttpGetWithRetry in terratest?
问题
我正在测试使用下面的terratest代码通过触发多个IP地址来创建具有正确子网和规则的多个AWS实例,以测试AWS基础架构。但是它只使用第一个IP地址并连续命中4次。一旦它收到超时消息,它就会退出而不会转到下一个IP地址。我期望它应该命中来自main.tf terraform文件的两个IP地址并连接到服务器。
我对此还不熟悉,所以想了解我漏掉了什么?如果有人能帮助我,请告诉我。
我查阅了一些stackoverflow网站和一些文档。
英文:
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.
</details>
# 答案1
**得分**: 1
原因是`HttpGetWithRetry`方法。根据Terratest的注释:
> `HttpGetWithRetry`将在给定的URL上重复执行HTTP GET,直到返回给定的状态码和正文,或者达到最大次数。
理想情况下,你会想使用类似`HTTPDoE`的方法,它将在给定的URL上执行给定的HTTP方法,并返回HTTP状态码、正文和任何错误,这样你就可以将每个请求的状态码和IP保存到一个映射数组中。
稍后,你可以验证这个数组,检查状态码是否不是200,并打印结果。
<details>
<summary>英文:</summary>
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.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论