Skip to content

Commit 58900fa

Browse files
committed
Error on use of ImportStateVerify with plannable import
1 parent e96524f commit 58900fa

File tree

4 files changed

+38
-44
lines changed

4 files changed

+38
-44
lines changed

helper/resource/importstate/import_block_in_config_directory_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,10 @@ 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+
ConfigDirectory: config.StaticDirectory(`testdata/2`),
4342
},
4443
},
4544
})

helper/resource/importstate/import_block_in_config_file_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,10 @@ 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+
ConfigFile: config.StaticFile(`testdata/2/examplecloud_container_import.tf`),
4342
},
4443
},
4544
})

helper/resource/importstate/import_block_with_id_test.go

Lines changed: 13 additions & 19 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
})
@@ -290,11 +288,9 @@ func TestTest_TestStep_ImportBlockId_ImportStateVerifyIgnore_Real_Example(t *tes
290288
resource "examplecloud_container" "test" {
291289
name = "somename"
292290
}`,
293-
ResourceName: "examplecloud_container.test",
294-
ImportState: true,
295-
ImportStateKind: r.ImportBlockWithID,
296-
ImportStateVerify: true,
297-
ImportStateVerifyIgnore: []string{"password"},
291+
ResourceName: "examplecloud_container.test",
292+
ImportState: true,
293+
ImportStateKind: r.ImportBlockWithID,
298294
},
299295
},
300296
})
@@ -375,11 +371,9 @@ func TestTest_TestStep_ImportBlockId_ImportStateVerifyIgnore(t *testing.T) {
375371
Config: `resource "examplecloud_container" "test" {}`,
376372
},
377373
{
378-
ResourceName: "examplecloud_container.test",
379-
ImportState: true,
380-
ImportStateKind: r.ImportBlockWithID,
381-
ImportStateVerify: true,
382-
ImportStateVerifyIgnore: []string{"password"},
374+
ResourceName: "examplecloud_container.test",
375+
ImportState: true,
376+
ImportStateKind: r.ImportBlockWithID,
383377
},
384378
},
385379
})

helper/resource/testing_new_import_state.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import (
99
"reflect"
1010
"strings"
1111

12-
"github.com/hashicorp/go-version"
13-
1412
tfjson "github.com/hashicorp/terraform-json"
1513

1614
"github.com/google/go-cmp/cmp"
@@ -32,17 +30,8 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest
3230
kind := step.ImportStateKind
3331
importStatePersist := step.ImportStatePersist
3432

35-
if kind.plannable() {
36-
// Instead of calling [t.Fatal], return an error. This package's unit tests can use [TestStep.ExpectError] to match on the error message.
37-
// An alternative, [plugintest.TestExpectTFatal], does not have access to logged error messages, so it is open to false positives on this
38-
// complex code path.
39-
if err := requirePlannableImport(t, *helper.TerraformVersion()); err != nil {
40-
return err
41-
}
42-
43-
if importStatePersist {
44-
return fmt.Errorf("ImportStatePersist is not supported with plannable import blocks")
45-
}
33+
if err := importStatePreconditions(ctx, t, helper, step, cfgRaw); err != nil {
34+
return err
4635
}
4736

4837
configRequest := teststep.PrepareConfigurationRequest{
@@ -408,15 +397,28 @@ func appendImportBlock(config string, resourceName string, importID string) stri
408397
resourceName, importID)
409398
}
410399

411-
func requirePlannableImport(t testing.T, versionUnderTest version.Version) error {
400+
func importStatePreconditions(ctx context.Context, t testing.T, helper *plugintest.Helper, step TestStep, cfgRaw string) error {
412401
t.Helper()
413402

414-
if versionUnderTest.LessThan(tfversion.Version1_5_0) {
403+
kind := step.ImportStateKind
404+
versionUnderTest := *helper.TerraformVersion()
405+
406+
// Instead of calling [t.Fatal], we return an error. This package's unit tests can use [TestStep.ExpectError] to match
407+
// on the error message. An alternative, [plugintest.TestExpectTFatal], does not have access to logged error messages,
408+
// so it is open to false positives on this complex code path.
409+
switch {
410+
case kind.plannable() && versionUnderTest.LessThan(tfversion.Version1_5_0):
415411
return fmt.Errorf(
416412
`ImportState steps using plannable import blocks require Terraform 1.5.0 or later. Either ` +
417413
`upgrade the Terraform version running the test or add a ` + "`TerraformVersionChecks`" + ` to ` +
418414
`the test case to skip this test.` + "\n\n" +
419415
`https://developer.hashicorp.com/terraform/plugin/testing/acceptance-tests/tfversion-checks#skip-version-checks`)
416+
417+
case kind.plannable() && step.ImportStatePersist:
418+
return fmt.Errorf(`ImportStatePersist is not supported with plannable import blocks`)
419+
420+
case kind.plannable() && step.ImportStateVerify:
421+
return fmt.Errorf(`ImportStateVerify is not supported with plannable import blocks`)
420422
}
421423

422424
return nil

0 commit comments

Comments
 (0)