簡體   English   中英

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

有什么辦法可以像我申請時那樣瞄准嗎?

謝謝;

我認為這里有幾個問題:

  1. setup步驟中,您調用SaveTerraformOptions兩次,但您必須意識到第二次調用會覆蓋第一個調用!
  2. destroy步驟中,您只調用LoadTerraformOptionsDestroy一次,因此即使您有兩個terraform.Options結構,您仍然只會在其中一個上運行destroy

我認為要解決這個問題,在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)
})

我終於設法讓它工作。 使用@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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM