问题描述
我正在使用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,"module.two")
test_structure.SaveterraformOptions(t,terraformOptionsInfra)
test_structure.SaveterraformOptions(t,terraformOptionsConf)
terraform.InitAndApply(t,terraformOptionsInfra)
terraform.InitAndApply(t,terraformOptionsConf)
})
test_structure.RunTestStage(t,"validate",terraformDir)
testHello(t,terraformOptions)
})
}
func testHello(t *testing.T,terraformOptions *terraform.Options) {
fmt.Printf("Hello")
}
有什么方法可以像我申请时那样定位?
谢谢;
解决方法
我认为这里有几个问题:
- 在
SaveTerraformOptions
步骤中,您两次调用destroy
,但您必须意识到,第二个调用将覆盖第一个调用! - 在
LoadTerraformOptions
步骤中,您仅调用一次Destroy
和terraform.Options
,因此即使您同时拥有两个destroy
结构,您仍然只能运行{{ 1}}。
我想解决此问题,在setup
步骤中,您将使用不同的路径直接调用SaveTestData(SaveTerraformOptions
只是此方法的包装):
test_structure.SaveTestData(t,test_structure.FormatTestDataPath(terraformDir,"TerraformOptionsInfra.json"),terraformOptionsInfra)
test_structure.SaveTestData(t,"TerraformOptionsConf.json"),terraformOptionsConf)
然后,您需要两个destroy
步骤(例如destroy_infra
,destroy_conf
),每个步骤都应使用LoadTestData来取回数据并运行{{ 1}}:
Destroy
,
我终于设法使其正常运行。 使用@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,"module.conf")
terraform.Destroy(t,terraformOptionsConf)
terraform.Destroy(t,terraformOptionsInfra)
})
test_structure.RunTestStage(t,"setup","module.conf")
test_structure.SaveTerraformOptions(t,terraformOptionsInfra)
test_structure.SaveTerraformOptions(t,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")
}
我希望这可以帮助其他人。