Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions helper/resource/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,14 @@ type ExternalProvider struct {
Source string // the provider source
}

type ImportStateKind byte

const (
ImportCommandWithId ImportStateKind = iota
ImportBlockWithId
ImportBlockWithResourceIdentity
)

// TestStep is a single apply sequence of a test, done within the
// context of a state.
//
Expand Down Expand Up @@ -633,6 +641,8 @@ type TestStep struct {
// ID of that resource.
ImportState bool

ImportStateKind ImportStateKind

// ImportStateId is the ID to perform an ImportState operation with.
// This is optional. If it isn't set, then the resource ID is automatically
// determined by inspecting the state for ResourceName's ID.
Expand Down
16 changes: 2 additions & 14 deletions helper/resource/testing_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest

// use this to track last step successfully applied
// acts as default for import tests
var appliedCfg teststep.Config
var appliedCfg string
var stepNumber int

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

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

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

confRequest := teststep.PrepareConfigurationRequest{
Directory: step.ConfigDirectory,
File: step.ConfigFile,
Raw: mergedConfig,
TestStepConfigRequest: config.TestStepConfigRequest{
StepNumber: stepIndex + 1,
TestName: t.Name(),
},
}.Exec()

appliedCfg = teststep.Configuration(confRequest)

Comment on lines -439 to -450
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this was only ever needed and done for the import tests, so I've opted to move this into testing_new_import_state.go and pass the appliedConfig through as a string so it can be easily appended/modified based on the import mode/kind we're using in the test.

logging.HelperResourceDebug(ctx, "Finished TestStep")

continue
Expand Down
104 changes: 104 additions & 0 deletions helper/resource/testing_new_import_block_id_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package resource

import (
"testing"

"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tftypes"

"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/internal/testing/testsdk/resource"
"github.com/hashicorp/terraform-plugin-testing/tfversion"
)

func TestTest_TestStep_ImportBlockId(t *testing.T) {
t.Parallel()

UnitTest(t, TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_5_0), // ProtoV6ProviderFactories
},
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"examplecloud": providerserver.NewProviderServer(testprovider.Provider{
Resources: map[string]testprovider.Resource{
"examplecloud_container": {
CreateResponse: &resource.CreateResponse{
NewState: tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"id": tftypes.String,
"location": tftypes.String,
"name": tftypes.String,
},
},
map[string]tftypes.Value{
"id": tftypes.NewValue(tftypes.String, "westeurope/somevalue"),
"location": tftypes.NewValue(tftypes.String, "westeurope"),
"name": tftypes.NewValue(tftypes.String, "somevalue"),
},
),
},
ImportStateResponse: &resource.ImportStateResponse{
State: tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"id": tftypes.String,
"location": tftypes.String,
"name": tftypes.String,
},
},
map[string]tftypes.Value{
"id": tftypes.NewValue(tftypes.String, "westeurope/somevalue"),
"location": tftypes.NewValue(tftypes.String, "westeurope"),
"name": tftypes.NewValue(tftypes.String, "somevalue"),
},
),
},
SchemaResponse: &resource.SchemaResponse{
Schema: &tfprotov6.Schema{
Block: &tfprotov6.SchemaBlock{
Attributes: []*tfprotov6.SchemaAttribute{
{
Name: "id",
Type: tftypes.String,
Computed: true,
},
{
Name: "location",
Type: tftypes.String,
Required: true,
},
{
Name: "name",
Type: tftypes.String,
Required: true,
},
},
},
},
},
},
},
}),
},
Steps: []TestStep{
{
Config: `
resource "examplecloud_container" "test" {
location = "westeurope"
name = "somevalue"
}`,
},
{
ResourceName: "examplecloud_container.test",
ImportState: true,
ImportStateKind: ImportBlockWithId,
ImportStateVerify: true,
},
},
})
}
57 changes: 46 additions & 11 deletions helper/resource/testing_new_import_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/mitchellh/go-testing-interface"

"github.com/hashicorp/terraform-exec/tfexec"
"github.com/hashicorp/terraform-plugin-testing/config"
"github.com/hashicorp/terraform-plugin-testing/internal/teststep"
"github.com/hashicorp/terraform-plugin-testing/terraform"

"github.com/hashicorp/terraform-plugin-testing/internal/logging"
"github.com/hashicorp/terraform-plugin-testing/internal/plugintest"
"github.com/hashicorp/terraform-plugin-testing/internal/teststep"
"github.com/hashicorp/terraform-plugin-testing/terraform"
)

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

configRequest := teststep.PrepareConfigurationRequest{
Expand Down Expand Up @@ -93,11 +93,35 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest

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

// Create working directory for import tests
// Prepare the test config dependent on the kind of import test being performed
if testStepConfig == nil {
logging.HelperResourceTrace(ctx, "Using prior TestStep Config for import")

testStepConfig = cfg
switch step.ImportStateKind {
case ImportBlockWithResourceIdentity:
t.Fatalf("TODO implement me")
case ImportBlockWithId:
cfgRaw += fmt.Sprintf(`
import {
to = %s
id = %q
}
`, step.ResourceName, importId)
default:
// Not an import block test so nothing to do here
}

confRequest := teststep.PrepareConfigurationRequest{
Directory: step.ConfigDirectory,
File: step.ConfigFile,
Raw: cfgRaw,
TestStepConfigRequest: config.TestStepConfigRequest{
StepNumber: stepIndex + 1,
TestName: t.Name(),
},
}.Exec()

testStepConfig = teststep.Configuration(confRequest)
if testStepConfig == nil {
t.Fatal("Cannot import state with no specified config")
}
Expand Down Expand Up @@ -129,11 +153,22 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest
}
}

err = runProviderCommand(ctx, t, func() error {
return importWd.Import(ctx, step.ResourceName, importId)
}, importWd, providers)
if err != nil {
return err
if step.ImportStateKind == ImportBlockWithResourceIdentity || step.ImportStateKind == ImportBlockWithId {
var opts []tfexec.ApplyOption

err = runProviderCommand(ctx, t, func() error {
return importWd.Apply(ctx, opts...)
}, importWd, providers)
if err != nil {
return err
}
} else {
err = runProviderCommand(ctx, t, func() error {
return importWd.Import(ctx, step.ResourceName, importId)
}, importWd, providers)
if err != nil {
return err
}
}

var importState *terraform.State
Expand Down
Loading