Skip to content

Commit 3673753

Browse files
stephybunbbasata
andauthored
Add additional import test modes (#442)
* add test file for import block with id * update testing of new import states to support generating import blocks with an id string and calling apply * import ordering * remove duplicate nil check on testStepConfig * update comment * replace instances of stepIndex + 1 with stepNumber * extend test cases for import block tests * add comments to import kind enums * add validation to prevent new import kinds to be used with config files or directories * run plan and check for no-op when importing via a block * update comment on real example test to reflect current understanding --------- Co-authored-by: Baraa Basata <[email protected]>
1 parent 3d87b79 commit 3673753

File tree

4 files changed

+667
-30
lines changed

4 files changed

+667
-30
lines changed

helper/resource/testing.go

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

457+
type ImportStateKind byte
458+
459+
const (
460+
// ImportCommandWithId imports the state using the import command
461+
ImportCommandWithId ImportStateKind = iota
462+
// ImportBlockWithId imports the state using an import block with an ID
463+
ImportBlockWithId
464+
// ImportBlockWithResourceIdentity imports the state using an import block with a resource identity
465+
ImportBlockWithResourceIdentity
466+
)
467+
457468
// TestStep is a single apply sequence of a test, done within the
458469
// context of a state.
459470
//
@@ -633,6 +644,8 @@ type TestStep struct {
633644
// ID of that resource.
634645
ImportState bool
635646

647+
ImportStateKind ImportStateKind
648+
636649
// ImportStateId is the ID to perform an ImportState operation with.
637650
// This is optional. If it isn't set, then the resource ID is automatically
638651
// determined by inspecting the state for ResourceName's ID.

helper/resource/testing_new.go

Lines changed: 4 additions & 16 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 {
@@ -249,7 +249,7 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest
249249
File: step.ConfigFile,
250250
Raw: rawCfg,
251251
TestStepConfigRequest: config.TestStepConfigRequest{
252-
StepNumber: stepIndex + 1,
252+
StepNumber: stepNumber,
253253
TestName: t.Name(),
254254
},
255255
}.Exec()
@@ -289,7 +289,7 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest
289289
if step.ImportState {
290290
logging.HelperResourceTrace(ctx, "TestStep is ImportState mode")
291291

292-
err := testStepNewImportState(ctx, t, helper, wd, step, appliedCfg, providers, stepIndex)
292+
err := testStepNewImportState(ctx, t, helper, wd, step, appliedCfg, providers, stepNumber)
293293
if step.ExpectError != nil {
294294
logging.HelperResourceDebug(ctx, "Checking TestStep ExpectError")
295295
if err == nil {
@@ -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

0 commit comments

Comments
 (0)