forked from knative/serving
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsocket_test.go
360 lines (313 loc) · 10.4 KB
/
websocket_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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
//go:build e2e
// +build e2e
/*
Copyright 2019 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package e2e
import (
"context"
"testing"
"github.com/gorilla/websocket"
"golang.org/x/sync/errgroup"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/ptr"
"knative.dev/serving/pkg/apis/autoscaling"
v1 "knative.dev/serving/pkg/apis/serving/v1"
rtesting "knative.dev/serving/pkg/testing/v1"
"knative.dev/serving/test"
v1test "knative.dev/serving/test/v1"
)
const (
wsServerTestImageName = "wsserver"
NoDelay = ""
)
// Connects to a WebSocket target and executes `numReqs` requests.
// Collects the answer frequences and returns them.
// Returns nil map and error if any of the requests fails.
func webSocketResponseFreqs(t *testing.T, clients *test.Clients, url string, numReqs int) (map[string]int, error) {
t.Helper()
var g errgroup.Group
respCh := make(chan string, numReqs)
resps := map[string]int{}
for i := 0; i < numReqs; i++ {
g.Go(func() error {
// Establish the websocket connection. Since they are persistent
// we can't reuse.
conn, err := connect(t, clients, url, NoDelay)
if err != nil {
return err
}
defer conn.Close()
// Send a message.
t.Logf("Sending message %q to server.", message)
if err = conn.WriteMessage(websocket.TextMessage, []byte(message)); err != nil {
return err
}
t.Log("Message sent.")
// Read back the echoed message and put it into the channel.
_, recv, err := conn.ReadMessage()
if err != nil {
return err
}
respCh <- string(recv)
return nil
})
}
if err := g.Wait(); err != nil {
return nil, err
}
close(respCh)
for r := range respCh {
resps[r]++
}
return resps, nil
}
// TestWebSocket
// (1) creates a service based on the `wsserver` image,
// (2) connects to the service using websocket,
// (3) sends a message, and
// (4) verifies that we receive back the same message.
func TestWebSocket(t *testing.T) {
// TODO: https option with parallel leads to flakes.
// https://github.com/knative/serving/issues/11387
if !test.ServingFlags.HTTPS {
t.Parallel()
}
clients := Setup(t)
names := test.ResourceNames{
Service: test.ObjectNameForTest(t),
Image: wsServerTestImageName,
}
// Clean up in both abnormal and normal exits.
test.EnsureTearDown(t, clients, &names)
if _, err := v1test.CreateServiceReady(t, clients, &names); err != nil {
t.Fatal("Failed to create WebSocket server:", err)
}
// Validate the websocket connection.
if err := ValidateWebSocketConnection(t, clients, names, NoDelay); err != nil {
t.Error(err)
}
}
// and with -1 as target burst capacity and then validates that we can still serve.
func TestWebSocketViaActivator(t *testing.T) {
// TODO: https option with parallel leads to flakes.
// https://github.com/knative/serving/issues/11387
if !test.ServingFlags.HTTPS {
t.Parallel()
}
clients := Setup(t)
names := test.ResourceNames{
Service: test.ObjectNameForTest(t),
Image: wsServerTestImageName,
}
// Clean up in both abnormal and normal exits.
test.EnsureTearDown(t, clients, &names)
resources, err := v1test.CreateServiceReady(t, clients, &names,
rtesting.WithConfigAnnotations(map[string]string{
autoscaling.TargetBurstCapacityKey: "-1",
}),
)
if err != nil {
t.Fatal("Failed to create WebSocket server:", err)
}
// Wait for the activator endpoints to equalize.
if err := waitForActivatorEndpoints(&TestContext{
t: t,
resources: resources,
clients: clients,
}); err != nil {
t.Fatal("Never got Activator endpoints in the service:", err)
}
if err := ValidateWebSocketConnection(t, clients, names, NoDelay); err != nil {
t.Error(err)
}
}
func TestWebSocketBlueGreenRoute(t *testing.T) {
// TODO: https option with parallel leads to flakes.
// https://github.com/knative/serving/issues/11387
if !test.ServingFlags.HTTPS {
t.Parallel()
}
clients := test.Setup(t)
svcName := test.ObjectNameForTest(t)
// Long name hits this issue https://github.com/knative-extensions/net-certmanager/issues/214
if test.ServingFlags.HTTPS {
svcName = test.AppendRandomString("web-socket-blue-green")
}
names := test.ResourceNames{
// Set Service and Image for names to create the initial service
Service: svcName,
Image: wsServerTestImageName,
}
test.EnsureTearDown(t, clients, &names)
// Setup Initial Service
t.Log("Creating a new Service in runLatest")
objects, err := v1test.CreateServiceReady(t, clients, &names,
rtesting.WithEnv(corev1.EnvVar{
Name: "SUFFIX",
Value: "Blue",
}))
if err != nil {
t.Fatalf("Failed to create initial Service: %v: %v", names.Service, err)
}
blue := names
blue.TrafficTarget = "blue"
t.Log("Updating the Service to use a different suffix")
greenSvc, err := v1test.PatchService(t, clients, objects.Service, func(s *v1.Service) {
s.Spec.Template.Spec.Containers[0].Env[0].Value = "Green"
})
if err != nil {
t.Fatalf("Patch update for Service %s with new env var failed: %v", names.Service, err)
}
objects.Service = greenSvc
green := names
green.TrafficTarget = "green"
t.Log("Since the Service was updated a new Revision will be created and the Service will be updated")
green.Revision, err = v1test.WaitForServiceLatestRevision(clients, names)
if err != nil {
t.Fatalf("Service %s was not updated with the new Revision: %v", names.Service, err)
}
t.Log("Updating RouteSpec")
if _, err := v1test.PatchServiceRouteSpec(t, clients, names, v1.RouteSpec{
Traffic: []v1.TrafficTarget{{
Tag: blue.TrafficTarget,
RevisionName: blue.Revision,
Percent: ptr.Int64(50),
}, {
Tag: green.TrafficTarget,
RevisionName: green.Revision,
Percent: ptr.Int64(50),
}},
}); err != nil {
t.Fatal("Failed to update Service route spec:", err)
}
t.Log("Wait for the service domains to be ready")
if err := v1test.WaitForServiceState(clients.ServingClient, names.Service, v1test.IsServiceReady, "ServiceIsReady"); err != nil {
t.Fatalf("The Service %s was not marked as Ready to serve traffic: %v", names.Service, err)
}
service, err := clients.ServingClient.Services.Get(context.Background(), names.Service, metav1.GetOptions{})
if err != nil {
t.Fatalf("Error fetching Service %s: %v", names.Service, err)
}
// Update the names
for _, tt := range service.Status.Traffic {
if tt.Tag == green.TrafficTarget {
green.URL = tt.URL.URL()
}
}
if green.URL == nil {
t.Fatalf("Unable to fetch Green URL from traffic targets: %#v", service.Status.Traffic)
}
// Since we just validate that Network layer can properly route requests to different targets
// We'll just use the service URL.
tealURL := service.Status.URL.URL().Hostname()
// But since network programming takes some time to take effect
// and it doesn't have a Status, we'll probe `green` until it's ready first.
if err := ValidateWebSocketConnection(t, clients, green, NoDelay); err != nil {
t.Fatal("Error initializing WS connection:", err)
}
// The actual test.
const (
numReqs = 200
// Quite high, but makes sure we didn't get a one-off successful response from either target.
tolerance = 25
)
resps, err := webSocketResponseFreqs(t, clients, tealURL, numReqs)
if err != nil {
t.Error("Failed to send and receive websocket messages:", err)
}
if len(resps) != 2 {
t.Errorf("Number of responses: %d, want: 2", len(resps))
}
for k, f := range resps {
if got, want := abs(f-numReqs/2), tolerance; got > want {
t.Errorf("Target %s got %d responses, expect in [%d, %d] interval", k, f, numReqs/2-tolerance, numReqs/2+tolerance)
}
}
}
// TestWebSocketWithTimeout
// (1) creates a service based on the `wsserver` image,
// (2) connects to the service using websocket
// (3) and sets a delay using a request param, then
// (4) sends a message, and verifies that we receive back
// (5) the same message within the timeout or get an error.
func TestWebSocketWithTimeout(t *testing.T) {
clients := Setup(t)
testCases := []struct {
name string
timeoutSeconds int64
responseStartTimeoutSeconds int64
idleTimeoutSeconds int64
delay string
expectError bool
}{{
name: "does not exceed timeout seconds",
timeoutSeconds: 10,
delay: "2",
expectError: false,
}, {
name: "exceeds timeout seconds",
timeoutSeconds: 10,
delay: "20",
expectError: true,
}, {
name: "does not exceed response start timeout seconds",
responseStartTimeoutSeconds: 10,
delay: "2",
expectError: false,
}, {
name: "exceeds response start timeout seconds",
responseStartTimeoutSeconds: 10,
delay: "20",
expectError: true,
}, {
name: "does not exceed idle timeout seconds",
idleTimeoutSeconds: 10,
delay: "2",
expectError: false,
}, {
name: "exceeds idle timeout seconds",
idleTimeoutSeconds: 10,
delay: "20",
expectError: true,
}}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
names := test.ResourceNames{
Service: test.ObjectNameForTest(t),
Image: wsServerTestImageName,
}
// Clean up in both abnormal and normal exits.
test.EnsureTearDown(t, clients, &names)
_, err := v1test.CreateServiceReady(t, clients, &names,
rtesting.WithRevisionTimeoutSeconds(tc.timeoutSeconds),
rtesting.WithRevisionResponseStartTimeoutSeconds(tc.responseStartTimeoutSeconds),
rtesting.WithRevisionIdleTimeoutSeconds(tc.idleTimeoutSeconds))
if err != nil {
t.Fatal("Failed to create WebSocket server:", err)
}
// Validate the websocket connection.
err = ValidateWebSocketConnection(t, clients, names, tc.delay)
if (err == nil && tc.expectError) || (err != nil && !tc.expectError) {
t.Error(err)
}
})
}
}
func abs(a int) int {
if a < 0 {
return -a
}
return a
}