Run tests

Run tests #

Before you begin #

Generate the modified provider(s)

  1. Set up application default credentials for Terraform

    gcloud auth application-default login
    export GOOGLE_USE_DEFAULT_CREDENTIALS=true
    
  2. Set the following environment variables:

    export GOOGLE_PROJECT=PROJECT_ID
    export GOOGLE_REGION=us-central1
    export GOOGLE_ZONE=us-central1-a
    

    Replace PROJECT_ID with the ID of the Google Cloud project you are using for testing.

  3. Optional: Some tests may require additional variables to be set, such as:

    GOOGLE_ORG
    GOOGLE_BILLING_ACCOUNT
    

Run automated tests #

  1. Run unit tests and linters

    cd $GOPATH/src/github.com/hashicorp/terraform-provider-google
    make test
    make lint
    
  2. Run acceptance tests for only modified resources. (Full test runs can take over 9 hours.) See Go’s documentation for more information about -run and other flags.

    make testacc TEST=./google/services/container TESTARGS='-run=TestAccContainerNodePool'
    

[!NOTE] Acceptance tests create actual infrastructure which can incur costs. Acceptance tests may not clean up after themselves if interrupted, so you may want to check for stray resources and / or billing charges.

  1. Optional: Save verbose test output (including API requests and responses) to a file for analysis.

    TF_LOG=DEBUG make testacc TEST=./google/services/container TESTARGS='-run=TestAccContainerNodePool_basic' > output.log
    
  2. Optional: Debug tests with Delve. See dlv test documentation for information about available flags.

    cd google
    TF_ACC=1 dlv test -- --test.v --test.run TestAccComputeRegionBackendService_withCdnPolicy
    
  1. Run unit tests and linters

    cd $GOPATH/src/github.com/hashicorp/terraform-provider-google-beta
    make test
    make lint
    
  2. Run acceptance tests for only modified resources. (Full test runs can take over 9 hours.) See Go’s documentation for more information about -run and other flags.

    make testacc TEST=./google-beta/services/container TESTARGS='-run=TestAccContainerNodePool'
    

[!NOTE] Acceptance tests create actual infrastructure which can incur costs. Acceptance tests may not clean up after themselves if interrupted, so you may want to check for stray resources and / or billing charges.

  1. Optional: Save verbose test output to a file for analysis.

    TF_LOG=DEBUG make testacc TEST=./google-beta/services/container TESTARGS='-run=TestAccContainerNodePool_basic' > output.log
    
  2. Optional: Debug tests with Delve. See dlv test documentation for information about available flags.

    cd google-beta
    TF_ACC=1 dlv test -- --test.v --test.run TestAccComputeRegionBackendService_withCdnPolicy
    

Optional: Test with different terraform versions #

Tests will use whatever version of the terraform binary is found on your PATH. If you are testing a change that you know only impacts certain terraform versions, follow these steps:

  1. Install tfenv.

  2. Install the version of terraform you want to test.

    tfenv install VERSION
    

    Replace VERSION with the version you want to test.

  3. Run automated tests following the earlier section.

Optional: Test manually #

For manual testing, you can build the provider from source and run terraform apply to verify the behavior.

Before you begin #

