2 回答

TA貢獻1810條經驗 獲得超4個贊
我認為這里有幾個問題:
在這setup一步中,您調用SaveTerraformOptions了兩次,但您必須意識到第二次調用會覆蓋第一次調用!
在該destroy步驟中,您只調用LoadTerraformOptions一次Destroy,因此即使您擁有兩個terraform.Options結構,您仍然只能destroy在其中一個上運行。
我認為要解決此問題,在該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)
})

TA貢獻2011條經驗 獲得超2個贊
我終于設法讓它工作了。使用@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")
}
我希望這可以幫助其他人。
- 2 回答
- 0 關注
- 151 瀏覽
添加回答
舉報