Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func Test_ImportBlock_AsFirstStep(t *testing.T) {

r.UnitTest(t, r.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithId requires Terraform 1.5.0 or later
tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithID requires Terraform 1.5.0 or later
},
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"examplecloud": providerserver.NewProviderServer(testprovider.Provider{
Expand All @@ -37,7 +37,7 @@ func Test_ImportBlock_AsFirstStep(t *testing.T) {
ResourceName: "examplecloud_container.test",
ImportStateId: "examplecloud_container.test",
ImportState: true,
ImportStateKind: r.ImportBlockWithId,
ImportStateKind: r.ImportBlockWithID,
// ImportStateVerify: true,
Config: `resource "examplecloud_container" "test" {
name = "somevalue"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package importstate_test

import (
"testing"

"github.com/hashicorp/terraform-plugin-go/tfprotov6"

"github.com/hashicorp/terraform-plugin-testing/config"
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider"
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver"
"github.com/hashicorp/terraform-plugin-testing/tfversion"

r "github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func Test_ImportBlock_InConfigDirectory(t *testing.T) {
t.Parallel()

r.UnitTest(t, r.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithID requires Terraform 1.5.0 or later
},
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"examplecloud": providerserver.NewProviderServer(testprovider.Provider{
Resources: map[string]testprovider.Resource{
"examplecloud_container": examplecloudResource(),
},
}),
},
Steps: []r.TestStep{
{
ConfigDirectory: config.StaticDirectory(`testdata/1`),
},
{
ResourceName: "examplecloud_container.test",
ImportState: true,
ImportStateKind: r.ImportBlockWithID,
ImportStateVerify: true,
ConfigDirectory: config.StaticDirectory(`testdata/2`),
},
},
})
}
46 changes: 46 additions & 0 deletions helper/resource/importstate/import_block_in_config_file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package importstate_test

import (
"testing"

"github.com/hashicorp/terraform-plugin-go/tfprotov6"

"github.com/hashicorp/terraform-plugin-testing/config"
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider"
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver"
"github.com/hashicorp/terraform-plugin-testing/tfversion"

r "github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func Test_ImportBlock_InConfigFile(t *testing.T) {
t.Parallel()

r.UnitTest(t, r.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithID requires Terraform 1.5.0 or later
},
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"examplecloud": providerserver.NewProviderServer(testprovider.Provider{
Resources: map[string]testprovider.Resource{
"examplecloud_container": examplecloudResource(),
},
}),
},
Steps: []r.TestStep{
{
ConfigFile: config.StaticFile(`testdata/1/examplecloud_container.tf`),
},
{
ResourceName: "examplecloud_container.test",
ImportState: true,
ImportStateKind: r.ImportBlockWithID,
ImportStateVerify: true,
ConfigFile: config.StaticFile(`testdata/2/examplecloud_container_import.tf`),
},
},
})
}
24 changes: 12 additions & 12 deletions helper/resource/importstate/import_block_with_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func Test_TestStep_ImportBlockId(t *testing.T) {

r.UnitTest(t, r.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithId requires Terraform 1.5.0 or later
tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithID requires Terraform 1.5.0 or later
},
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"examplecloud": providerserver.NewProviderServer(testprovider.Provider{
Expand All @@ -46,7 +46,7 @@ func Test_TestStep_ImportBlockId(t *testing.T) {
{
ResourceName: "examplecloud_container.test",
ImportState: true,
ImportStateKind: r.ImportBlockWithId,
ImportStateKind: r.ImportBlockWithID,
ImportStateVerify: true,
},
},
Expand All @@ -58,7 +58,7 @@ func TestTest_TestStep_ImportBlockId_ExpectError(t *testing.T) {

r.UnitTest(t, r.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithId requires Terraform 1.5.0 or later
tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithID requires Terraform 1.5.0 or later
},
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"examplecloud": providerserver.NewProviderServer(testprovider.Provider{
Expand All @@ -83,9 +83,9 @@ func TestTest_TestStep_ImportBlockId_ExpectError(t *testing.T) {
}`,
ResourceName: "examplecloud_container.test",
ImportState: true,
ImportStateKind: r.ImportBlockWithId,
ImportStateKind: r.ImportBlockWithID,
ImportStateVerify: true,
ExpectError: regexp.MustCompile(`importing resource examplecloud_container.test should be a no-op, but got action update with plan(.?)`),
ExpectError: regexp.MustCompile(`importing resource examplecloud_container.test: expected a no-op resource action, got "update" action with plan(.?)`),
},
},
})
Expand Down Expand Up @@ -122,7 +122,7 @@ func TestTest_TestStep_ImportBlockId_FailWhenPlannableImportIsNotSupported(t *te
}`,
ResourceName: "examplecloud_container.test",
ImportState: true,
ImportStateKind: r.ImportBlockWithId,
ImportStateKind: r.ImportBlockWithID,
ImportStateVerify: true,
ExpectError: regexp.MustCompile(`Terraform 1.5.0`),
},
Expand All @@ -135,7 +135,7 @@ func TestTest_TestStep_ImportBlockId_SkipDataSourceState(t *testing.T) {

r.UnitTest(t, r.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithId requires Terraform 1.5.0 or later
tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithID requires Terraform 1.5.0 or later

},
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
Expand All @@ -161,7 +161,7 @@ func TestTest_TestStep_ImportBlockId_SkipDataSourceState(t *testing.T) {
{
ResourceName: "examplecloud_thing.test",
ImportState: true,
ImportStateKind: r.ImportBlockWithId,
ImportStateKind: r.ImportBlockWithID,
ImportStateCheck: func(is []*terraform.InstanceState) error {
if len(is) > 1 {
return fmt.Errorf("expected 1 state, got: %d", len(is))
Expand Down Expand Up @@ -204,7 +204,7 @@ func TestTest_TestStep_ImportBlockId_ImportStateVerifyIgnore_Real_Example(t *tes

r.UnitTest(t, r.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithId requires Terraform 1.5.0 or later
tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithID requires Terraform 1.5.0 or later
},
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"examplecloud": providerserver.NewProviderServer(testprovider.Provider{
Expand Down Expand Up @@ -292,7 +292,7 @@ func TestTest_TestStep_ImportBlockId_ImportStateVerifyIgnore_Real_Example(t *tes
}`,
ResourceName: "examplecloud_container.test",
ImportState: true,
ImportStateKind: r.ImportBlockWithId,
ImportStateKind: r.ImportBlockWithID,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"password"},
},
Expand All @@ -305,7 +305,7 @@ func TestTest_TestStep_ImportBlockId_ImportStateVerifyIgnore(t *testing.T) {

r.UnitTest(t, r.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithId requires Terraform 1.5.0 or later
tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithID requires Terraform 1.5.0 or later
},
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"examplecloud": providerserver.NewProviderServer(testprovider.Provider{
Expand Down Expand Up @@ -377,7 +377,7 @@ func TestTest_TestStep_ImportBlockId_ImportStateVerifyIgnore(t *testing.T) {
{
ResourceName: "examplecloud_container.test",
ImportState: true,
ImportStateKind: r.ImportBlockWithId,
ImportStateKind: r.ImportBlockWithID,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"password"},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@ func Test_ImportCommand_AsFirstStep(t *testing.T) {
},
Steps: []r.TestStep{
{
ResourceName: "examplecloud_container.test",
ImportStateId: "examplecloud_container.test",
ImportState: true,
ImportStateKind: r.ImportCommandWithId,
// ImportStateVerify: true,
ResourceName: "examplecloud_container.test",
ImportStateId: "examplecloud_container.test",
ImportState: true,
Config: `resource "examplecloud_container" "test" {
name = "somevalue"
location = "westeurope"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

resource "examplecloud_container" "test" {
name = "somevalue"
location = "westeurope"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

resource "examplecloud_container" "test" {
name = "somevalue"
location = "westeurope"
}

import {
to = examplecloud_container.test
id = "examplecloud_container.test"
}
28 changes: 21 additions & 7 deletions helper/resource/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,14 +457,28 @@ type ExternalProvider struct {
type ImportStateKind byte

const (
// ImportCommandWithId imports the state using the import command
ImportCommandWithId ImportStateKind = iota
// ImportBlockWithId imports the state using an import block with an ID
ImportBlockWithId
// ImportCommandWithID tests import by using the ID string with the `terraform import` command
ImportCommandWithID ImportStateKind = iota

// ImportBlockWithID tests import by using the ID string in an import configuration block with the `terraform plan` command
ImportBlockWithID

// ImportBlockWithResourceIdentity imports the state using an import block with a resource identity
ImportBlockWithResourceIdentity
)

func (kind ImportStateKind) plannable() bool {
return kind == ImportBlockWithID || kind == ImportBlockWithResourceIdentity
}

func (kind ImportStateKind) String() string {
return map[ImportStateKind]string{
ImportCommandWithID: "ImportCommandWithID",
ImportBlockWithID: "ImportBlockWithID",
ImportBlockWithResourceIdentity: "ImportBlockWithResourceIdentity",
}[kind]
}

// TestStep is a single apply sequence of a test, done within the
// context of a state.
//
Expand Down Expand Up @@ -963,17 +977,17 @@ func UnitTest(t testing.T, c TestCase) {
Test(t, c)
}

func testResource(c TestStep, state *terraform.State) (*terraform.ResourceState, error) {
func testResource(name string, state *terraform.State) (*terraform.ResourceState, error) {
for _, m := range state.Modules {
if len(m.Resources) > 0 {
if v, ok := m.Resources[c.ResourceName]; ok {
if v, ok := m.Resources[name]; ok {
return v, nil
}
}
}

return nil, fmt.Errorf(
"Resource specified by ResourceName couldn't be found: %s", c.ResourceName)
"Resource specified by ResourceName couldn't be found: %s", name)
}

// ComposeTestCheckFunc lets you compose multiple TestCheckFuncs into
Expand Down
Loading
Loading