Terratest 多个目标

我正在使用 terrates 来测试我的 terraform 代码。我的代码有 2 个模块,所以我设法配置 terratest 以在配置 terraformOptions 时使用目标选项,它创建了两个模块。


但是,在清理所有内容时,它只清理使用 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")

}

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


红颜莎娜
浏览 170回答 2
2回答

蝴蝶不菲

我认为这里有几个问题:在这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")}我希望这可以帮助其他人。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go