-
Notifications
You must be signed in to change notification settings - Fork 21
/
mocks_test.go
244 lines (195 loc) · 5.7 KB
/
mocks_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
// SPDX-FileCopyrightText: 2017 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package main
import (
"context"
"crypto"
"fmt"
"unicode/utf8"
"github.com/golang-jwt/jwt"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/mock"
"github.com/xmidt-org/clortho"
"github.com/xmidt-org/webpa-common/v2/device"
"github.com/xmidt-org/wrp-go/v3"
dto "github.com/prometheus/client_model/go"
)
type mockCollector struct {
}
func (m *mockCollector) Describe(chan<- *prometheus.Desc) {
}
func (m *mockCollector) Collect(chan<- prometheus.Metric) {
}
type mockMetric struct {
}
func (m *mockMetric) Desc() *prometheus.Desc {
return &prometheus.Desc{}
}
func (m *mockMetric) Write(*dto.Metric) error {
return nil
}
type mockURLFilter struct {
mock.Mock
}
func (m *mockURLFilter) Filter(v string) (string, error) {
arguments := m.Called(v)
return arguments.String(0), arguments.Error(1)
}
type mockRouter struct {
mock.Mock
}
func (m *mockRouter) Route(request *device.Request) (*device.Response, error) {
arguments := m.Called(request)
first, _ := arguments.Get(0).(*device.Response)
return first, arguments.Error(1)
}
type mockBinOp struct {
mock.Mock
}
func (m *mockBinOp) evaluate(left, right interface{}) (bool, error) {
arguments := m.Called(left, right)
return arguments.Bool(0), arguments.Error(1)
}
func (m *mockBinOp) name() string {
arguments := m.Called()
return arguments.String(0)
}
type mockDeviceAccess struct {
mock.Mock
}
func (m *mockDeviceAccess) authorizeWRP(ctx context.Context, message *wrp.Message) error {
arguments := m.Called(ctx, message)
return arguments.Error(0)
}
type mockJWTParser struct {
mock.Mock
}
func (parser *mockJWTParser) ParseJWT(token string, claims jwt.Claims, parseFunc jwt.Keyfunc) (*jwt.Token, error) {
arguments := parser.Called(token, claims, parseFunc)
jwtToken, _ := arguments.Get(0).(*jwt.Token)
return jwtToken, arguments.Error(1)
}
// mockHistogram provides the mock implementation of the metrics.Histogram object
type mockHistogram struct {
mockCollector
mock.Mock
}
func (m *mockHistogram) Observe(value float64) {
m.Called(value)
}
func (m *mockHistogram) With(labelValues prometheus.Labels) prometheus.Observer {
for k, v := range labelValues {
if !utf8.ValidString(k) {
panic(fmt.Sprintf("key `%s`, value `%s`: key is not UTF-8", k, v))
} else if !utf8.ValidString(v) {
panic(fmt.Sprintf("key `%s`, value `%s`: value is not UTF-8", k, v))
}
}
m.Called(labelValues)
return m
}
func (m *mockHistogram) CurryWith(labels prometheus.Labels) (o prometheus.ObserverVec, err error) {
return m, nil
}
func (m *mockHistogram) GetMetricWith(labels prometheus.Labels) (o prometheus.Observer, err error) {
return m, nil
}
func (m *mockHistogram) GetMetricWithLabelValues(...string) (o prometheus.Observer, err error) {
return m, nil
}
func (m *mockHistogram) MustCurryWith(labels prometheus.Labels) (o prometheus.ObserverVec) {
return m
}
func (m *mockHistogram) WithLabelValues(lvs ...string) (o prometheus.Observer) {
return m
}
// mockCounter provides the mock implementation of the metrics.Counter object
type mockCounter struct {
mockCollector
mockMetric
mock.Mock
// port over testCounter functionality
count float64
labels []string
labelPairs prometheus.Labels
}
func (m *mockCounter) Add(delta float64) {
m.count += delta
m.Called(delta)
}
func (m *mockCounter) Inc() {}
func (m *mockCounter) With(labelPairs prometheus.Labels) prometheus.Counter {
for k, v := range labelPairs {
if !utf8.ValidString(k) {
panic(fmt.Sprintf("key `%s`, value `%s`: key is not UTF-8", k, v))
} else if !utf8.ValidString(v) {
panic(fmt.Sprintf("key `%s`, value `%s`: value is not UTF-8", k, v))
}
}
m.labelPairs = labelPairs
m.Called(labelPairs)
return m
}
func (m *mockCounter) CurryWith(labels prometheus.Labels) (c *prometheus.CounterVec, err error) {
return &prometheus.CounterVec{}, nil
}
func (m *mockCounter) GetMetricWith(labels prometheus.Labels) (c prometheus.Counter, err error) {
return m, nil
}
func (m *mockCounter) GetMetricWithLabelValues(lvs ...string) (c prometheus.Counter, err error) {
return m, nil
}
func (m *mockCounter) MustCurryWith(labels prometheus.Labels) (c *prometheus.CounterVec) {
return &prometheus.CounterVec{}
}
func (m *mockCounter) WithLabelValues(lvs ...string) (c prometheus.Counter) {
// port over testCounter functionality
if len(lvs) != len(m.labels) {
panic(fmt.Sprintf("expected %d label values but got %d", len(m.labels), len(lvs)))
}
for i, l := range m.labels {
m.labelPairs[l] = lvs[i]
}
return m
}
// mockKey is a mock for key.
type mockKey struct {
mock.Mock
clortho.Thumbprinter
}
func (key *mockKey) Public() crypto.PublicKey {
arguments := key.Called()
return arguments.Get(0)
}
func (key *mockKey) KeyType() string {
arguments := key.Called()
return arguments.String(0)
}
func (key *mockKey) KeyID() string {
arguments := key.Called()
return arguments.String(0)
}
func (key *mockKey) KeyUsage() string {
arguments := key.Called()
return arguments.String(0)
}
func (key *mockKey) Raw() interface{} {
arguments := key.Called()
return arguments.Get(0)
}
// MockResolver is a stretchr mock for Resolver. It's exposed for other package tests.
type MockResolver struct {
mock.Mock
}
func (resolver *MockResolver) Resolve(ctx context.Context, keyId string) (clortho.Key, error) {
arguments := resolver.Called(ctx, keyId)
if key, ok := arguments.Get(0).(clortho.Key); ok {
return key, arguments.Error(1)
} else {
return nil, arguments.Error(1)
}
}
func (resolver *MockResolver) AddListener(l clortho.ResolveListener) clortho.CancelListenerFunc {
arguments := resolver.Called(l)
return arguments.Get(0).(clortho.CancelListenerFunc)
}