Skip to content

Commit 8242c05

Browse files
committed
Use tfjson.State
1 parent b0abb5b commit 8242c05

File tree

6 files changed

+84
-44
lines changed

6 files changed

+84
-44
lines changed

helper/resource/importstate/import_block_in_config_directory_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ func Test_ImportBlock_InConfigDirectory(t *testing.T) {
3535
ConfigDirectory: config.StaticDirectory(`testdata/1`),
3636
},
3737
{
38-
ResourceName: "examplecloud_container.test",
39-
ImportState: true,
40-
ImportStateKind: r.ImportBlockWithID,
41-
ImportStateVerify: true,
42-
ConfigDirectory: config.StaticDirectory(`testdata/2`),
38+
ResourceName: "examplecloud_container.test",
39+
ImportState: true,
40+
ImportStateKind: r.ImportBlockWithID,
41+
ImportPlanVerify: true,
42+
ConfigDirectory: config.StaticDirectory(`testdata/2`),
4343
},
4444
},
4545
})

helper/resource/importstate/import_block_in_config_file_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ func Test_ImportBlock_InConfigFile(t *testing.T) {
3535
ConfigFile: config.StaticFile(`testdata/1/examplecloud_container.tf`),
3636
},
3737
{
38-
ResourceName: "examplecloud_container.test",
39-
ImportState: true,
40-
ImportStateKind: r.ImportBlockWithID,
41-
ImportStateVerify: true,
42-
ConfigFile: config.StaticFile(`testdata/2/examplecloud_container_import.tf`),
38+
ResourceName: "examplecloud_container.test",
39+
ImportState: true,
40+
ImportStateKind: r.ImportBlockWithID,
41+
ImportPlanVerify: true,
42+
ConfigFile: config.StaticFile(`testdata/2/examplecloud_container_import.tf`),
4343
},
4444
},
4545
})

helper/resource/importstate/import_block_with_id_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ func TestTest_TestStep_ImportBlockId_ExpectError(t *testing.T) {
8383
}`,
8484
ResourceName: "examplecloud_container.test",
8585
ImportState: true,
86-
ImportStateKind: r.ImportBlockWithId,
86+
ImportStateKind: r.ImportBlockWithID,
8787
ImportPlanVerify: true,
88-
ExpectError: regexp.MustCompile(`importing resource examplecloud_container.test should be a no-op, but got action update with plan(.?)`),
88+
ExpectError: regexp.MustCompile(`importing resource examplecloud_container.test: expected a no-op resource action, got "update" action with plan(.?)`),
8989
},
9090
},
9191
})
@@ -122,7 +122,7 @@ func TestTest_TestStep_ImportBlockId_FailWhenPlannableImportIsNotSupported(t *te
122122
}`,
123123
ResourceName: "examplecloud_container.test",
124124
ImportState: true,
125-
ImportStateKind: r.ImportBlockWithId,
125+
ImportStateKind: r.ImportBlockWithID,
126126
ImportPlanVerify: true,
127127
ExpectError: regexp.MustCompile(`Terraform 1.5.0`),
128128
},

helper/resource/testing_new_import_state.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,6 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest
7272
t.Fatalf("Error getting state: %s", err)
7373
}
7474

75-
// TODO: this statement is a placeholder -- it simply prevents stateJSON from being unused
76-
logging.HelperResourceTrace(ctx, fmt.Sprintf("State before import: values %v", stateJSON.Values != nil))
77-
7875
// Determine the ID to import
7976
var importId string //nolint:revive
8077
switch {
@@ -197,7 +194,7 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest
197194
}
198195

