-
Notifications
You must be signed in to change notification settings - Fork 17
importstate: compare plan to previous state #467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 27 commits
2e6c321
288ec26
e924715
4b76aba
f7076a9
6865074
70f740d
d8f5ccb
5e5f6d6
0872e07
5d6bf83
f45d8fe
a36cde2
3b822d6
3134be2
99f1fae
42f36b7
024ac49
bd29ab0
c0fd026
b0a502e
4cf6a87
f085ea6
eb4679f
3a7afd5
f5f93c1
58ec6b8
9643933
f9f6ac4
2b23914
c3a6290
b0abb5b
8242c05
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| // Copyright (c) HashiCorp, Inc. | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| package importstate_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-go/tfprotov6" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider" | ||
| "github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver" | ||
| "github.com/hashicorp/terraform-plugin-testing/knownvalue" | ||
| "github.com/hashicorp/terraform-plugin-testing/plancheck" | ||
| "github.com/hashicorp/terraform-plugin-testing/tfjsonpath" | ||
| "github.com/hashicorp/terraform-plugin-testing/tfversion" | ||
|
|
||
| r "github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
| ) | ||
|
|
||
| func Test_ImportBlock_AsFirstStep(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| r.UnitTest(t, r.TestCase{ | ||
| TerraformVersionChecks: []tfversion.TerraformVersionCheck{ | ||
| tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithID requires Terraform 1.5.0 or later | ||
| }, | ||
| ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ | ||
| "examplecloud": providerserver.NewProviderServer(testprovider.Provider{ | ||
| Resources: map[string]testprovider.Resource{ | ||
| "examplecloud_container": examplecloudResource(), | ||
| }, | ||
| }), | ||
| }, | ||
| Steps: []r.TestStep{ | ||
| { | ||
| ResourceName: "examplecloud_container.test", | ||
| ImportStateId: "examplecloud_container.test", | ||
| ImportState: true, | ||
| ImportStateKind: r.ImportBlockWithID, | ||
| Config: `resource "examplecloud_container" "test" { | ||
| name = "somevalue" | ||
| location = "westeurope" | ||
| }`, | ||
| ImportPlanChecks: r.ImportPlanChecks{ | ||
| PreApply: []plancheck.PlanCheck{ | ||
| plancheck.ExpectResourceAction("examplecloud_container.test", plancheck.ResourceActionNoop), | ||
| plancheck.ExpectKnownValue("examplecloud_container.test", tfjsonpath.New("id"), knownvalue.StringExact("westeurope/somevalue")), | ||
| plancheck.ExpectKnownValue("examplecloud_container.test", tfjsonpath.New("name"), knownvalue.StringExact("somevalue")), | ||
| plancheck.ExpectKnownValue("examplecloud_container.test", tfjsonpath.New("location"), knownvalue.StringExact("westeurope")), | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| // Copyright (c) HashiCorp, Inc. | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| package importstate_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-go/tfprotov6" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-testing/config" | ||
| "github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider" | ||
| "github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver" | ||
| "github.com/hashicorp/terraform-plugin-testing/tfversion" | ||
|
|
||
| r "github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
| ) | ||
|
|
||
| func Test_ImportBlock_InConfigDirectory(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| r.UnitTest(t, r.TestCase{ | ||
| TerraformVersionChecks: []tfversion.TerraformVersionCheck{ | ||
| tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithID requires Terraform 1.5.0 or later | ||
| }, | ||
| ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ | ||
| "examplecloud": providerserver.NewProviderServer(testprovider.Provider{ | ||
| Resources: map[string]testprovider.Resource{ | ||
| "examplecloud_container": examplecloudResource(), | ||
| }, | ||
| }), | ||
| }, | ||
| Steps: []r.TestStep{ | ||
| { | ||
| ConfigDirectory: func(config.TestStepConfigRequest) string { | ||
| return `testdata/1` | ||
| }, | ||
| }, | ||
| { | ||
| ResourceName: "examplecloud_container.test", | ||
| ImportState: true, | ||
| ImportStateKind: r.ImportBlockWithID, | ||
| ImportPlanVerify: true, | ||
| ConfigDirectory: func(config.TestStepConfigRequest) string { | ||
| return `testdata/2` | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| // Copyright (c) HashiCorp, Inc. | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| package importstate_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-go/tfprotov6" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-testing/config" | ||
| "github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider" | ||
| "github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver" | ||
| "github.com/hashicorp/terraform-plugin-testing/tfversion" | ||
|
|
||
| r "github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
| ) | ||
|
|
||
| func Test_ImportBlock_InConfigFile(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| r.UnitTest(t, r.TestCase{ | ||
| TerraformVersionChecks: []tfversion.TerraformVersionCheck{ | ||
| tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithID requires Terraform 1.5.0 or later | ||
| }, | ||
| ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ | ||
| "examplecloud": providerserver.NewProviderServer(testprovider.Provider{ | ||
| Resources: map[string]testprovider.Resource{ | ||
| "examplecloud_container": examplecloudResource(), | ||
| }, | ||
| }), | ||
| }, | ||
| Steps: []r.TestStep{ | ||
| { | ||
| ConfigFile: func(config.TestStepConfigRequest) string { | ||
| return `testdata/1/examplecloud_container.tf` | ||
| }, | ||
| }, | ||
| { | ||
| ResourceName: "examplecloud_container.test", | ||
| ImportState: true, | ||
| ImportStateKind: r.ImportBlockWithID, | ||
| ImportPlanVerify: true, | ||
| ConfigFile: func(config.TestStepConfigRequest) string { | ||
| return `testdata/2/examplecloud_container_import.tf` | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| // Copyright (c) HashiCorp, Inc. | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| package importstate_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-go/tfprotov6" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider" | ||
| "github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver" | ||
| "github.com/hashicorp/terraform-plugin-testing/tfversion" | ||
|
|
||
| r "github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
| ) | ||
|
|
||
| func Test_ImportBlock_VerifyPlan(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| r.UnitTest(t, r.TestCase{ | ||
| TerraformVersionChecks: []tfversion.TerraformVersionCheck{ | ||
| tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithID requires Terraform 1.5.0 or later | ||
| }, | ||
| ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ | ||
| "examplecloud": providerserver.NewProviderServer(testprovider.Provider{ | ||
| Resources: map[string]testprovider.Resource{ | ||
| "examplecloud_container": examplecloudResource(), | ||
| }, | ||
| }), | ||
| }, | ||
| Steps: []r.TestStep{ | ||
| { | ||
| Config: ` | ||
| resource "examplecloud_container" "test" { | ||
| location = "westeurope" | ||
| name = "somevalue" | ||
| }`, | ||
| }, | ||
| { | ||
| ResourceName: "examplecloud_container.test", | ||
| ImportState: true, | ||
| ImportStateKind: r.ImportBlockWithID, | ||
| ImportPlanVerify: true, | ||
| }, | ||
| }, | ||
| }) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,12 +21,12 @@ import ( | |
| r "github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
| ) | ||
|
|
||
| func TestTest_TestStep_ImportBlockId(t *testing.T) { | ||
| func Test_TestStep_ImportBlockId(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| r.UnitTest(t, r.TestCase{ | ||
| TerraformVersionChecks: []tfversion.TerraformVersionCheck{ | ||
| tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithId requires Terraform 1.5.0 or later | ||
| tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithID requires Terraform 1.5.0 or later | ||
| }, | ||
| ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ | ||
| "examplecloud": providerserver.NewProviderServer(testprovider.Provider{ | ||
|
|
@@ -44,10 +44,9 @@ func TestTest_TestStep_ImportBlockId(t *testing.T) { | |
| }`, | ||
| }, | ||
| { | ||
| ResourceName: "examplecloud_container.test", | ||
| ImportState: true, | ||
| ImportStateKind: r.ImportBlockWithId, | ||
| ImportStateVerify: true, | ||
| ResourceName: "examplecloud_container.test", | ||
| ImportState: true, | ||
| ImportStateKind: r.ImportBlockWithID, | ||
|
||
| }, | ||
| }, | ||
| }) | ||
|
|
@@ -58,7 +57,7 @@ func TestTest_TestStep_ImportBlockId_ExpectError(t *testing.T) { | |
|
|
||
| r.UnitTest(t, r.TestCase{ | ||
| TerraformVersionChecks: []tfversion.TerraformVersionCheck{ | ||
| tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithId requires Terraform 1.5.0 or later | ||
| tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithID requires Terraform 1.5.0 or later | ||
| }, | ||
| ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ | ||
| "examplecloud": providerserver.NewProviderServer(testprovider.Provider{ | ||
|
|
@@ -81,11 +80,10 @@ func TestTest_TestStep_ImportBlockId_ExpectError(t *testing.T) { | |
| location = "eastus" | ||
| name = "somevalue" | ||
| }`, | ||
| ResourceName: "examplecloud_container.test", | ||
| ImportState: true, | ||
| ImportStateKind: r.ImportBlockWithId, | ||
| ImportStateVerify: true, | ||
| ExpectError: regexp.MustCompile(`importing resource examplecloud_container.test should be a no-op, but got action update with plan(.?)`), | ||
| ResourceName: "examplecloud_container.test", | ||
| ImportState: true, | ||
| ImportStateKind: r.ImportBlockWithID, | ||
| ExpectError: regexp.MustCompile(`importing resource examplecloud_container.test: expected a no-op resource action, got "update" action with plan(.?)`), | ||
| }, | ||
| }, | ||
| }) | ||
|
|
@@ -120,11 +118,10 @@ func TestTest_TestStep_ImportBlockId_FailWhenPlannableImportIsNotSupported(t *te | |
| location = "eastus" | ||
| name = "somevalue" | ||
| }`, | ||
| ResourceName: "examplecloud_container.test", | ||
| ImportState: true, | ||
| ImportStateKind: r.ImportBlockWithId, | ||
| ImportStateVerify: true, | ||
| ExpectError: regexp.MustCompile(`Terraform 1.5.0`), | ||
| ResourceName: "examplecloud_container.test", | ||
| ImportState: true, | ||
| ImportStateKind: r.ImportBlockWithID, | ||
| ExpectError: regexp.MustCompile(`Terraform 1.5.0`), | ||
| }, | ||
| }, | ||
| }) | ||
|
|
@@ -135,7 +132,7 @@ func TestTest_TestStep_ImportBlockId_SkipDataSourceState(t *testing.T) { | |
|
|
||
| r.UnitTest(t, r.TestCase{ | ||
| TerraformVersionChecks: []tfversion.TerraformVersionCheck{ | ||
| tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithId requires Terraform 1.5.0 or later | ||
| tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithID requires Terraform 1.5.0 or later | ||
|
|
||
| }, | ||
| ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ | ||
|
|
@@ -161,7 +158,7 @@ func TestTest_TestStep_ImportBlockId_SkipDataSourceState(t *testing.T) { | |
| { | ||
| ResourceName: "examplecloud_thing.test", | ||
| ImportState: true, | ||
| ImportStateKind: r.ImportBlockWithId, | ||
| ImportStateKind: r.ImportBlockWithID, | ||
| ImportStateCheck: func(is []*terraform.InstanceState) error { | ||
| if len(is) > 1 { | ||
| return fmt.Errorf("expected 1 state, got: %d", len(is)) | ||
|
|
@@ -200,11 +197,16 @@ func TestTest_TestStep_ImportBlockId_ImportStateVerifyIgnore_Real_Example(t *tes | |
| I also need to omit the `password` in the import config, otherwise the value in the config is used when importing the | ||
| with an import block and the test ends up passing regardless of whether `ImportStateVerifyIgnore` has been specified or not | ||
| */ | ||
|
|
||
| // In prerelease, we are choosing that ImportBlockWithID will not perform an apply, so it will not produce a new state, | ||
| // and there is no new state for ImportStateVerify to do anything meaningful with. | ||
|
Comment on lines
+204
to
+205
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Are we planning on revisiting this decision later? If so, maybe a TODO prefix here (I know I'll forget what the original intention was of this comment 😆) Same question for the other test below |
||
| t.Skip() | ||
|
|
||
| t.Parallel() | ||
|
|
||
| r.UnitTest(t, r.TestCase{ | ||
| TerraformVersionChecks: []tfversion.TerraformVersionCheck{ | ||
| tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithId requires Terraform 1.5.0 or later | ||
| tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithID requires Terraform 1.5.0 or later | ||
| }, | ||
| ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ | ||
| "examplecloud": providerserver.NewProviderServer(testprovider.Provider{ | ||
|
|
@@ -292,7 +294,7 @@ func TestTest_TestStep_ImportBlockId_ImportStateVerifyIgnore_Real_Example(t *tes | |
| }`, | ||
| ResourceName: "examplecloud_container.test", | ||
| ImportState: true, | ||
| ImportStateKind: r.ImportBlockWithId, | ||
| ImportStateKind: r.ImportBlockWithID, | ||
| ImportStateVerify: true, | ||
| ImportStateVerifyIgnore: []string{"password"}, | ||
| }, | ||
|
|
@@ -301,11 +303,15 @@ func TestTest_TestStep_ImportBlockId_ImportStateVerifyIgnore_Real_Example(t *tes | |
| } | ||
|
|
||
| func TestTest_TestStep_ImportBlockId_ImportStateVerifyIgnore(t *testing.T) { | ||
| // In prerelease, we are choosing that ImportBlockWithID will not perform an apply, so it will not produce a new state, | ||
| // and there is no new state for ImportStateVerify to do anything meaningful with. | ||
| t.Skip() | ||
|
|
||
| t.Parallel() | ||
|
|
||
| r.UnitTest(t, r.TestCase{ | ||
| TerraformVersionChecks: []tfversion.TerraformVersionCheck{ | ||
| tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithId requires Terraform 1.5.0 or later | ||
| tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithID requires Terraform 1.5.0 or later | ||
| }, | ||
| ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ | ||
| "examplecloud": providerserver.NewProviderServer(testprovider.Provider{ | ||
|
|
@@ -377,7 +383,7 @@ func TestTest_TestStep_ImportBlockId_ImportStateVerifyIgnore(t *testing.T) { | |
| { | ||
| ResourceName: "examplecloud_container.test", | ||
| ImportState: true, | ||
| ImportStateKind: r.ImportBlockWithId, | ||
| ImportStateKind: r.ImportBlockWithID, | ||
| ImportStateVerify: true, | ||
| ImportStateVerifyIgnore: []string{"password"}, | ||
| }, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I'd love to see a negative test here as well, like one where the follow-up plan has changes.
Might need to do some awkwardness to create that "inconsistency" without having a remote system, especially since the end of test step 1 will refresh. In the past I've used the provider factories on each test step to inject the differences, which is a potential option: https://github.com/hashicorp/terraform-provider-corner/blob/52e22f4ee878680727ff68a2e44967b496242a76/internal/protocolv6provider/writeonly_datacheck_test.go#L182-L204