-
Notifications
You must be signed in to change notification settings - Fork 16
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 all 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,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 |
---|---|---|
|
@@ -58,10 +58,11 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest | |
|
||
// get state from check sequence | ||
var state *terraform.State | ||
var stateJSON *tfjson.State | ||
var err error | ||
|
||
err = runProviderCommand(ctx, t, func() error { | ||
state, err = getState(ctx, t, wd) | ||
stateJSON, state, err = getState(ctx, t, wd) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -72,7 +73,7 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest | |
} | ||
|
||
// Determine the ID to import | ||
var importId string | ||
var importId string //nolint:revive | ||
bbasata marked this conversation as resolved.
Show resolved
Hide resolved
|
||
switch { | ||
case step.ImportStateIdFunc != nil: | ||
logging.HelperResourceTrace(ctx, "Using TestStep ImportStateIdFunc for import identifier") | ||
|
@@ -185,37 +186,20 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest | |
return err | ||
} | ||
|
||
if plan.ResourceChanges != nil { | ||
if len(plan.ResourceChanges) > 0 { | ||
logging.HelperResourceDebug(ctx, fmt.Sprintf("ImportBlockWithId: %d resource changes", len(plan.ResourceChanges))) | ||
|
||
for _, rc := range plan.ResourceChanges { | ||
if rc.Address != resourceName { | ||
// we're only interested in the changes for the resource being imported | ||
continue | ||
} | ||
if rc.Change != nil && rc.Change.Actions != nil { | ||
// should this be length checked and used as a condition, if it's a no-op then there shouldn't be any other changes here | ||
for _, action := range rc.Change.Actions { | ||
if action != "no-op" { | ||
var stdout string | ||
err = runProviderCommand(ctx, t, func() error { | ||
var err error | ||
stdout, err = importWd.SavedPlanRawStdout(ctx) | ||
return err | ||
}, importWd, providers) | ||
if err != nil { | ||
return fmt.Errorf("retrieving formatted plan output: %w", err) | ||
} | ||
|
||
return fmt.Errorf("importing resource %s: expected a no-op resource action, got %q action with plan \nstdout:\n\n%s", rc.Address, action, stdout) | ||
} | ||
} | ||
if err := requireNoopResourceAction(ctx, t, plan, resourceName, importWd, providers); err != nil { | ||
return err | ||
} | ||
|
||
if step.ImportPlanVerify { | ||
if err := teststep.VerifyImportPlan(plan, stateJSON); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
// TODO compare plan to state from previous step | ||
|
||
if err := runPlanChecks(ctx, t, plan, step.ImportPlanChecks.PreApply); err != nil { | ||
return err | ||
} | ||
|
@@ -230,7 +214,7 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest | |
|
||
var importState *terraform.State | ||
err = runProviderCommand(ctx, t, func() error { | ||
importState, err = getState(ctx, t, importWd) | ||
_, importState, err = getState(ctx, t, importWd) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -275,6 +259,10 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest | |
identifierAttribute = "id" | ||
} | ||
|
||
if len(newResources) == 0 { | ||
return fmt.Errorf("ImportStateVerify: no new resources imported") | ||
} | ||
|
||
for _, r := range newResources { | ||
rIdentifier, ok := r.Primary.Attributes[identifierAttribute] | ||
|
||
|
@@ -389,6 +377,46 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest | |
return nil | ||
} | ||
|
||
func requireNoopResourceAction(ctx context.Context, t testing.T, plan *tfjson.Plan, resourceName string, importWd *plugintest.WorkingDir, providers *providerFactories) error { | ||
t.Helper() | ||
|
||
rc := findResourceChangeInPlan(t, plan, resourceName) | ||
if rc == nil || rc.Change == nil || rc.Change.Actions == nil { | ||
// does this matter? | ||
return nil | ||
Comment on lines
+385
to
+386
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. 🤔 - What this would be signifying is that there were planned changes, but they weren't to the resource we were testing. I'm actually not sure what we should do here, but I'm leaning towards throwing an error? I can't really see why you'd want/have this scenario in your test. Something like: "expected entire plan to be no-op, found resource "" with changes", might need to refactor a little to get the affected resource name in the error here. 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. The other scenario this could indicate is that there were no actual changes, but we found the resource (i.e. Terraform has a bug I'd imagine?) |
||
} | ||
|
||
// should this be length checked and used as a condition, if it's a no-op then there shouldn't be any other changes here | ||
for _, action := range rc.Change.Actions { | ||
if action != tfjson.ActionNoop { | ||
var stdout string | ||
err := runProviderCommand(ctx, t, func() error { | ||
var err error | ||
stdout, err = importWd.SavedPlanRawStdout(ctx) | ||
return err | ||
}, importWd, providers) | ||
if err != nil { | ||
return fmt.Errorf("retrieving formatted plan output: %w", err) | ||
} | ||
|
||
return fmt.Errorf("importing resource %s: expected a no-op resource action, got %q action with plan \nstdout:\n\n%s", rc.Address, action, stdout) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func findResourceChangeInPlan(t testing.T, plan *tfjson.Plan, resourceName string) *tfjson.ResourceChange { | ||
t.Helper() | ||
|
||
for _, rc := range plan.ResourceChanges { | ||
if rc.Address == resourceName { | ||
return rc | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func appendImportBlock(config string, resourceName string, importID string) string { | ||
return config + fmt.Sprintf(``+"\n"+ | ||
`import {`+"\n"+ | ||
|
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: 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