在使用 Terratest 测试具有多个模块的 Terraform 代码时,销毁所有内容时仅清除最后一个模块的问题困扰着开发者。通过修改代码,使用不同的路径直接调用 SaveTestData 保存不同模块的 TerraformOptions,并在销毁步骤中分别使用 LoadTestData 取回数据并运行销毁,可以解决此问题。这样做确保了每个模块在销毁过程中都被正确处理。
我正在使用 terrates 来测试我的 terraform 代码。 我的代码有 2 个模块,因此我设法在配置 terraformoptions 时将 terratest 配置为使用目标选项,并创建了这两个模块。
但是,当涉及到清理所有内容时,它仅使用 defer 清理最后一个模块。 这是我的代码。
package test
import (
"fmt"
"os"
"testing"
"github.com/gruntwork-io/terratest/modules/terraform"
test_structure "github.com/gruntwork-io/terratest/modules/test-structure"
)
func configureTerraformOptions(t *testing.T, terraformDir string, tfModule string) (*terraform.Options) {
awsRegion := os.Getenv("AWS_REGION")
terraformOptions := &terraform.Options{
TerraformDir: terraformDir,
Targets: []string{tfModule},
Vars: map[string]interface{}{
"aws_region": awsRegion,
},
}
return terraformOptions
}
func TestInfra(t *testing.T) {
t.Parallel()
terraformDir := test_structure.CopyTerraformFolderToTemp(t, "../", "tests/terraform")
defer test_structure.RunTestStage(t, "destroy", func() {
terraformOptions := test_structure.LoadTerraformOptions(t, terraformDir)
terraform.Destroy(t, terraformOptions)
})
test_structure.RunTestStage(t, "setup", func() {
terraformOptionsInfra := configureTerraformOptions(t, terraformDir, "module.one")
terraformOptionsConf := configureTerraformOptions(t, terraformDir, "module.two")
test_structure.SaveTerraformOptions(t, terraformDir, terraformOptionsInfra)
test_structure.SaveTerraformOptions(t, terraformDir, terraformOptionsConf)
terraform.InitAndApply(t, terraformOptionsInfra)
terraform.InitAndApply(t, terraformOptionsConf)
})
test_structure.RunTestStage(t, "validate", func() {
terraformOptions := test_structure.LoadTerraformOptions(t, terraformDir)
testHello(t, terraformOptions)
})
}
func testHello(t *testing.T, terraformOptions *terraform.Options) {
fmt.Printf("Hello")
}
有什么方法可以像我申请时那样定位吗?
谢谢;
解决方案
我认为这里存在几个问题:
- 在
setup步骤中,您调用saveterraformoptions两次,但您必须意识到第二次调用会覆盖第一个调用! - 在
desry中,您可以调用loadterraformoptions和destroyzqbendcbendcbendcbendcbendcbendcbendcbendcbbendcbb oft一次其中一个qb。</li> </ol> <p>我想解决这个问题,在setup步骤中,您将使用不同的路径直接调用 SaveTestData (saveterraformoptions只是此方法的包装):test_structure.savetestdata(t, test_structure.formattestdatapath(terraformdir, "terraformoptionsinfra.json"), terraformoptionsinfra) test_structure.savetestdata(t, test_structure.formattestdatapath(terraformdir, "terraformoptionsconf.json"), terraformoptionsconf)
然后您需要两个
destroy步骤(例如destroy_infra、destroy_conf),每个步骤都应使用 LoadTestData 取回数据并对其运行destroy:defer test_structure.RunTestStage(t, "destroy_infra", func() { var terraformOptionsInfra *terraform.Options test_structure.LoadTestData(t, test_structure.FormatTestDataPath(terraformDir, "TerraformOptionsInfra.json"), terraformOptionsInfra) terraform.Destroy(t, terraformOptionsInfra) }) defer test_structure.RunTestStage(t, "destroy_conf", func() { var terraformOptionsConf *terraform.Options test_structure.LoadTestData(t, test_structure.FormatTestDataPath(terraformDir, "TerraformOptionsConf.json"), terraformOptionsConf) terraform.Destroy(t, terraformOptionsConf) })理论要掌握,实操不能落!以上关于《针对多个目标的 Terratest》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!
本文内容来源于互联网,如有侵权请联系删除。
