0

版本

  • Terraform v0.13.5
  • 转到 v1.15.2
  • Terratest v0.30.23

问题

我根据Terratest Docs创建了一个测试。测试代码可以在下面的标题下找到:iam_role_standard_test.go

当我在本地运行测试时,一切都按预期工作。

但是,我使用 GitHub Actions 作为我的 CI/CD 工具。当工作流作业执行时,它会失败并出现以下错误(工作流文件位于以下标题workflow.yaml下):

错误跟踪:iam_role_standard_test.go:63
错误:不等于:
预期:“TestIamRole_dRGsKn”
实际:“[command]/home/runner/work/_temp/e5c5b8d2-62c5-4e73-b99f-d2e79eaa9765/terraform-b​​in 输出 -no-颜色名称\nTestIamRole_dRGsKn\n::debug::Terraform 以代码 0 退出。\n::debug::stdout: TestIamRole_dRGsKn%0A\n::debug::stderr: \n::debug::exitcode: 0\n ::set-output name=stdout::TestIamRole_dRGsKn%0A\n::set-output name=stderr::\n::set-output name=exitcode::0"
Diff:
--- 预期
+++ 实际
@ @ -1 +1,9 @@
+[command]/home/runner/work/_temp/e5c5b8d2-62c5-4e73-b99f-d2e79eaa9765/terraform-b​​in output -no-color name
TestIamRole_dRGsKn
+::debug::Terraform exited代码为 0。
+::debug::stdout: TestIamRole_dRGsKn%0A
+::debug::stderr:
+::debug::exitcode: 0
+::set-output name=stdout::TestIamRole_dRGsKn%0A
+::set-output name= stderr::
+::set-output name=exitcode::0
测试:TestIamRole

据我所知,哪个是失败的,因为与之"TestIamRole_dRGsKn"相比TestIamRole_dRGsKn是不一样的。

问题

如何格式化 terraform 的输出,以便将字符串断言为相等?

iam_role_standard_test.go

package test

import (
    "fmt"
    "testing"

    "github.com/stretchr/testify/assert"

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

func TestIamRole(t *testing.T) {

    uniqueId := random.UniqueId()
    expectedRoleName := fmt.Sprintf("TestIamRole_%s", uniqueId)

    // Retryable errors in terraform testing.
    terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
        TerraformDir: "../path/to/terraform",

        Lock: true,

        // Configure backend to assume IamManager role
        BackendConfig: map[string]interface{}{
            "bucket":         "terraform-state-bucket",
            "dynamodb_table": "terraform_state_lock",
            "encrypt":        true,
            "key":            "my/key/terraform.tfstate",
            "kms_key_id":     "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
            "region":         "us-east-1",
            "role_arn":       "arn:aws:iam::123456789012:role/my-role",
        },

        // Variables to pass to our Terraform code using -var options
        Vars: map[string]interface{}{
            "create":                "true",
            "account_id":            "123456789012",
            "is_instance_profile":   "false",
            "name":                  expectedRoleName,
            "description":           "A test IAM role",
            "path":                  "/",
            "force_detach_policies": "true",
            "max_session_duration":  "43200",
        },
    })

    defer terraform.Destroy(t, terraformOptions)

    // Deploy configuration
    terraform.InitAndApply(t, terraformOptions)

    // Capture outputs
    actualRoleName := terraform.Output(t, terraformOptions, "name")

    // Assert output vaules
    assert.Equal(t, expectedRoleName, actualRoleName)

工作流.yaml

name: terratest
on: [ push ]
jobs:
    terratest:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-go@v2
        with:
          go-version: '^1.15.2'
      - uses: hashicorp/setup-terraform@v1
        with:
          terraform_version: 0.13.5
      - run: go mod init github.com/my-org/my-repo
      - run: go test -v -count=1 -timeout 30m
        working-directory: tests
    env:
      AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
      AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
4

1 回答 1

0

发现问题。

它在 terratest GitHub repo的问题中被引用

在这里发布答案,以防其他人为此浪费生命!

更改我的workflow.yaml文件解决了它:

name: terratest
on: [ push ]
jobs:
    terratest:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-go@v2
        with:
          go-version: '^1.15.2'
      - uses: hashicorp/setup-terraform@v1
        with:
          terraform_version: 0.13.5
          terraform_wrapper: false
      - run: go mod init github.com/my-org/my-repo
      - run: go test -v -count=1 -timeout 30m
        working-directory: tests
    env:
      AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
      AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
于 2020-12-23T12:16:00.173 回答