forked from knative/serving
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebsocket_test.go
295 lines (251 loc) · 8.2 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
// +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"
"fmt"
"strings"
"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"
)
const message = "Hello, websocket"
func validateWebSocketConnection(t *testing.T, clients *test.Clients, names test.ResourceNames) error {
t.Helper()
// Establish the websocket connection.
conn, err := connect(t, clients, names.URL.Hostname())
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 compared with sent.
_, recv, err := conn.ReadMessage()
if err != nil {
return err
} else if strings.HasPrefix(string(recv), message) {
t.Logf("Received message %q from echo server.", recv)
return nil
}
return fmt.Errorf("expected to receive back the message: %q but received %q", message, string(recv))
}
// 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)
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) {
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); 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) {
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); err != nil {
t.Error(err)
}
}
func TestWebSocketBlueGreenRoute(t *testing.T) {
t.Parallel()
clients := test.Setup(t)
names := test.ResourceNames{
// Set Service and Image for names to create the initial service
Service: test.ObjectNameForTest(t),
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.UpdateServiceRouteSpec(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); 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)
}
}
}
func abs(a int) int {
if a < 0 {
return -a
}
return a
}