-
Notifications
You must be signed in to change notification settings - Fork 17
Update ExpectKnownValue query check to use queryfilter and accept multiple knownvalue checks
#583
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f9752dc
update expect known value query check to use query filter
stephybun 3019f49
import order
stephybun 01185b6
update expect known value check to accept multiple known value checks
stephybun d74b8de
update name of querycheck to ExpectResourceKnownValues
stephybun 310a802
copyright headers
stephybun 377b8e6
changelog
stephybun b2d2040
resolve review comments
stephybun 577c721
remove unnecessary comment
stephybun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| kind: FEATURES | ||
| body: 'querycheck: Adds `ExpectResourceKnownValues` query check to assert resource values on a filtered query result.' | ||
| time: 2025-12-02T11:33:47.606282+01:00 | ||
| custom: | ||
| Issue: "583" |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| // Copyright (c) HashiCorp, Inc. | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| package querycheck | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| tfjson "github.com/hashicorp/terraform-json" | ||
| "strings" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-testing/querycheck/queryfilter" | ||
| "github.com/hashicorp/terraform-plugin-testing/tfjsonpath" | ||
| ) | ||
|
|
||
| var _ QueryResultCheck = expectResourceKnownValues{} | ||
| var _ QueryResultCheckWithFilters = expectResourceKnownValues{} | ||
|
|
||
| type expectResourceKnownValues struct { | ||
| listResourceAddress string | ||
| filter queryfilter.QueryFilter | ||
| knownValueChecks []KnownValueCheck | ||
| } | ||
|
|
||
| func (e expectResourceKnownValues) QueryFilters(ctx context.Context) []queryfilter.QueryFilter { | ||
| if e.filter == nil { | ||
| return []queryfilter.QueryFilter{} | ||
| } | ||
|
|
||
| return []queryfilter.QueryFilter{ | ||
| e.filter, | ||
| } | ||
| } | ||
|
|
||
| func (e expectResourceKnownValues) CheckQuery(_ context.Context, req CheckQueryRequest, resp *CheckQueryResponse) { | ||
| listRes := make([]tfjson.ListResourceFoundData, 0) | ||
| var diags []error | ||
| for _, res := range req.Query { | ||
| if e.listResourceAddress == strings.TrimPrefix(res.Address, "list.") { | ||
| listRes = append(listRes, res) | ||
| } | ||
| } | ||
|
|
||
| if len(listRes) == 0 { | ||
| resp.Error = fmt.Errorf("%s - no query results found after filtering", e.listResourceAddress) | ||
| return | ||
| } | ||
|
|
||
| if len(listRes) > 1 { | ||
| resp.Error = fmt.Errorf("%s - more than 1 query result found after filtering", e.listResourceAddress) | ||
| return | ||
| } | ||
|
|
||
| res := listRes[0] | ||
|
|
||
| if res.ResourceObject == nil { | ||
| resp.Error = fmt.Errorf("%s - no resource object was returned, ensure `include_resource` has been set to `true` in the list resource config`", e.listResourceAddress) | ||
| return | ||
| } | ||
|
|
||
| for _, c := range e.knownValueChecks { | ||
| resource, err := tfjsonpath.Traverse(res.ResourceObject, c.Path) | ||
| if err != nil { | ||
| resp.Error = err | ||
| return | ||
| } | ||
|
|
||
| if err := c.KnownValue.CheckValue(resource); err != nil { | ||
| diags = append(diags, fmt.Errorf("error checking value for attribute at path: %s for resource with identity %s, err: %s", c.Path.String(), e.filter, err)) | ||
| } | ||
| } | ||
|
|
||
| if diags != nil { | ||
| var diagsStr string | ||
| for _, diag := range diags { | ||
| diagsStr += diag.Error() + "; " | ||
| } | ||
| resp.Error = fmt.Errorf("the following errors were found while checking values: %s", diagsStr) | ||
| return | ||
| } | ||
| } | ||
|
|
||
| // ExpectResourceKnownValues returns a query check which asserts that a resource object identified by a query filter | ||
| // passes the given query checks. | ||
| // | ||
| // This query check can only be used with managed resources that support resource identity and query. Query is only supported in Terraform v1.14+ | ||
| func ExpectResourceKnownValues(listResourceAddress string, filter queryfilter.QueryFilter, knownValues []KnownValueCheck) QueryResultCheck { | ||
| return expectResourceKnownValues{ | ||
| listResourceAddress: listResourceAddress, | ||
| filter: filter, | ||
| knownValueChecks: knownValues, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| // Copyright (c) HashiCorp, Inc. | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| package querycheck | ||
|
|
||
| import ( | ||
| "github.com/hashicorp/terraform-plugin-testing/knownvalue" | ||
| "github.com/hashicorp/terraform-plugin-testing/tfjsonpath" | ||
| ) | ||
|
|
||
| // KnownValueCheck represents a check of a known value at a specific JSON path | ||
| // and is used to specify multiple known value checks to assert against a | ||
| // single resource object returned by a query. | ||
| type KnownValueCheck struct { | ||
| // Path specifies the JSON path to check within the resource object. | ||
| Path tfjsonpath.Path | ||
| // KnownValue specifies the expected known value check to perform at the given path. | ||
| KnownValue knownvalue.Check | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.