我正在使用terrate来测试我的terraform代码。我的代码有两个模块,所以我设法在配置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")
}有没有办法像我申请的时候那样达到目标?
谢谢;
发布于 2020-09-09 16:02:20
我认为这里有几个问题:
setup步骤中,您将调用SaveTerraformOptions两次,但您必须意识到,第二个调用将覆盖第一个!destroy步骤中,您只调用了LoadTerraformOptions和Destroy一次,因此,即使您拥有两个<代码>D9结构,您仍然只能在其中一个结构上运行。<代码>H211<代码>G212我认为要解决这个问题,在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)
})发布于 2020-09-10 21:36:59
我终于设法让它工作起来了。使用@yevgeniy的想法,我想出了以下代码。
package test
import (
"fmt"
"time"
"os"
"testing"
"net/http"
"log"
"io/ioutil"
"github.com/gruntwork-io/terratest/modules/terraform"
"github.com/gruntwork-io/terratest/modules/retry"
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() {
terraformOptionsInfra := configureTerraformOptions(t, terraformDir, "module.infra")
terraformOptionsConf := configureTerraformOptions(t, terraformDir, "module.conf")
terraform.Destroy(t, terraformOptionsConf)
terraform.Destroy(t, terraformOptionsInfra)
})
test_structure.RunTestStage(t, "setup", func() {
terraformOptionsInfra := configureTerraformOptions(t, terraformDir, "module.infra")
terraformOptionsConf := configureTerraformOptions(t, terraformDir, "module.conf")
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")
}我希望这能帮助其他人。
https://stackoverflow.com/questions/63775990
复制相似问题