Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 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
14 changes: 14 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 (
TerraformImportCommand ImportStateKind = iota
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Naming is ... naming.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The thought is: we're expanding to 3 sub-modes of import testing. Make it explicit, not inferred.

TerraformImportCommand is what we do today & will be the compatibility default.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Suggested change
TerraformImportCommand ImportStateKind = iota
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,12 @@ type TestStep struct {
// ID of that resource.
ImportState bool

ImportStateKind ImportStateKind // or ImportStateStrategy or ImportStateSubmode or ImportStateFlavor or ...

// ImportStateBlockConfig, if non-empty, supplies declarative import
// configuration. This is (?mutually exclusive of ImportStateID + ResourceName?).
ImportStateBlockConfig string
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If we can generate config on our first try, then the usefulness of this is secondary.


// 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
100 changes: 100 additions & 0 deletions helper/resource/testing_new_import_block_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// 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_ImportBlockVerify(t *testing.T) {
t.Parallel()

UnitTest(t, TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
// import blocks are only available in v1.5.0 and later
tfversion.SkipBelow(tfversion.Version1_5_0),
},
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"examplecloud": providerserver.NewProviderServer(testprovider.Provider{
Resources: map[string]testprovider.Resource{
"examplecloud_bucket": exampleCloudBucketResource(t),
},
}),
},
Steps: []TestStep{
{
Config: `
resource "examplecloud_bucket" "storage" {
bucket = "test-bucket"
description = "A bucket for testing."
}`,
},
{
ImportState: true,
ImportStateKind: ImportBlockWithResourceIdentity,
ResourceName: "examplecloud_bucket.storage",
},
},
})
}

func exampleCloudBucketResource(t *testing.T) testprovider.Resource {
t.Helper()

return testprovider.Resource{
CreateResponse: &resource.CreateResponse{
NewResourceIdentityData: tftypes.NewValue(
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

New and shiny!

tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"region": tftypes.String,
"bucket": tftypes.String,
},
},
map[string]tftypes.Value{
"region": tftypes.NewValue(tftypes.String, "test-region"),
"bucket": tftypes.NewValue(tftypes.String, "test-bucket"),
},
),
NewState: tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"bucket": tftypes.String,
"description": tftypes.String,
},
},
map[string]tftypes.Value{
"bucket": tftypes.NewValue(tftypes.String, "test-bucket"),
"description": tftypes.NewValue(tftypes.String, "A bucket for testing."),
},
),
},
ImportStateResponse: &resource.ImportStateResponse{},
SchemaResponse: &resource.SchemaResponse{
Schema: &tfprotov6.Schema{
Block: &tfprotov6.SchemaBlock{
Attributes: []*tfprotov6.SchemaAttribute{
{
Name: "bucket",
Type: tftypes.String,
Required: true,
},
{
Name: "description",
Type: tftypes.String,
Optional: true,
},
},
},
},
},
}
}
26 changes: 14 additions & 12 deletions helper/resource/testing_new_import_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,23 +118,25 @@
t.Fatalf("Error setting test config: %s", err)
}

logging.HelperResourceDebug(ctx, "Running Terraform CLI init and import")
switch step.ImportStateKind {
case TerraformImportCommand:
logging.HelperResourceDebug(ctx, "Running Terraform CLI init and import")

if !step.ImportStatePersist {
err = runProviderCommand(ctx, t, func() error {
return importWd.Init(ctx)
}, importWd, providers)
if err != nil {
t.Fatalf("Error running init: %s", err)
}
}

if !step.ImportStatePersist {
err = runProviderCommand(ctx, t, func() error {
return importWd.Init(ctx)
return importWd.Import(ctx, step.ResourceName, importId)
}, importWd, providers)
if err != nil {
t.Fatalf("Error running init: %s", err)
return err
}
}

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

var importState *terraform.State
err = runProviderCommand(ctx, t, func() error {
Expand Down Expand Up @@ -315,4 +317,4 @@
}

return nil
}

Check failure on line 320 in helper/resource/testing_new_import_state.go

View workflow job for this annotation

GitHub Actions / golangci-lint

expected '}', found 'EOF' (typecheck)
12 changes: 10 additions & 2 deletions internal/testing/testsdk/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ type CreateRequest struct {
}

type CreateResponse struct {
Diagnostics []*tfprotov6.Diagnostic
NewState tftypes.Value
Diagnostics []*tfprotov6.Diagnostic
NewState tftypes.Value
NewResourceIdentityData tftypes.Value
}

type DeleteRequest struct {
Expand Down Expand Up @@ -70,6 +71,13 @@ type ReadResponse struct {
NewState tftypes.Value
}

type ResourceIdentitySchemaRequest struct{}

type ResourceIdentitySchemaResponse struct {
Diagnostics []*tfprotov6.Diagnostic
ResourceIdentitySchema tftypes.Value // *tfprotov6.Schema
}

type SchemaRequest struct{}

type SchemaResponse struct {
Expand Down
Loading