简体   繁体   English

Terratest 断言字符串因 GitHub 操作而失败

[英]Terratest assert string fails with GitHub Actions

Versions版本

  • Terraform v0.13.5 Terraform v0.13.5
  • Go v1.15.2 Go v1.15.2
  • Terratest v0.30.23 Terratest v0.30.23

Issue问题

I've created a test according to the Terratest Docs .我根据Terratest Docs创建了一个测试。 The code for the test can be found below under the heading: iam_role_standard_test.go测试代码可以在下面的标题下找到: iam_role_standard_test.go

When I run the test locally, everything works as expected.当我在本地运行测试时,一切都按预期工作。

However, I'm using GitHub Actions as my CI/CD tool.但是,我使用 GitHub Actions 作为我的 CI/CD 工具。 When the workflow job executes it fails with the following error (The workflow file is below under the heading workflow.yaml ):当工作流作业执行时,它会失败并出现以下错误(工作流文件位于标题workflow.yaml下方):

Error Trace: iam_role_standard_test.go:63错误跟踪:iam_role_standard_test.go:63
Error: Not equal:错误:不等于:
expected: "TestIamRole_dRGsKn"预期:“TestIamRole_dRGsKn”
actual: "[command]/home/runner/work/_temp/e5c5b8d2-62c5-4e73-b99f-d2e79eaa9765/terraform-bin output -no-color name\nTestIamRole_dRGsKn\n::debug::Terraform exited with code 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" actual: "[command]/home/runner/work/_temp/e5c5b8d2-62c5-4e73-b99f-d2e79eaa9765/terraform-bin output -no-color name\nTestIamRole_dRGsKn\n::debug::Terraform exited with code 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"
Diff:差异:
--- Expected - - 预期的
+++ Actual +++ 实际
@@ -1 +1,9 @@ @@ -1 +1,9 @@
+[command]/home/runner/work/_temp/e5c5b8d2-62c5-4e73-b99f-d2e79eaa9765/terraform-bin output -no-color name +[命令]/home/runner/work/_temp/e5c5b8d2-62c5-4e73-b99f-d2e79eaa9765/terraform-bin output -无颜色名称
TestIamRole_dRGsKn TestIamRole_dRGsKn
+::debug::Terraform exited with code 0. +::debug::Terraform 以代码 0 退出。
+::debug::stdout: TestIamRole_dRGsKn%0A +::debug::stdout: TestIamRole_dRGsKn%0A
+::debug::stderr: +::调试::标准错误:
+::debug::exitcode: 0 +::调试::退出代码:0
+::set-output name=stdout::TestIamRole_dRGsKn%0A +::设置输出名称=stdout::TestIamRole_dRGsKn%0A
+::set-output name=stderr:: +::设置输出名称=stderr::
+::set-output name=exitcode::0 +::设置输出名称=退出代码::0
Test: TestIamRole测试:TestIamRole

Which, from what I can make out, is failing because "TestIamRole_dRGsKn" is compared to TestIamRole_dRGsKn which is not the same.据我所知,这是失败的,因为"TestIamRole_dRGsKn"与不一样的TestIamRole_dRGsKn进行了比较。

Question问题

How do I format the output from terraform so that the strings will be asserted as equal?如何格式化 terraform 中的 output 以便将字符串断言为相等?

iam_role_standard_test.go 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工作流程.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 }}

Found the issue.发现了问题。

It's referenced in the issues of the terratest GitHub repo它在 terratest GitHub repo的问题中被引用

Posting the answer here in case anyone else wastes a moment of their life on this!在这里发布答案,以防其他人为此浪费生命!

Changing my workflow.yaml file solved it:更改我的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 }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM