Skip to content

Commit 99e9c88

Browse files
committed
Added helper/resource parts for query
1 parent 954b002 commit 99e9c88

File tree

5 files changed

+287
-1
lines changed

5 files changed

+287
-1
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package query_test
5+
6+
import (
7+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
8+
"github.com/hashicorp/terraform-plugin-go/tftypes"
9+
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider"
10+
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/resource"
11+
"github.com/hashicorp/terraform-plugin-testing/internal/teststep"
12+
)
13+
14+
func examplecloudResource() testprovider.Resource {
15+
return testprovider.Resource{
16+
CreateResponse: &resource.CreateResponse{
17+
NewState: tftypes.NewValue(
18+
tftypes.Object{
19+
AttributeTypes: map[string]tftypes.Type{
20+
"id": tftypes.String,
21+
"location": tftypes.String,
22+
"name": tftypes.String,
23+
},
24+
},
25+
map[string]tftypes.Value{
26+
"id": tftypes.NewValue(tftypes.String, "westeurope/somevalue"),
27+
"location": tftypes.NewValue(tftypes.String, "westeurope"),
28+
"name": tftypes.NewValue(tftypes.String, "somevalue"),
29+
},
30+
),
31+
NewIdentity: teststep.Pointer(tftypes.NewValue(
32+
tftypes.Object{
33+
AttributeTypes: map[string]tftypes.Type{
34+
"id": tftypes.String,
35+
},
36+
},
37+
map[string]tftypes.Value{
38+
"id": tftypes.NewValue(tftypes.String, "westeurope/somevalue"),
39+
},
40+
)),
41+
},
42+
ReadResponse: &resource.ReadResponse{
43+
NewState: tftypes.NewValue(
44+
tftypes.Object{
45+
AttributeTypes: map[string]tftypes.Type{
46+
"id": tftypes.String,
47+
"location": tftypes.String,
48+
"name": tftypes.String,
49+
},
50+
},
51+
map[string]tftypes.Value{
52+
"id": tftypes.NewValue(tftypes.String, "westeurope/somevalue"),
53+
"location": tftypes.NewValue(tftypes.String, "westeurope"),
54+
"name": tftypes.NewValue(tftypes.String, "somevalue"),
55+
},
56+
),
57+
NewIdentity: teststep.Pointer(tftypes.NewValue(
58+
tftypes.Object{
59+
AttributeTypes: map[string]tftypes.Type{
60+
"id": tftypes.String,
61+
},
62+
},
63+
map[string]tftypes.Value{
64+
"id": tftypes.NewValue(tftypes.String, "westeurope/somevalue"),
65+
},
66+
)),
67+
},
68+
ImportStateResponse: &resource.ImportStateResponse{
69+
State: tftypes.NewValue(
70+
tftypes.Object{
71+
AttributeTypes: map[string]tftypes.Type{
72+
"id": tftypes.String,
73+
"location": tftypes.String,
74+
"name": tftypes.String,
75+
},
76+
},
77+
map[string]tftypes.Value{
78+
"id": tftypes.NewValue(tftypes.String, "westeurope/somevalue"),
79+
"location": tftypes.NewValue(tftypes.String, "westeurope"),
80+
"name": tftypes.NewValue(tftypes.String, "somevalue"),
81+
},
82+
),
83+
Identity: teststep.Pointer(tftypes.NewValue(
84+
tftypes.Object{
85+
AttributeTypes: map[string]tftypes.Type{
86+
"id": tftypes.String,
87+
},
88+
},
89+
map[string]tftypes.Value{
90+
"id": tftypes.NewValue(tftypes.String, "westeurope/somevalue"),
91+
},
92+
)),
93+
},
94+
SchemaResponse: &resource.SchemaResponse{
95+
Schema: &tfprotov6.Schema{
96+
Block: &tfprotov6.SchemaBlock{
97+
Attributes: []*tfprotov6.SchemaAttribute{
98+
ComputedStringAttribute("id"),
99+
RequiredStringAttribute("location"),
100+
RequiredStringAttribute("name"),
101+
},
102+
},
103+
},
104+
},
105+
IdentitySchemaResponse: &resource.IdentitySchemaResponse{
106+
Schema: &tfprotov6.ResourceIdentitySchema{
107+
Version: 1,
108+
IdentityAttributes: []*tfprotov6.ResourceIdentitySchemaAttribute{
109+
{
110+
Name: "id",
111+
Type: tftypes.String,
112+
RequiredForImport: true,
113+
},
114+
},
115+
},
116+
},
117+
}
118+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package query_test
5+
6+
import (
7+
"testing"
8+
9+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
10+
r "github.com/hashicorp/terraform-plugin-testing/helper/resource"
11+
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider"
12+
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/list"
13+
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver"
14+
"github.com/hashicorp/terraform-plugin-testing/tfversion"
15+
)
16+
17+
func TestQuery(t *testing.T) {
18+
t.Parallel()
19+
20+
r.UnitTest(t, r.TestCase{
21+
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
22+
tfversion.SkipBelow(tfversion.Version1_13_0), // Query mode requires Terraform 1.13.0 or later
23+
},
24+
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
25+
"examplecloud": providerserver.NewProviderServer(testprovider.Provider{
26+
ListResources: map[string]testprovider.ListResource{
27+
"examplecloud_containerette": {
28+
SchemaResponse: &list.SchemaResponse{
29+
Schema: &tfprotov6.Schema{
30+
Block: &tfprotov6.SchemaBlock{
31+
Attributes: []*tfprotov6.SchemaAttribute{
32+
ComputedStringAttribute("id"),
33+
},
34+
},
35+
},
36+
},
37+
ListResultsStream: &list.ListResultsStream{
38+
Results: func(push func(list.ListResult) bool) {
39+
},
40+
},
41+
},
42+
},
43+
Resources: map[string]testprovider.Resource{
44+
"examplecloud_containerette": examplecloudResource(),
45+
},
46+
}),
47+
},
48+
Steps: []r.TestStep{
49+
{
50+
Query: true,
51+
Config: `
52+
provider "examplecloud" {}
53+
list "examplecloud_containerette" "test" {
54+
provider = examplecloud
55+
56+
config {
57+
id = "bat"
58+
}
59+
}`,
60+
},
61+
},
62+
})
63+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package query_test
5+
6+
import (
7+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
8+
"github.com/hashicorp/terraform-plugin-go/tftypes"
9+
)
10+
11+
func RequiredBoolAttribute(name string) *tfprotov6.SchemaAttribute {
12+
return &tfprotov6.SchemaAttribute{
13+
Name: name,
14+
Type: tftypes.Bool,
15+
Required: true,
16+
}
17+
}
18+
19+
func OptionalComputedListAttribute(name string, elementType tftypes.Type) *tfprotov6.SchemaAttribute {
20+
return &tfprotov6.SchemaAttribute{
21+
Name: name,
22+
Type: tftypes.List{ElementType: elementType},
23+
Optional: true,
24+
Computed: true,
25+
}
26+
}
27+
28+
func RequiredListAttribute(name string, elementType tftypes.Type) *tfprotov6.SchemaAttribute {
29+
return &tfprotov6.SchemaAttribute{
30+
Name: name,
31+
Type: tftypes.List{ElementType: elementType},
32+
Required: true,
33+
}
34+
}
35+
36+
func RequiredNumberAttribute(name string) *tfprotov6.SchemaAttribute {
37+
return &tfprotov6.SchemaAttribute{
38+
Name: name,
39+
Type: tftypes.Number,
40+
Required: true,
41+
}
42+
}
43+
44+
func ComputedStringAttribute(name string) *tfprotov6.SchemaAttribute {
45+
return &tfprotov6.SchemaAttribute{
46+
Name: name,
47+
Type: tftypes.String,
48+
Computed: true,
49+
}
50+
}
51+
52+
func OptionalStringAttribute(name string) *tfprotov6.SchemaAttribute {
53+
return &tfprotov6.SchemaAttribute{
54+
Name: name,
55+
Type: tftypes.String,
56+
Optional: true,
57+
}
58+
}
59+
60+
func RequiredStringAttribute(name string) *tfprotov6.SchemaAttribute {
61+
return &tfprotov6.SchemaAttribute{
62+
Name: name,
63+
Type: tftypes.String,
64+
Required: true,
65+
}
66+
}

