【问题标题】:Terratest assert string fails with GitHub ActionsTerratest 断言字符串因 GitHub 操作而失败
【发布时间】:2021-03-31 03:21:24
【问题描述】:

版本

  • 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 输出-无颜色名称\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 -输出名称=stderr::\n::set-输出名称=exitcode::0"
差异:
--- 预期
+++ 实际
@@ -1 +1,9 @@
+[命令]/home/runner/work/_temp/e5c5b8d2-62c5-4e73-b99f-d2e79eaa9765/terraform-b​​in 输出-无颜色名称
TestIamRole_dRGsKn
+::debug::Terraform 以代码 0 退出。
+::debug::stdout: TestIamRole_dRGsKn%0A
+::debug::stderr:
+::debug::exitcode: 0
+::设置输出名称=stdout::TestIamRole_dRGsKn%0A
+::设置输出名称=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)

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

【问题讨论】:

    标签: go terratest


    【解决方案1】:

    发现问题。

    引用了 terratest GitHub repo 的 in the issues

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

    更改我的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 }}
    

    【讨论】:

      猜你喜欢
      • 2022-01-03
      • 2022-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多