首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Terratest -如何在函数之间传递Terraform输出

Terratest -如何在函数之间传递Terraform输出
EN

Stack Overflow用户
提问于 2021-12-21 13:46:43
回答 1查看 759关注 0票数 1

我试着使用'test_structure.SaveTerraformOptions‘,但它并没有节省资源I。例如,我运行两个模块-模块1创建网络和子网,模块2需要模块的1个子网ids。我怎样才能在他们之间传递这个信息?

当我在同一个“test_structure.RunTestStage”中运行两个函数时,我可以将资源id从一个函数传递到另一个函数,但它不会测试第一个函数,也不会在最后销毁。

我将编写一个助手函数,它将只加载所有内容,其他函数将进行测试。也许这个能帮上忙。

然而,这方面的最佳做法是什么?

这是我的密码:

代码语言:javascript
复制
package test

import (
    "testing"

    //"github.com/gruntwork-io/terratest/modules/random"

    "github.com/gruntwork-io/terratest/modules/azure"
    "github.com/gruntwork-io/terratest/modules/terraform"
    test_structure "github.com/gruntwork-io/terratest/modules/test-structure"
    "github.com/stretchr/testify/assert"
)

// An example of how to test the simple Terraform module in examples/terraform-basic-example using Terratest.

func TestRunAll(t *testing.T) {
    t.Parallel()

    environmentName := "dev"
    projectName := "toha"
    rgName := environmentName + "-" + projectName + "-rg"
    subscriptionID := "96b72f1a-1bdc-4fc7-b971-05e7cea7d850"

    rg_net := test_structure.CopyTerraformFolderToTemp(t, "../", "001_networking")

    test_structure.RunTestStage(t, "test_azure_resource_group", func() {
        terraformOptions := testAzureResourceGroup(t, rgName, subscriptionID)
        test_structure.SaveTerraformOptions(t, rg_net, terraformOptions)
        
        testTerraformAzureFunctionApp(t, rgName, terraformOptions)
        terraform.Destroy(t, terraformOptions)
    })
}

func testAzureResourceGroup(t *testing.T, rgName string, subscriptionID string) *terraform.Options {

    //uniquePostfix := strings.ToLower(random.UniqueId())
    terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{

        TerraformDir: "../001_networking",

        Vars: map[string]interface{}{
            "rg_name": rgName,
        },

        VarFiles: []string{"../../environments/dev/vars.tfvars"},

        // Disable colors in Terraform commands so its easier to parse stdout/stderr
        NoColor: true,
    })

    terraform.InitAndApply(t, terraformOptions)
    rg_name_out := terraform.Output(t, terraformOptions, "rg-name")

    assert.Equal(t, rgName, rg_name_out)

    return terraformOptions
}

func testTerraformAzureFunctionApp(t *testing.T, rgName string, netOpts *terraform.Options) {

    azurePortalIPRange := []string{"61.100.129.0/24"}

    subnet_1 := terraform.Output(t, netOpts, "azurerm_subnet_1")
    subnet_2 := terraform.Output(t, netOpts, "azurerm_subnet_2")

    terraformOptions := &terraform.Options{
        TerraformDir: "../002_function_app",

        Vars: map[string]interface{}{
            "subnet_id_1":           subnet_1,
            "subnet_id_2":           subnet_2,
            "rg_name":               rgName,
            "azure_portal_ip_range": azurePortalIPRange,
            "cosmos_db_endpoint":    "test",
            "cosmos_db_password":    "test",
        },

        VarFiles: []string{"../../environments/dev/vars.tfvars"},
    }

    defer terraform.Destroy(t, terraformOptions)
    terraform.InitAndApply(t, terraformOptions)

    resourceGroupName := terraform.Output(t, terraformOptions, "resource_group_name")
    appName := terraform.Output(t, terraformOptions, "function_app_name")

    appId := terraform.Output(t, terraformOptions, "function_app_id")
    appDefaultHostName := terraform.Output(t, terraformOptions, "default_hostname")
    appKind := terraform.Output(t, terraformOptions, "function_app_kind")

    // website::tag::4:: Assert
    assert.True(t, azure.AppExists(t, appName, resourceGroupName, ""))
    site := azure.GetAppService(t, appName, resourceGroupName, "")

    assert.Equal(t, appId, *site.ID)
    assert.Equal(t, appDefaultHostName, *site.DefaultHostName)
    assert.Equal(t, appKind, *site.Kind)

    assert.NotEmpty(t, *site.OutboundIPAddresses)
    assert.Equal(t, "Running", *site.State)
}
EN

回答 1

Stack Overflow用户

发布于 2022-05-12 23:20:56

我认为使用test_structure的最佳方法是按阶段分离(https://pkg.go.dev/github.com/gruntwork-io/terratest/modules/test-structure#RunTestStage):

  1. 配置terratest,将特定配置的选项保存到terraform -> SETUP

test_structure.RunTestStage(t,"SETUP",func() { terraformOption := &terraform.Options{ TerraformDir:"your/path/here",} test_structure.SaveTerraformOptions(t,目录,terraformOption) terraform.InitAndApply(t,terraformOption) })

  1. 在测试结束时摧毁你的潜艇->拆卸

延迟test_structure.RunTestStage(t,"TEARDOWN",func() { terraformOptions := test_structure.LoadTerraformOptions(t,目录) terraform.Destroy(t,terraformOptions) })

  1. 您的测试用例->测试

test_structure.RunTestStage(t,"TESTS",func() ){ terraformOption := test_structure.LoadTerraformOptions(t," Your /path/") //您的输出resourceGroupName := terraform.Output(t,terraformOptions,"resource_group_name") appName := terraform.Output(t,terraformOptions,"function_app_name") appId := terraform.Output(t,appId)"function_app_id") //您的函数//它可以接收t.Run("Test1",func(t *testing.T) { testAzureResourceGroup(t,resourceGroupName,subscriptionID) }) t.Run("Test2",func(t *testing.T) { testTerraformAzureFunctionApp(t,appId,appName) } })

微软有一个例子更多地解释https://learn.microsoft.com/en-us/azure/developer/terraform/best-practices-end-to-end-testing :)

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

https://stackoverflow.com/questions/70436489

复制
相关文章

相似问题

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