forked from linkerd/linkerd2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedges_test.go
181 lines (160 loc) · 5.12 KB
/
edges_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
package api
import (
"context"
"errors"
"fmt"
"testing"
pkgK8s "github.com/linkerd/linkerd2/pkg/k8s"
pb "github.com/linkerd/linkerd2/viz/metrics-api/gen/viz"
"github.com/prometheus/common/model"
"google.golang.org/protobuf/proto"
)
const (
serverIDLabel = model.LabelName("server_id")
resourceLabel = model.LabelName("deployment")
podLabel = model.LabelName("pod")
)
type edgesExpected struct {
expectedStatRPC
req *pb.EdgesRequest // the request we would like to test
expectedResponse *pb.EdgesResponse // the edges response we expect
}
func genOutboundPromSample(resourceNamespace, resourceName, resourceNameDst, resourceNamespaceDst, serverID string) *model.Sample {
dstResourceLabel := "dst_" + resourceLabel
return &model.Sample{
Metric: model.Metric{
resourceLabel: model.LabelValue(resourceName),
namespaceLabel: model.LabelValue(resourceNamespace),
dstNamespaceLabel: model.LabelValue(resourceNamespaceDst),
dstResourceLabel: model.LabelValue(resourceNameDst),
serverIDLabel: model.LabelValue(serverID),
podLabel: model.LabelValue(resourceName + "-0"),
},
Value: 123,
Timestamp: 456,
}
}
func genPod(name, namespace, sa string) string {
return fmt.Sprintf(`apiVersion: v1
kind: Pod
metadata:
name: %s
namespace: %s
spec:
containers:
- name: linkerd-proxy
env:
- name: LINKERD2_PROXY_IDENTITY_LOCAL_NAME
value: $(_pod_sa).$(_pod_ns).serviceaccount.identity.linkerd.cluster.local
serviceAccountName: %s
status:
phase: Running
`, name, namespace, sa)
}
func testEdges(t *testing.T, expectations []edgesExpected) {
for _, exp := range expectations {
mockProm, fakeGrpcServer, err := newMockGrpcServer(exp.expectedStatRPC)
if err != nil {
t.Fatalf("Error creating mock grpc server: %s", err)
}
rsp, err := fakeGrpcServer.Edges(context.TODO(), exp.req)
if !errors.Is(err, exp.err) {
t.Fatalf("Expected error: %s, Got: %s", exp.err, err)
}
err = exp.verifyPromQueries(mockProm)
if err != nil {
t.Fatal(err)
}
rspEdgeRows := rsp.GetOk().Edges
if len(rspEdgeRows) != len(exp.expectedResponse.GetOk().Edges) {
t.Fatalf(
"Expected [%d] edge rows, got [%d].\nExpected:\n%s\nGot:\n%s",
len(exp.expectedResponse.GetOk().Edges),
len(rspEdgeRows),
exp.expectedResponse.GetOk().Edges,
rspEdgeRows,
)
}
for i, st := range rspEdgeRows {
expected := exp.expectedResponse.GetOk().Edges[i]
if !proto.Equal(st, expected) {
t.Fatalf("Expected: %+v\n Got: %+v\n", expected, st)
}
}
if !proto.Equal(exp.expectedResponse.GetOk(), rsp.GetOk()) {
t.Fatalf("Expected edgesOkResp: %+v\n Got: %+v", &exp.expectedResponse, rsp)
}
}
}
func TestEdges(t *testing.T) {
mockPromResponse := model.Vector{
genOutboundPromSample("emojivoto", "web", "emoji", "emojivoto", "emoji.emojivoto.serviceaccount.identity.linkerd.cluster.local"),
genOutboundPromSample("emojivoto", "web", "voting", "emojivoto", "voting.emojivoto.serviceaccount.identity.linkerd.cluster.local"),
genOutboundPromSample("emojivoto", "vote-bot", "web", "emojivoto", "web.emojivoto.serviceaccount.identity.linkerd.cluster.local"),
genOutboundPromSample("linkerd", "linkerd-identity", "linkerd-prometheus", "linkerd", "linkerd-prometheus.linkerd.serviceaccount.identity.linkerd.cluster.local"),
}
pods := []string{
genPod("web-0", "emojivoto", "web"),
genPod("vote-bot-0", "emojivoto", "default"),
genPod("linkerd-identity-0", "linkerd", "linkerd-identity"),
}
t.Run("Successfully returns edges for resource type Deployment and namespace emojivoto", func(t *testing.T) {
expectations := []edgesExpected{
{
expectedStatRPC: expectedStatRPC{
err: nil,
mockPromResponse: mockPromResponse,
k8sConfigs: pods,
},
req: &pb.EdgesRequest{
Selector: &pb.ResourceSelection{
Resource: &pb.Resource{
Namespace: "emojivoto",
Type: pkgK8s.Deployment,
},
},
},
expectedResponse: GenEdgesResponse("deployment", "emojivoto"),
}}
testEdges(t, expectations)
})
t.Run("Successfully returns edges for resource type Deployment and namespace linkerd", func(t *testing.T) {
expectations := []edgesExpected{
{
expectedStatRPC: expectedStatRPC{
err: nil,
mockPromResponse: mockPromResponse,
k8sConfigs: pods,
},
req: &pb.EdgesRequest{
Selector: &pb.ResourceSelection{
Resource: &pb.Resource{
Namespace: "linkerd",
Type: pkgK8s.Deployment,
},
},
},
expectedResponse: GenEdgesResponse("deployment", "linkerd"),
}}
testEdges(t, expectations)
})
t.Run("Successfully returns edges for resource type Deployment and all namespaces", func(t *testing.T) {
expectations := []edgesExpected{
{
expectedStatRPC: expectedStatRPC{
err: nil,
mockPromResponse: mockPromResponse,
k8sConfigs: pods,
},
req: &pb.EdgesRequest{
Selector: &pb.ResourceSelection{
Resource: &pb.Resource{
Type: pkgK8s.Deployment,
},
},
},
expectedResponse: GenEdgesResponse("deployment", "all"),
}}
testEdges(t, expectations)
})
}