[英]Terratest multiple targets
I am using terrates to test my terraform code.我正在使用 terrates 来测试我的 terraform 代码。 My code have 2 modules so I managed to configure terratest to use target option while configuring the terraformOptions and it creates both modules.
我的代码有 2 个模块,因此我设法在配置 terraformOptions 时将 terratest 配置为使用目标选项,并创建了两个模块。
However, when it comes to clean everything, it cleans only the last module using Defer.但是,在清理所有内容时,它只使用 Defer 清理最后一个模块。 Here is my code.
这是我的代码。
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")
}
Is there any way to target like when I apply ?有什么办法可以像我申请时那样瞄准吗?
Thanks;谢谢;
I think there are a couple issues here:我认为这里有几个问题:
setup
step, you're calling SaveTerraformOptions
twice, but what you have to realize is that the second call is overwriting the first one!setup
步骤中,您调用SaveTerraformOptions
两次,但您必须意识到第二次调用会覆盖第一个调用!destroy
step, you call LoadTerraformOptions
and Destroy
just once, so even if you had both terraform.Options
structs, you'd still only be running destroy
on one of them.destroy
步骤中,您只调用LoadTerraformOptions
和Destroy
一次,因此即使您有两个terraform.Options
结构,您仍然只会在其中一个上运行destroy
。 I think to fix this, in the setup
step, you'll to call SaveTestData directly ( SaveTerraformOptions
is just a wrapper for this method) with different paths:我认为要解决这个问题,在
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)
And then you'll want two destroy
steps (eg, destroy_infra
, destroy_conf
), and each should use LoadTestData to get your data back and run Destroy
on it:然后你需要两个
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)
})
I finally managed to get it working.我终于设法让它工作。 Using @yevgeniy idea, I came up with following code.
使用@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")
}
I hope this can help other people.我希望这可以帮助其他人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.