Skip to content

Commit daee136

Browse files
committed
how about this
1 parent 55b88aa commit daee136

File tree

4 files changed

+97
-74
lines changed

4 files changed

+97
-74
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+
TerraformImportCommand 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 // or ImportStateStrategy or ImportStateSubmode or ImportStateFlavor or ...
645+
636646
// ImportStateBlockConfig, if non-empty, supplies declarative import
637647
// configuration. This is (?mutually exclusive of ImportStateID + ResourceName?).
638648
ImportStateBlockConfig string

helper/resource/testing_new_import_block_test.go

Lines changed: 63 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -20,78 +20,81 @@ func TestTest_TestStep_ImportBlockVerify(t *testing.T) {
2020

2121
UnitTest(t, TestCase{
2222
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
23-
tfversion.SkipBelow(tfversion.Version1_5_0), // import blocks are only available in v1.5.0 and later
23+
// import blocks are only available in v1.5.0 and later
24+
tfversion.SkipBelow(tfversion.Version1_5_0),
2425
},
2526
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
2627
"examplecloud": providerserver.NewProviderServer(testprovider.Provider{
2728
Resources: map[string]testprovider.Resource{
28-
"examplecloud_thing": {
29-
CreateResponse: &resource.CreateResponse{
30-
NewState: tftypes.NewValue(
31-
tftypes.Object{
32-
AttributeTypes: map[string]tftypes.Type{
33-
"id": tftypes.String,
34-
"other": tftypes.String,
35-
},
36-
},
37-
map[string]tftypes.Value{
38-
"id": tftypes.NewValue(tftypes.String, "resource-test"),
39-
"other": tftypes.NewValue(tftypes.String, "testvalue"),
40-
},
41-
),
42-
},
43-
ImportStateResponse: &resource.ImportStateResponse{
44-
State: tftypes.NewValue(
45-
tftypes.Object{
46-
AttributeTypes: map[string]tftypes.Type{
47-
"id": tftypes.String,
48-
"other": tftypes.String,
49-
},
50-
},
51-
map[string]tftypes.Value{
52-
"id": tftypes.NewValue(tftypes.String, "resource-test"),
53-
"other": tftypes.NewValue(tftypes.String, "testvalue"),
54-
},
55-
),
56-
},
57-
SchemaResponse: &resource.SchemaResponse{
58-
Schema: &tfprotov6.Schema{
59-
Block: &tfprotov6.SchemaBlock{
60-
Attributes: []*tfprotov6.SchemaAttribute{
61-
{
62-
Name: "id",
63-
Type: tftypes.String,
64-
Computed: true,
65-
},
66-
{
67-
Name: "other",
68-
Type: tftypes.String,
69-
Computed: true,
70-
},
71-
},
72-
},
73-
},
74-
},
75-
},
29+
"examplecloud_bucket": exampleCloudBucketResource(t),
7630
},
7731
}),
7832
},
7933
Steps: []TestStep{
8034
{
81-
Config: `resource "examplecloud_thing" "test" {}`,
35+
Config: `
36+
resource "examplecloud_bucket" "storage" {
37+
bucket = "test-bucket"
38+
description = "A bucket for testing."
39+
}`,
8240
},
8341
{
84-
ImportState: true,
85-
ImportStateVerify: true,
86-
ImportStateBlockConfig: `
87-
import {
88-
to = examplecloud_thing.test
89-
identity = {
90-
hat = "derby"
91-
cat = "garfield"
92-
}
93-
}`,
42+
ImportState: true,
43+
ImportStateKind: ImportBlockWithResourceIdentity,
44+
ResourceName: "examplecloud_bucket.storage",
9445
},
9546
},
9647
})
9748
}
49+
50+
func exampleCloudBucketResource(t *testing.T) testprovider.Resource {
51+
t.Helper()
52+
53+
return testprovider.Resource{
54+
CreateResponse: &resource.CreateResponse{
55+
NewResourceIdentityData: tftypes.NewValue(
56+
tftypes.Object{
57+
AttributeTypes: map[string]tftypes.Type{
58+
"region": tftypes.String,
59+
"bucket": tftypes.String,
60+
},
61+
},
62+
map[string]tftypes.Value{
63+
"region": tftypes.NewValue(tftypes.String, "test-region"),
64+
"bucket": tftypes.NewValue(tftypes.String, "test-bucket"),
65+
},
66+
),
67+
NewState: tftypes.NewValue(
68+
tftypes.Object{
69+
AttributeTypes: map[string]tftypes.Type{
70+
"bucket": tftypes.String,
71+
"description": tftypes.String,
72+
},
73+
},
74+
map[string]tftypes.Value{
75+
"bucket": tftypes.NewValue(tftypes.String, "test-bucket"),
76+
"description": tftypes.NewValue(tftypes.String, "A bucket for testing."),
77+
},
78+
),
79+
},
80+
ImportStateResponse: &resource.ImportStateResponse{},
81+
SchemaResponse: &resource.SchemaResponse{
82+
Schema: &tfprotov6.Schema{
83+
Block: &tfprotov6.SchemaBlock{
84+
Attributes: []*tfprotov6.SchemaAttribute{
85+
{
86+
Name: "bucket",
87+
Type: tftypes.String,
88+
Required: true,
89+
},
90+
{
91+
Name: "description",
92+
Type: tftypes.String,
93+
Optional: true,
94+
},
95+
},
96+
},
97+
},
98+
},
99+
}
100+
}

helper/resource/testing_new_import_state.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,23 +118,25 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest
118118
t.Fatalf("Error setting test config: %s", err)
119119
}
120120

121-
logging.HelperResourceDebug(ctx, "Running Terraform CLI init and import")
121+
switch step.ImportStateKind {
122+
case TerraformImportCommand:
123+
logging.HelperResourceDebug(ctx, "Running Terraform CLI init and import")
124+
125+
if !step.ImportStatePersist {
126+
err = runProviderCommand(ctx, t, func() error {
127+
return importWd.Init(ctx)
128+
}, importWd, providers)
129+
if err != nil {
130+
t.Fatalf("Error running init: %s", err)
131+
}
132+
}
122133

123-
if !step.ImportStatePersist {
124134
err = runProviderCommand(ctx, t, func() error {
125-
return importWd.Init(ctx)
135+
return importWd.Import(ctx, step.ResourceName, importId)
126136
}, importWd, providers)
127137
if err != nil {
128-
t.Fatalf("Error running init: %s", err)
138+
return err
129139
}
130-
}
131-
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
137-
}
138140

139141
var importState *terraform.State
140142
err = runProviderCommand(ctx, t, func() error {

internal/testing/testsdk/resource/resource.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ type CreateRequest struct {
2727
}
2828

2929
type CreateResponse struct {
30-
Diagnostics []*tfprotov6.Diagnostic
31-
NewState tftypes.Value
30+
Diagnostics []*tfprotov6.Diagnostic
31+
NewState tftypes.Value
32+
NewResourceIdentityData tftypes.Value
3233
}
3334

3435
type DeleteRequest struct {
@@ -70,6 +71,13 @@ type ReadResponse struct {
7071
NewState tftypes.Value
7172
}
7273

74+
type ResourceIdentitySchemaRequest struct{}
75+
76+
type ResourceIdentitySchemaResponse struct {
77+
Diagnostics []*tfprotov6.Diagnostic
78+
ResourceIdentitySchema tftypes.Value // *tfprotov6.Schema
79+
}
80+
7381
type SchemaRequest struct{}
7482

7583
type SchemaResponse struct {

0 commit comments

Comments
 (0)