簡體   English   中英

Terratest 斷言字符串因 GitHub 操作而失敗

[英]Terratest assert string fails with GitHub Actions

版本

  • Terraform v0.13.5
  • Go 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”
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"
差異:
- - 預期的
+++ 實際
@@ -1 +1,9 @@
+[命令]/home/runner/work/_temp/e5c5b8d2-62c5-4e73-b99f-d2e79eaa9765/terraform-bin output -無顏色名稱
TestIamRole_dRGsKn
+::debug::Terraform 以代碼 0 退出。
+::debug::stdout: TestIamRole_dRGsKn%0A
+::調試::標准錯誤:
+::調試::退出代碼:0
+::設置輸出名稱=stdout::TestIamRole_dRGsKn%0A
+::設置輸出名稱=stderr::
+::設置輸出名稱=退出代碼::0
測試:TestIamRole

據我所知,這是失敗的,因為"TestIamRole_dRGsKn"與不一樣的TestIamRole_dRGsKn進行了比較。

問題

如何格式化 terraform 中的 output 以便將字符串斷言為相等?

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

發現了問題。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM