亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Terratest 多個目標

Terratest 多個目標

Go
紅顏莎娜 2022-06-27 16:16:30
我正在使用 terrates 來測試我的 terraform 代碼。我的代碼有 2 個模塊,所以我設法配置 terratest 以在配置 terraformOptions 時使用目標選項,它創建了兩個模塊。但是,在清理所有內容時,它只清理使用 Defer 的最后一個模塊。這是我的代碼。    package testimport (    "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")}有什么方法可以像我申請時一樣定位嗎?
查看完整描述

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)

})


查看完整回答
反對 回復 2022-06-27
?
森林海

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")

}

我希望這可以幫助其他人。


查看完整回答
反對 回復 2022-06-27
  • 2 回答
  • 0 關注
  • 151 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號