Skip to content

Commit 79b2189

Browse files
committed
review comments
1 parent cca1521 commit 79b2189

File tree

11 files changed

+36
-63
lines changed

11 files changed

+36
-63
lines changed

helper/resource/query/query_checks.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,41 +14,35 @@ import (
1414
"github.com/hashicorp/terraform-plugin-testing/querycheck"
1515
)
1616

17-
func RunQueryChecks(ctx context.Context, t testing.T, query *[]tfjson.LogMsg, queryChecks []querycheck.QueryResultCheck) error {
17+
func RunQueryChecks(ctx context.Context, t testing.T, query []tfjson.LogMsg, queryChecks []querycheck.QueryResultCheck) error {
1818
t.Helper()
1919

2020
var result []error
2121

22-
if query == nil || len(*query) == 0 {
23-
result = append(result, fmt.Errorf("No query results found"))
22+
if query == nil {
23+
result = append(result, fmt.Errorf("no query results found"))
2424
}
2525

2626
found := make([]tfjson.ListResourceFoundData, 0)
27-
complete := tfjson.ListCompleteData{}
27+
summary := tfjson.ListCompleteData{}
2828

29-
for _, msg := range *query {
29+
for _, msg := range query {
3030
switch v := msg.(type) {
3131
case tfjson.ListResourceFoundMessage:
3232
found = append(found, v.ListResourceFound)
3333
case tfjson.ListCompleteMessage:
34-
complete = v.ListComplete
34+
summary = v.ListComplete
3535
// TODO diagnostics and errors?
3636
default:
37-
// ignore other message types
37+
continue
3838
}
3939
}
4040

41-
// TODO check diagnostics in LogMsg to see if there are any errors we can return here?
42-
var err error
43-
if len(found) == 0 {
44-
return fmt.Errorf("no resources found by query: %+v", err)
45-
}
46-
4741
for _, queryCheck := range queryChecks {
4842
resp := querycheck.CheckQueryResponse{}
4943
queryCheck.CheckQuery(ctx, querycheck.CheckQueryRequest{
50-
Query: &found,
51-
CompletedQuery: &complete,
44+
Query: found,
45+
QuerySummary: &summary,
5246
}, &resp)
5347

5448
result = append(result, resp.Error)

helper/resource/query/query_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func TestQuery(t *testing.T) {
6464
}
6565
}
6666
`,
67-
ConfigQueryResultChecks: []querycheck.QueryResultCheck{
67+
QueryResultChecks: []querycheck.QueryResultCheck{
6868
querycheck.ExpectIdentity("examplecloud_containerette.test", map[string]knownvalue.Check{
6969
"id": knownvalue.StringExact("westeurope/somevalue1"),
7070
"location": knownvalue.StringExact("westeurope"),
@@ -110,7 +110,7 @@ func TestQuery(t *testing.T) {
110110
}
111111
}
112112
`,
113-
ConfigQueryResultChecks: []querycheck.QueryResultCheck{
113+
QueryResultChecks: []querycheck.QueryResultCheck{
114114
querycheck.ExpectLength("examplecloud_containerette.test", knownvalue.Int64Exact(3)),
115115
querycheck.ExpectLength("examplecloud_containerette.test2", knownvalue.Int64Exact(3)),
116116
},
@@ -134,7 +134,7 @@ func TestQuery(t *testing.T) {
134134
}
135135
}
136136
`,
137-
ConfigQueryResultChecks: []querycheck.QueryResultCheck{
137+
QueryResultChecks: []querycheck.QueryResultCheck{
138138
querycheck.ExpectLengthAtLeast("examplecloud_containerette.test", 2),
139139
querycheck.ExpectLengthAtLeast("examplecloud_containerette.test2", 1),
140140
},

helper/resource/testing.go

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -642,9 +642,9 @@ type TestStep struct {
642642
// Custom state checks can be created by implementing the [statecheck.StateCheck] interface, or by using a StateCheck implementation from the provided [statecheck] package.
643643
ConfigStateChecks []statecheck.StateCheck
644644

645-
// ConfigQueryResultChecks allow assertions to be made against the query file during a Config test using a query check.
645+
// QueryResultChecks allow assertions to be made against a collection of found resources that were returned by a query using a query check.
646646
// Custom query checks can be created by implementing the [querycheck.QueryResultCheck] interface, or by using a QueryResultCheck implementation from the provided [querycheck] package.
647-
ConfigQueryResultChecks []querycheck.QueryResultCheck
647+
QueryResultChecks []querycheck.QueryResultCheck
648648

649649
// PlanOnly can be set to only run `plan` with this configuration, and not
650650
// actually apply it. This is useful for ensuring config changes result in
@@ -861,21 +861,6 @@ type ConfigPlanChecks struct {
861861
PostApplyPostRefresh []plancheck.PlanCheck
862862
}
863863

864-
// ConfigQueryChecks defines the different points in a Config TestStep when query checks can be run.
865-
type ConfigQueryChecks struct {
866-
// PreApply runs all query checks in the slice. This occurs before the apply of a Config test is run. This slice cannot be populated
867-
// with TestStep.QueryOnly, as there is no PreApply query run with that flag set. All errors by query checks in this slice are aggregated, reported, and will result in a test failure.
868-
PreApply []querycheck.QueryResultCheck
869-
870-
// PostApplyPreRefresh runs all query checks in the slice. This occurs after the apply and before the refresh of a Config test is run.
871-
// All errors by query checks in this slice are aggregated, reported, and will result in a test failure.
872-
PostApplyPreRefresh []querycheck.QueryResultCheck
873-
874-
// PostApplyPostRefresh runs all query checks in the slice. This occurs after the apply and refresh of a Config test are run.
875-
// All errors by query checks in this slice are aggregated, reported, and will result in a test failure.
876-
PostApplyPostRefresh []querycheck.QueryResultCheck
877-
}
878-
879864
// ImportPlanChecks defines the different points in an Import TestStep when plan checks can be run.
880865
type ImportPlanChecks struct {
881866
// PreApply runs all plan checks in the slice. This occurs after the plan of an Import test is computed. This slice cannot be populated

helper/resource/testing_new.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest
388388
t.Fatalf("Step %d/%d error running query: %s", stepNumber, len(c.Steps), err)
389389
}
390390

391-
err = query.RunQueryChecks(ctx, t, &queryOut, step.ConfigQueryResultChecks)
391+
err = query.RunQueryChecks(ctx, t, queryOut, step.QueryResultChecks)
392392
if err != nil {
393393
t.Fatalf("Step %d/%d error running query checks: %s", stepNumber, len(c.Steps), err)
394394
}

internal/plugintest/working_dir.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ func (wd *WorkingDir) Query(ctx context.Context) ([]tfjson.LogMsg, error) {
543543
}
544544

545545
if msg.Err != nil {
546-
return nil, fmt.Errorf("retrieving next message: %w", msg.Err)
546+
return nil, fmt.Errorf("retrieving message: %w", msg.Err)
547547
}
548548

549549
if msg.Msg.Level() == tfjson.Error {

querycheck/contains.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type contains struct {
1717
}
1818

1919
func (c contains) CheckQuery(_ context.Context, req CheckQueryRequest, resp *CheckQueryResponse) {
20-
for _, res := range *req.Query {
20+
for _, res := range req.Query {
2121
if strings.EqualFold(c.check, res.DisplayName) {
2222
return
2323
}
@@ -27,10 +27,10 @@ func (c contains) CheckQuery(_ context.Context, req CheckQueryRequest, resp *Che
2727

2828
}
2929

30-
// Contains returns a query check that asserts that a resource with a given display name exists within the returned results of the query.
30+
// ContainsResourceWithName returns a query check that asserts that a resource with a given display name exists within the returned results of the query.
3131
//
3232
// This query check can only be used with managed resources that support query. Query is only supported in Terraform v1.14+
33-
func Contains(resourceAddress string, displayName string) QueryResultCheck {
33+
func ContainsResourceWithName(resourceAddress string, displayName string) QueryResultCheck {
3434
return contains{
3535
resourceAddress: resourceAddress,
3636
check: displayName,

querycheck/expect_identity.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type expectIdentity struct {
2323

2424
// CheckQuery implements the query check logic.
2525
func (e expectIdentity) CheckQuery(_ context.Context, req CheckQueryRequest, resp *CheckQueryResponse) {
26-
for _, res := range *req.Query {
26+
for _, res := range req.Query {
2727
var errCollection []error
2828

2929
if e.listResourceAddress != strings.TrimPrefix(res.Address, "list.") {

querycheck/expect_known_value.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type expectKnownValue struct {
2222
}
2323

2424
func (e expectKnownValue) CheckQuery(_ context.Context, req CheckQueryRequest, resp *CheckQueryResponse) {
25-
for _, res := range *req.Query {
25+
for _, res := range req.Query {
2626
diags := make([]error, 0)
2727

2828
if e.listResourceAddress == strings.TrimPrefix(res.Address, "list.") && e.resourceName == res.DisplayName {

querycheck/expect_result_length_atleast.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ type expectLengthAtLeast struct {
1717

1818
// CheckQuery implements the query check logic.
1919
func (e expectLengthAtLeast) CheckQuery(_ context.Context, req CheckQueryRequest, resp *CheckQueryResponse) {
20-
if req.CompletedQuery == nil {
20+
if req.QuerySummary == nil {
2121
resp.Error = fmt.Errorf("no completed query information available")
2222
return
2323
}
2424

25-
if req.CompletedQuery.Total < e.check {
26-
resp.Error = fmt.Errorf("Query result of at least length %v - expected but got %v.", e.check, req.CompletedQuery.Total)
25+
if req.QuerySummary.Total < e.check {
26+
resp.Error = fmt.Errorf("Query result of at least length %v - expected but got %v.", e.check, req.QuerySummary.Total)
2727
return
2828
}
2929
}

querycheck/expect_result_length_exact.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,33 @@ package querycheck
55

66
import (
77
"context"
8-
"encoding/json"
98
"fmt"
10-
"strconv"
11-
12-
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
139
)
1410

1511
var _ QueryResultCheck = expectLength{}
1612

1713
type expectLength struct {
1814
resourceAddress string
19-
check knownvalue.Check
15+
check int
2016
}
2117

2218
// CheckQuery implements the query check logic.
2319
func (e expectLength) CheckQuery(_ context.Context, req CheckQueryRequest, resp *CheckQueryResponse) {
24-
if req.CompletedQuery == nil {
25-
resp.Error = fmt.Errorf("no completed query information available")
20+
if req.QuerySummary == nil {
21+
resp.Error = fmt.Errorf("no query summary information available")
2622
return
2723
}
2824

29-
lengthCheck := json.Number(strconv.Itoa(req.CompletedQuery.Total))
30-
31-
if err := e.check.CheckValue(lengthCheck); err != nil {
32-
resp.Error = fmt.Errorf("Query result of length %v - expected but got %v.", e.check, req.CompletedQuery.Total)
25+
if e.check != req.QuerySummary.Total {
26+
resp.Error = fmt.Errorf("number of found resources %v - expected but got %v.", e.check, req.QuerySummary.Total)
3327
return
3428
}
3529
}
3630

3731
// ExpectLength returns a query check that asserts that the length of the query result is exactly the given value.
3832
//
3933
// This query check can only be used with managed resources that support query. Query is only supported in Terraform v1.14+
40-
func ExpectLength(resourceAddress string, length knownvalue.Check) QueryResultCheck {
34+
func ExpectLength(resourceAddress string, length int) QueryResultCheck {
4135
return expectLength{
4236
resourceAddress: resourceAddress,
4337
check: length,

0 commit comments

Comments
 (0)