helper/resource/testing_new.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,10 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest
254254

255255
testStepConfig = teststep.Configuration(confRequest)
256256

257-
err = wd.SetConfig(ctx, testStepConfig, step.ConfigVariables)
257+
if !step.Query {
258+
fmt.Println("Writing pre-switch configuration:", rawCfg)
259+
err = wd.SetConfig(ctx, testStepConfig, step.ConfigVariables)
260+
}
258261

259262
if err != nil {
260263
logging.HelperResourceError(ctx,
@@ -356,6 +359,39 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest
356359
continue
357360
}
358361

362+
if step.Query {
363+
logging.HelperResourceTrace(ctx, "TestStep is Query mode")
364+
365+
queryConfigRequest := teststep.ConfigurationRequest{
366+
Raw: &step.Config,
367+
}
368+
err := wd.SetQuery(ctx, teststep.Configuration(queryConfigRequest), step.ConfigVariables)
369+
if err != nil {
370+
t.Fatalf("Step %d/%d error setting query: %s", stepNumber, len(c.Steps), err)
371+
}
372+
373+
err = runProviderCommand(ctx, t, wd, providers, func() error {
374+
return wd.Init(ctx)
375+
})
376+
if err != nil {
377+
t.Fatalf("Step %d/%d error running init: %s", stepNumber, len(c.Steps), err)
378+
}
379+
380+
var queryOut []string
381+
err = runProviderCommand(ctx, t, wd, providers, func() error {
382+
var err error
383+
queryOut, err = wd.Query(ctx)
384+
return err
385+
})
386+
if err != nil {
387+
fmt.Printf("Step %d/%d Query Output:\n%s\n", stepNumber, len(c.Steps), queryOut)
388+
t.Fatalf("Step %d/%d error running query: %s", stepNumber, len(c.Steps), err)
389+
}
390+
391+
fmt.Printf("Step %d/%d Query Output:\n%s\n", stepNumber, len(c.Steps), queryOut)
392+
continue
393+
}
394+
359395
if cfg != nil {
360396
logging.HelperResourceTrace(ctx, "TestStep is Config mode")
361397

@@ -567,6 +603,8 @@ func testIDRefresh(ctx context.Context, t testing.T, c TestCase, wd *plugintest.
567603

568604
testStepConfigDefer := teststep.Configuration(confRequest)
569605

606+
fmt.Println("Writing the reset to original configuration:", rawCfg)
607+
570608
err = wd.SetConfig(ctx, testStepConfigDefer, step.ConfigVariables)
571609

572610
if err != nil {

helper/resource/testing_new_config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ func testStepNewConfig(ctx context.Context, t testing.T, c TestCase, wd *plugint
435435
// this fails. If refresh isn't read-only, then this will have
436436
// caught a different bug.
437437
if idRefreshCheck != nil {
438+
fmt.Println("Not Writing by testing ID Refresh")
438439
if err := testIDRefresh(ctx, t, c, wd, step, idRefreshCheck, providers, stepIndex, helper); err != nil {
439440
return fmt.Errorf(
440441
"[ERROR] Test: ID-only test failed: %s", err)

0 commit comments

Comments
 (0)