Skip to content

Commit 77d04e2

Browse files
committed
ImportPlanVerify
1 parent c0fd026 commit 77d04e2

File tree

5 files changed

+70
-23
lines changed

5 files changed

+70
-23
lines changed

helper/resource/importstate/import_block_in_config_directory_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ func Test_ImportBlock_InConfigDirectory(t *testing.T) {
3737
},
3838
},
3939
{
40-
ResourceName: "examplecloud_container.test",
41-
ImportState: true,
42-
ImportStateKind: r.ImportBlockWithID,
43-
ImportStateVerify: true,
40+
ResourceName: "examplecloud_container.test",
41+
ImportState: true,
42+
ImportStateKind: r.ImportBlockWithID,
43+
// ImportStateVerify: true,
4444
ConfigDirectory: func(config.TestStepConfigRequest) string {
4545
return `testdata/2`
4646
},

helper/resource/importstate/import_block_in_config_file_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ func Test_ImportBlock_InConfigFile(t *testing.T) {
3737
},
3838
},
3939
{
40-
ResourceName: "examplecloud_container.test",
41-
ImportState: true,
42-
ImportStateKind: r.ImportBlockWithID,
43-
ImportStateVerify: true,
40+
ResourceName: "examplecloud_container.test",
41+
ImportState: true,
42+
ImportStateKind: r.ImportBlockWithID,
43+
// ImportStateVerify: true,
4444
ConfigFile: func(config.TestStepConfigRequest) string {
4545
return `testdata/2/examplecloud_container_import.tf`
4646
},

helper/resource/importstate/import_block_with_id_test.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,9 @@ func Test_TestStep_ImportBlockId(t *testing.T) {
4444
}`,
4545
},
4646
{
47-
ResourceName: "examplecloud_container.test",
48-
ImportState: true,
49-
ImportStateKind: r.ImportBlockWithID,
50-
ImportStateVerify: true,
47+
ResourceName: "examplecloud_container.test",
48+
ImportState: true,
49+
ImportStateKind: r.ImportBlockWithID,
5150
},
5251
},
5352
})
@@ -81,11 +80,10 @@ func TestTest_TestStep_ImportBlockId_ExpectError(t *testing.T) {
8180
location = "eastus"
8281
name = "somevalue"
8382
}`,
84-
ResourceName: "examplecloud_container.test",
85-
ImportState: true,
86-
ImportStateKind: r.ImportBlockWithID,
87-
ImportStateVerify: true,
88-
ExpectError: regexp.MustCompile(`importing resource examplecloud_container.test: expected a no-op resource action, got "update" action with plan(.?)`),
83+
ResourceName: "examplecloud_container.test",
84+
ImportState: true,
85+
ImportStateKind: r.ImportBlockWithID,
86+
ExpectError: regexp.MustCompile(`importing resource examplecloud_container.test: expected a no-op resource action, got "update" action with plan(.?)`),
8987
},
9088
},
9189
})
@@ -120,11 +118,10 @@ func TestTest_TestStep_ImportBlockId_FailWhenPlannableImportIsNotSupported(t *te
120118
location = "eastus"
121119
name = "somevalue"
122120
}`,
123-
ResourceName: "examplecloud_container.test",
124-
ImportState: true,
125-
ImportStateKind: r.ImportBlockWithID,
126-
ImportStateVerify: true,
127-
ExpectError: regexp.MustCompile(`Terraform 1.5.0`),
121+
ResourceName: "examplecloud_container.test",
122+
ImportState: true,
123+
ImportStateKind: r.ImportBlockWithID,
124+
ExpectError: regexp.MustCompile(`Terraform 1.5.0`),
128125
},
129126
},
130127
})

helper/resource/testing.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,8 @@ type TestStep struct {
700700
// [plancheck]: https://pkg.go.dev/github.com/hashicorp/terraform-plugin-testing/plancheck
701701
ImportPlanChecks ImportPlanChecks
702702

703+
ImportPlanVerify bool
704+
703705
// ImportStateVerify, if true, will also check that the state values
704706
// that are finally put into the state after import match for all the
705707
// IDs returned by the Import. Note that this checks for strict equality

helper/resource/testing_new_import_state.go

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest
185185
return err
186186
}
187187

188-
if plan.ResourceChanges != nil {
188+
if len(plan.ResourceChanges) > 0 {
189189
logging.HelperResourceDebug(ctx, fmt.Sprintf("ImportBlockWithId: %d resource changes", len(plan.ResourceChanges)))
190190

191191
if err := requireNoopResourceAction(ctx, t, plan, resourceName, importWd, providers); err != nil {
@@ -194,6 +194,54 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest
194194
}
195195

196196
// TODO compare plan to state from previous step
197+
if len(plan.ResourceChanges) > 0 {
198+
logging.HelperResourceDebug(ctx, fmt.Sprintf("ImportBlockWithId: %d resource changes", len(plan.ResourceChanges)))
199+
200+
oldResources := make(map[string]*terraform.ResourceState)
201+
for logicalResourceName, resourceState := range state.RootModule().Resources {
202+
if !strings.HasPrefix(logicalResourceName, "data.") {
203+
oldResources[logicalResourceName] = resourceState
204+
}
205+
}
206+
if step.ImportPlanVerify {
207+
for _, rc := range plan.ResourceChanges {
208+
if rc.Change == nil || rc.Change.Actions == nil {
209+
// does this matter?
210+
continue
211+
}
212+
213+
if !rc.Change.Actions.NoOp() {
214+
return fmt.Errorf("importing resource %s: expected a no-op resource action, got %q action", rc.Address, rc.Change.Actions)
215+
}
216+
217+
if rc.Change.Importing == nil {
218+
return fmt.Errorf("importing resource %s: expected importing to be true", rc.Address)
219+
}
220+
221+
after, ok := rc.Change.After.(map[string]interface{})
222+
if !ok {
223+
panic(fmt.Sprintf("unexpected type %T", rc.Change.After))
224+
}
225+
226+
for k, v := range after {
227+
vs, ok := v.(string)
228+
if !ok {
229+
panic(fmt.Sprintf("unexpected type %T", v))
230+
}
231+
oldResource := oldResources[rc.Address]
232+
if oldResource == nil {
233+
// does this matter?
234+
return fmt.Errorf("importing resource %s: expected resource %s to exist", rc.Address, rc.Change.Importing.ID)
235+
}
236+
attr, ok := oldResource.Primary.Attributes[k]
237+
if attr != vs {
238+
return fmt.Errorf("importing resource %s: expected %s to be %q, got %q", rc.Address, k, oldResource.Primary.Attributes[k], vs)
239+
}
240+
}
241+
}
242+
}
243+
244+
}
197245

198246
if err := runPlanChecks(ctx, t, plan, step.ImportPlanChecks.PreApply); err != nil {
199247
return err

0 commit comments

Comments
 (0)