第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

Terratest 多個目標(biāo)

Terratest 多個目標(biāo)

Go
紅顏莎娜 2022-06-27 16:16:30
我正在使用 terrates 來測試我的 terraform 代碼。我的代碼有 2 個模塊,所以我設(shè)法配置 terratest 以在配置 terraformOptions 時使用目標(biāo)選項,它創(chuàng)建了兩個模塊。但是,在清理所有內(nèi)容時,它只清理使用 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貢獻(xiàn)1810條經(jīng)驗 獲得超4個贊

我認(rèn)為這里有幾個問題:


在這setup一步中,您調(diào)用SaveTerraformOptions了兩次,但您必須意識到第二次調(diào)用會覆蓋第一次調(diào)用!

在該destroy步驟中,您只調(diào)用LoadTerraformOptions一次Destroy,因此即使您擁有兩個terraform.Options結(jié)構(gòu),您仍然只能destroy在其中一個上運行。

我認(rèn)為要解決此問題,在該setup步驟中,您將使用不同的路徑直接調(diào)用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,每個步驟都應(yīng)該使用LoadTestData來取回你的數(shù)據(jù)并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)

})


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

TA貢獻(xiàn)2011條經(jīng)驗 獲得超2個贊

我終于設(shè)法讓它工作了。使用@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")

}

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


查看完整回答
反對 回復(fù) 2022-06-27
  • 2 回答
  • 0 關(guān)注
  • 161 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號