Skip to content

Commit bc4b461

Browse files
committed
a passing ImportBlockWithResourceIdentity test that really uses an ID
1 parent ff949e2 commit bc4b461

File tree

3 files changed

+62
-17
lines changed

3 files changed

+62
-17
lines changed

helper/resource/testing_new.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest
134134
// use this to track last step successfully applied
135135
// acts as default for import tests
136136
var appliedCfg teststep.Config
137+
var appliedMergedCfg string
137138
var stepNumber int
138139

139140
for stepIndex, step := range c.Steps {
@@ -289,7 +290,7 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest
289290
if step.ImportState {
290291
logging.HelperResourceTrace(ctx, "TestStep is ImportState mode")
291292

292-
err := testStepNewImportState(ctx, t, helper, wd, step, appliedCfg, providers, stepIndex)
293+
err := testStepNewImportState(ctx, t, helper, wd, step, appliedCfg, appliedMergedCfg, providers, stepIndex)
293294
if step.ExpectError != nil {
294295
logging.HelperResourceDebug(ctx, "Checking TestStep ExpectError")
295296
if err == nil {
@@ -446,7 +447,8 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest
446447
},
447448
}.Exec()
448449

449-
appliedCfg = teststep.Configuration(confRequest)
450+
appliedCfg = teststep.Configuration(confRequest) // magical
451+
appliedMergedCfg = mergedConfig
450452

451453
logging.HelperResourceDebug(ctx, "Finished TestStep")
452454

helper/resource/testing_new_import_block_test.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ func TestTest_TestStep_ImportBlockVerify(t *testing.T) {
3939
}`,
4040
},
4141
{
42-
ImportState: true,
43-
ImportStateKind: ImportBlockWithResourceIdentity,
44-
ResourceName: "examplecloud_bucket.storage",
42+
ImportState: true,
43+
ImportStateKind: ImportBlockWithResourceIdentity,
44+
ImportStateVerify: true,
45+
ImportStateVerifyIdentifierAttribute: "bucket", // upgrade to resource identity
46+
ResourceName: "examplecloud_bucket.storage",
4547
},
4648
},
4749
})
@@ -77,7 +79,20 @@ func exampleCloudBucketResource(t *testing.T) testprovider.Resource {
7779
},
7880
),
7981
},
80-
ImportStateResponse: &resource.ImportStateResponse{},
82+
ImportStateResponse: &resource.ImportStateResponse{
83+
State: tftypes.NewValue(
84+
tftypes.Object{
85+
AttributeTypes: map[string]tftypes.Type{
86+
"bucket": tftypes.String,
87+
"description": tftypes.String,
88+
},
89+
},
90+
map[string]tftypes.Value{
91+
"bucket": tftypes.NewValue(tftypes.String, "test-bucket"),
92+
"description": tftypes.NewValue(tftypes.String, "A bucket for testing."),
93+
},
94+
),
95+
},
8196
SchemaResponse: &resource.SchemaResponse{
8297
Schema: &tfprotov6.Schema{
8398
Block: &tfprotov6.SchemaBlock{

helper/resource/testing_new_import_state.go

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"github.com/hashicorp/terraform-plugin-testing/internal/plugintest"
2121
)
2222

23-
func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest.Helper, wd *plugintest.WorkingDir, step TestStep, cfg teststep.Config, providers *providerFactories, stepIndex int) error {
23+
func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest.Helper, wd *plugintest.WorkingDir, step TestStep, cfg teststep.Config, cfgHcl string, providers *providerFactories, stepIndex int) error {
2424
t.Helper()
2525

2626
configRequest := teststep.PrepareConfigurationRequest{
@@ -31,9 +31,7 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest
3131
StepNumber: stepIndex + 1,
3232
TestName: t.Name(),
3333
},
34-
}.Exec()
35-
36-
testStepConfig := teststep.Configuration(configRequest)
34+
}
3735

3836
if step.ResourceName == "" {
3937
t.Fatal("ResourceName is required for an import state test")
@@ -93,13 +91,24 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest
9391

9492
logging.HelperResourceTrace(ctx, fmt.Sprintf("Using import identifier: %s", importId))
9593

96-
// Create working directory for import tests
97-
if testStepConfig == nil {
98-
logging.HelperResourceTrace(ctx, "Using prior TestStep Config for import")
99-
100-
testStepConfig = cfg
94+
var testStepConfig teststep.Config
95+
if step.ImportStateKind == ImportBlockWithResourceIdentity {
96+
cfgHcl += `
97+
import {
98+
to = examplecloud_bucket.storage
99+
id = "test-bucket" // to be replaced with identity
100+
}`
101+
configRequest.Raw = cfgHcl
102+
testStepConfig = teststep.Configuration(configRequest.Exec())
103+
} else {
104+
// Create working directory for import tests
101105
if testStepConfig == nil {
102-
t.Fatal("Cannot import state with no specified config")
106+
logging.HelperResourceTrace(ctx, "Using prior TestStep Config for import")
107+
108+
testStepConfig = cfg
109+
if testStepConfig == nil {
110+
t.Fatal("Cannot import state with no specified config")
111+
}
103112
}
104113
}
105114

@@ -140,7 +149,26 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest
140149
case ImportBlockWithId:
141150
t.Fatalf("not yet implemented")
142151
case ImportBlockWithResourceIdentity:
143-
t.Fatalf("not yet implemented")
152+
if !step.ImportStatePersist {
153+
err = runProviderCommand(ctx, t, func() error {
154+
return importWd.Init(ctx)
155+
}, importWd, providers)
156+
if err != nil {
157+
t.Fatalf("Error running init: %s", err)
158+
}
159+
}
160+
err = runProviderCommand(ctx, t, func() error {
161+
return importWd.CreatePlan(ctx)
162+
}, importWd, providers)
163+
if err != nil {
164+
t.Fatalf("Error running plan: %s", err)
165+
}
166+
err = runProviderCommand(ctx, t, func() error {
167+
return importWd.Apply(ctx)
168+
}, importWd, providers)
169+
if err != nil {
170+
t.Fatalf("Error running apply: %s", err)
171+
}
144172
default:
145173
t.Fatalf(`\o/`)
146174
}

0 commit comments

Comments
 (0)