-
Notifications
You must be signed in to change notification settings - Fork 18
query: introduce new ExpectTotalLengthForMatching query check
#607
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
Open
stephybun
wants to merge
6
commits into
main
Choose a base branch
from
f/regex-expect-length
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0e366d5
add expect exact length with regex query check
stephybun 699487c
apply ExpectLength fix to ExpectLengthAtLeast query check
stephybun 19faf70
rename query check to ExpectLengthForMultiple
stephybun 17912ba
add changelog
stephybun 273cddb
address review comments
stephybun fad7e90
rename tests and files
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: BUG FIXES | ||
| body: 'querycheck: Fix behaviour of `ExpectLength` querycheck to match on the provided resource address before comparing the count' | ||
| time: 2026-02-11T10:30:28.750109+01:00 | ||
| custom: | ||
| Issue: "604" |
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: BUG FIXES | ||
| body: 'querycheck: Fix behaviour of `ExpectLengthAtLeast` querycheck to match on the provided resource address before comparing the count' | ||
| time: 2026-02-11T12:28:25.372146+01:00 | ||
| custom: | ||
| Issue: "607" |
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: Add new `ExpectLengthForMultiple` querycheck for validating the number of found resources for multiple list blocks' | ||
| time: 2026-02-11T12:28:45.926523+01:00 | ||
| custom: | ||
| Issue: "607" | ||
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
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,54 @@ | ||
| // Copyright IBM Corp. 2014, 2026 | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| package querycheck | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "regexp" | ||
| ) | ||
|
|
||
| var _ QueryResultCheck = expectTotalLengthForMatching{} | ||
|
|
||
| type expectTotalLengthForMatching struct { | ||
| regex *regexp.Regexp | ||
| check int | ||
| } | ||
|
|
||
| // CheckQuery implements the query check logic. | ||
| func (e expectTotalLengthForMatching) CheckQuery(_ context.Context, req CheckQueryRequest, resp *CheckQueryResponse) { | ||
| if req.QuerySummary == nil && len(req.QuerySummaries) == 0 { | ||
| resp.Error = fmt.Errorf("no query summary information available") | ||
| return | ||
| } | ||
|
|
||
| total := 0 | ||
| matchFound := false | ||
| for _, summary := range req.QuerySummaries { | ||
| if e.regex.MatchString(summary.Address) { | ||
| total += summary.Total | ||
| matchFound = true | ||
| } | ||
| } | ||
|
|
||
| if !matchFound { | ||
| resp.Error = fmt.Errorf("no list resources matching the provided regex pattern %s were found in the query results", e.regex.String()) | ||
| return | ||
| } | ||
|
|
||
| if total != e.check { | ||
| resp.Error = fmt.Errorf("expected total of found resources to be %d, got %d", e.check, total) | ||
| } | ||
| } | ||
|
|
||
| // ExpectTotalLengthForMatching returns a query check that asserts that the sum of query result lengths | ||
| // produced by multiple list blocks is exactly the given value. | ||
| // | ||
| // This query check can only be used with managed resources that support query. Query is only supported in Terraform v1.14+ | ||
| func ExpectTotalLengthForMatching(regex *regexp.Regexp, length int) QueryResultCheck { | ||
| return expectTotalLengthForMatching{ | ||
| regex: regex, | ||
| check: length, | ||
| } | ||
| } |
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.
To match the new name 🙂