Skip to content

Commit f9fc468

Browse files
committed
Merge branch 'import-compare-plan-to-previous-state' into import-block-with-resource-identity
2 parents 955994d + f5f93c1 commit f9fc468

13 files changed

+514
-88
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package importstate_test
5+
6+
import (
7+
"testing"
8+
9+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
10+
11+
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider"
12+
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver"
13+
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
14+
"github.com/hashicorp/terraform-plugin-testing/plancheck"
15+
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
16+
"github.com/hashicorp/terraform-plugin-testing/tfversion"
17+
18+
r "github.com/hashicorp/terraform-plugin-testing/helper/resource"
19+
)
20+
21+
func Test_ImportBlock_AsFirstStep(t *testing.T) {
22+
t.Parallel()
23+
24+
r.UnitTest(t, r.TestCase{
25+
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
26+
tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithID requires Terraform 1.5.0 or later
27+
},
28+
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
29+
"examplecloud": providerserver.NewProviderServer(testprovider.Provider{
30+
Resources: map[string]testprovider.Resource{
31+
"examplecloud_container": examplecloudResource(),
32+
},
33+
}),
34+
},
35+
Steps: []r.TestStep{
36+
{
37+
ResourceName: "examplecloud_container.test",
38+
ImportStateId: "examplecloud_container.test",
39+
ImportState: true,
40+
ImportStateKind: r.ImportBlockWithID,
41+
Config: `resource "examplecloud_container" "test" {
42+
name = "somevalue"
43+
location = "westeurope"
44+
}`,
45+
ImportPlanChecks: r.ImportPlanChecks{
46+
PreApply: []plancheck.PlanCheck{
47+
plancheck.ExpectResourceAction("examplecloud_container.test", plancheck.ResourceActionNoop),
48+
plancheck.ExpectKnownValue("examplecloud_container.test", tfjsonpath.New("id"), knownvalue.StringExact("westeurope/somevalue")),
49+
plancheck.ExpectKnownValue("examplecloud_container.test", tfjsonpath.New("name"), knownvalue.StringExact("somevalue")),
50+
plancheck.ExpectKnownValue("examplecloud_container.test", tfjsonpath.New("location"), knownvalue.StringExact("westeurope")),
51+
},
52+
},
53+
},
54+
},
55+
})
56+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package importstate_test
5+
6+
import (
7+
"testing"
8+
9+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
10+
11+
"github.com/hashicorp/terraform-plugin-testing/config"
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/tfversion"
15+
16+
r "github.com/hashicorp/terraform-plugin-testing/helper/resource"
17+
)
18+
19+
func Test_ImportBlock_InConfigDirectory(t *testing.T) {
20+
t.Parallel()
21+
22+
r.UnitTest(t, r.TestCase{
23+
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
24+
tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithID requires Terraform 1.5.0 or later
25+
},
26+
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
27+
"examplecloud": providerserver.NewProviderServer(testprovider.Provider{
28+
Resources: map[string]testprovider.Resource{
29+
"examplecloud_container": examplecloudResource(),
30+
},
31+
}),
32+
},
33+
Steps: []r.TestStep{
34+
{
35+
ConfigDirectory: func(config.TestStepConfigRequest) string {
36+
return `testdata/1`
37+
},
38+
},
39+
{
40+
ResourceName: "examplecloud_container.test",
41+
ImportState: true,
42+
ImportStateKind: r.ImportBlockWithID,
43+
ImportPlanVerify: true,
44+
ConfigDirectory: func(config.TestStepConfigRequest) string {
45+
return `testdata/2`
46+
},
47+
},
48+
},
49+
})
50+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package importstate_test
5+
6+
import (
7+
"testing"
8+
9+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
10+
11+
"github.com/hashicorp/terraform-plugin-testing/config"
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/tfversion"
15+
16+
r "github.com/hashicorp/terraform-plugin-testing/helper/resource"
17+
)
18+
19+
func Test_ImportBlock_InConfigFile(t *testing.T) {
20+
t.Parallel()
21+
22+
r.UnitTest(t, r.TestCase{
23+
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
24+
tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithID requires Terraform 1.5.0 or later
25+
},
26+
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
27+
"examplecloud": providerserver.NewProviderServer(testprovider.Provider{
28+
Resources: map[string]testprovider.Resource{
29+
"examplecloud_container": examplecloudResource(),
30+
},
31+
}),
32+
},
33+
Steps: []r.TestStep{
34+
{
35+
ConfigFile: func(config.TestStepConfigRequest) string {
36+
return `testdata/1/examplecloud_container.tf`
37+
},
38+
},
39+
{
40+
ResourceName: "examplecloud_container.test",
41+
ImportState: true,
42+
ImportStateKind: r.ImportBlockWithID,
43+
ImportPlanVerify: true,
44+
ConfigFile: func(config.TestStepConfigRequest) string {
45+
return `testdata/2/examplecloud_container_import.tf`
46+
},
47+
},
48+
},
49+
})
50+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package importstate_test
5+
6+
import (
7+
"testing"
8+
9+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
10+
11+
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider"
12+
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver"
13+
"github.com/hashicorp/terraform-plugin-testing/tfversion"
14+
15+
r "github.com/hashicorp/terraform-plugin-testing/helper/resource"
16+
)
17+
18+
func Test_ImportBlock_VerifyPlan(t *testing.T) {
19+
t.Parallel()
20+
21+
r.UnitTest(t, r.TestCase{
22+
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
23+
tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithID requires Terraform 1.5.0 or later
24+
},
25+
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
26+
"examplecloud": providerserver.NewProviderServer(testprovider.Provider{
27+
Resources: map[string]testprovider.Resource{
28+
"examplecloud_container": examplecloudResource(),
29+
},
30+
}),
31+
},
32+
Steps: []r.TestStep{
33+
{
34+
Config: `
35+
resource "examplecloud_container" "test" {
36+
location = "westeurope"
37+
name = "somevalue"
38+
}`,
39+
},
40+
{
41+
ResourceName: "examplecloud_container.test",
42+
ImportState: true,
43+
ImportStateKind: r.ImportBlockWithID,
44+
ImportPlanVerify: true,
45+
},
46+
},
47+
})
48+
}

