hashicorp / terraform-plugin-testing

Module for testing Terraform providers
Mozilla Public License 2.0
44 stars 11 forks source link

Add docs for `ExternalProviders ` in TestCase #349

Open zliang-akamai opened 1 month ago

zliang-akamai commented 1 month ago

Use cases

There are some useful utility provider like null and http, which might be used in the acceptance tests of the another provider.

Attempted solutions

Importing those providers as Go packages and placing them into the provider factory. This doesn't work well because many providers doesn't follow Golang's package standard.

See this example: https://github.com/hashicorp/terraform-provider-http/pull/428

Proposal

Maybe we can have a new field on Test struct to allow setup of other providers?

bflad commented 1 month ago

Hi again, @zliang-akamai 👋 Looks like you already created the issue I alluded to creating over in https://github.com/hashicorp/terraform-provider-http/issues/427. Thanks for that. 😄

For others benefit, this functionality does already exist in the terraform-plugin-testing Go module. I'll copy some of the verbiage described in that separate issue:

you can configure the terraform-plugin-testing logic to use the hashicorp/http provider in acceptance tests by configuring helper/resource.TestCase.ExternalProviders or per-step with helper/resource.TestStep.ExternalProviders. This can be done alongside configuring the other provider-under-test fields to combine external provider usage with under-test provider usage. For example:

resource.Test(t, resource.TestCase{
    ExternalProviders: map[string]resource.ExternalProvider{
        "http": {
            Source: "hashicorp/http",
            Version: "3.4.2",
        },
    },
    // ... ProviderFactories, ProtoV5ProviderFactories, ProtoV6ProviderFactories, etc.
    Steps: []resource.TestStep{
        {
            Config: `
data "http" "test" {
  url = "..."
}

# ...
`,
            // ... other fields ...
        },
    },
})

This functionality can be used for any provider that is available to Terraform while the testing is running. Website documentation about this testing functionality does not appear to be too findable (if at all) though. Since this functionality does already exist, but the documentation is lacking, I have changed this feature request into a documentation request.

zliang-akamai commented 1 month ago

@bflad Ah, thank you so much! Didn't realize that feature was already there.