-
Notifications
You must be signed in to change notification settings - Fork 16
importstate: a working example of ImportState as the first TestStep #461
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
7f09062
381042c
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,58 @@ | ||
// 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, | ||
// ImportStateVerify: true, | ||
Config: `resource "examplecloud_container" "test" { | ||
name = "somevalue" | ||
location = "westeurope" | ||
}`, | ||
ImportStatePersist: true, | ||
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,64 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package importstate_test | ||
|
||
import ( | ||
"fmt" | ||
"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/terraform" | ||
"github.com/hashicorp/terraform-plugin-testing/tfversion" | ||
|
||
r "github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
) | ||
|
||
func Test_ImportCommand_AsFirstStep(t *testing.T) { | ||
t.Parallel() | ||
|
||
r.UnitTest(t, r.TestCase{ | ||
TerraformVersionChecks: []tfversion.TerraformVersionCheck{ | ||
tfversion.SkipBelow(tfversion.Version1_0_0), // ProtoV6ProviderFactories needs Terraform 1.0.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.ImportCommandWithId, | ||
// ImportStateVerify: true, | ||
Config: `resource "examplecloud_container" "test" { | ||
name = "somevalue" | ||
location = "westeurope" | ||
}`, | ||
ImportStatePersist: true, | ||
ImportStateCheck: func(states []*terraform.InstanceState) error { | ||
if len(states) != 1 { | ||
return fmt.Errorf("expected 1 state; got %d", len(states)) | ||
} | ||
if states[0].ID != "westeurope/somevalue" { | ||
return fmt.Errorf("unexpected ID: %s", states[0].ID) | ||
} | ||
if states[0].Attributes["name"] != "somevalue" { | ||
return fmt.Errorf("unexpected name: %s", states[0].Attributes["name"]) | ||
} | ||
if states[0].Attributes["location"] != "westeurope" { | ||
return fmt.Errorf("unexpected location: %s", states[0].Attributes["location"]) | ||
} | ||
return nil | ||
}, | ||
}, | ||
}, | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -178,30 +178,31 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest | |
t.Fatalf("Error setting test config: %s", err) | ||
} | ||
|
||
logging.HelperResourceDebug(ctx, "Running Terraform CLI init and import") | ||
|
||
if !step.ImportStatePersist { | ||
err = runProviderCommand(ctx, t, func() error { | ||
logging.HelperResourceDebug(ctx, "Run terraform init") | ||
return importWd.Init(ctx) | ||
}, importWd, providers) | ||
if err != nil { | ||
t.Fatalf("Error running init: %s", err) | ||
} | ||
} | ||
|
||
var plan *tfjson.Plan | ||
if step.ImportStateKind == ImportBlockWithResourceIdentity || step.ImportStateKind == ImportBlockWithId { | ||
var opts []tfexec.PlanOption | ||
|
||
err = runProviderCommand(ctx, t, func() error { | ||
logging.HelperResourceDebug(ctx, "Run terraform plan") | ||
return importWd.CreatePlan(ctx, opts...) | ||
}, importWd, providers) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var plan *tfjson.Plan | ||
err = runProviderCommand(ctx, t, func() error { | ||
var err error | ||
logging.HelperResourceDebug(ctx, "Run terraform show") | ||
plan, err = importWd.SavedPlan(ctx) | ||
return err | ||
}, importWd, providers) | ||
|
@@ -210,6 +211,8 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest | |
} | ||
|
||
if plan.ResourceChanges != nil { | ||
logging.HelperResourceDebug(ctx, fmt.Sprintf("ImportBlockWithId: %d resource changes", len(plan.ResourceChanges))) | ||
|
||
for _, rc := range plan.ResourceChanges { | ||
if rc.Address != step.ResourceName { | ||
// we're only interested in the changes for the resource being imported | ||
|
@@ -237,8 +240,14 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest | |
} | ||
|
||
// TODO compare plan to state from previous step | ||
|
||
if err := runPlanChecks(ctx, t, plan, step.ImportPlanChecks.PreApply); err != nil { | ||
return err | ||
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: Should we wrap the error messages produced by the plan checks for readability? return fmt.Errorf("Import plan check(s) failed:\n%w", err) 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. 💯 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. ✅ Noted for a follow-up PR -- it will be easier to see the current picture after we merge this & the two PRs "stacked" atop it. |
||
} | ||
|
||
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. Not directly related to this PR, but I noticed that we don't immediately return after running this import/plan and we probably should? Or wrap the remaining of the logic in this function in the following else block. 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. ✅ Noted for a follow-up PR -- it will be easier to see the current picture after we merge this & the two PRs "stacked" atop it. |
||
} else { | ||
err = runProviderCommand(ctx, t, func() error { | ||
logging.HelperResourceDebug(ctx, "Run terraform import") | ||
return importWd.Import(ctx, step.ResourceName, importId) | ||
}, importWd, providers) | ||
if err != nil { | ||
|
@@ -258,6 +267,8 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest | |
t.Fatalf("Error getting state: %s", err) | ||
} | ||
|
||
logging.HelperResourceDebug(ctx, fmt.Sprintf("State after import: %d resources in the root module", len(importState.RootModule().Resources))) | ||
|
||
// Go through the imported state and verify | ||
if step.ImportStateCheck != nil { | ||
logging.HelperResourceTrace(ctx, "Using TestStep ImportStateCheck") | ||
|
Uh oh!
There was an error while loading. Please reload this page.