我试着使用'test_structure.SaveTerraformOptions‘,但它并没有节省资源I。例如,我运行两个模块-模块1创建网络和子网,模块2需要模块的1个子网ids。我怎样才能在他们之间传递这个信息?
当我在同一个“test_structure.RunTestStage”中运行两个函数时,我可以将资源id从一个函数传递到另一个函数,但它不会测试第一个函数,也不会在最后销毁。
我将编写一个助手函数,它将只加载所有内容,其他函数将进行测试。也许这个能帮上忙。
然而,这方面的最佳做法是什么?
这是我的密码:
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)
}发布于 2022-05-12 23:20:56
我认为使用test_structure的最佳方法是按阶段分离(https://pkg.go.dev/github.com/gruntwork-io/terratest/modules/test-structure#RunTestStage):
test_structure.RunTestStage(t,"SETUP",func() { terraformOption := &terraform.Options{ TerraformDir:"your/path/here",} test_structure.SaveTerraformOptions(t,目录,terraformOption) terraform.InitAndApply(t,terraformOption) })
延迟test_structure.RunTestStage(t,"TEARDOWN",func() { terraformOptions := test_structure.LoadTerraformOptions(t,目录) terraform.Destroy(t,terraformOptions) })
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 :)
https://stackoverflow.com/questions/70436489
复制相似问题