Terratest is a Go library that makes it easier to write automated tests for your infrastructure code.

Terratest

Maintained by Gruntwork.io CircleCI Go Report Card go.dev reference go.mod version

Terratest is a Go library that makes it easier to write automated tests for your infrastructure code. It provides a variety of helper functions and patterns for common infrastructure testing tasks, including:

  • Testing Terraform code
  • Testing Packer templates
  • Testing Docker images
  • Executing commands on servers over SSH
  • Working with AWS APIs
  • Working with Azure APIs
  • Working with GCP APIs
  • Working with Kubernetes APIs
  • Testing Helm Charts
  • Making HTTP requests
  • Running shell commands
  • And much more

Please see the following for more info:

License

This code is released under the Apache 2.0 License. Please see LICENSE and NOTICE for more details.

Copyright © 2020 Gruntwork, Inc.

Owner
Gruntwork
Your entire infrastructure. Defined as code. In about a day.
Gruntwork
Comments
  • Try to stabilize tests; fix terraform.Options Clone method.

    Try to stabilize tests; fix terraform.Options Clone method.

    For some reason, as of a couple days ago, some of the most basic Terratest tests have started to fail.

    For example, in this test run, we saw the following failures:

    • TestTerraformHttpExample and TestTerraformSshExample: The logs show the same sort of error:

      TestTerraformHttpExample 2020-10-30T09:07:06Z logger.go:66: aws_instance.example: Creating...
      TestTerraformHttpExample 2020-10-30T09:07:16Z logger.go:66: aws_instance.example: Still creating... [10s elapsed]
      TestTerraformHttpExample 2020-10-30T09:07:22Z logger.go:66: 
      TestTerraformHttpExample 2020-10-30T09:07:22Z logger.go:66: Error: Error waiting for instance (i-06fd6e62f3a53fe39) to become ready: Failed to reach target state. Reason: Client.InternalError: Client error on launch
      

      AWS docs say this has to do with KMS or EBS volume permissions, but this example uses neither one!

    • TestTerraformRemoteExecExample: The logs show:

      TestTerraformRemoteExecExample 2020-10-30T09:07:47Z logger.go:66: aws_instance.example_public: Creation complete after 33s [id=i-08487e48c4080c63b]
      TestTerraformRemoteExecExample 2020-10-30T09:07:51Z logger.go:66: null_resource.example_provisioner: Creating...
      TestTerraformRemoteExecExample 2020-10-30T09:07:51Z logger.go:66: null_resource.example_provisioner: Provisioning with 'file'...
      TestTerraformRemoteExecExample 2020-10-30T09:07:51Z logger.go:66: 
      TestTerraformRemoteExecExample 2020-10-30T09:07:53Z logger.go:66: 
      TestTerraformRemoteExecExample 2020-10-30T09:07:53Z logger.go:66: Error: SSH agent requested but SSH_AUTH_SOCK not-specified
      TestTerraformRemoteExecExample 2020-10-30T09:07:53Z logger.go:66: 
      TestTerraformRemoteExecExample 2020-10-30T09:07:53Z logger.go:66: 
      TestTerraformRemoteExecExample 2020-10-30T09:07:53Z retry.go:99: Returning due to fatal error: FatalError{Underlying: error while running command: exit status 1; 
      Error: SSH agent requested but SSH_AUTH_SOCK not-specified
      
      }
      

      Not sure what this is yet. Will need to look into it.

    • TestGetCapacityInfoForAsg: The logs give almost no help:

      === RUN   TestGetCapacityInfoForAsg
      === PAUSE TestGetCapacityInfoForAsg
      === CONT  TestGetCapacityInfoForAsg
      TestGetCapacityInfoForAsg 2020-10-30T08:53:24Z region.go:91: Using region sa-east-1
      --- FAIL: TestGetCapacityInfoForAsg (20.19s)
      
  • using a interface instead of *testing.T

    using a interface instead of *testing.T

    I would like to use terratest with ginkgo. Because most of the terratest functions expect a *testing.T object, I thought I could use GinkgoT() however this doesn't work because terratest doesn't use an interface but uses the concrete impl.

    This PR attempts to fix that by creating a testing.TestingT template that is then used throughout the project instead of *testing.T object.

  • Terraform Apply hang unresponsive

    Terraform Apply hang unresponsive

    Terraform Project Structure:

    -- terraform-root
    ---- main.tf
    ---- variables.tf
    ---- outputs.tf
    ---- test/
    -------- basic_test.go
    ---- examples/
    -------- elasticsearch-auth-iam-user/main.tf
    

    my basic_test.go looks like the following

    package test
    
    import (
        "fmt"
        "testing"
        "github.com/gruntwork-io/terratest/modules/terraform"
    )
    
    func TestTerraformBasicExample(t *testing.T) {
    
    	terraformOptions := &terraform.Options{
    		TerraformDir: "../ examples/elasticsearch-auth-iam-user",
    
    		Vars: map[string]interface{}{
                            "domain_name": "test",
                	        "stack": "elasticsearch",
                            "workspace": "stg",
    		},
    	}
    
    	defer terraform.Destroy(t, terraformOptions)
    	terraform.InitAndApply(t, terraformOptions)
    }
    

    cd into test run "go test -timeout 5m".

    I get "terraform init" succeeded but "terraform apply" hanged without any outputs until timing out.

    Log looks like

    [ec2-user@ip-10-104-88-81 test]$ go test -timeout 5m
    init...................TestTerraformBasicExample 2018-08-20T10:37:20Z retry.go:69: Running terraform [init -upgrade=false]
    TestTerraformBasicExample 2018-08-20T10:37:20Z command.go:52: Running command terraform with args [init -upgrade=false]
    TestTerraformBasicExample 2018-08-20T10:37:20Z command.go:96: Initializing modules...
    TestTerraformBasicExample 2018-08-20T10:37:20Z command.go:96: - module.elasticsearch_security_group
    TestTerraformBasicExample 2018-08-20T10:37:20Z command.go:96:   Getting source "git::ssh://[email protected]/iot-infrastructure/tf-module-aws-security-group.git?ref=master"
    TestTerraformBasicExample 2018-08-20T10:37:21Z command.go:96:
    TestTerraformBasicExample 2018-08-20T10:37:21Z command.go:96: Initializing provider plugins...
    TestTerraformBasicExample 2018-08-20T10:37:21Z command.go:96: - Checking for available provider plugins on https://releases.hashicorp.com...
    TestTerraformBasicExample 2018-08-20T10:37:21Z command.go:96: - Downloading plugin for provider "aws" (1.32.0)...
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:96:
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:96: The following providers do not have any version constraints in configuration,
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:96: so the latest version was installed.
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:96:
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:96: To prevent automatic upgrades to new major versions that may contain breaking
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:96: changes, it is recommended to add version = "..." constraints to the
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:96: corresponding provider blocks in configuration, with the constraint strings
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:96: suggested below.
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:96:
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:96: * provider.aws: version = "~> 1.32"
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:96:
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:96: Terraform has been successfully initialized!
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:96:
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:96: You may now begin working with Terraform. Try running "terraform plan" to see
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:96: any changes that are required for your infrastructure. All Terraform commands
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:96: should now work.
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:96:
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:96: If you ever set or change modules or backend configuration for Terraform,
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:96: rerun this command to reinitialize your working directory. If you forget, other
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:96: commands will detect it and remind you to do so if necessary.
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:100: 2018/08/20 10:37:20 [INFO] Terraform version: 0.11.7  41e50bd32a8825a84535e353c3674af8ce799161
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:100: 2018/08/20 10:37:20 [INFO] Go runtime version: go1.10.1
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:100: 2018/08/20 10:37:20 [INFO] CLI args: []string{"/usr/local/bin/terraform", "init", "-upgrade=false"}
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:100: 2018/08/20 10:37:20 [DEBUG] Attempting to open CLI config file: /home/ec2-user/.terraformrc
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:100: 2018/08/20 10:37:20 [DEBUG] File doesn't exist, but doesn't need to. Ignoring.
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:100: 2018/08/20 10:37:20 [INFO] CLI command args: []string{"init", "-upgrade=false"}
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:100: 2018/08/20 10:37:20 [DEBUG] command: loading backend config file: /home/ec2-user/go/src/tf-module-aws-elasticsearch
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:100: 2018/08/20 10:37:20 [ERR] Checkpoint error: Get https://checkpoint-api.hashicorp.com/v1/check/terraform?arch=386&os=linux&signature=1876a657-05db-8573-ee03-ee17a78287f6&version=0.11.7: Forbidden
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:100: 2018/08/20 10:37:21 [DEBUG] found "git::ssh://[email protected]/iot-infrastructure/tf-module-aws-security-group.git?ref=master" in ".terraform/modules/30207133dde280c8832b06615f77d133": true
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:100: 2018/08/20 10:37:21 [TRACE] "git::ssh://[email protected]/iot-infrastructure/tf-module-aws-security-group.git?ref=master" stored in ".terraform/modules/30207133dde280c8832b06615f77d133"
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:100: 2018/08/20 10:37:21 [INFO] command: empty terraform config, returning nil
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:100: 2018/08/20 10:37:21 [DEBUG] command: no data state file found for backend config
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:100: 2018/08/20 10:37:21 [DEBUG] New state was assigned lineage "33329760-d197-8ad8-7d93-841dc70d9b45"
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:100: 2018/08/20 10:37:21 [INFO] command: backend initialized: <nil>
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:100: 2018/08/20 10:37:21 [DEBUG] checking for provider in "."
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:100: 2018/08/20 10:37:21 [DEBUG] checking for provider in "/usr/local/bin"
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:100: 2018/08/20 10:37:21 [DEBUG] checking for provisioner in "."
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:100: 2018/08/20 10:37:21 [DEBUG] checking for provisioner in "/usr/local/bin"
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:100: 2018/08/20 10:37:21 [INFO] Failed to read plugin lock file .terraform/plugins/linux_386/lock.json: open .terraform/plugins/linux_386/lock.json: no such file or directory
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:100: 2018/08/20 10:37:21 [INFO] command: backend <nil> is not enhanced, wrapping in local
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:100: 2018/08/20 10:37:21 [DEBUG] checking for provider in "."
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:100: 2018/08/20 10:37:21 [DEBUG] checking for provider in "/usr/local/bin"
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:100: 2018/08/20 10:37:21 [DEBUG] plugin requirements: "aws"=""
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:100: 2018/08/20 10:37:21 [DEBUG] fetching provider info for aws version 1.32.0
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:100: 2018/08/20 10:37:21 [DEBUG] getting provider "aws" version "1.32.0"
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:100: 2018/08/20 10:37:21 [DEBUG] plugin cache is disabled, so downloading aws 1.32.0 from https://releases.hashicorp.com/terraform-provider-aws/1.32.0/terraform-provider-aws_1.32.0_linux_386.zip?checksum=sha256:b9b35d1040c7d764e7dc0a1083826f8d9b63f49f46a53ee521c6c6c138130d64
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:100: 2018/08/20 10:37:22 [DEBUG] looking for the aws 1.32.0 plugin we just installed
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:100: 2018/08/20 10:37:22 [DEBUG] checking for provider in ".terraform/plugins/linux_386"
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:100: 2018/08/20 10:37:22 [DEBUG] found provider "terraform-provider-aws_v1.32.0_x4"
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:100: 2018/08/20 10:37:22 [DEBUG] all plugins found discovery.PluginMetaSet{discovery.PluginMeta{Name:"aws", Version:"1.32.0", Path:"/home/ec2-user/go/src/tf-module-aws-elasticsearch/.terraform/plugins/linux_386/terraform-provider-aws_v1.32.0_x4"}:struct {}{}}
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:100: 2018/08/20 10:37:22 [DEBUG] filtered plugins discovery.PluginMetaSet{discovery.PluginMeta{Name:"aws", Version:"1.32.0", Path:"/home/ec2-user/go/src/tf-module-aws-elasticsearch/.terraform/plugins/linux_386/terraform-provider-aws_v1.32.0_x4"}:struct {}{}}
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:100: 2018/08/20 10:37:22 [DEBUG] checking for provider in "."
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:100: 2018/08/20 10:37:22 [DEBUG] checking for provider in "/usr/local/bin"
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:100: 2018/08/20 10:37:22 [DEBUG] checking for provider in ".terraform/plugins/linux_386"
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:100: 2018/08/20 10:37:22 [DEBUG] found provider "terraform-provider-aws_v1.32.0_x4"
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:100: 2018/08/20 10:37:22 [DEBUG] found valid plugin: "aws", "1.32.0", "/home/ec2-user/go/src/tf-module-aws-elasticsearch/.terraform/plugins/linux_386/terraform-provider-aws_v1.32.0_x4"
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:100: 2018/08/20 10:37:23 [DEBUG] checking for provider in ".terraform/plugins/linux_386"
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:100: 2018/08/20 10:37:23 [DEBUG] found provider "terraform-provider-aws_v1.32.0_x4"
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:100: 2018/08/20 10:37:23 [DEBUG] plugin: waiting for all plugin processes to complete...
    apply...................TestTerraformBasicExample 2018-08-20T10:37:23Z retry.go:69: Running terraform [apply -input=false -lock=false -auto-approve -var access_policies="a" -var domain_name="test" -var stack="elasticsearch" -var workspace="stg"]
    TestTerraformBasicExample 2018-08-20T10:37:23Z command.go:52: Running command terraform with args [apply -input=false -lock=false -auto-approve -var access_policies="a" -var domain_name="test" -var stack="elasticsearch" -var workspace="stg"]
    

    main.tf looks like

    # If comment out this module usage, terratest works
    /*
    module "elasticsearch_security_group" {
      source = "git::ssh://[email protected]/iot-infrastructure/tf-module-aws-security-group.git?ref=master"
      name = "elasticsearch_security_group"
      vpc_id = "${var.vpc_id}"
      ingress_cidr_blocks = ["0.0.0.0/0"]
    }
    */
    resource "aws_elasticsearch_domain" "es_test" {
      domain_name           = "test"
    
      cluster_config {
        instance_type = "r3.large.elasticsearch"
        instance_count = "1"
        dedicated_master_enabled = "false"
        zone_awareness_enabled = "false"
      }
    }
    
    # meta parameters for module
    
    variable "vpc_id" {
      type = "string"
      default = "vpc-42f2792a"
    }
    
    # Provider
    provider "aws" {
      region = "eu-central-1"
    }
    

    If I comment module "elasticsearch_security_group" out. It works. Can it possible terratest has problem with module ?

  • 576 Add methods to check SSH connections/commands with retry

    576 Add methods to check SSH connections/commands with retry

    Early PR for feedback on approach for addressing #576

    @rhoboat, I'm working on the helper methods you suggested in 576 and I want to make sure this is an approach you are comfortable with.

    I wrote CheckSshConnectionWithRetry and CheckSshConnectionWithRetryE to accept an optional function. They default to wrapping CheckSshConnectionE, but the option to inject means we can swap it for a mock function and unit test much easier. My worry is that the interface might not be as clear as it could be to users. What do you think?

    If this looks good, I'll move on to CheckSshCommandWithRetry and CheckSshCommandWithRetryE. Thanks!

  • Kubernetes Secret helper method

    Kubernetes Secret helper method

    I create a helper method for situations where a k8s secret is created asynchronously, for example, by a certificate issuer like letsencrypt. We use this method at work and it has been very useful for our e2e infrastructure tests. I assume this would be useful for others, but I will understand if it violates the "bloating the Terratest API" criteria.

  • Add support for ListOfObjects and MapOfObjects outputs from terraform.

    Add support for ListOfObjects and MapOfObjects outputs from terraform.

    I needed support for a more complex output structure than only Lists or only Maps. So I added an output function for lists of maps. I believe this could close https://github.com/gruntwork-io/terratest/issues/300 but am not sure what the requirements were for that issue.

    I read the instructions at https://terratest.gruntwork.io/docs/community/contributing/ but couldn't find information on running the tests so here are the outputs from the test cases I created:

    https://gist.github.com/deberon/4e32f0bea2a26c5be66d4fc47a479bad

  • modules/terraform/var-file.go:142:8: undefined: hcl.Decode

    modules/terraform/var-file.go:142:8: undefined: hcl.Decode

    Latest release is causing an error:

    vendor/github.com/gruntwork-io/terratest/modules/terraform/var-file.go:142:8: undefined: hcl.Decode
    FAIL	git.*******.com/mongodbatlas-tf/terratest/mongodbatlas-cluster [build failed]
    FAIL
    
    Exited with code exit status 2
    

    https://github.com/gruntwork-io/terratest/blob/88ba3f070a939aca8351eae616a3c899bfe28b91/modules/terraform/var-file.go#L142

  • adding function to extract terraform vars from var file

    adding function to extract terraform vars from var file

    This PR adds the following functionality to the terraform Options module:

    • Exposes a function that allows you to get a map of all the variables contained in each terraform VarFile specified in a terraform options object. This would allow you to reference these easily later on in a test for comparison, etc.

    This is my first time contributing to this repo, so I tried to follow the style guides and everything to the best of my ability. Any feedback would be greatly appreciated!

  • Kubernetes Load Balancer type of service is not being recognized by Terratest tests

    Kubernetes Load Balancer type of service is not being recognized by Terratest tests

    I have a test that creates a Kubernetes deployment and a service of LoadBalancer type. The test then looks for this service to execute an HTTP request. The test keeps waiting for the service to be provisioned, even though the service is up and running and then eventually fails. I executed a curl request on the service and it returned a successful response. On changing the service type from LoadBalancer to ClusterIP, leads to a success result. I had logged the same bug a couple of months ago and it had worked perfectly for version 0.23.5. I pulled the newest Terratest version (0.26.1) yesterday and suddenly this issue cropped up. Am I missing any step here? I am new to both Terratest and Go, so I am not sure if I am following the right steps. I pulled the latest Terratest version using the command go get -d -v github.com/gruntwork-io/[email protected]. The content of the go.mod file is as shown below:

    module Terratest
    
    go 1.13
    
    require (
            github.com/go-sql-driver/mysql v1.4.1
            github.com/gruntwork-io/terratest v0.26.1
    )
    

    The last time, all I had done was to run the go get command with version 0.23.5 and it had started to work.

  • Commercial support subpage, footer edits, navbar edits, contact form

    Commercial support subpage, footer edits, navbar edits, contact form

    Hello, we've added:

    • a new subpage - /commercial support
    • new subpage - /contact
    • misc edits like navbar & footer link, 'new' badge etc The contact form is linked to the formbucket. We've created a new bucket for test purposes.
  • tunnel.go from k8s package seems to be broken

    tunnel.go from k8s package seems to be broken

    One of the imports from "https://github.com/gruntwork-io/terratest/blob/master/modules/k8s/tunnel.go" is "k8s.io/kubernetes/pkg/kubectl/generate". But the "generate" package seems to be moved to staging by kubernetes folks and this dependent package is not coming while building terratest code in our project. This has broken the terratest. Can you provide any solution for this?

  • Scicoria/add eventhub mod

    Scicoria/add eventhub mod

    Description

    Fixes #000.

    TODOs

    Read the Gruntwork contribution guidelines.

    • [ ] Update the docs.
    • [ ] Run the relevant tests successfully, including pre-commit checks.
    • [ ] Ensure any 3rd party code adheres with our license policy or delete this line if its not applicable.
    • [ ] Include release notes. If this PR is backward incompatible, include a migration guide.

    Release Notes (draft)

    Added / Removed / Updated [X].

    Migration Guide

  •  Error: Backend initialization required, please run

    Error: Backend initialization required, please run "terraform init"

    I use S3 as my backend, in my terraform codebase, please be easy with me because I'm starting to learn terratest. So, I use below code base for my terratest

    func TestApiGateway(t *testing.T) {
        t.Parallel()
        awsRegion := "us-east-1"
        uniqueId := random.UniqueId()
        // Create an S3 bucket where we can store state
        bucketName := fmt.Sprintf("test-terraform-backend-example-%s", strings.ToLower(uniqueId))
        defer cleanupS3Bucket(t, awsRegion, bucketName)
        aws.CreateS3Bucket(t, awsRegion, bucketName)
    
        key := fmt.Sprintf("%s/terraform.tfstate", uniqueId)
        secretKey := "GXLbU7+igRxRHubTmJ45F"
        accessKey := "I6Q2SCYX"
        token := "IQoJb3JpZ2luX2VjEAkaCXVz"
    
    	terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
    	  TerraformDir: "../../",
          // Environment variables to set when running Terraform
          EnvVars: map[string]string{
              "AWS_DEFAULT_REGION": awsRegion,
              "AWS_ACCESS_KEY_ID": accessKey,
              "AWS_SECRET_ACCESS_KEY": secretKey,
             "AWS_SESSION_TOKEN": token,
          },
    Vars: map[string]interface{}{
                "token": token,
                "region": awsRegion,
                "access_key": accessKey,
                "secret_key": secretKey,
          },
          BackendConfig: map[string]interface{}{
            "bucket": bucketName,
            "key":    key,
            "region": awsRegion,
           },
    	})
    	defer terraform.Destroy(t, terraformOptions)
    	terraform.InitAndApply(t, terraformOptions) 
    stageUrl := terraform.Output(t, terraformOptions, "deployment_invoke_url")
    	time.Sleep(30 * time.Second)
    	statusCode := DoGetRequest(t, stageUrl)
    	assert.Equal(t, 200, statusCode)
    }
    
    func DoGetRequest(t *testing.T, api string) int {
    	resp, err := http.Get(api)
    	if err != nil {
    		log.Fatalln(err)
    	}
    	//We Read the response status on the line below.
    	return resp.StatusCode
    }
    

    As per https://github.com/gruntwork-io/terratest/blob/master/modules/terraform/options.go EnvVars map[string]string // Environment variables to set when running Terraform
    EnvVars sets up the environment variables prior running the terraform. But I'm getting below error:

    Error finding AWS credentials. Did you set the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables or configure an AWS profile? Underlying error: NoCredentialProviders: no valid providers in chain. Deprecated.
                                            For verbose messaging see aws.Config.CredentialsChainVerboseErrors
    

    my backend.tf lookslike this

    terraform {
      backend "s3" {}
    }
    terraform {
      required_providers {
        aws = {
          source  = "hashicorp/aws"
          version = "~> 4.47.0"
        }
      }
    }   
    provider "aws" {
      region     = var.aws_region
      access_key = var.access_key
      secret_key = var.secret_key
      token      = var.token
    }
    

    So as a workaround I use the export command,

    export AWS_SECRET_ACCESS_KEY="actual_secret_access_key"
    export AWS_ACCESS_KEY_ID="actual_access_key"
    export AWS_SESSION_TOKEN="actual_token"
    

    then I'm getting

     Error: Backend initialization required, please run "terraform init"
    TestApiGateway 2023-01-08T18:03:46+01:00 logger.go:66: │ 
    TestApiGateway 2023-01-08T18:03:46+01:00 logger.go:66: │ Reason: Initial configuration of the requested backend "s3"
    TestApiGateway 2023-01-08T18:03:46+01:00 logger.go:66: │ 
    TestApiGateway 2023-01-08T18:03:46+01:00 logger.go:66: │ The "backend" is the interface that Terraform uses to store state,
    TestApiGateway 2023-01-08T18:03:46+01:00 logger.go:66: │ perform operations, etc. If this message is showing up, it means that the
    TestApiGateway 2023-01-08T18:03:46+01:00 logger.go:66: │ Terraform configuration you're using is using a custom configuration for
    TestApiGateway 2023-01-08T18:03:46+01:00 logger.go:66: │ the Terraform backend.
    
  • add `pod.Status.{Reason,Message}` to `PodNotAvailable` error

    add `pod.Status.{Reason,Message}` to `PodNotAvailable` error

    Signed-off-by: Charly Molter [email protected]

    When writing tests using terratest it's useful to figure why a pod failed to start.

    For example we use heavily terratest in our e2e suite. Some tests are flaky for many possible reasons at the moment we have to switch to using: WaitUntilPodAvailableE and use the error to get the pod status.

    Fix #1222

    Add extra info in the error message.

    Read the Gruntwork contribution guidelines.

    • [x] Update the docs. -- No docs for this
    • [ ] Run the relevant tests successfully, including pre-commit checks. -- Trying to figure out how to do this
    • [x] Ensure any 3rd party code adheres with our license policy or delete this line if its not applicable.
    • [x] Include release notes. If this PR is backward incompatible, include a migration guide.

    Release Notes (draft)

    Added / Removed / Updated [X].

    Added context to PodNotAvailable error to make troubleshooting easier.

    Migration Guide

  • More context on error on `WaitUntilPodAvailable`

    More context on error on `WaitUntilPodAvailable`

    When writing tests it's useful to figure why a pod failed to start.

    For example we use heavily terratest in our e2e suite on kumahq/kuma. Some tests are flaky for many possible reasons. When investigating them we have to switch to using: WaitUntilPodAvailableE and use the error to get the pod again to extract the status. Something like:

    err := WaitUntilPodAvailableE(...)
    if err, ok := err.(k8s.PodNotAvailable); ok {
       p := GetPod(...)
       Logf("bad status", p.status.Message, p.status.Status)
    }
    require.NoError(err)
    

    Not only there's a race here (possibly the pod gets healthy right after failing but it's also not super easy.

    2 options:

    1. add pod.Status.{Reason,Message} to PodNotAvailable error
    2. make Pod in PodNotAvailable accessible outside the package.

    I have a preference for 1.

  • terratest_log_parser problems after upgrading GO from 1.13 to 1.19

    terratest_log_parser problems after upgrading GO from 1.13 to 1.19

    Hi,

    Recently in our Terratest project we upgraded GO from 1.13 to 1.19 and stopped seeing test failure details in parsed log files. Parsing still works on old branches using 1.13 so I am pretty confident that the issue is due to GO upgrade.

    FTR we are using Terratest v0.35.3 and stretchr/testify v1.5.1, we can upgrade if that helps but I checked the release notes and haven't found any fixes that would be related to this problem.

    Below some analysis that I did on the build log and parsed output:

    GO 1.13 full build log:

    [03:40:01] : [Step 1/1] TestClusterScaleOut 2022-12-30T03:40:01Z logger.go:66: Destroy complete! Resources: 37 destroyed. [0m [03:40:01] : [Step 1/1] --- FAIL: TestClusterScaleOut (5811.77s) [03:40:01] : [Step 1/1] cluster_scale_out_test.go:611: [03:40:01] : [Step 1/1] Error Trace: cluster_scale_out_test.go:611 [03:40:01] : [Step 1/1] cluster_scale_out_test.go:366 [03:40:01] : [Step 1/1] Error: Not equal: [03:40:01] : [Step 1/1] expected: 1442 [03:40:01] : [Step 1/1] actual : 1441 [03:40:01] : [Step 1/1] Test: TestClusterScaleOut [03:40:02] : [Step 1/1] TestMSKACLsWithSentinel/TestCases/TestRemoveACLs 2022-12-30T03:40:02Z logger.go:66: [0m ... [04:58:00]F: [stash.guidewire.com/CCS/msk/modules/cluster/test] test.TestClusterScaleOut [04:58:00]F: [test.TestClusterScaleOut] Failed [04:58:00]F: [test.TestClusterScaleOut] Error Trace: cluster_scale_out_test.go:611 cluster_scale_out_test.go:366 Error: Not equal: expected: 1442 actual : 1441 Test: TestClusterScaleOut

    This produces following output in TestClusterScaleOut.log:

    TestClusterScaleOut 2022-12-30T03:40:01Z logger.go:66: Destroy complete! Resources: 37 destroyed. --- FAIL: TestClusterScaleOut (5811.77s) --- FAIL: TestClusterScaleOut (5811.77s) cluster_scale_out_test.go:611: Error Trace: cluster_scale_out_test.go:611 cluster_scale_out_test.go:366 Error: Not equal: expected: 1442 actual : 1441 Test: TestClusterScaleOut

    Note that the error message is printed once in failure details (in build log it is printed twice) which is not perfect but good enough to avoid looking into full build log

    Now the full build log for GO 1.19

    [07:18:38] : [Step 1/1] TestClusterScaleOut 2023-01-05T07:18:38Z commons.go:370: command 'xxxx [07:18:38] : [Step 1/1] S ... [07:18:38] : [Step 1/1] S [07:18:38] : [Step 1/1] Processed a total of 1261 messages [07:18:38] : [Step 1/1] ' [07:18:38] : [Step 1/1] === CONT TestClusterScaleOut [07:18:38] : [Step 1/1] cluster_scale_out_test.go:611: [07:18:38] : [Step 1/1] Error Trace: cluster_scale_out_test.go:611 [07:18:38] : [Step 1/1] cluster_scale_out_test.go:366 [07:18:38] : [Step 1/1] Error: Not equal: [07:18:38] : [Step 1/1] expected: 1262 [07:18:38] : [Step 1/1] actual : 1261 [07:18:38] : [Step 1/1] Test: TestClusterScaleOut [07:18:38] : [Step 1/1] TestClusterScaleOut 2023-01-05T07:18:38Z commons.go:326: xxx ... [07:33:03] : [Step 1/1] TestClusterScaleOut 2023-01-05T07:33:03Z logger.go:66: Destroy complete! Resources: 37 destroyed. [0m [07:33:03] : [Step 1/1] --- FAIL: TestClusterScaleOut (5399.05s) [07:33:07] : [Step 1/1] TestPatchClusterVersion 2023-01-05T07:33:07Z logger.go:66: [0m [1mmodule.basic-setup.module.msk.aws_msk_cluster.msk_cluster: Still modifying... [id=arn:aws:xxx, 1h4m10s elapsed] [0m [0m ... [08:53:43]F: [stash.guidewire.com/CCS/msk/modules/cluster/test] test.TestClusterScaleOut [08:53:43]F: [test.TestClusterScaleOut] Failed [08:53:43]F: [test.TestClusterScaleOut] cluster_scale_out_test.go:611: TestClusterScaleOut 2023-01-05T07:18:38Z commons.go:326: ... ...

    This produces following output in TestClusterScaleOut.log:

    TestClusterScaleOut 2023-01-05T07:33:03Z logger.go:66: Destroy complete! Resources: 37 destroyed. --- FAIL: TestClusterScaleOut (5399.05s) --- FAIL: TestClusterScaleOut (5399.05s)

    In this case we see only the failure message but the error context is lost. Looks like with GO 1.19 build log prints out all error details only once during the test run. As I mentioned before those error details are ignored by Terratest (see 1.13 output). When the test fails, GO 1.19 prints the line number of the error ([08:53:43]F: [test.TestClusterScaleOut] cluster_scale_out_test.go:611: ) but that is not parsed into test log, plus is not enough to figure out what is wrong without looking into full build log.

    So it seems that there are two issues:

    1. Log parser is not able to parse assertion errors in the middle of test run - this affects both GO 1.13 and GO 1.19
    2. With GO 1.19 the details of failure message are minimised and parser is not able to parse the minimal details. This leads to FAIL report without any details and we still need to go back to build log to find the details.

    I think the best fix for this would be to start parsing error messages that pop up during the test run as they provide necessary details to diagnose the root cause of the test failure and are printed both in GO 1.13 and 1.19.

    Last but not least, seems to me that this problem may already have been reported here: https://github.com/gruntwork-io/terratest/issues/1200 . I am not 100% sure if this is the same problem so decided to report separately.

Learning go with tests! Also setting up automated versioning via SemVer.

learn-go-with-tests The purpose of this repo is to complete the learn-go-with-test course located here. I am also interested in learning how to automa

Nov 23, 2021
Rr-e2e-tests - Roadrunner end-to-end tests repository
Rr-e2e-tests - Roadrunner end-to-end tests repository

RoadRunner end-to-end plugins tests License: The MIT License (MIT). Please see L

Dec 15, 2022
Go testing in the browser. Integrates with `go test`. Write behavioral tests in Go.
Go testing in the browser. Integrates with `go test`. Write behavioral tests in Go.

GoConvey is awesome Go testing Welcome to GoConvey, a yummy Go testing tool for gophers. Works with go test. Use it in the terminal or browser accordi

Dec 30, 2022
Ruby on Rails like test fixtures for Go. Write tests against a real database

testfixtures Warning: this package will wipe the database data before loading the fixtures! It is supposed to be used on a test database. Please, doub

Jan 8, 2023
Go testing in the browser. Integrates with `go test`. Write behavioral tests in Go.
Go testing in the browser. Integrates with `go test`. Write behavioral tests in Go.

GoConvey is awesome Go testing Welcome to GoConvey, a yummy Go testing tool for gophers. Works with go test. Use it in the terminal or browser accordi

Dec 30, 2022
Demo repository for Infrastructure as Code testing tools and frameworks.

Testing Infrastructure as Code Demo repository for Infrastructure as Code testing tools and frameworks. Maintainer M.-Leander Reimer (@lreimer), mario

Jan 23, 2022
Test your code without writing mocks with ephemeral Docker containers 📦 Setup popular services with just a couple lines of code ⏱️ No bash, no yaml, only code 💻

Gnomock – tests without mocks ??️ Spin up entire dependency stack ?? Setup initial dependency state – easily! ?? Test against actual, close to product

Dec 29, 2022
Record and replay your HTTP interactions for fast, deterministic and accurate tests

go-vcr go-vcr simplifies testing by recording your HTTP interactions and replaying them in future runs in order to provide fast, deterministic and acc

Dec 25, 2022
Automatically update your Go tests

autogold - automatically update your Go tests autogold makes go test -update automatically update your Go tests (golden files and Go values in e.g. fo

Dec 25, 2022
A Master list of Go Programming Tutorials, their write-ups, their source code and their current build status!
A Master list of Go Programming Tutorials, their write-ups, their source code and their current build status!

TutorialEdge TutorialEdge.net Go Tutorials ??‍?? ??‍?? Welcome to the TutorialEdge Go Repository! The goal of this repo is to be able to keep track of

Dec 18, 2022
gostub is a library to make stubbing in unit tests easy

gostub gostub is a library to make stubbing in unit tests easy. Getting started Import the following package: github.com/prashantv/gostub Click here t

Dec 28, 2022
gomonkey is a library to make monkey patching in unit tests easy

gomonkey is a library to make monkey patching in unit tests easy, and the core idea of monkey patching comes from Bouke, you can read this blogpost for an explanation on how it works.

Jan 4, 2023
A simple and expressive HTTP server mocking library for end-to-end tests in Go.

mockhttp A simple and expressive HTTP server mocking library for end-to-end tests in Go. Installation go get -d github.com/americanas-go/mockhttp Exa

Dec 19, 2021
Easier way to unit test terraform

Unit testing terraform (WIP) Disclaimer Currently, the only way to compare values is using JSON query path and all types are strings. want := terraf

Aug 16, 2022
Package for comparing Go values in tests

Package for equality of Go values This package is intended to be a more powerful and safer alternative to reflect.DeepEqual for comparing whether two

Dec 29, 2022
Extremely flexible golang deep comparison, extends the go testing package and tests HTTP APIs
Extremely flexible golang deep comparison, extends the go testing package and tests HTTP APIs

go-testdeep Extremely flexible golang deep comparison, extends the go testing package. Latest news Synopsis Description Installation Functions Availab

Dec 22, 2022
Testing framework for Go. Allows writing self-documenting tests/specifications, and executes them concurrently and safely isolated. [UNMAINTAINED]

GoSpec GoSpec is a BDD-style testing framework for the Go programming language. It allows writing self-documenting tests/specs, and executes them in p

Nov 28, 2022
A simple `fs.FS` implementation to be used inside tests.

testfs A simple fs.FS which is contained in a test (using testing.TB's TempDir()) and with a few helper methods. PS: This lib only works on Go 1.16+.

Mar 3, 2022
Golang application focused on tests
Golang application focused on tests

Maceio Golang application that listens for webhook events coming from Github, runs tests previously defined in a configuration file and returns the ou

Sep 8, 2021