【问题标题】:Terratest multiple targetsTerratest 多个目标
【发布时间】:2020-12-25 18:45:57
【问题描述】:

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

但是,在清理所有内容时,它只清理使用 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")
}

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

谢谢;

【问题讨论】:

    标签: go terraform terratest


    【解决方案1】:

    我认为这里有几个问题:

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

    【讨论】:

    • 我的意思是我必须将两行 SaveTerraformOptions 替换为 SaveTestData 吗?因为在那种情况下,我猜test_structure.RunTestStage 也会有一些变化
    • 是的,您需要更新验证阶段才能使用LoadTestData
    【解决方案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-07-08
      • 1970-01-01
      • 2022-11-03
      • 1970-01-01
      • 1970-01-01
      • 2020-03-22
      • 2020-08-03
      • 2022-12-17
      • 2018-02-06
      相关资源
      最近更新 更多