-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpc.go
160 lines (128 loc) · 4.07 KB
/
grpc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package integration
import (
"context"
"encoding/json"
"errors"
"fmt"
"reflect"
"github.com/jarcoal/httpmock"
"github.com/kinbiko/jsonassert"
"github.com/lucasvmiguel/integration/assertion"
"github.com/lucasvmiguel/integration/call"
"github.com/lucasvmiguel/integration/expect"
"github.com/lucasvmiguel/integration/internal/utils"
"google.golang.org/grpc/status"
)
// GRPCTestCase describes a GRPC test case that will run
type GRPCTestCase struct {
// Description describes a test case
// It can be really useful to understand which tests are breaking
Description string
// Call is what the test case will try to call
Call call.Call
// Output is going to be used to assert if the GRPC response returned what was expected
Output expect.Output
// Assertions that will run in test case
Assertions []assertion.Assertion
}
// Test runs an GRPC test case
func (t *GRPCTestCase) Test() error {
err := t.validate()
if err != nil {
return errors.New(errString(err, t.Description, "failed to validate test case"))
}
if assertion.AnyHTTP(t.Assertions) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
}
err = t.setupAssertions()
if err != nil {
return errors.New(errString(err, t.Description, "failed to setup assertions"))
}
resp, err := t.call()
if err != nil {
return errors.New(errString(err, t.Description, "failed to call GRPC endpoint"))
}
err = t.assert(resp)
if err != nil {
return errors.New(errString(err, t.Description, "failed to assert GRPC response"))
}
if t.Assertions != nil {
for _, assertion := range t.Assertions {
err := assertion.Assert()
if err != nil {
return errors.New(errString(err, t.Description, "failed to assert"))
}
}
}
return nil
}
func (t *GRPCTestCase) setupAssertions() error {
if t.Assertions != nil {
for _, assertion := range t.Assertions {
err := assertion.Setup()
if err != nil {
return fmt.Errorf("failed to setup assertion: %w", err)
}
}
}
return nil
}
func (t *GRPCTestCase) assert(resp []reflect.Value) error {
respErr, _ := resp[1].Interface().(error)
respValueJSON, err := json.Marshal(resp[0].Interface())
if err != nil {
return fmt.Errorf("failed to marshal grpc response to json: %w", err)
}
expectedValueJSON, err := json.Marshal(t.Output.Message)
if err != nil {
return fmt.Errorf("failed to marshal grpc expected response to json: %w", err)
}
je := utils.JsonError{}
jsonassert.New(&je).Assertf(string(respValueJSON), string(expectedValueJSON))
if je.Err != nil {
return fmt.Errorf("body does not match: %v", je.Err.Error())
}
if respErr == nil && t.Output.Err == nil {
return nil
}
if (respErr != nil && t.Output.Err == nil) || (respErr == nil && t.Output.Err != nil) {
return fmt.Errorf("error response should be %v it got %v", t.Output.Err, respErr)
}
if respErr != nil && t.Output.Err != nil {
status, ok := status.FromError(respErr)
if !ok {
return fmt.Errorf("failed to get error status %v", respErr)
}
if t.Output.Err.Code() != status.Code() {
return fmt.Errorf("error response status should be %v it got %v", t.Output.Err.Code(), status.Code())
}
if t.Output.Err.Message() != status.Message() {
return fmt.Errorf("error response message should be %v it got %v", t.Output.Err.Message(), status.Message())
}
}
return nil
}
func (t *GRPCTestCase) call() ([]reflect.Value, error) {
if t.Call.ServiceClient == nil {
return nil, fmt.Errorf("%s: failed because GRPC client is nil", t.Description)
}
args := []reflect.Value{reflect.ValueOf(context.Background()), reflect.ValueOf(t.Call.Message)}
function := reflect.ValueOf(t.Call.ServiceClient).MethodByName(t.Call.Function)
if !function.IsValid() {
return nil, errors.New(fmt.Sprintf("%s: failed because GRPC function is not valid", t.Description))
}
return function.Call(args), nil
}
func (t *GRPCTestCase) validate() error {
if t.Call.ServiceClient == nil {
return errors.New("grpc client is required")
}
if t.Call.Function == "" {
return errors.New("grpc function is required")
}
if t.Call.Message == nil {
return errors.New("grpc message is required")
}
return nil
}