Skip to content

Commit c75a3db

Browse files
committed
What if ... ImportStateBlockConfig
1 parent ea92159 commit c75a3db

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

helper/resource/testing.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,10 @@ type TestStep struct {
633633
// ID of that resource.
634634
ImportState bool
635635

636+
// ImportStateBlockConfig, if non-empty, supplies declarative import
637+
// configuration. This is (?mutually exclusive of ImportStateID + ResourceName?).
638+
ImportStateBlockConfig string
639+
636640
// ImportStateId is the ID to perform an ImportState operation with.
637641
// This is optional. If it isn't set, then the resource ID is automatically
638642
// determined by inspecting the state for ResourceName's ID.
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package resource
5+
6+
import (
7+
"testing"
8+
9+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
10+
"github.com/hashicorp/terraform-plugin-go/tftypes"
11+
12+
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider"
13+
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver"
14+
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/resource"
15+
"github.com/hashicorp/terraform-plugin-testing/tfversion"
16+
)
17+
18+
func TestTest_TestStep_ImportBlockVerify(t *testing.T) {
19+
t.Parallel()
20+
21+
UnitTest(t, TestCase{
22+
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
23+
tfversion.SkipBelow(tfversion.Version1_5_0), // import blocks are only available in v1.5.0 and later
24+
},
25+
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
26+
"examplecloud": providerserver.NewProviderServer(testprovider.Provider{
27+
Resources: map[string]testprovider.Resource{
28+
"examplecloud_thing": {
29+
CreateResponse: &resource.CreateResponse{
30+
NewState: tftypes.NewValue(
31+
tftypes.Object{
32+
AttributeTypes: map[string]tftypes.Type{
33+
"id": tftypes.String,
34+
"other": tftypes.String,
35+
},
36+
},
37+
map[string]tftypes.Value{
38+
"id": tftypes.NewValue(tftypes.String, "resource-test"),
39+
"other": tftypes.NewValue(tftypes.String, "testvalue"),
40+
},
41+
),
42+
},
43+
ImportStateResponse: &resource.ImportStateResponse{
44+
State: tftypes.NewValue(
45+
tftypes.Object{
46+
AttributeTypes: map[string]tftypes.Type{
47+
"id": tftypes.String,
48+
"other": tftypes.String,
49+
},
50+
},
51+
map[string]tftypes.Value{
52+
"id": tftypes.NewValue(tftypes.String, "resource-test"),
53+
"other": tftypes.NewValue(tftypes.String, "testvalue"),
54+
},
55+
),
56+
},
57+
SchemaResponse: &resource.SchemaResponse{
58+
Schema: &tfprotov6.Schema{
59+
Block: &tfprotov6.SchemaBlock{
60+
Attributes: []*tfprotov6.SchemaAttribute{
61+
{
62+
Name: "id",
63+
Type: tftypes.String,
64+
Computed: true,
65+
},
66+
{
67+
Name: "other",
68+
Type: tftypes.String,
69+
Computed: true,
70+
},
71+
},
72+
},
73+
},
74+
},
75+
},
76+
},
77+
}),
78+
},
79+
Steps: []TestStep{
80+
{
81+
Config: `resource "examplecloud_thing" "test" {}`,
82+
},
83+
{
84+
ImportState: true,
85+
ImportStateVerify: true,
86+
ImportStateBlockConfig: `import {
87+
to = examplecloud_thing.test
88+
identity = {
89+
hat = "derby"
90+
cat = "garfield"
91+
}
92+
}`,
93+
},
94+
},
95+
})
96+
}

0 commit comments

Comments
 (0)