forked from GoogleContainerTools/skaffold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc_test.go
440 lines (379 loc) · 12 KB
/
rpc_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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
Copyright 2019 The Skaffold 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 integration
import (
"context"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"strconv"
"strings"
"testing"
"time"
"github.com/golang/protobuf/jsonpb" //nolint:golint,staticcheck
"github.com/golang/protobuf/ptypes/empty"
"google.golang.org/grpc"
"github.com/GoogleContainerTools/skaffold/integration/skaffold"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/event"
"github.com/GoogleContainerTools/skaffold/proto"
"github.com/GoogleContainerTools/skaffold/testutil"
)
var (
connectionRetries = 2
readRetries = 20
numLogEntries = 7
waitTime = 1 * time.Second
)
func TestEventsRPC(t *testing.T) {
MarkIntegrationTest(t, CanRunWithoutGcp)
rpcAddr := randomPort()
setupSkaffoldWithArgs(t, "--rpc-port", rpcAddr, "--status-check=false")
// start a grpc client and make sure we can connect properly
var (
conn *grpc.ClientConn
err error
client proto.SkaffoldServiceClient
)
// connect to the skaffold grpc server
for i := 0; i < connectionRetries; i++ {
conn, err = grpc.Dial(fmt.Sprintf(":%s", rpcAddr), grpc.WithInsecure())
if err != nil {
t.Logf("unable to establish skaffold grpc connection: retrying...")
time.Sleep(waitTime)
continue
}
defer conn.Close()
client = proto.NewSkaffoldServiceClient(conn)
break
}
if client == nil {
t.Fatalf("error establishing skaffold grpc connection")
}
ctx, ctxCancel := context.WithCancel(context.Background())
defer ctxCancel()
// read the event log stream from the skaffold grpc server
var stream proto.SkaffoldService_EventsClient
for i := 0; i < readRetries; i++ {
stream, err = client.Events(ctx, &empty.Empty{})
if err != nil {
t.Logf("waiting for connection...")
time.Sleep(waitTime)
continue
}
}
if stream == nil {
t.Fatalf("error retrieving event log: %v\n", err)
}
// read a preset number of entries from the event log
var logEntries []*proto.LogEntry
entriesReceived := 0
for {
entry, err := stream.Recv()
if err != nil {
t.Errorf("error receiving entry from stream: %s", err)
}
if entry != nil {
logEntries = append(logEntries, entry)
entriesReceived++
}
if entriesReceived == numLogEntries {
break
}
}
metaEntries, buildEntries, deployEntries, devLoopEntries := 0, 0, 0, 0
for _, entry := range logEntries {
switch entry.Event.GetEventType().(type) {
case *proto.Event_MetaEvent:
metaEntries++
t.Logf("meta event %d: %v", metaEntries, entry.Event)
case *proto.Event_BuildEvent:
buildEntries++
t.Logf("build event %d: %v", buildEntries, entry.Event)
case *proto.Event_DeployEvent:
deployEntries++
t.Logf("deploy event %d: %v", deployEntries, entry.Event)
case *proto.Event_DevLoopEvent:
devLoopEntries++
t.Logf("devloop event event %d: %v", devLoopEntries, entry.Event)
default:
t.Logf("unknown event: %v", entry.Event)
}
}
// make sure we have exactly 1 meta entry, 2 deploy entries and 2 build entries and 2 devLoopEntries
testutil.CheckDeepEqual(t, 1, metaEntries)
testutil.CheckDeepEqual(t, 2, deployEntries)
testutil.CheckDeepEqual(t, 2, buildEntries)
testutil.CheckDeepEqual(t, 2, devLoopEntries)
}
func TestEventLogHTTP(t *testing.T) {
MarkIntegrationTest(t, CanRunWithoutGcp)
tests := []struct {
description string
endpoint string
}{
{
//TODO deprecate (https://github.com/GoogleContainerTools/skaffold/issues/3168)
description: "/v1/event_log",
endpoint: "/v1/event_log",
},
{
description: "/v1/events",
endpoint: "/v1/events",
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
httpAddr := randomPort()
setupSkaffoldWithArgs(t, "--rpc-http-port", httpAddr, "--status-check=false")
time.Sleep(500 * time.Millisecond) // give skaffold time to process all events
httpResponse, err := http.Get(fmt.Sprintf("http://localhost:%s%s", httpAddr, test.endpoint))
if err != nil {
t.Fatalf("error connecting to gRPC REST API: %s", err.Error())
}
defer httpResponse.Body.Close()
numEntries := 0
var logEntries []proto.LogEntry
for {
e := make([]byte, 1024)
l, err := httpResponse.Body.Read(e)
if err != nil {
t.Errorf("error reading body from http response: %s", err.Error())
}
e = e[0:l] // remove empty bytes from slice
// sometimes reads can encompass multiple log entries, since Read() doesn't count newlines as EOF.
readEntries := strings.Split(string(e), "\n")
for _, entryStr := range readEntries {
if entryStr == "" {
continue
}
var entry proto.LogEntry
// the HTTP wrapper sticks the proto messages into a map of "result" -> message.
// attempting to JSON unmarshal drops necessary proto information, so we just manually
// strip the string off the response and unmarshal directly to the proto message
entryStr = strings.Replace(entryStr, "{\"result\":", "", 1)
entryStr = entryStr[:len(entryStr)-1]
if err := jsonpb.UnmarshalString(entryStr, &entry); err != nil {
t.Errorf("error converting http response %s to proto: %s", entryStr, err.Error())
}
numEntries++
logEntries = append(logEntries, entry)
}
if numEntries >= numLogEntries {
break
}
}
metaEntries, buildEntries, deployEntries, devLoopEntries := 0, 0, 0, 0
for _, entry := range logEntries {
switch entry.Event.GetEventType().(type) {
case *proto.Event_MetaEvent:
metaEntries++
t.Logf("meta event %d: %v", metaEntries, entry.Event)
case *proto.Event_BuildEvent:
buildEntries++
t.Logf("build event %d: %v", buildEntries, entry.Event)
case *proto.Event_DeployEvent:
deployEntries++
t.Logf("deploy event %d: %v", deployEntries, entry.Event)
case *proto.Event_DevLoopEvent:
devLoopEntries++
t.Logf("devloop event event %d: %v", devLoopEntries, entry.Event)
default:
t.Logf("unknown event: %v", entry.Event)
}
}
// make sure we have exactly 1 meta entry, 2 deploy entries, 2 build entries and 2 devLoopEntries
testutil.CheckDeepEqual(t, 1, metaEntries)
testutil.CheckDeepEqual(t, 2, deployEntries)
testutil.CheckDeepEqual(t, 2, buildEntries)
testutil.CheckDeepEqual(t, 2, devLoopEntries)
})
}
}
func TestGetStateRPC(t *testing.T) {
MarkIntegrationTest(t, CanRunWithoutGcp)
rpcAddr := randomPort()
// start a skaffold dev loop on an example
setupSkaffoldWithArgs(t, "--rpc-port", rpcAddr)
// start a grpc client and make sure we can connect properly
var (
conn *grpc.ClientConn
err error
client proto.SkaffoldServiceClient
)
for i := 0; i < connectionRetries; i++ {
conn, err = grpc.Dial(fmt.Sprintf(":%s", rpcAddr), grpc.WithInsecure())
if err != nil {
t.Logf("unable to establish skaffold grpc connection: retrying...")
time.Sleep(waitTime)
continue
}
defer conn.Close()
client = proto.NewSkaffoldServiceClient(conn)
break
}
if client == nil {
t.Fatalf("error establishing skaffold grpc connection")
}
ctx, ctxCancel := context.WithCancel(context.Background())
defer ctxCancel()
// try a few times and wait around until we see the build is complete, or fail.
success := false
var grpcState *proto.State
for i := 0; i < readRetries; i++ {
grpcState = retrieveRPCState(ctx, t, client)
if grpcState != nil && checkBuildAndDeployComplete(*grpcState) {
success = true
break
}
time.Sleep(waitTime)
}
if !success {
t.Errorf("skaffold build or deploy not complete. state: %+v\n", grpcState)
}
}
func TestGetStateHTTP(t *testing.T) {
MarkIntegrationTest(t, CanRunWithoutGcp)
httpAddr := randomPort()
setupSkaffoldWithArgs(t, "--rpc-http-port", httpAddr)
time.Sleep(3 * time.Second) // give skaffold time to process all events
success := false
var httpState proto.State
for i := 0; i < readRetries; i++ {
httpState = retrieveHTTPState(t, httpAddr)
if checkBuildAndDeployComplete(httpState) {
success = true
break
}
time.Sleep(waitTime)
}
if !success {
t.Errorf("skaffold build or deploy not complete. state: %+v\n", httpState)
}
}
func retrieveRPCState(ctx context.Context, t *testing.T, client proto.SkaffoldServiceClient) *proto.State {
attempts := 0
for {
grpcState, err := client.GetState(ctx, &empty.Empty{})
if err != nil {
if attempts >= connectionRetries {
t.Fatalf("error retrieving state: %v\n", err)
}
t.Logf("waiting for connection...")
attempts++
time.Sleep(waitTime)
continue
}
return grpcState
}
}
func retrieveHTTPState(t *testing.T, httpAddr string) proto.State {
var httpState proto.State
// retrieve the state via HTTP as well, and verify the result is the same
httpResponse, err := http.Get(fmt.Sprintf("http://localhost:%s/v1/state", httpAddr))
if err != nil {
t.Fatalf("error connecting to gRPC REST API: %s", err.Error())
}
defer httpResponse.Body.Close()
b, err := ioutil.ReadAll(httpResponse.Body)
if err != nil {
t.Errorf("error reading body from http response: %s", err.Error())
}
if err := jsonpb.UnmarshalString(string(b), &httpState); err != nil {
t.Errorf("error converting http response to proto: %s", err.Error())
}
return httpState
}
func setupSkaffoldWithArgs(t *testing.T, args ...string) {
Run(t, "testdata/dev", "sh", "-c", "echo foo > foo")
// Run skaffold build first to fail quickly on a build failure
skaffold.Build().InDir("testdata/dev").RunOrFail(t)
// start a skaffold dev loop on an example
ns, _ := SetupNamespace(t)
skaffold.Dev(append([]string{"--cache-artifacts=false"}, args...)...).InDir("testdata/dev").InNs(ns.Name).RunBackground(t)
t.Cleanup(func() {
Run(t, "testdata/dev", "rm", "foo")
})
}
// randomPort chooses a port in range [1024, 65535]
func randomPort() string {
return strconv.Itoa(1024 + rand.Intn(65536-1024))
}
func checkBuildAndDeployComplete(state proto.State) bool {
if state.BuildState == nil || state.DeployState == nil {
return false
}
for _, a := range state.BuildState.Artifacts {
if a != event.Complete {
return false
}
}
return state.DeployState.Status == event.Complete
}
func apiEvents(t *testing.T, rpcAddr string) (proto.SkaffoldServiceClient, chan *proto.LogEntry) {
client := setupRPCClient(t, rpcAddr)
stream, err := readEventAPIStream(client, t, readRetries)
if stream == nil {
t.Fatalf("error retrieving event log: %v\n", err)
}
// read entries from the log
entries := make(chan *proto.LogEntry)
go func() {
for {
entry, _ := stream.Recv()
if entry != nil {
entries <- entry
}
}
}()
return client, entries
}
func readEventAPIStream(client proto.SkaffoldServiceClient, t *testing.T, retries int) (proto.SkaffoldService_EventLogClient, error) {
t.Helper()
// read the event log stream from the skaffold grpc server
var stream proto.SkaffoldService_EventLogClient
var err error
for i := 0; i < retries; i++ {
stream, err = client.EventLog(context.Background())
if err != nil {
t.Logf("waiting for connection...")
time.Sleep(waitTime)
continue
}
}
return stream, err
}
func setupRPCClient(t *testing.T, port string) proto.SkaffoldServiceClient {
// start a grpc client
var (
conn *grpc.ClientConn
err error
client proto.SkaffoldServiceClient
)
// connect to the skaffold grpc server
for i := 0; i < connectionRetries; i++ {
conn, err = grpc.Dial(fmt.Sprintf(":%s", port), grpc.WithInsecure())
if err != nil {
t.Logf("unable to establish skaffold grpc connection: retrying...")
time.Sleep(waitTime)
continue
}
client = proto.NewSkaffoldServiceClient(conn)
break
}
if client == nil {
t.Fatalf("error establishing skaffold grpc connection")
}
t.Cleanup(func() { conn.Close() })
return client
}