Terraform Testing
Testing a Terraform Provider is critical for ensuring your customers are successfully able to write configurations that apply consistently without errors or unexpected behaviors.
There are two categories of Terraform Provider testing:
- Manual: Locally install the Terraform Provider with Terraform CLI configuration provider development overrides and run Terraform commands with individual configurations.
- Automated: Using native Go programming language testing functionality, automatically run Terraform configurations using real Terraform commands for provider code under development.
It is recommended to implement automated testing to simplify development and verification over time.
Prerequisites
For both categories of testing, install the Terraform CLI hashicorp/setup-terraform
action.
Manual Testing
After generating a Terraform Provider using Speakeasy, the README file includes instructions for how to manually test with Terraform CLI provider development overrides
In the repository root directory, build the provider binary:
Create or edit your Terraform CLI configuration file ~/.terraformrc
, to point to your provider directory:
Change to any directory containing Terraform configurations for your provider and run Terraform commands such as terraform apply
. The output should include a warning about the provider development overrides from the ~/.terraformrc
configuration.
Automated Testing
Info
Speakeasy intends to generate automated testing in the future, similar to SDKs. Over time, these steps will be automatically handled during generation.
Automated tests take Terraform configuration(s) and then perform create, read, import, update, and delete actions against those using real Terraform commands. Automated testing also supports writing assertions against the Terraform plan or state per test step. This is accomplished through the HashiCorp-maintained github.com/hashicorp/terraform-plugin-testing
Go module. Refer to the Terraform Provider testing documentation
There are a few steps required to get started:
- Add
github.com/hashicorp/terraform-plugin-testing
dependency to Speakeasy generation so it is automatically downloaded and installed. - Create provider code to testing code mapping function.
- Create resource test files and configurations.
- Run automated testing.
Add Dependency
In .speakeasy/gen.yaml
, add github.com/hashicorp/terraform-plugin-testing
to the terraform
section additionalDependencies
configuration. View the latest Go package documentation for terraform-plugin-testing
to retrieve the latest version number.
Provider Mapping Function
Create a shared function in internal/provider/provider_test.go
to reference the provider during testing. Ensure the provider package import matches your Go module name and provider name in the mapping matches the provider type (short name).
Resource Testing Files
Resource testing code is conventionally written as a Go test file (internal/provider/xxx_resource_test.go
) while configurations are in internal/provider/testdata
directories named after the test.
Create a resource test by creating a Go test file, such as internal/provider/thing_resource_test.go
. Modify the configuration and checks as necessary to match the resource implementation.
Create the associated testing configuration in internal/provider/testdata/TestThingResource_lifecycle/main.tf
:
Run Automated Testing
Note
Ensure Speakeasy generation, such as speakeasy run
, has been run at least
once beforehand to ensure the github.com/hashicorp/terraform-plugin-testing
dependency is downloaded and installed. Verify via go.mod
file contents.
Run automated testing via native Go programming language testing functionality, such as the go test
command. The github.com/hashicorp/terraform-plugin-testing
Go module requires the TF_ACC
environment variable to set, conventionally to 1
(enabled). Depending on your provider configuration requirements, you may also need to set other security or server URL environment variables.
Run the following commands to perform the automated testing:
The testing library will handle all the underlying details to run the provider code, call Terraform commands, and verify assertions.
Run individual tests with the -run
flag, which accepts a regular expression pattern. For example:
Last updated on