forked from linkerd/linkerd2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrpc_server_test.go
503 lines (468 loc) · 11.3 KB
/
grpc_server_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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
package api
import (
"context"
"errors"
"fmt"
"sort"
"strings"
"testing"
"github.com/go-test/deep"
"github.com/golang/protobuf/ptypes/duration"
"github.com/linkerd/linkerd2/controller/k8s"
pkgK8s "github.com/linkerd/linkerd2/pkg/k8s"
"github.com/linkerd/linkerd2/pkg/prometheus"
pb "github.com/linkerd/linkerd2/viz/metrics-api/gen/viz"
"github.com/prometheus/common/model"
)
type listPodsExpected struct {
err error
k8sRes []string
promRes model.Value
req *pb.ListPodsRequest
res *pb.ListPodsResponse
promReqNamespace string
}
type listServicesExpected struct {
err error
k8sRes []string
res *pb.ListServicesResponse
}
// sort Pods in ListPodResponses for easier comparison
type ByPod []*pb.Pod
func (bp ByPod) Len() int { return len(bp) }
func (bp ByPod) Swap(i, j int) { bp[i], bp[j] = bp[j], bp[i] }
func (bp ByPod) Less(i, j int) bool { return bp[i].Name <= bp[j].Name }
// sort Services in ListServiceResponses for easier comparison
type ByService []*pb.Service
func (bs ByService) Len() int { return len(bs) }
func (bs ByService) Swap(i, j int) { bs[i], bs[j] = bs[j], bs[i] }
func (bs ByService) Less(i, j int) bool { return bs[i].Name <= bs[j].Name }
func listPodResponsesEqual(a *pb.ListPodsResponse, b *pb.ListPodsResponse) bool {
if a == nil || b == nil {
return a == b
}
if len(a.Pods) != len(b.Pods) {
return false
}
sort.Sort(ByPod(a.Pods))
sort.Sort(ByPod(b.Pods))
for i := 0; i < len(a.Pods); i++ {
aPod := a.Pods[i]
bPod := b.Pods[i]
if (aPod.Name != bPod.Name) ||
(aPod.Added != bPod.Added) ||
(aPod.Status != bPod.Status) ||
(aPod.PodIP != bPod.PodIP) ||
(aPod.GetDeployment() != bPod.GetDeployment()) {
return false
}
if (aPod.SinceLastReport == nil && bPod.SinceLastReport != nil) ||
(aPod.SinceLastReport != nil && bPod.SinceLastReport == nil) {
return false
}
}
return true
}
func TestListPods(t *testing.T) {
t.Run("Queries to the ListPods endpoint", func(t *testing.T) {
expectations := []listPodsExpected{
{
err: nil,
promRes: model.Vector{
&model.Sample{
Metric: model.Metric{"pod": "emojivoto-meshed"},
Timestamp: 456,
},
},
k8sRes: []string{`
apiVersion: v1
kind: Pod
metadata:
name: emojivoto-meshed
namespace: emojivoto
labels:
pod-template-hash: hash-meshed
ownerReferences:
- apiVersion: apps/v1
kind: ReplicaSet
name: rs-emojivoto-meshed
status:
phase: Running
podIP: 1.2.3.4
`, `
apiVersion: v1
kind: Pod
metadata:
name: emojivoto-not-meshed
namespace: emojivoto
labels:
pod-template-hash: hash-not-meshed
ownerReferences:
- apiVersion: apps/v1
kind: ReplicaSet
name: rs-emojivoto-not-meshed
status:
phase: Pending
podIP: 4.3.2.1
`, `
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: rs-emojivoto-meshed
namespace: emojivoto
ownerReferences:
- apiVersion: apps/v1
kind: Deployment
name: meshed-deployment
spec:
selector:
matchLabels:
pod-template-hash: hash-meshed
`, `
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: rs-emojivoto-not-meshed
namespace: emojivoto
ownerReferences:
- apiVersion: apps/v1
kind: Deployment
name: not-meshed-deployment
spec:
selector:
matchLabels:
pod-template-hash: hash-not-meshed
`,
},
req: &pb.ListPodsRequest{},
res: &pb.ListPodsResponse{
Pods: []*pb.Pod{
{
Name: "emojivoto/emojivoto-meshed",
Added: true,
SinceLastReport: &duration.Duration{},
Status: "Running",
PodIP: "1.2.3.4",
Owner: &pb.Pod_Deployment{Deployment: "emojivoto/meshed-deployment"},
},
{
Name: "emojivoto/emojivoto-not-meshed",
Status: "Pending",
PodIP: "4.3.2.1",
Owner: &pb.Pod_Deployment{Deployment: "emojivoto/not-meshed-deployment"},
},
},
},
},
{
err: nil,
promRes: model.Vector{
&model.Sample{
Metric: model.Metric{"pod": "emojivoto-meshed"},
Timestamp: 456,
},
},
k8sRes: []string{},
req: &pb.ListPodsRequest{
Selector: &pb.ResourceSelection{
Resource: &pb.Resource{
Namespace: "testnamespace",
},
},
},
res: &pb.ListPodsResponse{},
promReqNamespace: "testnamespace",
},
{
err: nil,
promRes: model.Vector{
&model.Sample{
Metric: model.Metric{"pod": "emojivoto-meshed"},
Timestamp: 456,
},
},
k8sRes: []string{},
req: &pb.ListPodsRequest{
Selector: &pb.ResourceSelection{
Resource: &pb.Resource{
Type: pkgK8s.Namespace,
Name: "testnamespace",
},
},
},
res: &pb.ListPodsResponse{},
promReqNamespace: "testnamespace",
},
// non-matching owner type -> no pod in the result
{
err: nil,
promRes: model.Vector{
&model.Sample{
Metric: model.Metric{"pod": "emojivoto-meshed"},
Timestamp: 456,
},
},
k8sRes: []string{`
apiVersion: v1
kind: Pod
metadata:
name: emojivoto-meshed
namespace: emojivoto
labels:
pod-template-hash: hash-meshed
ownerReferences:
- apiVersion: apps/v1
kind: Deployment
name: meshed-deployment
status:
phase: Running
podIP: 1.2.3.4
`,
},
req: &pb.ListPodsRequest{
Selector: &pb.ResourceSelection{
Resource: &pb.Resource{
Type: pkgK8s.Pod,
Name: "non-existing-pod",
},
},
},
res: &pb.ListPodsResponse{},
},
// matching owner type -> pod is part of the result
{
err: nil,
promRes: model.Vector{
&model.Sample{
Metric: model.Metric{"pod": "emojivoto-meshed"},
Timestamp: 456,
},
},
k8sRes: []string{`
apiVersion: v1
kind: Pod
metadata:
name: emojivoto-meshed
namespace: emojivoto
labels:
pod-template-hash: hash-meshed
ownerReferences:
- apiVersion: apps/v1
kind: Deployment
name: meshed-deployment
status:
phase: Running
podIP: 1.2.3.4
`,
},
req: &pb.ListPodsRequest{
Selector: &pb.ResourceSelection{
Resource: &pb.Resource{
Type: pkgK8s.Deployment,
Name: "meshed-deployment",
},
},
},
res: &pb.ListPodsResponse{
Pods: []*pb.Pod{
{
Name: "emojivoto/emojivoto-meshed",
Added: true,
SinceLastReport: &duration.Duration{},
Status: "Running",
PodIP: "1.2.3.4",
Owner: &pb.Pod_Deployment{Deployment: "emojivoto/meshed-deployment"},
},
},
},
},
// matching label in request -> pod is in the response
{
err: nil,
promRes: model.Vector{
&model.Sample{
Metric: model.Metric{"pod": "emojivoto-meshed"},
Timestamp: 456,
},
},
k8sRes: []string{`
apiVersion: v1
kind: Pod
metadata:
name: emojivoto-meshed
namespace: emojivoto
labels:
pod-template-hash: hash-meshed
ownerReferences:
- apiVersion: apps/v1
kind: Deployment
name: meshed-deployment
status:
phase: Running
podIP: 1.2.3.4
`,
},
req: &pb.ListPodsRequest{
Selector: &pb.ResourceSelection{
LabelSelector: "pod-template-hash=hash-meshed",
},
},
res: &pb.ListPodsResponse{
Pods: []*pb.Pod{
{
Name: "emojivoto/emojivoto-meshed",
Added: true,
SinceLastReport: &duration.Duration{},
Status: "Running",
PodIP: "1.2.3.4",
Owner: &pb.Pod_Deployment{Deployment: "emojivoto/meshed-deployment"},
},
},
},
},
// NOT matching label in request -> pod is NOT in the response
{
err: nil,
promRes: model.Vector{
&model.Sample{
Metric: model.Metric{"pod": "emojivoto-meshed"},
Timestamp: 456,
},
},
k8sRes: []string{`
apiVersion: v1
kind: Pod
metadata:
name: emojivoto-meshed
namespace: emojivoto
labels:
pod-template-hash: hash-meshed
ownerReferences:
- apiVersion: apps/v1
kind: Deployment
name: meshed-deployment
status:
phase: Running
podIP: 1.2.3.4
`,
},
req: &pb.ListPodsRequest{
Selector: &pb.ResourceSelection{
LabelSelector: "non-existent-label=value",
},
},
res: &pb.ListPodsResponse{},
},
}
for _, exp := range expectations {
k8sAPI, err := k8s.NewFakeAPI(exp.k8sRes...)
if err != nil {
t.Fatalf("NewFakeAPI returned an error: %s", err)
}
mProm := prometheus.MockProm{Res: exp.promRes}
fakeGrpcServer := grpcServer{
prometheusAPI: &mProm,
k8sAPI: k8sAPI,
controllerNamespace: "linkerd",
clusterDomain: "mycluster.local",
ignoredNamespaces: []string{},
}
k8sAPI.Sync(nil)
rsp, err := fakeGrpcServer.ListPods(context.TODO(), exp.req)
if diff := deep.Equal(err, exp.err); diff != nil {
t.Fatalf("%+v", diff)
}
if !listPodResponsesEqual(exp.res, rsp) {
t.Fatalf("Expected: %+v, Got: %+v", exp.res, rsp)
}
if exp.promReqNamespace != "" {
err := verifyPromQueries(&mProm, exp.promReqNamespace)
if err != nil {
t.Fatalf("Expected prometheus query with namespace: %s, Got error: %s", exp.promReqNamespace, err)
}
}
}
})
}
// TODO: consider refactoring with expectedStatRPC.verifyPromQueries
func verifyPromQueries(mProm *prometheus.MockProm, namespace string) error {
namespaceSelector := fmt.Sprintf("namespace=\"%s\"", namespace)
for _, element := range mProm.QueriesExecuted {
if strings.Contains(element, namespaceSelector) {
return nil
}
}
return fmt.Errorf("Prometheus queries incorrect. \nExpected query containing:\n%s \nGot:\n%+v",
namespaceSelector, mProm.QueriesExecuted)
}
func listServiceResponsesEqual(a *pb.ListServicesResponse, b *pb.ListServicesResponse) bool {
if len(a.Services) != len(b.Services) {
return false
}
sort.Sort(ByService(a.Services))
sort.Sort(ByService(b.Services))
for i := 0; i < len(a.Services); i++ {
aSvc := a.Services[i]
bSvc := b.Services[i]
if aSvc.Name != bSvc.Name || aSvc.Namespace != bSvc.Namespace {
return false
}
}
return true
}
func TestListServices(t *testing.T) {
t.Run("Successfully queries for services", func(t *testing.T) {
expectations := []listServicesExpected{
{
err: nil,
k8sRes: []string{`
apiVersion: v1
kind: Service
metadata:
name: service-foo
namespace: emojivoto
`, `
apiVersion: v1
kind: Service
metadata:
name: service-bar
namespace: default
`,
},
res: &pb.ListServicesResponse{
Services: []*pb.Service{
{
Name: "service-foo",
Namespace: "emojivoto",
},
{
Name: "service-bar",
Namespace: "default",
},
},
},
},
}
for _, exp := range expectations {
k8sAPI, err := k8s.NewFakeAPI(exp.k8sRes...)
if err != nil {
t.Fatalf("NewFakeAPI returned an error: %s", err)
}
fakeGrpcServer := grpcServer{
prometheusAPI: &prometheus.MockProm{},
k8sAPI: k8sAPI,
controllerNamespace: "linkerd",
clusterDomain: "mycluster.local",
ignoredNamespaces: []string{},
}
k8sAPI.Sync(nil)
rsp, err := fakeGrpcServer.ListServices(context.TODO(), &pb.ListServicesRequest{})
if !errors.Is(err, exp.err) {
t.Fatalf("Expected error: %s, Got: %s", exp.err, err)
}
if !listServiceResponsesEqual(exp.res, rsp) {
t.Fatalf("Expected: %+v, Got: %+v", &exp.res, rsp)
}
}
})
}