/
test_helper.go
173 lines (150 loc) · 5.04 KB
/
test_helper.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
package public
import (
"context"
"io"
"sync"
"time"
healthcheckPb "github.com/linkerd/linkerd2/controller/gen/common/healthcheck"
pb "github.com/linkerd/linkerd2/controller/gen/public"
"github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/model"
"google.golang.org/grpc"
)
type MockApiClient struct {
ErrorToReturn error
VersionInfoToReturn *pb.VersionInfo
ListPodsResponseToReturn *pb.ListPodsResponse
StatSummaryResponseToReturn *pb.StatSummaryResponse
SelfCheckResponseToReturn *healthcheckPb.SelfCheckResponse
Api_TapClientToReturn pb.Api_TapClient
Api_TapByResourceClientToReturn pb.Api_TapByResourceClient
}
func (c *MockApiClient) StatSummary(ctx context.Context, in *pb.StatSummaryRequest, opts ...grpc.CallOption) (*pb.StatSummaryResponse, error) {
return c.StatSummaryResponseToReturn, c.ErrorToReturn
}
func (c *MockApiClient) Version(ctx context.Context, in *pb.Empty, opts ...grpc.CallOption) (*pb.VersionInfo, error) {
return c.VersionInfoToReturn, c.ErrorToReturn
}
func (c *MockApiClient) ListPods(ctx context.Context, in *pb.ListPodsRequest, opts ...grpc.CallOption) (*pb.ListPodsResponse, error) {
return c.ListPodsResponseToReturn, c.ErrorToReturn
}
func (c *MockApiClient) Tap(ctx context.Context, in *pb.TapRequest, opts ...grpc.CallOption) (pb.Api_TapClient, error) {
return c.Api_TapClientToReturn, c.ErrorToReturn
}
func (c *MockApiClient) TapByResource(ctx context.Context, in *pb.TapByResourceRequest, opts ...grpc.CallOption) (pb.Api_TapByResourceClient, error) {
return c.Api_TapByResourceClientToReturn, c.ErrorToReturn
}
func (c *MockApiClient) SelfCheck(ctx context.Context, in *healthcheckPb.SelfCheckRequest, _ ...grpc.CallOption) (*healthcheckPb.SelfCheckResponse, error) {
return c.SelfCheckResponseToReturn, c.ErrorToReturn
}
type MockApi_TapClient struct {
TapEventsToReturn []pb.TapEvent
ErrorsToReturn []error
grpc.ClientStream
}
func (a *MockApi_TapClient) Recv() (*pb.TapEvent, error) {
var eventPopped pb.TapEvent
var errorPopped error
if len(a.TapEventsToReturn) == 0 && len(a.ErrorsToReturn) == 0 {
return nil, io.EOF
}
if len(a.TapEventsToReturn) != 0 {
eventPopped, a.TapEventsToReturn = a.TapEventsToReturn[0], a.TapEventsToReturn[1:]
}
if len(a.ErrorsToReturn) != 0 {
errorPopped, a.ErrorsToReturn = a.ErrorsToReturn[0], a.ErrorsToReturn[1:]
}
return &eventPopped, errorPopped
}
type MockApi_TapByResourceClient struct {
TapEventsToReturn []pb.TapEvent
ErrorsToReturn []error
grpc.ClientStream
}
func (a *MockApi_TapByResourceClient) Recv() (*pb.TapEvent, error) {
var eventPopped pb.TapEvent
var errorPopped error
if len(a.TapEventsToReturn) == 0 && len(a.ErrorsToReturn) == 0 {
return nil, io.EOF
}
if len(a.TapEventsToReturn) != 0 {
eventPopped, a.TapEventsToReturn = a.TapEventsToReturn[0], a.TapEventsToReturn[1:]
}
if len(a.ErrorsToReturn) != 0 {
errorPopped, a.ErrorsToReturn = a.ErrorsToReturn[0], a.ErrorsToReturn[1:]
}
return &eventPopped, errorPopped
}
//
// Prometheus client
//
type MockProm struct {
Res model.Value
QueriesExecuted []string // expose the queries our Mock Prometheus receives, to test query generation
rwLock sync.Mutex
}
type PodCounts struct {
MeshedPods uint64
RunningPods uint64
FailedPods uint64
}
// satisfies v1.API
func (m *MockProm) Query(ctx context.Context, query string, ts time.Time) (model.Value, error) {
m.rwLock.Lock()
defer m.rwLock.Unlock()
m.QueriesExecuted = append(m.QueriesExecuted, query)
return m.Res, nil
}
func (m *MockProm) QueryRange(ctx context.Context, query string, r v1.Range) (model.Value, error) {
m.rwLock.Lock()
defer m.rwLock.Unlock()
m.QueriesExecuted = append(m.QueriesExecuted, query)
return m.Res, nil
}
func (m *MockProm) LabelValues(ctx context.Context, label string) (model.LabelValues, error) {
return nil, nil
}
func (m *MockProm) Series(ctx context.Context, matches []string, startTime time.Time, endTime time.Time) ([]model.LabelSet, error) {
return nil, nil
}
func GenStatSummaryResponse(resName, resType, resNs string, counts *PodCounts) pb.StatSummaryResponse {
statTableRow := &pb.StatTable_PodGroup_Row{
Resource: &pb.Resource{
Namespace: resNs,
Type: resType,
Name: resName,
},
Stats: &pb.BasicStats{
SuccessCount: 123,
FailureCount: 0,
LatencyMsP50: 123,
LatencyMsP95: 123,
LatencyMsP99: 123,
TlsRequestCount: 123,
},
TimeWindow: "1m",
}
if counts != nil {
statTableRow.MeshedPodCount = counts.MeshedPods
statTableRow.RunningPodCount = counts.RunningPods
statTableRow.FailedPodCount = counts.FailedPods
}
resp := pb.StatSummaryResponse{
Response: &pb.StatSummaryResponse_Ok_{ // https://github.com/golang/protobuf/issues/205
Ok: &pb.StatSummaryResponse_Ok{
StatTables: []*pb.StatTable{
&pb.StatTable{
Table: &pb.StatTable_PodGroup_{
PodGroup: &pb.StatTable_PodGroup{
Rows: []*pb.StatTable_PodGroup_Row{
statTableRow,
},
},
},
},
},
},
},
}
return resp
}