Terratest多个目标

问题描述

我正在使用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,"module.two")

        test_structure.SaveterraformOptions(t,terraformOptionsInfra)

        test_structure.SaveterraformOptions(t,terraformOptionsConf)

        terraform.InitAndApply(t,terraformOptionsInfra)
        terraform.InitAndApply(t,terraformOptionsConf)
    })
    test_structure.RunTestStage(t,"validate",terraformDir)
        testHello(t,terraformOptions)

    })
}

func testHello(t *testing.T,terraformOptions *terraform.Options) {
   fmt.Printf("Hello")
}

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

谢谢;

解决方法

我认为这里有几个问题:

  1. SaveTerraformOptions步骤中,您两次调用destroy,但您必须意识到,第二个调用将覆盖第一个调用!
  2. LoadTerraformOptions步骤中,您仅调用一次Destroyterraform.Options,因此即使您同时拥有两个destroy结构,您仍然只能运行{{ 1}}。

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

test_structure.SaveTestData(t,test_structure.FormatTestDataPath(terraformDir,"TerraformOptionsInfra.json"),terraformOptionsInfra)
test_structure.SaveTestData(t,"TerraformOptionsConf.json"),terraformOptionsConf)

然后,您需要两个destroy步骤(例如destroy_infradestroy_conf),每个步骤都应使用LoadTestData来取回数据并运行{{ 1}}:

Destroy
,

我终于设法使其正常运行。 使用@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,"module.conf")
            terraform.Destroy(t,terraformOptionsConf)
            terraform.Destroy(t,terraformOptionsInfra)
        })
    
        test_structure.RunTestStage(t,"setup","module.conf")
    
            test_structure.SaveTerraformOptions(t,terraformOptionsInfra)
    
            test_structure.SaveTerraformOptions(t,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")
}

我希望这可以帮助其他人。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...