Terraform
Acceptance Tests: Import mode
Import is the workflow that brings existing infrastructure into Terraform, without altering the infrastructure itself. For reference information about the Import workflow: Terraform CLI, Framework, SDKv2.
In provider acceptance tests, Import mode is used for testing resource functionality to import existing infrastructure into a Terraform statefile, using real Terraform import functionality.
At its core, Import mode runs the Import workflow and checks: does a resource added by Import match a resource added by Lifecycle methods (i.e. Create)?
In common testing terminology, Import mode uses a resource added to Terraform by Create as the expected result.
Import mode uses a resource added to Terraform by Import as the actual result.
Import mode runs a deep comparison of the two data structures. The test step passes only if the two data structures match.
Examples
Testing the terraform import
workflow
func TestImportCommand(t *testing.T) {
r.ParallelTest(t, r.TestCase{
ProtoV5ProviderFactories: providerFactories,
Steps: []r.TestStep{
{
Config: `resource "examplecloud_thing" "test" {}`,
},
{
ImportState: true,
ResourceName: "examplecloud_thing.test",
ImportStateVerify: true,
},
},
})
}
Testing the plannable import workflow
func TestImportBlockWithID(t *testing.T) {
r.ParallelTest(t, r.TestCase{
ProtoV5ProviderFactories: providerFactories,
Steps: []r.TestStep{
{
Config: `resource "examplecloud_thing" "test" {}`,
},
{
ImportState: true,
ImportStateKind: resource.ImportBlockWithID,
ResourceName: "examplecloud_thing.test",
},
},
})
}
Testing the plannable import workflow using a managed resource identity
func TestImportBlockWithResourceIdentity(t *testing.T) {
r.ParallelTest(t, r.TestCase{
ProtoV5ProviderFactories: providerFactories,
Steps: []r.TestStep{
{
Config: `resource "examplecloud_thing" "test" {}`,
},
{
ImportState: true,
ImportStateKind: resource.ImportBlockWithResourceIdentity,
ResourceName: "examplecloud_thing.test",
},
},
})
}