英文:
Terraform Terratest - undefined Destroy function error
问题
尝试运行 terratest 在调用 AWS 实例并托管静态网站的 Terraform 文件上,但在运行 terratest 模块后,destroy 函数出现未定义错误。
Terratest 代码:
`package test
import (
"fmt"
http_helper "github.com/gruntwork-io/terratest/modules/http-helper"
"github.com/gruntwork-io/terratest/modules/terraform"
"testing"
"time"
)
func TestTerraformHelloWorldExample(t *testing.T) {
t.Parallel()
// 构建 terraform 选项,使用默认的可重试错误处理最常见的可重试错误。
terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
// 设置要测试的 Terraform 代码的路径。
TerraformDir: "../project-3/webserver",
})
terraform.InitAndApply(t, terraformOptions)
defer terraform.Destory(t, terraformOptions)
publicIp := terraform.Output(t, terraformOptions, "public_ip")
url := fmt.Sprintf("http://%s:8080", publicIp)
http_helper.HttpGetWithRetry(t, url, nil, 200, "I have made a Terraform module", 30, 5*time.Second)
}
`
~~~~~~~~~~~~
当我运行测试时的输出:
> go test webserver_test.go
# command-line-arguments [command-line-arguments.test]
./webserver_test.go:23:18: undefined: terraform.Destory
FAIL command-line-arguments [build failed]
FAIL
我尝试找出为什么会出现这个问题,但无法找到根本原因。请问有人可以帮忙吗?
我查看了文档和一些教程视频,但对这个问题没有太多了解。
<details>
<summary>英文:</summary>
Tried to run terratest over terraform file which invokes a aws instance and host a static website but post running this terratest module, it gives an undefined error for destroy function.
Terratest code:
`package test
import (
"fmt"
http_helper "github.com/gruntwork-io/terratest/modules/http-helper"
"github.com/gruntwork-io/terratest/modules/terraform"
"testing"
"time"
)
func TestTerraformHelloWorldExample(t *testing.T) {
t.Parallel()
// 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: "../project-3/webserver",
})
terraform.InitAndApply(t, terraformOptions)
defer terraform.Destory(t, terraformOptions)
publicIp := terraform.Output(t, terraformOptions, "public_ip")
url := fmt.Sprintf("http://%s:8080", publicIp)
http_helper.HttpGetWithRetry(t, url, nil, 200, "I have made a Terraform module", 30, 5*time.Second)
}
`
Output when I ran the test :
> go test webserver_test.go
# command-line-arguments [command-line-arguments.test]
./webserver_test.go:23:18: undefined: terraform.Destory
FAIL command-line-arguments [build failed]
FAIL
Tried to figure out why this is an issue but not able to get root cause. Could someone please help.
I did check the documentation and couple of tutorial video but didn't get much insight on the issue.
</details>
# 答案1
**得分**: 2
似乎是一个拼写错误,所以尝试使用以下代码:
```go
defer terraform.Destroy(t, terraformOptions)
```
而不是
```go
defer terraform.Destory(t, terraformOptions)
```
请参考 https://github.com/gruntwork-io/terratest/blob/e1570b571f726e7d2389b059519101a345b442d6/modules/terraform/destroy.go#L9
<details>
<summary>英文:</summary>
Seems a typo, so try:
```
defer terraform. Destroy(t, terraformOptions)
```
instead of
```
defer terraform.Destory(t, terraformOptions)
```
See https://github.com/gruntwork-io/terratest/blob/e1570b571f726e7d2389b059519101a345b442d6/modules/terraform/destroy.go#L9
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论