Description
terraform-plugin-testing version
latest
Use cases
Running acceptance tests requires access to a live provider which involves making API calls that may or may not cost a lot of money. Additionally, some platforms which offer or have had Terraform providers built, may have certain features or functions locked behind flags or paywall gates that aren't accessible to common users/developers.
Providers, such as terraform-provider-google
and terraform-provider-okta
, have implemented the use of Go VCR, to allow for cheaper, more efficient, and a more accessible contributor experience. Other providers have implemented other tools as well, some more specific to the particular API/service being modeled in Terraform.
Attempted solutions
This has already been accomplished in a ad-hoc manner in a variety of "similar" but also slightly different ways:
- Generally all providers using this logic have a "separate" set of provider init functions used with testing. These functions use some of the Go VCR environment variables to trigger logic to replace the HTTP clients used with the API/SDK the provider is making resources for.
- Nearly all situations involve a custom "acceptance test only" version of the Provider structs and override of the
Configure
function for "framework" and theConfigureContextFunc
to handle this HTTP configuration. Which then are passed into the variousProtoProviderFactories
inputs on the the test case. This must be done since each test case will start a new instance of the configuration / providers by convention. - Apart from that, there some level of custom code which really has to be provider / developer specific, related to customizing the VCR recording to strip sensitive fields, replacing/normalizing unique values to the original recorder, dealing with API quirks, etc. Additionally, there is "admin" type decisions, such as how the provider wants to manage the recording and storage of the VCRs, commonly seen "by acceptance test" due to the nature of the config reload, but it can be done in a variety of ways.
For the most part, this works out decently well provided the SDK being used is nice about replacing the HTTP client it uses, as well as good code cleanliness and standards. However, in it's current form, this does require a pretty significant effort and self-motivated desire to get this done.
Proposal
My personal statement would be that the benefits of a VCR/playback based acceptance test system are significant enough that the testing framework for providers should attempt to offer some level of "reduced boilerplate" tools to making the necessary changes during acceptance tests and some level of "encouragement" to include these types of acceptance tests tools in the provider.
My proposal for this would be.
- Inside of the "Test Case", there should be some sort of interface to allow us to modify/override the "provider client" AFTER the test case init process of the actual provider objects is done. Something similar to the "access" we get to the client inside of the various CRUD functions where things like pointers to a HTTP client on the given SDK client and other options could be modified. As some of the examples (see my references) show, there will have to be some consideration and guidance given to the developer to ensure the various "goroutines" aren't stepping on each other. Potentially this could be another top level configuration which takes a function making these changes, which is expected to be "goroutine" safe and has certain asks to ensure whatever is changed is also routine safe.
- Additionally, in some of the example code and Hashicorp documentation, references to using this interface and what it's intended purposes are (aka things like this). I would also request that the "example" providers and development/testing documentation have some toy examples of this being used in practice (using Go VCR or whatever Hashi / you decide you want to do), and some level of encouragement of this type of pattern when providers are being developed.
References
Google provider: https://github.com/hashicorp/terraform-provider-google/blob/main/google/acctest/vcr_utils.go
Okta provider: https://github.com/okta/terraform-provider-okta/blob/master/okta/provider_test.go