Skip to content

Conversation

bbasata
Copy link
Collaborator

@bbasata bbasata commented Jul 11, 2025

Description

This pull request is not intended to be merged. It is a rugged, working proof-of-concept that identifies the wiring that would enable a terraform query acceptance test.

cc: @stephybun @rainkwan fyi

Rollback Plan

  • If a change needs to be reverted, we will roll out an update to the code within 7 days.

Changes to Security Controls

None

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

func examplecloudResource() testprovider.Resource {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Copied from ../importstate.

},
Steps: []r.TestStep{
{
Query: true,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Query mode!

@@ -0,0 +1,66 @@
// Copyright (c) HashiCorp, Inc.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Copied from ../importstate.

@@ -254,7 +255,10 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest

testStepConfig = teststep.Configuration(confRequest)

err = wd.SetConfig(ctx, testStepConfig, step.ConfigVariables)
if !step.Query {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

For purposes of this PR, SetConfig and SetQuery are 2 different things because one writes/manages a .tf file and one writes/manages a .tfquery.hcl file.

Does if !step.Query feel right here? Likely not. The goal of this PR is more make-it-work than make-it-right/make-it-well-factored 😃

t.Fatalf("Step %d/%d error running query: %s", stepNumber, len(c.Steps), err)
}

fmt.Printf("Step %d/%d Query Output:\n%s\n", stepNumber, len(c.Steps), queryOut)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This simply echoes the Terraform output. So a test only fails if terraform query fails.

In the real world, we would make assertions, call "query checks," ...

@@ -444,3 +526,38 @@ func (wd *WorkingDir) Schemas(ctx context.Context) (*tfjson.ProviderSchemas, err

return providerSchemas, err
}

func (wd *WorkingDir) Query(ctx context.Context) ([]string, error) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This function is a temporary solution until https://github.com/hashicorp/terraform-exec supports the query subcommand.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

I've merged the PR:

go get github.com/hashicorp/terraform-exec@061a850a52d2e3a408d33066da3601983067b3d9

logging.HelperResourceTrace(ctx, "Called Terraform CLI providers query command")

output := buffer.String()
return []string{output}, nil
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Someday, https://github.com/hashicorp/terraform-json may parse this output. Until then, we may want to json-decode it.

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package testprovider
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

testprovider and testsdk are internal implementations based on terraform-plugin-go. They are independent of SDKv2 and Framework.

@bbasata bbasata marked this pull request as ready for review July 11, 2025 19:56
@bbasata bbasata requested a review from a team as a code owner July 11, 2025 19:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants