登录
首页 >  Golang >  Go问答

针对多个目标的 Terratest

来源:stackoverflow

时间:2024-03-20 17:45:30 433浏览 收藏

在使用 Terratest 测试具有多个模块的 Terraform 代码时,销毁所有内容时仅清除最后一个模块的问题困扰着开发者。通过修改代码,使用不同的路径直接调用 SaveTestData 保存不同模块的 TerraformOptions,并在销毁步骤中分别使用 LoadTestData 取回数据并运行销毁,可以解决此问题。这样做确保了每个模块在销毁过程中都被正确处理。

问题内容

我正在使用 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")
}

有什么方法可以像我申请时那样定位吗?

谢谢;


解决方案


我认为这里存在几个问题:

  1. setup 步骤中,您调用 saveterraformoptions 两次,但您必须意识到第二次调用会覆盖第一个调用!
  2. desry中,您可以调用loadterraformoptionsdestroyzqbendcbendcbendcbendcbendcbendcbendcbendcbbendcbb oft一次其中一个qb。

我想解决这个问题,在 setup 步骤中,您将使用不同的路径直接调用 SaveTestDatasaveterraformoptions 只是此方法的包装):

test_structure.savetestdata(t, test_structure.formattestdatapath(terraformdir, "terraformoptionsinfra.json"), terraformoptionsinfra)
test_structure.savetestdata(t, test_structure.formattestdatapath(terraformdir, "terraformoptionsconf.json"), terraformoptionsconf)

然后您需要两个 destroy 步骤(例如 destroy_infradestroy_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)
})

理论要掌握,实操不能落!以上关于《针对多个目标的 Terratest》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>