Skip to content

Commit 82a6a66

Browse files
committed
Run terraform query and echo the output
1 parent b061422 commit 82a6a66

File tree

4 files changed

+69
-10
lines changed

4 files changed

+69
-10
lines changed

helper/resource/query/query_test.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
77
r "github.com/hashicorp/terraform-plugin-testing/helper/resource"
88
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider"
9+
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/list"
910
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver"
1011
"github.com/hashicorp/terraform-plugin-testing/tfversion"
1112
)
@@ -19,24 +20,41 @@ func TestQuery(t *testing.T) {
1920
},
2021
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
2122
"examplecloud": providerserver.NewProviderServer(testprovider.Provider{
23+
ListResources: map[string]testprovider.ListResource{
24+
"examplecloud_containerette": {
25+
SchemaResponse: &list.SchemaResponse{
26+
Schema: &tfprotov6.Schema{
27+
Block: &tfprotov6.SchemaBlock{
28+
Attributes: []*tfprotov6.SchemaAttribute{
29+
ComputedStringAttribute("id"),
30+
},
31+
},
32+
},
33+
},
34+
ListResultsStream: &list.ListResultsStream{
35+
Results: func(push func(list.ListResult) bool) {
36+
},
37+
},
38+
},
39+
},
2240
Resources: map[string]testprovider.Resource{
23-
"examplecloud_container": examplecloudResource(),
41+
"examplecloud_containerette": examplecloudResource(),
2442
},
2543
}),
2644
},
2745
Steps: []r.TestStep{
2846
{
47+
Query: true,
2948
Config: `
30-
resource "examplecloud_container" "test" {
31-
location = "westeurope"
32-
name = "somevalue"
49+
provider "examplecloud" {}
50+
list "examplecloud_containerette" "test" {
51+
provider = examplecloud
52+
53+
config {
54+
id = "bat"
55+
}
3356
}`,
3457
},
35-
{
36-
ResourceName: "examplecloud_container.test",
37-
ImportState: true,
38-
ImportStateKind: r.ImportBlockWithID,
39-
},
4058
},
4159
})
4260
}

helper/resource/testing.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,8 @@ type TestStep struct {
835835
// for performing import testing where the prior TestStep configuration
836836
// contained a provider outside the one under test.
837837
ExternalProviders map[string]ExternalProvider
838+
839+
Query bool
838840
}
839841

840842
// ConfigPlanChecks defines the different points in a Config TestStep when plan checks can be run.

helper/resource/testing_new.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest
106106
},
107107
)
108108

109+
fmt.Println("Writing provider configuration:", c.providerConfig(ctx, false))
109110
err := wd.SetConfig(ctx, config, nil)
110111

111112
if err != nil {
@@ -254,7 +255,10 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest
254255

255256
testStepConfig = teststep.Configuration(confRequest)
256257

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

259263
if err != nil {
260264
logging.HelperResourceError(ctx,
@@ -356,6 +360,39 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest
356360
continue
357361
}
358362

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

@@ -567,6 +604,7 @@ func testIDRefresh(ctx context.Context, t testing.T, c TestCase, wd *plugintest.
567604

568605
testStepConfigDefer := teststep.Configuration(confRequest)
569606

607+
fmt.Println("Writing the reset to original configuration:", rawCfg)
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)