You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Terratest测试Terraform脚本时遭遇exit status 1错误求助

问题:Terratest测试Terraform Azure资源时,销毁后报错输出变量未找到

问题场景

使用Terratest测试Terraform代码,验证Azure的Resource Group、Container Registry和AKS是否成功创建。执行go test -v -timeout 60m后,资源能正常创建并销毁,但最终测试失败。

错误信息

TestTerraformAzureResources 2024-02-09T18:45:51+05:00 logger.go:66: 
Destroy complete! Resources: 4 destroyed.
TestTerraformAzureResources 2024-02-09T18:45:51+05:00 logger.go:66: 
--- FAIL: TestTerraformAzureResources (691.95s)
FAIL
exit status 1
FAIL    my-terratest-project    691.996s

补充错误详情:

Error: Received unexpected error: FatalError{Underlying: error while running command: exit status 1; Error: Output "resource_group_name" not found The output variable requested could not be found in the state file. If you recently added this to your configuration, be sure to run terraform apply

测试代码(terraform_test.go)

package test

import (
"testing"

"github.com/gruntwork-io/terratest/modules/terraform"
"github.com/stretchr/testify/assert"
)

func TestTerraformAzureResources(t *testing.T) {
terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
    TerraformDir: "../terraform scripts",
})

defer terraform.Destroy(t, terraformOptions)

terraform.InitAndApply(t, terraformOptions)

// Retrieve the outputs of the Terraform configuration
resourceGroupName := terraform.Output(t, terraformOptions, "resource_group_name")
acrName := terraform.Output(t, terraformOptions, "acr_name")
aksName := terraform.Output(t, terraformOptions, "aks_name")

// Define the expected values
expectedResourceGroupName := "microservice-deployment-demo-tf-scripttest"
expectedACRName := "microserviceimagestfscripttest"
expectedAKSName := "example-aks1-tf-scripttest"

// Compare the outputs with the expected values
assert.Equal(t, expectedResourceGroupName, resourceGroupName, "Resource group name does not match")
assert.Equal(t, expectedACRName, acrName, "Azure Container Registry name does not match")
assert.Equal(t, expectedAKSName, aksName, "Azure Kubernetes Cluster name does not match")
}

排查与解决

核心原因

报错本质是Terraform状态文件中找不到resource_group_name等输出变量,大概率是你的Terraform配置未定义对应的output块,或者terraform apply过程中出现未捕获的错误导致输出未生成。

解决步骤

  • 添加Terraform输出定义:
    在Terraform代码目录下创建或修改outputs.tf,添加对应资源的输出(注意替换代码中的资源标识符为你实际使用的名称):

    output "resource_group_name" {
      description = "名称:资源组"
      value       = azurerm_resource_group.demo.name
    }
    
    output "acr_name" {
      description = "名称:容器注册表"
      value       = azurerm_container_registry.acr_demo.name
    }
    
    output "aks_name" {
      description = "名称:AKS集群"
      value       = azurerm_kubernetes_cluster.aks_demo.name
    }
    

    示例中的azurerm_resource_group.demo需要和你Terraform代码中定义资源时的名称一致,比如你定义资源时写的是resource "azurerm_resource_group" "microservice_rg" { ... },就改成azurerm_resource_group.microservice_rg.name。

  • 修正Terraform目录路径:
    测试代码中TerraformDir: "../terraform scripts"包含空格,建议改为无空格路径(如"../terraform-scripts"),避免潜在的路径解析问题。

  • 确认Apply执行状态:
    运行测试时查看terraform init和terraform apply的完整日志,确保资源创建过程无报错。如果Apply失败,状态文件会不完整,导致输出变量无法被读取。

内容的提问来源于stack exchange,提问作者Abaiz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 11:27:08