gruntwork-io / terratest

Terratest is a Go library that makes it easier to write automated tests for your infrastructure code.
https://terratest.gruntwork.io/
Apache License 2.0
7.4k stars 1.32k forks source link

Question: Duplicate Log Output #1068

Open TheYorkshireDev opened 2 years ago

TheYorkshireDev commented 2 years ago

Hi all,

First, I wanted to say terratest is an excellent tool and I've really enjoyed learning to use it. I do however have a question around the output of terratest specifically in CI.

I've been experimenting with testing a module I'm writing that uses integrations/github and while setting up terratest I've noticed that there is what appears to be duplicate output and also newlines rendered as %0A which I'd prefer not to have in my CI output.

For brevity, I've reduced the pipeline to not even call the module and just call defer terraform.Destroy(t, tfOptions) which still shows the duplicate output and escaped newlines. If anyone can suggest a way to remove the debug/duplicate output it would be much appreciated. I've included the Test file, CI output (including image) to assist in my explanation.

Resources

_defaults_exampletest.go

package main

import (
    "testing"

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

func TestDefaultsExampleWithOnlyRequiredInputs(t *testing.T) {
    //
    // Arrange
    //
    repositoryName := "RepoName"

    tfOptions := &terraform.Options{
        // The path to where our Terraform code is located
        TerraformDir: "../examples/defaults",

        // EnvVars: map[string]string{
        //  "TF_IN_AUTOMATION": "true",
        // },

        // Variables to pass to our Terraform code using -var options
        Vars: map[string]interface{}{
            "required_name": repositoryName,
        },

        // NoColor: true,
    }

    //
    // Act
    //
    defer terraform.Destroy(t, tfOptions)
}

CI Output

2022-02-22T21:07:52.7782006Z go: downloading github.com/gruntwork-io/terratest v0.40.2
2022-02-22T21:07:53.6248900Z go: downloading github.com/hashicorp/go-multierror v1.1.0
2022-02-22T21:07:53.6290280Z go: downloading github.com/hashicorp/hcl/v2 v2.9.1
2022-02-22T21:07:53.7071418Z go: downloading github.com/hashicorp/terraform-json v0.13.0
2022-02-22T21:07:53.7081488Z go: downloading github.com/jinzhu/copier v0.0.0-20190924061706-b57f9002281a
2022-02-22T21:07:53.7144831Z go: downloading github.com/stretchr/testify v1.7.0
2022-02-22T21:07:53.7421646Z go: downloading github.com/tmccombs/hcl2json v0.3.3
2022-02-22T21:07:53.7485173Z go: downloading github.com/zclconf/go-cty v1.9.1
2022-02-22T21:07:53.7966033Z go: downloading github.com/mattn/go-zglob v0.0.2-0.20190814121620-e3c945676326
2022-02-22T21:07:53.7978827Z go: downloading github.com/hashicorp/go-getter v1.5.9
2022-02-22T21:07:53.8050848Z go: downloading golang.org/x/net v0.0.0-20210614182718-04defd469f4e
2022-02-22T21:07:53.8547511Z go: downloading golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a
2022-02-22T21:07:54.0163827Z go: downloading github.com/hashicorp/errwrap v1.0.0
2022-02-22T21:07:54.0221065Z go: downloading github.com/agext/levenshtein v1.2.3
2022-02-22T21:07:54.0273768Z go: downloading github.com/apparentlymart/go-textseg/v13 v13.0.0
2022-02-22T21:07:54.0388645Z go: downloading github.com/mitchellh/go-wordwrap v1.0.1
2022-02-22T21:07:54.0609826Z go: downloading github.com/davecgh/go-spew v1.1.1
2022-02-22T21:07:54.0674846Z go: downloading github.com/pmezard/go-difflib v1.0.0
2022-02-22T21:07:54.0763630Z go: downloading gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
2022-02-22T21:07:54.0934027Z go: downloading golang.org/x/text v0.3.6
2022-02-22T21:07:54.1337434Z go: downloading github.com/hashicorp/go-version v1.3.0
2022-02-22T21:07:54.2195798Z go: downloading cloud.google.com/go/storage v1.10.0
2022-02-22T21:07:54.2690706Z go: downloading cloud.google.com/go v0.83.0
2022-02-22T21:07:54.6238127Z go: downloading github.com/aws/aws-sdk-go v1.40.56
2022-02-22T21:07:57.6060976Z go: downloading github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d
2022-02-22T21:07:57.6077708Z go: downloading github.com/hashicorp/go-cleanhttp v0.5.2
2022-02-22T21:07:57.6104925Z go: downloading github.com/hashicorp/go-safetemp v1.0.0
2022-02-22T21:07:57.6146984Z go: downloading github.com/klauspost/compress v1.13.0
2022-02-22T21:07:57.6177368Z go: downloading github.com/mitchellh/go-homedir v1.1.0
2022-02-22T21:07:57.6209543Z go: downloading github.com/mitchellh/go-testing-interface v1.0.0
2022-02-22T21:07:57.6238988Z go: downloading github.com/ulikunitz/xz v0.5.8
2022-02-22T21:07:57.6740433Z go: downloading google.golang.org/api v0.47.0
2022-02-22T21:07:58.1498920Z go: downloading github.com/googleapis/gax-go/v2 v2.0.5
2022-02-22T21:08:00.4737031Z go: downloading google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c
2022-02-22T21:08:01.8419737Z go: downloading github.com/golang/snappy v0.0.3
2022-02-22T21:08:01.8551562Z go: downloading google.golang.org/grpc v1.38.0
2022-02-22T21:08:02.0319060Z go: downloading go.opencensus.io v0.23.0
2022-02-22T21:08:02.0336299Z go: downloading golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c
2022-02-22T21:08:02.1075867Z go: downloading github.com/golang/protobuf v1.5.2
2022-02-22T21:08:02.1091730Z go: downloading google.golang.org/protobuf v1.26.0
2022-02-22T21:08:02.3364081Z go: downloading github.com/jmespath/go-jmespath v0.4.0
2022-02-22T21:08:02.3539663Z go: downloading github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e
2022-02-22T21:08:02.3982713Z go: downloading golang.org/x/sys v0.0.0-20210603125802-9665404d3644
2022-02-22T21:08:34.8364583Z === RUN   TestDefaultsExampleWithOnlyRequiredInputs
2022-02-22T21:08:34.8370699Z TestDefaultsExampleWithOnlyRequiredInputs 2022-02-22T21:08:34Z retry.go:91: terraform [destroy -auto-approve -input=false -var required_name=RepoName -lock=false]
2022-02-22T21:08:34.8372011Z TestDefaultsExampleWithOnlyRequiredInputs 2022-02-22T21:08:34Z logger.go:66: Running command terraform with args [destroy -auto-approve -input=false -var required_name=RepoName -lock=false]
2022-02-22T21:08:36.5999664Z TestDefaultsExampleWithOnlyRequiredInputs 2022-02-22T21:08:36Z logger.go:66: [command]/home/runner/work/_temp/495e7d4c-29c8-433b-8a24-9d58dfd5532c/terraform-bin destroy -auto-approve -input=false -var required_name=RepoName -lock=false
2022-02-22T21:08:36.6636591Z TestDefaultsExampleWithOnlyRequiredInputs 2022-02-22T21:08:36Z logger.go:66: ╷
2022-02-22T21:08:36.6638343Z TestDefaultsExampleWithOnlyRequiredInputs 2022-02-22T21:08:36Z logger.go:66: │ Error: Module not installed
2022-02-22T21:08:36.6639349Z TestDefaultsExampleWithOnlyRequiredInputs 2022-02-22T21:08:36Z logger.go:66: │ 
2022-02-22T21:08:36.6640691Z TestDefaultsExampleWithOnlyRequiredInputs 2022-02-22T21:08:36Z logger.go:66: │   on main.tf line 6:
2022-02-22T21:08:36.6641861Z TestDefaultsExampleWithOnlyRequiredInputs 2022-02-22T21:08:36Z logger.go:66: │    6: module "default" {
2022-02-22T21:08:36.6642810Z TestDefaultsExampleWithOnlyRequiredInputs 2022-02-22T21:08:36Z logger.go:66: │ 
2022-02-22T21:08:36.6643989Z TestDefaultsExampleWithOnlyRequiredInputs 2022-02-22T21:08:36Z logger.go:66: │ This module is not yet installed. Run "terraform init" to install all
2022-02-22T21:08:36.6645283Z TestDefaultsExampleWithOnlyRequiredInputs 2022-02-22T21:08:36Z logger.go:66: │ modules required by this configuration.
2022-02-22T21:08:36.6646295Z TestDefaultsExampleWithOnlyRequiredInputs 2022-02-22T21:08:36Z logger.go:66: ╵
2022-02-22T21:08:36.6704464Z TestDefaultsExampleWithOnlyRequiredInputs 2022-02-22T21:08:36Z logger.go:66: ::debug::Terraform exited with code 1.
2022-02-22T21:08:36.6705782Z TestDefaultsExampleWithOnlyRequiredInputs 2022-02-22T21:08:36Z logger.go:66: ::debug::stdout: 
2022-02-22T21:08:36.6711031Z TestDefaultsExampleWithOnlyRequiredInputs 2022-02-22T21:08:36Z logger.go:66: ::debug::stderr: ╷%0A│ Error: Module not installed%0A│ %0A│   on main.tf line 6:%0A│    6: module "default" {%0A│ %0A│ This module is not yet installed. Run "terraform init" to install all%0A│ modules required by this configuration.%0A╵%0A
2022-02-22T21:08:36.6713274Z TestDefaultsExampleWithOnlyRequiredInputs 2022-02-22T21:08:36Z logger.go:66: ::debug::exitcode: 1
2022-02-22T21:08:36.6715954Z TestDefaultsExampleWithOnlyRequiredInputs 2022-02-22T21:08:36Z logger.go:66: ::set-output name=stdout::
2022-02-22T21:08:36.6765829Z TestDefaultsExampleWithOnlyRequiredInputs 2022-02-22T21:08:36Z logger.go:66: ::set-output name=stderr::╷%0A│ Error: Module not installed%0A│ %0A│   on main.tf line 6:%0A│    6: module "default" {%0A│ %0A│ This module is not yet installed. Run "terraform init" to install all%0A│ modules required by this configuration.%0A╵%0A
2022-02-22T21:08:36.6767144Z TestDefaultsExampleWithOnlyRequiredInputs 2022-02-22T21:08:36Z logger.go:66: ::set-output name=exitcode::1
2022-02-22T21:08:36.6768412Z TestDefaultsExampleWithOnlyRequiredInputs 2022-02-22T21:08:36Z logger.go:66: ::error::Terraform exited with code 1.
2022-02-22T21:08:36.6856451Z TestDefaultsExampleWithOnlyRequiredInputs 2022-02-22T21:08:36Z retry.go:99: Returning due to fatal error: FatalError{Underlying: error while running command: exit status 1; }
2022-02-22T21:08:36.6866062Z     destroy.go:11: 
2022-02-22T21:08:36.6866806Z            Error Trace:    destroy.go:11
2022-02-22T21:08:36.6867672Z                                        defaults_example_test.go:35
2022-02-22T21:08:36.6869236Z            Error:          Received unexpected error:
2022-02-22T21:08:36.6870551Z                            FatalError{Underlying: error while running command: exit status 1; }
2022-02-22T21:08:36.6871344Z            Test:           TestDefaultsExampleWithOnlyRequiredInputs
2022-02-22T21:08:36.6872263Z --- FAIL: TestDefaultsExampleWithOnlyRequiredInputs (1.85s)
2022-02-22T21:08:36.6872650Z FAIL
2022-02-22T21:08:36.6873194Z FAIL   test    1.859s
2022-02-22T21:08:36.6895218Z FAIL

CI Image

image

Analysis

You can see in the resources above the output appears twice, the second with ::debug::stdout:. Is there a way to suppress this output?

Thanks for any assistance

denis256 commented 2 years ago

Hi, I think %0A artifacts are from the wrong interpretation of color output - try to run with terraform options NoColor: true, NoStderr: true, additionally can be tried to define env variable TF_CLI_ARGS="-no-color".

Repeated outputs I think appear because of executed retries - intermediary outputs can be reduced if set Logger: logger.Discard on terraform.Options