Skip to content

Commit 6865074

Browse files
committed
importstate: a working example of ImportState as the first TestStep
1 parent f7076a9 commit 6865074

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package importstate_test
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
8+
9+
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider"
10+
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver"
11+
"github.com/hashicorp/terraform-plugin-testing/terraform"
12+
"github.com/hashicorp/terraform-plugin-testing/tfversion"
13+
14+
r "github.com/hashicorp/terraform-plugin-testing/helper/resource"
15+
)
16+
17+
func Test_TestStep_Import_AsFirstStep(t *testing.T) {
18+
t.Parallel()
19+
20+
r.UnitTest(t, r.TestCase{
21+
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
22+
tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithId requires Terraform 1.5.0 or later
23+
},
24+
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
25+
"examplecloud": providerserver.NewProviderServer(testprovider.Provider{
26+
Resources: map[string]testprovider.Resource{
27+
"examplecloud_container": examplecloudResource(),
28+
},
29+
}),
30+
},
31+
Steps: []r.TestStep{
32+
{
33+
ResourceName: "examplecloud_container.test",
34+
ImportStateId: "examplecloud_container.test",
35+
ImportState: true,
36+
ImportStateKind: r.ImportCommandWithId,
37+
// ImportStateVerify: true,
38+
Config: `resource "examplecloud_container" "test" {
39+
name = "somevalue"
40+
location = "westeurope"
41+
}`,
42+
ImportStatePersist: true,
43+
ImportStateCheck: func(states []*terraform.InstanceState) error {
44+
if len(states) != 1 {
45+
return fmt.Errorf("expected 1 state; got %d", len(states))
46+
}
47+
if states[0].ID != "westeurope/somevalue" {
48+
return fmt.Errorf("unexpected ID: %s", states[0].ID)
49+
}
50+
if states[0].Attributes["name"] != "somevalue" {
51+
return fmt.Errorf("unexpected name: %s", states[0].Attributes["name"])
52+
}
53+
if states[0].Attributes["location"] != "westeurope" {
54+
return fmt.Errorf("unexpected location: %s", states[0].Attributes["location"])
55+
}
56+
return nil
57+
},
58+
},
59+
{
60+
RefreshState: true,
61+
Check: r.TestCheckResourceAttr("examplecloud_container.test", "name", "somevalue"),
62+
},
63+
},
64+
})
65+
}

helper/resource/importstate/import_block_with_id_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
r "github.com/hashicorp/terraform-plugin-testing/helper/resource"
2222
)
2323

24-
func TestTest_TestStep_ImportBlockId(t *testing.T) {
24+
func Test_TestStep_ImportBlockId(t *testing.T) {
2525
t.Parallel()
2626

2727
r.UnitTest(t, r.TestCase{

0 commit comments

Comments
 (0)