-
Notifications
You must be signed in to change notification settings - Fork 114
Add terraform-plugin-testing documentation for query mode and query checks
#1451
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
stephybun
merged 4 commits into
f/plugin-testing-1-14-0
from
f/plugin-testing-query-checks
Dec 10, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
95 changes: 95 additions & 0 deletions
95
...in-testing/v1.14.x/docs/plugin/testing/acceptance-tests/query-checks/custom.mdx
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,95 @@ | ||
| --- | ||
| page_title: 'Plugin Development - Acceptance Testing: Query Checks' | ||
| description: >- | ||
| Query Checks are test assertions that can inspect the query results during a TestStep. Custom Query Checks can be implemented. | ||
| --- | ||
|
|
||
| # Custom Query Checks | ||
|
|
||
| The package [`querycheck`](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-testing/querycheck) also provides the [`QueryResultCheck`](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-testing/querycheck#QueryResultCheck) interface, which can be implemented for a custom query check. | ||
|
|
||
| The [`querycheck.CheckQueryRequest`](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-testing/querycheck#CheckQueryResultRequest) contains the current query results, parsed by the [terraform-json package](https://pkg.go.dev/github.com/hashicorp/terraform-json#ListResourceFoundData). | ||
|
|
||
| Here is an example implementation of a query check that asserts that a specific query result resource object attribute has a known type and value: | ||
|
|
||
| ```go | ||
| package example_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| tfjson "github.com/hashicorp/terraform-json" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-testing/knownvalue" | ||
| "github.com/hashicorp/terraform-plugin-testing/querycheck" | ||
| "github.com/hashicorp/terraform-plugin-testing/querycheck/queryfilter" | ||
| "github.com/hashicorp/terraform-plugin-testing/tfjsonpath" | ||
| ) | ||
|
|
||
| var _ querycheck.QueryResultCheck = expectKnownValue{} | ||
| var _ querycheck.QueryResultCheckWithFilters = expectKnownValue{} | ||
|
|
||
| type expectKnownValue struct { | ||
| listResourceAddress string | ||
| filter queryfilter.QueryFilter | ||
| attributePath tfjsonpath.Path | ||
| knownValueCheck knownvalue.Check | ||
| } | ||
|
|
||
| func (e expectResourceKnownValues) QueryFilters(_ context.Context) []queryfilter.QueryFilter { | ||
| if e.filter == nil { | ||
| return []queryfilter.QueryFilter{} | ||
| } | ||
|
|
||
| return []queryfilter.QueryFilter{ | ||
| e.filter, | ||
| } | ||
| } | ||
|
|
||
| func (e expectKnownValue) CheckQuery(_ context.Context, req querycheck.CheckQueryRequest, resp *querycheck.CheckQueryResponse) { | ||
| listRes := make([]tfjson.ListResourceFoundData, 0) | ||
| 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 | ||
| } | ||
|
|
||
| attribute, err := tfjsonpath.Traverse(res.ResourceObject, e.attributePath) | ||
| if err != nil { | ||
| resp.Error = err | ||
| return | ||
| } | ||
|
|
||
| if err := e.KnownValue.CheckValue(attribute); err != nil { | ||
| resp.Error = fmt.Errorf("error checking value for attribute at path: %s for resource with identity %s, err: %s", e.attributePath, e.filter, err) | ||
| return | ||
| } | ||
| } | ||
|
|
||
| func ExpectKnownValues(listResourceAddress string, filter queryfilter.QueryFilter, attributePath tfjsonpath.Path, knownValueCheck knownvalue.Check) QueryResultCheck { | ||
| return expectKnownValues{ | ||
| listResourceAddress: listResourceAddress, | ||
| filter: filter, | ||
| attributePath: attributePath, | ||
| knownValueCheck: knownValueCheck, | ||
| } | ||
| } | ||
| ``` | ||
18 changes: 18 additions & 0 deletions
18
...gin-testing/v1.14.x/docs/plugin/testing/acceptance-tests/query-checks/index.mdx
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,18 @@ | ||
| --- | ||
| page_title: 'Plugin Development - Acceptance Testing: Query Checks' | ||
| description: >- | ||
| Query Checks are test assertions that can inspect the query results during a Query TestStep. The testing module | ||
| provides built-in Query Checks for common use-cases, and custom Query Checks can also be implemented. | ||
| --- | ||
|
|
||
| # Query Checks | ||
| During the **Query** [mode](/terraform/plugin/testing/acceptance-tests/teststep#test-modes) of a `TestStep`, the configuration supplied is used as the content of a `.tfquery.hcl` query file and the testing framework will run `terraform query`. | ||
|
|
||
| The execution of `terraform query` results in a set of query results. | ||
|
|
||
| A **query check** is a test assertion that inspects the query results. Multiple query checks can be run, all assertion errors returned are aggregated, reported as a test failure, and all test cleanup logic is executed. | ||
|
|
||
| Refer to: | ||
|
|
||
| - [Query Result Query Checks](/terraform/plugin/testing/acceptance-tests/query-checks/query-results) for built-in query checks. | ||
| - [Custom Query Checks](/terraform/plugin/testing/acceptance-tests/query-checks/custom) for defining bespoke query checks. |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.