-
Notifications
You must be signed in to change notification settings - Fork 17
query: refactor query test step logic into testStepNewQuery()
#568
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
1488e4e
e447089
2f5a039
18dffe0
13b7a39
7c7e498
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,11 +4,15 @@ | |
| package query_test | ||
|
|
||
| import ( | ||
| "regexp" | ||
| "testing" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-go/tfprotov6" | ||
| "github.com/hashicorp/terraform-plugin-go/tftypes" | ||
|
|
||
| r "github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
| "github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider" | ||
| "github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/list" | ||
| "github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver" | ||
| "github.com/hashicorp/terraform-plugin-testing/knownvalue" | ||
| "github.com/hashicorp/terraform-plugin-testing/querycheck" | ||
|
|
@@ -100,3 +104,88 @@ func TestQuery(t *testing.T) { | |
| }, | ||
| }) | ||
| } | ||
|
|
||
| func TestQuery_ExpectError_ValidationError(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| r.UnitTest(t, r.TestCase{ | ||
| TerraformVersionChecks: []tfversion.TerraformVersionCheck{ | ||
| tfversion.SkipBelow(tfversion.Version1_14_0), | ||
| }, | ||
| ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ | ||
| "examplecloud": providerserver.NewProviderServer(testprovider.Provider{ | ||
| ListResources: map[string]testprovider.ListResource{ | ||
| "examplecloud_containerette": { | ||
| IncludeResource: true, | ||
| SchemaResponse: &list.SchemaResponse{ | ||
| Schema: &tfprotov6.Schema{ | ||
| Block: &tfprotov6.SchemaBlock{ | ||
| Attributes: []*tfprotov6.SchemaAttribute{ | ||
| { | ||
| Name: "resource_group_name", | ||
| Type: tftypes.String, | ||
| Required: true, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| ValidateListConfigResponse: &list.ValidateListConfigResponse{ | ||
| Diagnostics: []*tfprotov6.Diagnostic{ | ||
| { | ||
| Severity: tfprotov6.DiagnosticSeverityError, | ||
| Summary: "Diagnostic summary", | ||
| Detail: "Diagnostic details", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| Resources: map[string]testprovider.Resource{ | ||
| "examplecloud_containerette": examplecloudResource(), | ||
| }, | ||
| }), | ||
| }, | ||
| Steps: []r.TestStep{ | ||
| { // config mode step 1 needs tf file with terraform providers block | ||
| // this step should provision all the resources that the query is support to list | ||
| // for simplicity we're only "provisioning" one here | ||
| Config: ` | ||
| resource "examplecloud_containerette" "primary" { | ||
| name = "banana" | ||
| resource_group_name = "foo" | ||
| location = "westeurope" | ||
|
|
||
| instances = 5 | ||
| }`, | ||
| }, | ||
| { // Query mode step 2, operates on .tfquery.hcl files (needs tf file with terraform providers block) | ||
| // ```provider "examplecloud" {}``` has a slightly different syntax for a .tfquery.hcl file | ||
| // provider bock simulates a real providers workflow | ||
| // "config" in this case means configuration of the list resource/filters | ||
|
|
||
| Query: true, | ||
| Config: ` | ||
| provider "examplecloud" {} | ||
|
|
||
| list "examplecloud_containerette" "test" { | ||
| provider = examplecloud | ||
|
|
||
| config { | ||
| resource_group_name = "foo" | ||
| } | ||
| } | ||
|
|
||
| list "examplecloud_containerette" "test2" { | ||
| provider = examplecloud | ||
|
|
||
| config { | ||
| resource_group_name = "bar" | ||
| } | ||
| } | ||
| `, | ||
| ExpectError: regexp.MustCompile(`Diagnostic summary`), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Since --- FAIL: TestQuery_ExpectError_ValidationError (0.49s)
/Users/austin.valle/code/terraform-plugin-testing/helper/resource/query/query_test.go:111: Step 2/2 error running query, expected an error with pattern (Not the correct diagnostic summary), no match on: running terraform query command returned diagnostics: [{baseLogMessage:{Lvl:error Msg:Error: Diagnostic summary Time:2025-10-29 09:10:02.151561 -0400 EDT} Diagnostic:{Severity:error Summary:Diagnostic summary Detail:Diagnostic details Range:0x140004547c0 Snippet:0x14000030640}} {baseLogMessage:{Lvl:error Msg:Error: Diagnostic summary Time:2025-10-29 09:10:02.151735 -0400 EDT} Diagnostic:{Severity:error Summary:Diagnostic summary Detail:Diagnostic details Range:0x14000454900 Snippet:0x14000030690}}]
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, that's a good point. Maybe we should introduce a string formatter for machine readable error messaging in a follow-up PR. |
||
| }, | ||
| }, | ||
| }) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.