Skip to content

Commit c465a33

Browse files
committed
update CheckQueryRequest to provide ListResourceFoundData and ListCompleteData and refactor a little
1 parent 3331b2b commit c465a33

File tree

7 files changed

+53
-116
lines changed

7 files changed

+53
-116
lines changed

helper/resource/query/query_checks.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package query
66
import (
77
"context"
88
"errors"
9+
"fmt"
910

1011
tfjson "github.com/hashicorp/terraform-json"
1112
"github.com/mitchellh/go-testing-interface"
@@ -18,9 +19,35 @@ func RunQueryChecks(ctx context.Context, t testing.T, query *[]tfjson.LogMsg, qu
1819

1920
var result []error
2021

22+
if query == nil || len(*query) == 0 {
23+
// TODO return error
24+
}
25+
26+
found := make([]tfjson.ListResourceFoundData, 0)
27+
complete := tfjson.ListCompleteData{}
28+
29+
for _, msg := range *query {
30+
switch v := msg.(type) {
31+
case tfjson.ListResourceFoundMessage:
32+
found = append(found, v.ListResourceFound)
33+
case tfjson.ListCompleteMessage:
34+
complete = v.ListComplete
35+
// TODO diagnostics and errors?
36+
}
37+
}
38+
39+
// TODO check diagnostics in LogMsg to see if there are any errors we can return here?
40+
var err error
41+
if len(found) == 0 {
42+
return fmt.Errorf("no resources found by query: %+v", err)
43+
}
44+
2145
for _, queryCheck := range queryChecks {
2246
resp := querycheck.CheckQueryResponse{}
23-
queryCheck.CheckQuery(ctx, querycheck.CheckQueryRequest{Query: query}, &resp)
47+
queryCheck.CheckQuery(ctx, querycheck.CheckQueryRequest{
48+
Query: &found,
49+
CompletedQuery: &complete,
50+
}, &resp)
2451

2552
result = append(result, resp.Error)
2653
}

helper/resource/query/query_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ func TestQuery(t *testing.T) {
134134
ConfigQueryResultChecks: []querycheck.QueryResultCheck{
135135
querycheck.ExpectLengthAtLeast("examplecloud_containerette.test", 2),
136136
querycheck.ExpectLengthAtLeast("examplecloud_containerette.test2", 1),
137-
querycheck.Contains(),
138137
},
139138
},
140139
},

querycheck/contains.go

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"context"
55
"fmt"
66
"strings"
7-
8-
tfjson "github.com/hashicorp/terraform-json"
97
)
108

119
var _ QueryResultCheck = contains{}
@@ -16,37 +14,11 @@ type contains struct {
1614
}
1715

