-
Notifications
You must be signed in to change notification settings - Fork 444
/
test_upstream.go
340 lines (293 loc) · 9.11 KB
/
test_upstream.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package v1helpers
import (
"bytes"
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"strconv"
"time"
"github.com/golang/protobuf/ptypes/wrappers"
"github.com/golang/protobuf/proto"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
gloov1 "github.com/solo-io/gloo/projects/gloo/pkg/api/v1"
static_plugin_gloo "github.com/solo-io/gloo/projects/gloo/pkg/api/v1/options/static"
"github.com/solo-io/gloo/test/helpers"
testgrpcservice "github.com/solo-io/gloo/test/v1helpers/test_grpc_service"
"github.com/solo-io/solo-kit/pkg/api/v1/resources/core"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
)
type ReceivedRequest struct {
Method string
Headers map[string][]string
URL *url.URL
Body []byte
Host string
GRPCRequest proto.Message
Port uint32
}
func NewTestHttpUpstream(ctx context.Context, addr string) *TestUpstream {
backendPort, responses := runTestServer(ctx, "", false)
return newTestUpstream(addr, []uint32{backendPort}, responses)
}
func NewTestHttpUpstreamWithReply(ctx context.Context, addr, reply string) *TestUpstream {
backendPort, responses := runTestServer(ctx, reply, false)
return newTestUpstream(addr, []uint32{backendPort}, responses)
}
func NewTestHttpUpstreamWithReplyAndHealthReply(ctx context.Context, addr, reply, healthReply string) *TestUpstream {
backendPort, responses := runTestServerWithHealthReply(ctx, reply, healthReply, false)
return newTestUpstream(addr, []uint32{backendPort}, responses)
}
func NewTestHttpsUpstreamWithReply(ctx context.Context, addr, reply string) *TestUpstream {
backendPort, responses := runTestServer(ctx, reply, true)
return newTestUpstream(addr, []uint32{backendPort}, responses)
}
func NewTestGRPCUpstream(ctx context.Context, addr string, replicas int) *TestUpstream {
grpcServices := make([]*testgrpcservice.TestGRPCServer, replicas)
for i := range grpcServices {
grpcServices[i] = testgrpcservice.RunServer(ctx)
}
received := make(chan *ReceivedRequest, 100)
for _, srv := range grpcServices {
srv := srv
go func() {
defer GinkgoRecover()
for r := range srv.C {
received <- &ReceivedRequest{GRPCRequest: r, Port: srv.Port}
}
}()
}
ports := make([]uint32, 0, len(grpcServices))
for _, v := range grpcServices {
ports = append(ports, v.Port)
}
us := newTestUpstream(addr, ports, received)
us.Upstream.UseHttp2 = &wrappers.BoolValue{Value: true}
us.GrpcServers = grpcServices
return us
}
type TestUpstream struct {
Upstream *gloov1.Upstream
C <-chan *ReceivedRequest
Address string
Port uint32
GrpcServers []*testgrpcservice.TestGRPCServer
}
func (tu *TestUpstream) FailGrpcHealthCheck() *testgrpcservice.TestGRPCServer {
for _, v := range tu.GrpcServers[:len(tu.GrpcServers)-1] {
v.HealthChecker.Fail()
}
return tu.GrpcServers[len(tu.GrpcServers)-1]
}
var id = 0
func newTestUpstream(addr string, ports []uint32, responses <-chan *ReceivedRequest) *TestUpstream {
id += 1
hosts := make([]*static_plugin_gloo.Host, len(ports))
for i, port := range ports {
hosts[i] = &static_plugin_gloo.Host{
Addr: addr,
Port: port,
}
}
u := &gloov1.Upstream{
Metadata: &core.Metadata{
Name: fmt.Sprintf("local-%d", id),
Namespace: "default",
},
UpstreamType: &gloov1.Upstream_Static{
Static: &static_plugin_gloo.UpstreamSpec{
Hosts: hosts,
},
},
}
return &TestUpstream{
Upstream: u,
C: responses,
Port: ports[0],
}
}
func runTestServer(ctx context.Context, reply string, serveTls bool) (uint32, <-chan *ReceivedRequest) {
return runTestServerWithHealthReply(ctx, reply, "OK", serveTls)
}
func runTestServerWithHealthReply(ctx context.Context, reply, healthReply string, serveTls bool) (uint32, <-chan *ReceivedRequest) {
bodyChan := make(chan *ReceivedRequest, 100)
handlerFunc := func(rw http.ResponseWriter, r *http.Request) {
var rr ReceivedRequest
rr.Method = r.Method
var body []byte
if r.Body != nil {
body, _ = ioutil.ReadAll(r.Body)
_ = r.Body.Close()
if len(body) != 0 {
rr.Body = body
}
}
if reply != "" {
_, _ = rw.Write([]byte(reply))
} else if body != nil {
_, _ = rw.Write(body)
}
rr.Host = r.Host
rr.URL = r.URL
rr.Headers = r.Header
bodyChan <- &rr
}
listener, err := net.Listen("tcp", ":0")
if err != nil {
panic(err)
}
addr := listener.Addr().String()
_, portStr, err := net.SplitHostPort(addr)
if err != nil {
panic(err)
}
port, err := strconv.Atoi(portStr)
if err != nil {
panic(err)
}
mux := http.NewServeMux()
mux.Handle("/", http.HandlerFunc(handlerFunc))
mux.Handle("/health", http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
rw.Write([]byte(healthReply))
}))
go func() {
defer GinkgoRecover()
h := &http.Server{Handler: mux}
if serveTls {
certs, err := tls.X509KeyPair([]byte(helpers.Certificate()), []byte(helpers.PrivateKey()))
if err != nil {
Expect(err).NotTo(HaveOccurred())
}
listener = tls.NewListener(listener, &tls.Config{
Certificates: []tls.Certificate{certs},
})
}
go func() {
defer GinkgoRecover()
if err := h.Serve(listener); err != nil {
if err != http.ErrServerClosed {
panic(err)
}
}
}()
<-ctx.Done()
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
_ = h.Shutdown(ctx)
cancel()
// close channel, the http handler may panic but this should be caught by the http code.
close(bodyChan)
}()
return uint32(port), bodyChan
}
func TestUpstreamReachable(envoyPort uint32, tu *TestUpstream, rootca *string) {
TestUpstreamReachableWithOffset(2, envoyPort, tu, rootca)
}
func TestUpstreamReachableWithOffset(offset int, envoyPort uint32, tu *TestUpstream, rootca *string) {
body := []byte("solo.io test")
ExpectHttpOK(body, rootca, envoyPort, "")
timeout := time.After(15 * time.Second)
var receivedRequest *ReceivedRequest
for {
select {
case <-timeout:
if receivedRequest != nil {
fmt.Fprintf(GinkgoWriter, "last received request: %v", *receivedRequest)
}
Fail("timeout testing upstream reachability")
case receivedRequest = <-tu.C:
if receivedRequest.Method == "POST" &&
bytes.Equal(receivedRequest.Body, body) {
return
}
}
}
}
func ExpectHttpOK(body []byte, rootca *string, envoyPort uint32, response string) {
ExpectHttpOKWithOffset(1, body, rootca, envoyPort, response)
}
func ExpectHttpOKWithOffset(offset int, body []byte, rootca *string, envoyPort uint32, response string) {
ExpectHttpStatusWithOffset(offset+1, body, rootca, envoyPort, response, http.StatusOK)
}
func ExpectHttpUnavailableWithOffset(offset int, body []byte, rootca *string, envoyPort uint32, response string) {
ExpectHttpStatusWithOffset(offset+1, body, rootca, envoyPort, response, http.StatusServiceUnavailable)
}
func ExpectHttpStatusWithOffset(offset int, body []byte, rootca *string, envoyPort uint32, response string, status int) {
var res *http.Response
EventuallyWithOffset(2, func() error {
// send a request with a body
var buf bytes.Buffer
buf.Write(body)
var client http.Client
scheme := "http"
if rootca != nil {
scheme = "https"
caCertPool := x509.NewCertPool()
ok := caCertPool.AppendCertsFromPEM([]byte(*rootca))
if !ok {
return fmt.Errorf("ca cert is not OK")
}
client.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: caCertPool,
InsecureSkipVerify: true,
},
}
}
var err error
res, err = client.Post(fmt.Sprintf("%s://%s:%d/1", scheme, "localhost", envoyPort), "application/octet-stream", &buf)
if err != nil {
return err
}
if res.StatusCode != status {
return fmt.Errorf("received status code (%v) is not expected status code (%v)", res.StatusCode, status)
}
return nil
}, "30s", "1s").Should(BeNil())
if response != "" {
body, err := ioutil.ReadAll(res.Body)
ExpectWithOffset(offset, err).NotTo(HaveOccurred())
defer res.Body.Close()
ExpectWithOffset(offset, string(body)).To(Equal(response))
}
}
func ExpectGrpcHealthOK(rootca *string, envoyPort uint32, service string) {
EventuallyWithOffset(2, func() error {
// send a request with a body
opts := []grpc.DialOption{grpc.WithBlock()}
if rootca != nil {
caCertPool := x509.NewCertPool()
ok := caCertPool.AppendCertsFromPEM([]byte(*rootca))
if !ok {
Fail("ca cert is not OK")
}
creds := credentials.NewTLS(&tls.Config{
ClientCAs: caCertPool,
InsecureSkipVerify: true,
})
opts = append(opts, grpc.WithTransportCredentials(creds))
} else {
opts = append(opts, grpc.WithInsecure())
}
conn, err := grpc.Dial(fmt.Sprintf("%s:%d", "localhost", envoyPort), opts...)
ExpectWithOffset(2, err).NotTo(HaveOccurred())
defer conn.Close()
c := healthpb.NewHealthClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
resp, err := c.Check(ctx, &healthpb.HealthCheckRequest{Service: service})
cancel()
if err != nil {
return err
}
if resp.GetStatus() != healthpb.HealthCheckResponse_SERVING {
return fmt.Errorf("%v is not SERVING", resp.GetStatus())
}
return nil
}, "30s", "1s").Should(BeNil())
}