-
-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathgrpc_consumer_test.go
237 lines (200 loc) · 6.92 KB
/
grpc_consumer_test.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//go:build consumer
// +build consumer
package grpc
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/pact-foundation/pact-go/v2/examples/grpc/routeguide"
"github.com/pact-foundation/pact-go/v2/log"
message "github.com/pact-foundation/pact-go/v2/message/v4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
func TestGetFeatureSuccess(t *testing.T) {
p, _ := message.NewSynchronousPact(message.Config{
Consumer: "grpcconsumer",
Provider: "grpcprovider",
PactDir: filepath.ToSlash(fmt.Sprintf("%s/../pacts", dir)),
})
log.SetLogLevel("DEBUG")
dir, _ := os.Getwd()
path := fmt.Sprintf("%s/routeguide/route_guide.proto", strings.ReplaceAll(dir, "\\", "/"))
grpcInteraction := `{
"pact:proto": "` + path + `",
"pact:proto-service": "RouteGuide/GetFeature",
"pact:content-type": "application/protobuf",
"request": {
"latitude": "matching(number, 180)",
"longitude": "matching(number, 200)"
},
"response": {
"name": "notEmpty('Big Tree')",
"location": {
"latitude": "matching(number, 180)",
"longitude": "matching(number, 200)"
}
}
}`
err := p.AddSynchronousMessage("Route guide - GetFeature").
Given("feature 'Big Tree' exists").
UsingPlugin(message.PluginConfig{
Plugin: "protobuf",
Version: "0.3.15",
}).
WithContents(grpcInteraction, "application/protobuf").
StartTransport("grpc", "127.0.0.1", nil). // For plugin tests, we can't assume if a transport is needed, so this is optional
ExecuteTest(t, func(transport message.TransportConfig, m message.SynchronousMessage) error {
fmt.Println("gRPC transport running on", transport)
// Establish the gRPC connection
conn, err := grpc.NewClient(fmt.Sprintf("127.0.0.1:%d", transport.Port), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatal("unable to communicate to grpc server", err)
}
defer conn.Close()
// Create the gRPC client
c := routeguide.NewRouteGuideClient(conn)
point := &routeguide.Point{
Latitude: 180,
Longitude: 200,
}
// Now we can make a normal gRPC request
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
feature, err := c.GetFeature(ctx, point)
if err != nil {
t.Fatal(err.Error())
}
feature.GetLocation()
assert.Equal(t, "Big Tree", feature.GetName())
assert.Equal(t, int32(180), feature.GetLocation().GetLatitude())
return nil
})
assert.NoError(t, err)
}
func TestGetFeatureError(t *testing.T) {
log.SetLogLevel("DEBUG")
p, _ := message.NewSynchronousPact(message.Config{
Consumer: "grpcconsumer",
Provider: "grpcprovider",
PactDir: filepath.ToSlash(fmt.Sprintf("%s/../pacts", dir)),
})
dir, _ := os.Getwd()
path := fmt.Sprintf("%s/routeguide/route_guide.proto", strings.ReplaceAll(dir, "\\", "/"))
grpcInteraction := `{
"pact:proto": "` + path + `",
"pact:proto-service": "RouteGuide/GetFeature",
"pact:content-type": "application/protobuf",
"request": {
"latitude": "matching(number, -1)",
"longitude": "matching(number, -1)"
},
"responseMetadata": {
"grpc-status": "NOT_FOUND",
"grpc-message": "matching(type, 'no feature was found at latitude:-1 longitude:-1')"
}
}`
err := p.AddSynchronousMessage("Route guide - GetFeature - error response").
Given("feature does not exist at -1, -1").
UsingPlugin(message.PluginConfig{
Plugin: "protobuf",
Version: "0.3.15",
}).
WithContents(grpcInteraction, "application/protobuf").
StartTransport("grpc", "127.0.0.1", nil). // For plugin tests, we can't assume if a transport is needed, so this is optional
ExecuteTest(t, func(transport message.TransportConfig, m message.SynchronousMessage) error {
fmt.Println("gRPC transport running on", transport)
// Establish the gRPC connection
conn, err := grpc.NewClient(fmt.Sprintf("127.0.0.1:%d", transport.Port), grpc.WithTransportCredentials(insecure.NewCredentials()))
require.NoError(t, err)
defer conn.Close()
// Create the gRPC client
c := routeguide.NewRouteGuideClient(conn)
point := &routeguide.Point{
Latitude: -1,
Longitude: -1,
}
// Now we can make a normal gRPC request
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_, err = c.GetFeature(ctx, point)
require.Error(t, err)
// TODO: uncomment once new FFI and new pact-protobuf plugin are released with a fix
// https://github.com/pact-foundation/pact-reference/commit/29b326e59b48a6a78a019b37e378b7742c728da5
// require.ErrorContains(t, err, "no feature was found at latitude:-1 longitude:-1")
return nil
})
assert.NoError(t, err)
}
func TestSaveFeature(t *testing.T) {
p, _ := message.NewSynchronousPact(message.Config{
Consumer: "grpcconsumer",
Provider: "grpcprovider",
PactDir: filepath.ToSlash(fmt.Sprintf("%s/../pacts", dir)),
})
log.SetLogLevel("INFO")
dir, _ := os.Getwd()
path := fmt.Sprintf("%s/routeguide/route_guide.proto", strings.ReplaceAll(dir, "\\", "/"))
grpcInteraction := `{
"pact:proto": "` + path + `",
"pact:proto-service": "RouteGuide/SaveFeature",
"pact:content-type": "application/protobuf",
"request": {
"name": "notEmpty('A shed')",
"location": {
"latitude": "matching(number, 99)",
"longitude": "matching(number, 99)"
}
},
"response": {
"name": "notEmpty('A shed')",
"location": {
"latitude": "matching(number, 99)",
"longitude": "matching(number, 99)"
}
}
}`
err := p.AddSynchronousMessage("Route guide - SaveFeature").
Given("feature does not exist at -1, -1").
UsingPlugin(message.PluginConfig{
Plugin: "protobuf",
Version: "0.3.15",
}).
WithContents(grpcInteraction, "application/protobuf").
StartTransport("grpc", "127.0.0.1", nil). // For plugin tests, we can't assume if a transport is needed, so this is optional
ExecuteTest(t, func(transport message.TransportConfig, m message.SynchronousMessage) error {
fmt.Println("gRPC transport running on", transport)
// Establish the gRPC connection
conn, err := grpc.NewClient(fmt.Sprintf("127.0.0.1:%d", transport.Port), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatal("unable to communicate to grpc server", err)
}
defer conn.Close()
// Create the gRPC client
c := routeguide.NewRouteGuideClient(conn)
feature := &routeguide.Feature{
Name: "A shed",
Location: &routeguide.Point{
Latitude: 99,
Longitude: 99,
},
}
// Now we can make a normal gRPC request
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
response, err := c.SaveFeature(ctx, feature)
if err != nil {
t.Fatal(err.Error())
}
assert.Equal(t, feature.GetName(), response.GetName())
assert.Equal(t, feature.GetLocation().GetLatitude(), feature.GetLocation().GetLatitude())
return nil
})
assert.NoError(t, err)
}