Skip to content

Commit 2761fa9

Browse files
committed
update testing of new import states to support generating import blocks with an id string and calling apply
1 parent 35f4e6f commit 2761fa9

File tree

3 files changed

+60
-21
lines changed

3 files changed

+60
-21
lines changed

helper/resource/testing.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,14 @@ type ExternalProvider struct {
454454
Source string // the provider source
455455
}
456456

457+
type ImportStateKind byte
458+
459+
const (
460+
ImportCommandWithId ImportStateKind = iota
461+
ImportBlockWithId
462+
ImportBlockWithResourceIdentity
463+
)
464+
457465
// TestStep is a single apply sequence of a test, done within the
458466
// context of a state.
459467
//
@@ -633,6 +641,8 @@ type TestStep struct {
633641
// ID of that resource.
634642
ImportState bool
635643

644+
ImportStateKind ImportStateKind
645+
636646
// ImportStateId is the ID to perform an ImportState operation with.
637647
// This is optional. If it isn't set, then the resource ID is automatically
638648
// determined by inspecting the state for ResourceName's ID.

helper/resource/testing_new.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest
133133

134134
// use this to track last step successfully applied
135135
// acts as default for import tests
136-
var appliedCfg teststep.Config
136+
var appliedCfg string
137137
var stepNumber int
138138

139139
for stepIndex, step := range c.Steps {
@@ -426,7 +426,7 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest
426426
}
427427
}
428428

429-
mergedConfig, err := step.mergedConfig(ctx, c, hasTerraformBlock, hasProviderBlock, helper.TerraformVersion())
429+
appliedCfg, err = step.mergedConfig(ctx, c, hasTerraformBlock, hasProviderBlock, helper.TerraformVersion())
430430

431431
if err != nil {
432432
logging.HelperResourceError(ctx,
@@ -436,18 +436,6 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest
436436
t.Fatalf("Error generating merged configuration: %s", err)
437437
}
438438

439-
confRequest := teststep.PrepareConfigurationRequest{
440-
Directory: step.ConfigDirectory,
441-
File: step.ConfigFile,
442-
Raw: mergedConfig,
443-
TestStepConfigRequest: config.TestStepConfigRequest{
444-
StepNumber: stepIndex + 1,
445-
TestName: t.Name(),
446-
},
447-
}.Exec()
448-
449-
appliedCfg = teststep.Configuration(confRequest)
450-
451439
logging.HelperResourceDebug(ctx, "Finished TestStep")
452440

453441
continue

helper/resource/testing_new_import_state.go

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package resource
66
import (
77
"context"
88
"fmt"
9+
"github.com/hashicorp/terraform-exec/tfexec"
910
"reflect"
1011
"strings"
1112

@@ -20,7 +21,7 @@ import (
2021
"github.com/hashicorp/terraform-plugin-testing/internal/plugintest"
2122
)
2223

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 {
24+
func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest.Helper, wd *plugintest.WorkingDir, step TestStep, cfgRaw string, providers *providerFactories, stepIndex int) error {
2425
t.Helper()
2526

2627
configRequest := teststep.PrepareConfigurationRequest{
@@ -93,11 +94,40 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest
9394

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

97+
if testStepConfig == nil {
98+
logging.HelperResourceTrace(ctx, "Using prior TestStep Config for import")
99+
}
100+
96101
// Create working directory for import tests
97102
if testStepConfig == nil {
98103
logging.HelperResourceTrace(ctx, "Using prior TestStep Config for import")
99104

100-
testStepConfig = cfg
105+
switch step.ImportStateKind {
106+
case ImportBlockWithResourceIdentity:
107+
t.Fatalf("TODO implement me")
108+
case ImportBlockWithId:
109+
cfgRaw += fmt.Sprintf(`
110+
import {
111+
to = %s
112+
id = %q
113+
}
114+
`, step.ResourceName, importId)
115+
default:
116+
// Not an import block test so nothing to do here
117+
}
118+
119+
confRequest := teststep.PrepareConfigurationRequest{
120+
Directory: step.ConfigDirectory,
121+
File: step.ConfigFile,
122+
Raw: cfgRaw,
123+
TestStepConfigRequest: config.TestStepConfigRequest{
124+
StepNumber: stepIndex + 1,
125+
TestName: t.Name(),
126+
},
127+
}.Exec()
128+
129+
testStepConfig = teststep.Configuration(confRequest)
130+
//testStepConfig = cfg
101131
if testStepConfig == nil {
102132
t.Fatal("Cannot import state with no specified config")
103133
}
@@ -129,11 +159,22 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest
129159
}
130160
}
131161

132-
err = runProviderCommand(ctx, t, func() error {
133-
return importWd.Import(ctx, step.ResourceName, importId)
134-
}, importWd, providers)
135-
if err != nil {
136-
return err
162+
if step.ImportStateKind == ImportBlockWithResourceIdentity || step.ImportStateKind == ImportBlockWithId {
163+
var opts []tfexec.ApplyOption
164+
165+
err = runProviderCommand(ctx, t, func() error {
166+
return importWd.Apply(ctx, opts...)
167+
}, importWd, providers)
168+
if err != nil {
169+
return err
170+
}
171+
} else {
172+
err = runProviderCommand(ctx, t, func() error {
173+
return importWd.Import(ctx, step.ResourceName, importId)
174+
}, importWd, providers)
175+
if err != nil {
176+
return err
177+
}
137178
}
138179

139180
var importState *terraform.State

0 commit comments

Comments
 (0)