• Overview
    • Enforce Policy as Code
    • Infrastructure as Code
    • Inject Secrets into Terraform
    • Integrate with Existing Workflows
    • Manage Kubernetes
    • Manage Virtual Machine Images
    • Multi-Cloud Deployment
    • Network Infrastructure Automation
    • Terraform CLI
    • Terraform Cloud
    • Terraform Enterprise
  • Registry
  • Tutorials
    • About the Docs
    • Intro to Terraform
    • Configuration Language
    • Terraform CLI
    • Terraform Cloud
    • Terraform Enterprise
    • Provider Use
    • Plugin Development
    • Registry Publishing
    • Integration Program
    • Terraform Tools
    • CDK for Terraform
    • Glossary
  • Community
GitHubTerraform Cloud
Download

    Framework

  • Overview
  • Tutorial: Implement Create and Read
  • Provider Servers
  • Providers
    • Overview
    • Import
    • Plan Modification
    • State Upgrade
  • Data Sources
  • Schemas
  • Attribute Types
  • Accessing State, Config, and Plan
  • Writing State
  • Returning Errors and Warnings
  • Validation
  • Acceptance Tests
  • Debugging
  • Other Plugin Docs

  • Plugin Development
  • SDKv2
  • Logging
  • Combining and Translating
Type '/' to Search

»Acceptance Tests

You can implement testing with the acceptance test framework shipped with SDKv2.

Writing and running tests is similar to SDKv2 providers, with the following exceptions:

  • TestCase: Specify the provider with ProtoV6ProviderFactories or ProtoV5ProviderFactories, depending on the intended provider server setup.
  • Schema: A root level id attribute is required for resources and data sources.

»Specify Providers

Use one of the resource.TestCase type ProtoV6ProviderFactories field for protocol version 6 or ProtoV5ProviderFactories field for protocol version 5. It is only necessary to test with the single protocol version matching the intended provider server.

»Protocol Version 6

Use the providerserver.NewProtocol6WithError helper function to implement the provider server in the ProtoV6ProviderFactories field.

resource.Test(t, resource.TestCase{
    PreCheck: func() { testAccPreCheck(t) },
    ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error) {
        // newProvider is an example function that returns a tfsdk.Provider
        "example_provider": providerserver.NewProtocol6WithError(newProvider()),
    },
    CheckDestroy: testAccCheckExampleResourceDestroy,
    Steps: []resource.TestStep{
        {
            Config: testAccExampleResource,
            Check: testAccCheckExampleResourceExists,
        },
    },
})
resource.Test(t, resource.TestCase{
    PreCheck: func() { testAccPreCheck(t) },
    ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error) {
        // newProvider is an example function that returns a tfsdk.Provider
        "example_provider": providerserver.NewProtocol6WithError(newProvider()),
    },
    CheckDestroy: testAccCheckExampleResourceDestroy,
    Steps: []resource.TestStep{
        {
            Config: testAccExampleResource,
            Check: testAccCheckExampleResourceExists,
        },
    },
})

»Protocol Version 5

Use the providerserver.NewProtocol5WithError helper function to implement the provider server in the ProtoV5ProviderFactories field.

resource.Test(t, resource.TestCase{
    PreCheck: func() { testAccPreCheck(t) },
    ProtoV5ProviderFactories: map[string]func() (tfprotov5.ProviderServer, error) {
        // newProvider is an example function that returns a tfsdk.Provider
        "example_provider": providerserver.NewProtocol5WithError(newProvider()),
    },
    CheckDestroy: testAccCheckExampleResourceDestroy,
    Steps: []resource.TestStep{
        {
            Config: testAccExampleResource,
            Check: testAccCheckExampleResourceExists,
        },
    },
})
resource.Test(t, resource.TestCase{
    PreCheck: func() { testAccPreCheck(t) },
    ProtoV5ProviderFactories: map[string]func() (tfprotov5.ProviderServer, error) {
        // newProvider is an example function that returns a tfsdk.Provider
        "example_provider": providerserver.NewProtocol5WithError(newProvider()),
    },
    CheckDestroy: testAccCheckExampleResourceDestroy,
    Steps: []resource.TestStep{
        {
            Config: testAccExampleResource,
            Check: testAccCheckExampleResourceExists,
        },
    },
})

»Implement id Attribute

In SDKv2, resources and data sources automatically included an implicit, root level id attribute. In the framework, the id attribute is not implicitly added.

When testing resources and data sources without the id attribute, the acceptance testing framework will return errors such as:

testing_new_config.go:111: no "id" found in attributes
testing_new.go:53: no "id" found in attributes
testing_new_config.go:111: no "id" found in attributes
testing_new.go:53: no "id" found in attributes

To avoid this, add a root level id attribute to resource and data source schemas. Ensure the attribute value is appropriately written to state. Conventionally, id is a computed attribute that contains the identifier for the resource.

For example, in the GetSchema method implementation of a DataSourceType or ResourceType:

func (t exampleResourceType) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) {
    return tfsdk.Schema{
        // ... potentially other schema configuration ...
        Attributes: map[string]tfsdk.Attribute{
            // ... potentially other schema attributes ...
            "id": {
                Type:     types.StringType,
                Computed: true,
            },
        },
    }, nil
}
func (t exampleResourceType) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) {
    return tfsdk.Schema{
        // ... potentially other schema configuration ...
        Attributes: map[string]tfsdk.Attribute{
            // ... potentially other schema attributes ...
            "id": {
                Type:     types.StringType,
                Computed: true,
            },
        },
    }, nil
}
github logoEdit this page
  • Overview
  • Docs
  • Extend
  • Privacy
  • Security
  • Press Kit
  • Consent Manager