199196
if step.ImportPlanVerify {
200-
if err := teststep.VerifyImportPlan(plan, state); err != nil {
197+
if err := teststep.VerifyImportPlan(plan, stateJSON); err != nil {
201198
return err
202199
}
203200
}

internal/teststep/verify_import_plan.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,23 @@ import (
88
"strings"
99

1010
tfjson "github.com/hashicorp/terraform-json"
11-
"github.com/hashicorp/terraform-plugin-testing/terraform"
1211
)
1312

1413
// VerifyImportPlan compares a Terraform plan against a known good state
15-
func VerifyImportPlan(plan *tfjson.Plan, state *terraform.State) error {
14+
func VerifyImportPlan(plan *tfjson.Plan, state *tfjson.State) error {
1615
if state == nil {
1716
return fmt.Errorf("state is nil")
1817
}
1918
if plan == nil {
2019
return fmt.Errorf("plan is nil")
2120
}
22-
oldResources := make(map[string]*terraform.ResourceState)
23-
for logicalResourceName, resourceState := range state.RootModule().Resources {
24-
if !strings.HasPrefix(logicalResourceName, "data.") {
25-
oldResources[logicalResourceName] = resourceState
21+
22+
// TODO: if state.Values is nil ...
23+
24+
resourcesInState := make(map[string]*tfjson.StateResource)
25+
for _, resource := range state.Values.RootModule.Resources {
26+
if !strings.HasPrefix(resource.Address, "data.") {
27+
resourcesInState[resource.Address] = resource
2628
}
2729
}
2830
for _, rc := range plan.ResourceChanges {
@@ -52,18 +54,18 @@ func VerifyImportPlan(plan *tfjson.Plan, state *terraform.State) error {
5254
panic(fmt.Sprintf("unexpected type %T", v))
5355
}
5456

55-
oldResource := oldResources[rc.Address]
56-
if oldResource == nil {
57+
resourceInState := resourcesInState[rc.Address]
58+
if resourceInState == nil {
5759
// does this matter?
5860
return fmt.Errorf("importing resource %s: expected resource %s to exist in known state", rc.Address, rc.Change.Importing.ID)
5961
}
6062

61-
attr, ok := oldResource.Primary.Attributes[k]
63+
attr, ok := resourceInState.AttributeValues[k]
6264
if !ok {
6365
return fmt.Errorf("importing resource %s: expected %s in known state to exist", rc.Address, k)
6466
}
6567
if attr != vs {
66-
return fmt.Errorf("importing resource %s: expected %s in known state to be %q, got %q", rc.Address, k, oldResource.Primary.Attributes[k], vs)
68+
return fmt.Errorf("importing resource %s: expected %s in known state to be %q, got %q", rc.Address, k, attr, vs)
6769
}
6870
}
6971
}

internal/teststep/verify_import_plan_test.go

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,24 @@ import (
88

99
tfjson "github.com/hashicorp/terraform-json"
1010
"github.com/hashicorp/terraform-plugin-testing/internal/teststep"
11-
"github.com/hashicorp/terraform-plugin-testing/terraform"
1211
)
1312

14-
func Test_VerifyImportPlan(t *testing.T) {
13+
func TestVerifyImportPlan(t *testing.T) {
1514
t.Parallel()
1615

17-
state := &terraform.State{
18-
Version: 6,
19-
Modules: []*terraform.ModuleState{
20-
{
21-
Path: []string{"root"},
22-
Resources: map[string]*terraform.ResourceState{
23-
"example_resource.instance-1": {
24-
Primary: &terraform.InstanceState{
25-
Attributes: map[string]string{
26-
"attr1": "value1",
27-
},
28-
},
29-
},
30-
},
31-
},
16+
resource := &tfjson.StateResource{
17+
Address: "example_resource.instance-1",
18+
Type: "example_resource",
19+
Name: "instance-1",
20+
AttributeValues: map[string]interface{}{
21+
"attr1": "value1",
22+
},
23+
}
24+
25+
state := new(tfjson.State)
26+
state.Values = &tfjson.StateValues{
27+
RootModule: &tfjson.StateModule{
28+
Resources: []*tfjson.StateResource{resource},
3229
},
3330
}
3431

@@ -56,3 +53,47 @@ func Test_VerifyImportPlan(t *testing.T) {
5653
}
5754

5855
}
56+
57+
func TestVerifyImportPlan_AttributeValueMismatch(t *testing.T) {
58+
t.Parallel()
59+
60+
resource := &tfjson.StateResource{
61+
Address: "example_resource.instance-1",
62+
Type: "example_resource",
63+
Name: "instance-1",
64+
AttributeValues: map[string]interface{}{
65+
"attr1": "value1",
66+
},
67+
}
68+
69+
state := new(tfjson.State)
70+
state.Values = &tfjson.StateValues{
71+
RootModule: &tfjson.StateModule{
72+
Resources: []*tfjson.StateResource{resource},
73+
},
74+
}
75+
76+
plan := new(tfjson.Plan)
77+
plan.ResourceChanges = []*tfjson.ResourceChange{
78+
{
79+
Address: "example_resource.instance-1",
80+
Change: &tfjson.Change{
81+
Actions: []tfjson.Action{tfjson.ActionNoop},
82+
After: map[string]interface{}{
83+
"attr1": "value5",
84+
},
85+
Before: map[string]interface{}{
86+
"attr1": "value5",
87+
},
88+
Importing: &tfjson.Importing{
89+
ID: "instance-1",
90+
},
91+
},
92+
},
93+
}
94+
95+
if err := teststep.VerifyImportPlan(plan, state); err == nil {
96+
t.Fatal("expected error, got nil")
97+
}
98+
99+
}

0 commit comments

Comments
 (0)