首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Terratest多目标

Terratest多目标
EN

Stack Overflow用户
提问于 2020-09-07 18:42:27
回答 2查看 724关注 0票数 1

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

但是,当需要清理所有内容时,它只使用Defer清理最后一个模块。这是我的代码。

代码语言:javascript
复制
    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")
}

有没有办法像我申请的时候那样达到目标?

谢谢;

EN

回答 2

Stack Overflow用户

发布于 2020-09-09 16:02:20

我认为这里有几个问题:

  1. setup步骤中,您将调用SaveTerraformOptions两次,但您必须意识到,第二个调用将覆盖第一个!
  2. destroy步骤中,您只调用了LoadTerraformOptionsDestroy一次,因此,即使您拥有两个<代码>D9结构,您仍然只能在其中一个结构上运行。<代码>H211<代码>G212

我认为要解决这个问题,在setup步骤中,您将使用不同的路径直接调用SaveTestData (SaveTerraformOptions只是此方法的包装器):

代码语言:javascript
复制
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

代码语言:javascript
复制
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)
})
票数 3
EN

Stack Overflow用户

发布于 2020-09-10 21:36:59

我终于设法让它工作起来了。使用@yevgeniy的想法,我想出了以下代码。

代码语言:javascript
复制
    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")
}

我希望这能帮助其他人。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63775990

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档