Terraform Terratest – 未定义的 Destroy 函数错误

huangapple go评论89阅读模式
英文:

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"

&quot;testing&quot;
&quot;time&quot;

)

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, &amp;terraform.Options{
	// Set the path to the Terraform code that will be tested.
	TerraformDir: &quot;../project-3/webserver&quot;,
})
terraform.InitAndApply(t, terraformOptions)

defer terraform.Destory(t, terraformOptions)
publicIp := terraform.Output(t, terraformOptions, &quot;public_ip&quot;)
url := fmt.Sprintf(&quot;http://%s:8080&quot;, publicIp)
http_helper.HttpGetWithRetry(t, url, nil, 200, &quot;I have made  a Terraform module&quot;, 30, 5*time.Second)

}
`



Output when I ran the test :
&gt; 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&#39;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>



huangapple
  • 本文由 发表于 2023年2月6日 07:09:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356132.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定