Configure Terraform to use locally-built binaries for google and google-beta instead of downloading the latest versions.

  1. Find the location where built provider binaries are created. To do this, run this command and make a note of the path value:

    go env GOBIN
    
    ## If the above returns nothing, then run the command below and add "/bin" to the end of the output path.
    go env GOPATH
    
  2. Create an empty configuration file.

    ## create an empty file
    touch ~/tf-dev-override.tfrc
    
    ## open the file with a text editor of your choice, e.g:
    vi ~/tf-dev-override.tfrc
    

    Open the empty file with a text editor and paste in these contents:

    provider_installation {
    
      # Developer overrides will stop Terraform from downloading the listed
      # providers their origin provider registries.
      dev_overrides {
          "hashicorp/google" = "GO_BIN_PATH/bin"
          "hashicorp/google-beta" = "GO_BIN_PATH/bin"
      }
    
      # For all other providers, install them directly from their origin provider
      # registries as normal. If you omit this, Terraform will _only_ use
      # the dev_overrides block, and so no other providers will be available.
      direct {}
    }
    
  3. Edit the file to replace GO_BIN_PATH with the path you saved from the first step, making sure to keep /bin at the end of the path.

    Please note: the full path is required and environment variables cannot be used. For example, "/Users/UserName/go/bin" is a valid path for a user called UserName, but "~/go/bin" or "$HOME/go/bin" will not work.

  4. Save the file.

  1. Find the location where built provider binaries are created. To do this, run this command and make a note of the path value:

    echo %GOPATH%
    
  2. Create an empty configuration file in the %APPDATA% directory (use $env:APPDATA in PowerShell to find its location on your system).

    ## create an empty file
    type nul > "$($env:APPDATA)\tf-dev-override.tfrc"
    
    ## open the file with a text editor of your choice, e.g:
    notepad "$($env:APPDATA)\tf-dev-override.tfrc"
    

    Open the empty file with a text editor and paste in these contents:

    provider_installation {
    
      # Developer overrides will stop Terraform from downloading the listed
      # providers their origin provider registries.
      dev_overrides {
          "hashicorp/google" = "GO_BIN_PATH\bin"
          "hashicorp/google-beta" = "GO_BIN_PATH\bin"
      }
    
      # For all other providers, install them directly from their origin provider
      # registries as normal. If you omit this, Terraform will _only_ use
      # the dev_overrides block, and so no other providers will be available.
      direct {}
    }
    
  3. Edit the file to replace GO_BIN_PATH with the output you saved from the first step, making sure to keep \bin at the end of the path.

    Please note: The full path is required and environment variables cannot be used. For example, C:\Users\UserName\go\bin is a valid path for a user called UserName.

  4. Save the file.

Run manual tests #

  1. Generate the provider(s) you want to test

  2. Build the provider(s) you want to test

    ## google provider
    cd $GOPATH/src/github.com/hashicorp/terraform-provider-google
    make build
    
    ## google-beta provider
    cd $GOPATH/src/github.com/hashicorp/terraform-provider-google-beta
    make build
    
  3. Create a new directory and a main.tf file with your resource and its dependencies.

  4. In the new directory, run terraform plan as follows:

    TF_CLI_CONFIG_FILE="$HOME/tf-dev-override.tfrc" terraform plan
    

    Replace the TF_CLI_CONFIG_FILE value with the full path to your developer overrides file.

  5. Optional: Verify that developer overrides are working by looking for output like the following near the start of the output:

    │ Warning: Provider development overrides are in effect
    │ 
    │ The following provider development overrides are set in the CLI configuration:
    │  - hashicorp/google in /Users/UserName/go/bin
    │  - hashicorp/google-beta in /Users/UserName/go/bin
    │ 
    │ The behavior may therefore not match any released version of the provider and applying
    │ changes may cause the state to become incompatible with published releases.
    
  6. Run terraform apply with developer overrides.

    TF_CLI_CONFIG_FILE="$HOME/tf-dev-override.tfrc" terraform apply
    
  7. Optional: Save verbose terraform apply output (including API requests and responses) to a file for analysis.

    TF_LOG=DEBUG TF_LOG_PATH=output.log TF_CLI_CONFIG_FILE="$HOME/tf-dev-override.tfrc" terraform apply
    

Cleanup #

To stop using developer overrides, stop setting TF_CLI_CONFIG_FILE in the commands you are executing.

Terraform will resume its normal behaviour of pulling published provider versions from the public Registry. Any version constraints in your Terraform configuration will come back into effect. Also, you may need to run terraform init to download the required version of the provider into your project directory if you haven’t already.

What’s next? #