helper/resource/importstate/import_block_with_id_test.go

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ import (
2121
r "github.com/hashicorp/terraform-plugin-testing/helper/resource"
2222
)
2323

24-
func TestTest_TestStep_ImportBlockId(t *testing.T) {
24+
func Test_TestStep_ImportBlockId(t *testing.T) {
2525
t.Parallel()
2626

2727
r.UnitTest(t, r.TestCase{
2828
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
29-
tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithId requires Terraform 1.5.0 or later
29+
tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithID requires Terraform 1.5.0 or later
3030
},
3131
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
3232
"examplecloud": providerserver.NewProviderServer(testprovider.Provider{
@@ -44,10 +44,9 @@ func TestTest_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
})
@@ -58,7 +57,7 @@ func TestTest_TestStep_ImportBlockId_ExpectError(t *testing.T) {
5857

5958
r.UnitTest(t, r.TestCase{
6059
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
61-
tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithId requires Terraform 1.5.0 or later
60+
tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithID requires Terraform 1.5.0 or later
6261
},
6362
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
6463
"examplecloud": providerserver.NewProviderServer(testprovider.Provider{
@@ -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 should be a no-op, but got action update 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
})
@@ -135,7 +132,7 @@ func TestTest_TestStep_ImportBlockId_SkipDataSourceState(t *testing.T) {
135132

136133
r.UnitTest(t, r.TestCase{
137134
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
138-
tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithId requires Terraform 1.5.0 or later
135+
tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithID requires Terraform 1.5.0 or later
139136

140137
},
141138
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
@@ -161,7 +158,7 @@ func TestTest_TestStep_ImportBlockId_SkipDataSourceState(t *testing.T) {
161158
{
162159
ResourceName: "examplecloud_thing.test",
163160
ImportState: true,
164-
ImportStateKind: r.ImportBlockWithId,
161+
ImportStateKind: r.ImportBlockWithID,
165162
ImportStateCheck: func(is []*terraform.InstanceState) error {
166163
if len(is) > 1 {
167164
return fmt.Errorf("expected 1 state, got: %d", len(is))
@@ -200,11 +197,16 @@ func TestTest_TestStep_ImportBlockId_ImportStateVerifyIgnore_Real_Example(t *tes
200197
I also need to omit the `password` in the import config, otherwise the value in the config is used when importing the
201198
with an import block and the test ends up passing regardless of whether `ImportStateVerifyIgnore` has been specified or not
202199
*/
200+
201+
// In prerelease, we are choosing that ImportBlockWithID will not perform an apply, so it will not produce a new state,
202+
// and there is no new state for ImportStateVerify to do anything meaningful with.
203+
t.Skip()
204+
203205
t.Parallel()
204206

205207
r.UnitTest(t, r.TestCase{
206208
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
207-
tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithId requires Terraform 1.5.0 or later
209+
tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithID requires Terraform 1.5.0 or later
208210
},
209211
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
210212
"examplecloud": providerserver.NewProviderServer(testprovider.Provider{
@@ -292,7 +294,7 @@ func TestTest_TestStep_ImportBlockId_ImportStateVerifyIgnore_Real_Example(t *tes
292294
}`,
293295
ResourceName: "examplecloud_container.test",
294296
ImportState: true,
295-
ImportStateKind: r.ImportBlockWithId,
297+
ImportStateKind: r.ImportBlockWithID,
296298
ImportStateVerify: true,
297299
ImportStateVerifyIgnore: []string{"password"},
298300
},
@@ -301,11 +303,15 @@ func TestTest_TestStep_ImportBlockId_ImportStateVerifyIgnore_Real_Example(t *tes
301303
}
302304

303305
func TestTest_TestStep_ImportBlockId_ImportStateVerifyIgnore(t *testing.T) {
306+
// In prerelease, we are choosing that ImportBlockWithID will not perform an apply, so it will not produce a new state,
307+
// and there is no new state for ImportStateVerify to do anything meaningful with.
308+
t.Skip()
309+
304310
t.Parallel()
305311

306312
r.UnitTest(t, r.TestCase{
307313
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
308-
tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithId requires Terraform 1.5.0 or later
314+
tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithID requires Terraform 1.5.0 or later
309315
},
310316
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
311317
"examplecloud": providerserver.NewProviderServer(testprovider.Provider{
@@ -377,7 +383,7 @@ func TestTest_TestStep_ImportBlockId_ImportStateVerifyIgnore(t *testing.T) {
377383
{
378384
ResourceName: "examplecloud_container.test",
379385
ImportState: true,
380-
ImportStateKind: r.ImportBlockWithId,
386+
ImportStateKind: r.ImportBlockWithID,
381387
ImportStateVerify: true,
382388
ImportStateVerifyIgnore: []string{"password"},
383389
},

0 commit comments

Comments
 (0)