1816
func (c contains) CheckQuery(_ context.Context, req CheckQueryRequest, resp *CheckQueryResponse) {
19-
// TODO refactor below
20-
foundResources := make([]string, 0)
21-
22-
if req.Query == nil {
23-
resp.Error = fmt.Errorf("Query is nil")
24-
return
25-
}
26-
27-
for _, v := range *req.Query {
28-
switch i := v.(type) {
29-
case tfjson.ListResourceFoundMessage:
30-
prefix := "list."
31-
if strings.TrimPrefix(i.ListResourceFound.Address, prefix) == c.resourceAddress {
32-
foundResources = append(foundResources, i.ListResourceFound.DisplayName)
33-
}
34-
default:
35-
continue
36-
}
37-
}
38-
39-
if len(foundResources) == 0 {
40-
resp.Error = fmt.Errorf("%s - no resources found by query.", c.resourceAddress)
41-
42-
return
43-
}
44-
// TODO refactor above
45-
46-
for _, res := range foundResources {
47-
if c.check == res {
17+
for _, res := range *req.Query {
18+
if strings.EqualFold(c.check, res.DisplayName) {
4819
return
4920
}
21+
5022
}
5123

5224
resp.Error = fmt.Errorf("expected to find resource with display name %q in results but resource was not found", c.check)

querycheck/expect_identity.go

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ import (
88
"encoding/json"
99
"errors"
1010
"fmt"
11-
"strings"
1211

13-
tfjson "github.com/hashicorp/terraform-json"
1412
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
1513
)
1614

@@ -23,42 +21,15 @@ type expectIdentity struct {
2321

2422
// CheckQuery implements the query check logic.
2523
func (e expectIdentity) CheckQuery(ctx context.Context, req CheckQueryRequest, resp *CheckQueryResponse) {
26-
var foundIdentities []map[string]json.RawMessage
27-
28-
if req.Query == nil {
29-
resp.Error = fmt.Errorf("Query is nil")
30-
return
31-
}
32-
33-
for _, v := range *req.Query {
34-
switch i := v.(type) {
35-
case tfjson.ListResourceFoundMessage:
36-
prefix := "list."
37-
if strings.TrimPrefix(i.ListResourceFound.Address, prefix) == e.resourceAddress {
38-
foundIdentities = append(foundIdentities, i.ListResourceFound.Identity)
39-
}
40-
default:
41-
continue
42-
}
43-
}
44-
45-
if len(foundIdentities) == 0 {
46-
resp.Error = fmt.Errorf("%s - Identity not found in query.", e.resourceAddress)
47-
48-
return
49-
}
50-
51-
var err error
52-
53-
for _, resultIdentity := range foundIdentities {
24+
for _, res := range *req.Query {
5425
var errCollection []error
5526

5627
for attribute := range e.check {
5728
var val any
58-
var ok bool
5929
var unmarshalledVal any
6030

61-
if val, ok = resultIdentity[attribute]; !ok {
31+
val, ok := res.Identity[attribute]
32+
if !ok {
6233
resp.Error = fmt.Errorf("%s - expected attribute %q not in actual identity object", e.resourceAddress, attribute)
6334
return
6435
}
@@ -68,7 +39,7 @@ func (e expectIdentity) CheckQuery(ctx context.Context, req CheckQueryRequest, r
6839
resp.Error = fmt.Errorf("%s - expected json.RawMessage but got %T", e.resourceAddress, val)
6940
return
7041
}
71-
err = json.Unmarshal(rawMessage, &unmarshalledVal)
42+
err := json.Unmarshal(rawMessage, &unmarshalledVal)
7243

7344
if err != nil {
7445
resp.Error = fmt.Errorf("%s - Error decoding message type: %s", e.resourceAddress, err)
@@ -79,7 +50,6 @@ func (e expectIdentity) CheckQuery(ctx context.Context, req CheckQueryRequest, r
7950
errCollection = append(errCollection, fmt.Errorf("%s - %q identity attribute: %s\n", e.resourceAddress, e.check, err))
8051
}
8152
}
82-
8353
if errCollection == nil {
8454
return
8555
}

querycheck/expect_result_length_atleast.go

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ package querycheck
66
import (
77
"context"
88
"fmt"
9-
"strings"
10-
11-
tfjson "github.com/hashicorp/terraform-json"
129
)
1310

1411
var _ QueryResultCheck = expectLengthAtLeast{}
@@ -19,32 +16,17 @@ type expectLengthAtLeast struct {
1916
}
2017

2118
// CheckQuery implements the query check logic.
22-
func (e expectLengthAtLeast) CheckQuery(ctx context.Context, req CheckQueryRequest, resp *CheckQueryResponse) {
23-
if req.Query == nil {
24-
resp.Error = fmt.Errorf("Query is nil")
19+
func (e expectLengthAtLeast) CheckQuery(_ context.Context, req CheckQueryRequest, resp *CheckQueryResponse) {
20+
if req.CompletedQuery == nil {
21+
resp.Error = fmt.Errorf("no completed query information available")
2522
return
2623
}
2724

28-
for _, v := range *req.Query {
29-
switch i := v.(type) {
30-
case tfjson.ListCompleteMessage:
31-
prefix := "list."
32-
33-
if strings.TrimPrefix(i.ListComplete.Address, prefix) == e.resourceAddress {
34-
if i.ListComplete.Total < e.check {
35-
resp.Error = fmt.Errorf("Query result of at least length %v - expected but got %v.", e.check, i.ListComplete.Total)
36-
return
37-
} else {
38-
return
39-
}
40-
}
41-
default:
42-
continue
43-
}
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)
27+
return
4428
}
4529

46-
resp.Error = fmt.Errorf("%s - Address not found in query result.", e.resourceAddress)
47-
4830
return
4931
}
5032

querycheck/expect_result_length_exact.go

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ import (
88
"encoding/json"
99
"fmt"
1010
"strconv"
11-
"strings"
1211

13-
tfjson "github.com/hashicorp/terraform-json"
1412
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
1513
)
1614

@@ -22,32 +20,18 @@ type expectLength struct {
2220
}
2321

2422
// CheckQuery implements the query check logic.
25-
func (e expectLength) CheckQuery(ctx context.Context, req CheckQueryRequest, resp *CheckQueryResponse) {
26-
if req.Query == nil {
27-
resp.Error = fmt.Errorf("Query is nil")
23+
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")
2826
return
2927
}
3028

31-
for _, v := range *req.Query {
32-
switch i := v.(type) {
33-
case tfjson.ListCompleteMessage:
34-
prefix := "list."
35-
lengthCheck := json.Number((strconv.Itoa(i.ListComplete.Total)))
36-
37-
if strings.TrimPrefix(i.ListComplete.Address, prefix) == e.resourceAddress {
38-
if err := e.check.CheckValue(lengthCheck); err != nil {
39-
resp.Error = fmt.Errorf("Query result of length %v - expected but got %v.", e.check, i.ListComplete.Total)
40-
return
41-
} else {
42-
return
43-
}
44-
}
45-
default:
46-
continue
47-
}
48-
}
29+
lengthCheck := json.Number(strconv.Itoa(req.CompletedQuery.Total))
4930

50-
resp.Error = fmt.Errorf("%s - Address not found in query result.", e.resourceAddress)
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)
33+
return
34+
}
5135

5236
return
5337
}

querycheck/query_check.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ type QueryResultCheck interface {
1919
// CheckQueryRequest is a request for an invoke of the CheckQuery function.
2020
type CheckQueryRequest struct {
2121
// Query represents a parsed query file, retrieved via the `terraform show -json` command.
22-
Query *[]tfjson.LogMsg
22+
Query *[]tfjson.ListResourceFoundData
23+
24+
// CompletedQuery contains a summary of the completed query operation.
25+
CompletedQuery *tfjson.ListCompleteData
2326
}
2427

2528
// CheckQueryResponse is a response to an invoke of the CheckQuery function.

0 commit comments

Comments
 (0)