Skip to content

Commit f7076a9

Browse files
committed
Add a test expecting fail when plannable import is not supported
1 parent 4b76aba commit f7076a9

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

helper/resource/importstate/examplecloud_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
14
package importstate_test
25

36
import (

helper/resource/importstate/import_block_with_id_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,42 @@ func TestTest_TestStep_ImportBlockId_ExpectError(t *testing.T) {
9191
})
9292
}
9393

94+
func TestTest_TestStep_ImportBlockId_FailWhenPlannableImportIsNotSupported(t *testing.T) {
95+
t.Setenv("TF_ACC_TERRAFORM_PATH", "")
96+
t.Setenv("TF_ACC_TERRAFORM_VERSION", "1.4.7") // Plannable import is supported in Terraform 1.5.0 and later
97+
98+
r.UnitTest(t, r.TestCase{
99+
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
100+
"examplecloud": providerserver.NewProviderServer(testprovider.Provider{
101+
Resources: map[string]testprovider.Resource{
102+
"examplecloud_container": examplecloudResource(),
103+
},
104+
}),
105+
},
106+
Steps: []r.TestStep{
107+
{
108+
Config: `
109+
resource "examplecloud_container" "test" {
110+
location = "westeurope"
111+
name = "somevalue"
112+
}`,
113+
},
114+
{
115+
Config: `
116+
resource "examplecloud_container" "test" {
117+
location = "eastus"
118+
name = "somevalue"
119+
}`,
120+
ResourceName: "examplecloud_container.test",
121+
ImportState: true,
122+
ImportStateKind: r.ImportBlockWithId,
123+
ImportStateVerify: true,
124+
ExpectError: regexp.MustCompile(`Terraform 1.5.0`),
125+
},
126+
},
127+
})
128+
}
129+
94130
func TestTest_TestStep_ImportBlockId_SkipDataSourceState(t *testing.T) {
95131
t.Parallel()
96132

helper/resource/testing_new_import_state.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ package resource
66
import (
77
"context"
88
"fmt"
9-
tfjson "github.com/hashicorp/terraform-json"
109
"reflect"
1110
"strings"
1211

12+
"github.com/hashicorp/go-version"
13+
tfjson "github.com/hashicorp/terraform-json"
14+
1315
"github.com/google/go-cmp/cmp"
1416
"github.com/mitchellh/go-testing-interface"
1517

@@ -21,6 +23,21 @@ import (
2123
"github.com/hashicorp/terraform-plugin-testing/terraform"
2224
)
2325

26+
func requirePlannableImport(t testing.T, versionUnderTest version.Version) error {
27+
t.Helper()
28+
29+
minVersion, err := version.NewVersion("1.5.0")
30+
if err != nil {
31+
panic("failed to parse version string")
32+
}
33+
34+
if versionUnderTest.LessThan(minVersion) {
35+
return fmt.Errorf("ImportState steps require Terraform 1.5.0 or later")
36+
}
37+
38+
return nil
39+
}
40+
2441
func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest.Helper, wd *plugintest.WorkingDir, step TestStep, cfgRaw string, providers *providerFactories, stepNumber int) error {
2542
t.Helper()
2643

@@ -32,6 +49,13 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest
3249
}
3350
}
3451

52+
{
53+
err := requirePlannableImport(t, *helper.TerraformVersion())
54+
if err != nil {
55+
return err
56+
}
57+
}
58+
3559
configRequest := teststep.PrepareConfigurationRequest{
3660
Directory: step.ConfigDirectory,
3761
File: step.ConfigFile,

0 commit comments

Comments
 (0)