![](/img/trans.png)
[英]how to filter pods against multiple label selectors with terratest?
[英]Terratest multiple targets
我正在使用 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
两次,但您必须意识到第二次调用会覆盖第一个调用!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)
})
我终于设法让它工作。 使用@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")
}
我希望这可以帮助其他人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.