-
Notifications
You must be signed in to change notification settings - Fork 2
/
pusher_test.go
395 lines (333 loc) · 12.4 KB
/
pusher_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
// Copyright (c) 2020 rookie-ninja
//
// Use of this source code is governed by an Apache-style
// license that can be found in the LICENSE file.
package rkprom
import (
"github.com/rookie-ninja/rk-entry/entry"
"github.com/stretchr/testify/assert"
"go.uber.org/atomic"
"io/ioutil"
"net/http"
"runtime"
"strings"
"testing"
"time"
)
var (
intervalMs = 200 * time.Millisecond
remoteAddr = "localhost:1608"
jobName = "rk-prom-job"
basicAuth = "user:pass"
zapLoggerEntry = rkentry.NoopZapLoggerEntry()
eventLoggerEntry = rkentry.NoopEventLoggerEntry()
)
type HTTPDoerMock struct {
called *atomic.Bool
}
func (mock *HTTPDoerMock) Do(*http.Request) (*http.Response, error) {
mock.called.CAS(false, true)
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(strings.NewReader("mock response")),
}, nil
}
func TestNewPushGatewayPusher_WithNegativeIntervalMS(t *testing.T) {
negative := -1 * time.Microsecond
pusher, err := NewPushGatewayPusher(
WithIntervalMSPusher(negative),
WithRemoteAddressPusher(remoteAddr),
WithJobNamePusher(jobName),
WithBasicAuthPusher(basicAuth),
WithZapLoggerEntryPusher(zapLoggerEntry),
WithEventLoggerEntryPusher(eventLoggerEntry))
assert.Nil(t, pusher, "pusher should be nil")
assert.NotNil(t, err, "error should not be nil")
}
func TestNewPushGatewayPusher_WithEmptyURL(t *testing.T) {
pusher, err := NewPushGatewayPusher(
WithIntervalMSPusher(intervalMs),
WithRemoteAddressPusher(""),
WithJobNamePusher(jobName),
WithBasicAuthPusher(basicAuth),
WithZapLoggerEntryPusher(zapLoggerEntry),
WithEventLoggerEntryPusher(eventLoggerEntry))
assert.Nil(t, pusher, "pusher should be nil")
assert.NotNil(t, err, "error should not be nil")
}
func TestNewPushGatewayPusher_WithEmptyJobName(t *testing.T) {
pusher, err := NewPushGatewayPusher(
WithIntervalMSPusher(intervalMs),
WithRemoteAddressPusher(remoteAddr),
WithJobNamePusher(""),
WithBasicAuthPusher(basicAuth),
WithZapLoggerEntryPusher(zapLoggerEntry),
WithEventLoggerEntryPusher(eventLoggerEntry))
assert.Nil(t, pusher, "pusher should be nil")
assert.NotNil(t, err, "error should not be nil")
}
func TestNewPushGatewayPusher_WithNilLZapLoggerEntry(t *testing.T) {
pusher, err := NewPushGatewayPusher(
WithIntervalMSPusher(intervalMs),
WithRemoteAddressPusher(remoteAddr),
WithJobNamePusher(jobName),
WithBasicAuthPusher(basicAuth),
WithZapLoggerEntryPusher(nil),
WithEventLoggerEntryPusher(eventLoggerEntry))
assert.NotNil(t, pusher, "pusher should not be nil")
assert.Nil(t, err, "error should be nil")
}
func TestNewPushGatewayPusher_WithNilLEventLoggerEntry(t *testing.T) {
pusher, err := NewPushGatewayPusher(
WithIntervalMSPusher(intervalMs),
WithRemoteAddressPusher(remoteAddr),
WithJobNamePusher(jobName),
WithBasicAuthPusher(basicAuth),
WithZapLoggerEntryPusher(zapLoggerEntry),
WithEventLoggerEntryPusher(nil))
assert.NotNil(t, pusher, "pusher should not be nil")
assert.Nil(t, err, "error should be nil")
}
func TestNewPushGatewayPusher_WithEmptyBasicAuth(t *testing.T) {
pusher, err := NewPushGatewayPusher(
WithIntervalMSPusher(intervalMs),
WithRemoteAddressPusher(remoteAddr),
WithJobNamePusher(jobName),
WithBasicAuthPusher(""),
WithZapLoggerEntryPusher(zapLoggerEntry),
WithEventLoggerEntryPusher(eventLoggerEntry))
assert.NotNil(t, pusher, "pusher should not be nil")
assert.Nil(t, err, "error should be nil")
}
func TestNewPushGatewayPusher_WithInvalidBasicAuth(t *testing.T) {
pusher, err := NewPushGatewayPusher(
WithIntervalMSPusher(intervalMs),
WithRemoteAddressPusher(remoteAddr),
WithJobNamePusher(jobName),
WithBasicAuthPusher("user:pass:pass"),
WithZapLoggerEntryPusher(zapLoggerEntry),
WithEventLoggerEntryPusher(eventLoggerEntry))
assert.NotNil(t, pusher, "pusher should not be nil")
assert.Nil(t, err, "error should be nil")
}
func TestNewPushGatewayPusher_WithCert(t *testing.T) {
serverCert := `
-----BEGIN CERTIFICATE-----
MIIC/jCCAeagAwIBAgIUWVMP53O835+njsr23UZIX2KEXGYwDQYJKoZIhvcNAQEL
BQAwYDELMAkGA1UEBhMCQ04xEDAOBgNVBAgTB0JlaWppbmcxCzAJBgNVBAcTAkJK
MQswCQYDVQQKEwJSSzEQMA4GA1UECxMHUksgRGVtbzETMBEGA1UEAxM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C PRIVATE KEY-----
`
certStore := &rkentry.CertStore{
ServerCert: []byte(serverCert),
ServerKey: []byte(serverKey),
}
pusher, err := NewPushGatewayPusher(
WithCertStorePusher(certStore),
WithIntervalMSPusher(intervalMs),
WithRemoteAddressPusher(remoteAddr),
WithJobNamePusher(jobName),
WithBasicAuthPusher(basicAuth),
WithZapLoggerEntryPusher(zapLoggerEntry),
WithEventLoggerEntryPusher(eventLoggerEntry))
assert.NotNil(t, pusher, "pusher should not be nil")
assert.Nil(t, err, "error should be nil")
assert.Equal(t, intervalMs, pusher.IntervalMs)
assert.NotNil(t, pusher.Pusher, "pusher should not be nil")
assert.Equal(t, jobName, pusher.JobName)
assert.Equal(t, basicAuth, pusher.Credential)
assert.NotNil(t, pusher.lock, "lock should not be nil")
assert.False(t, pusher.Running.Load(), "isRunning should be false")
assert.Contains(t, pusher.RemoteAddress, "https")
}
func TestNewPushGatewayPusher_WithInvalidCert(t *testing.T) {
serverCert := `Invalid`
serverKey := `Invalid`
certStore := &rkentry.CertStore{
ServerCert: []byte(serverCert),
ServerKey: []byte(serverKey),
}
pusher, err := NewPushGatewayPusher(
WithCertStorePusher(certStore),
WithIntervalMSPusher(intervalMs),
WithRemoteAddressPusher(remoteAddr),
WithJobNamePusher(jobName),
WithBasicAuthPusher(basicAuth),
WithZapLoggerEntryPusher(zapLoggerEntry),
WithEventLoggerEntryPusher(eventLoggerEntry))
assert.NotNil(t, pusher, "pusher should not be nil")
assert.Nil(t, err, "error should be nil")
assert.Equal(t, intervalMs, pusher.IntervalMs)
assert.NotNil(t, pusher.Pusher, "pusher should not be nil")
assert.Equal(t, jobName, pusher.JobName)
assert.Equal(t, basicAuth, pusher.Credential)
assert.NotNil(t, pusher.lock, "lock should not be nil")
assert.False(t, pusher.Running.Load(), "isRunning should be false")
assert.Contains(t, pusher.RemoteAddress, "http")
}
func TestNewPushGatewayPusher_HappyCase(t *testing.T) {
pusher, err := NewPushGatewayPusher(
WithIntervalMSPusher(intervalMs),
WithRemoteAddressPusher(remoteAddr),
WithJobNamePusher(jobName),
WithBasicAuthPusher(basicAuth),
WithZapLoggerEntryPusher(zapLoggerEntry),
WithEventLoggerEntryPusher(eventLoggerEntry))
assert.NotNil(t, pusher, "pusher should not be nil")
assert.Nil(t, err, "error should be nil")
assert.Equal(t, intervalMs, pusher.IntervalMs)
assert.NotNil(t, pusher.Pusher, "pusher should not be nil")
assert.Equal(t, jobName, pusher.JobName)
assert.Equal(t, basicAuth, pusher.Credential)
assert.NotNil(t, pusher.lock, "lock should not be nil")
assert.False(t, pusher.Running.Load(), "isRunning should be false")
}
func TestPushGatewayPusher_Start_WithDuplicateStartCalls(t *testing.T) {
pusher, err := NewPushGatewayPusher(
WithIntervalMSPusher(intervalMs),
WithRemoteAddressPusher(remoteAddr),
WithJobNamePusher(jobName),
WithBasicAuthPusher(basicAuth),
WithZapLoggerEntryPusher(zapLoggerEntry),
WithEventLoggerEntryPusher(eventLoggerEntry))
assert.NotNil(t, pusher, "pusher should not be nil")
assert.Nil(t, err, "error should be nil")
// we expect a new go routines generated
prev := runtime.NumGoroutine()
// start at first time
pusher.Start()
assert.True(t, pusher.IsRunning())
assert.Equal(t, prev+1, runtime.NumGoroutine())
// call Start() again
pusher.Start()
// number of goroutines should be the same
assert.Equal(t, prev+1, runtime.NumGoroutine())
// call Stop()
pusher.Stop()
}
func TestPushGatewayPusher_Start_HappyCase(t *testing.T) {
pusher, err := NewPushGatewayPusher(
WithIntervalMSPusher(intervalMs),
WithRemoteAddressPusher(remoteAddr),
WithJobNamePusher(jobName),
WithBasicAuthPusher(basicAuth),
WithZapLoggerEntryPusher(zapLoggerEntry),
WithEventLoggerEntryPusher(eventLoggerEntry))
// mock a pusher for tracing
pusher.Pusher = pusher.Pusher.Client(&HTTPDoerMock{
called: atomic.NewBool(false),
})
assert.NotNil(t, pusher, "pusher should not be nil")
assert.Nil(t, err, "error should be nil")
// start at first time
pusher.Start()
assert.True(t, pusher.IsRunning())
// wait for one second in order to check whether new goroutine was generated
time.Sleep(time.Second)
// call Stop()
pusher.Stop()
}
func TestPushGatewayPusher_push(t *testing.T) {
pusher, err := NewPushGatewayPusher(
WithIntervalMSPusher(intervalMs),
WithRemoteAddressPusher(remoteAddr),
WithJobNamePusher(jobName),
WithBasicAuthPusher(basicAuth),
WithZapLoggerEntryPusher(zapLoggerEntry),
WithEventLoggerEntryPusher(eventLoggerEntry))
// mock a pusher for tracing
///doer := &HTTPDoerMock{}
doer := atomic.Value{}
doer.Store(&HTTPDoerMock{called: atomic.NewBool(false)})
pusher.Pusher = pusher.Pusher.Client(doer.Load().(*HTTPDoerMock))
assert.NotNil(t, pusher, "pusher should not be nil")
assert.Nil(t, err, "error should be nil")
// make state of pusher as running first
pusher.Running.CAS(false, true)
// run with extra go routine since push() method was an infinite loop
go pusher.push()
// sleep one seconds to make sure http request was called at least once
time.Sleep(1 * time.Second)
assert.True(t, doer.Load().(*HTTPDoerMock).called.Load(), "supposed to be called at least once")
}
func TestPushGatewayPusher_IsRunning_ExpectFalse(t *testing.T) {
pusher, err := NewPushGatewayPusher(
WithIntervalMSPusher(intervalMs),
WithRemoteAddressPusher(remoteAddr),
WithJobNamePusher(jobName),
WithBasicAuthPusher(basicAuth),
WithZapLoggerEntryPusher(zapLoggerEntry),
WithEventLoggerEntryPusher(eventLoggerEntry))
assert.NotNil(t, pusher, "pusher should not be nil")
assert.Nil(t, err, "error should be nil")
assert.False(t, pusher.IsRunning())
// start periodic job
pusher.Start()
assert.True(t, pusher.IsRunning())
defer pusher.Stop()
}
func TestPushGatewayPusher_IsRunning_ExpectTrue(t *testing.T) {
pusher, err := NewPushGatewayPusher(
WithIntervalMSPusher(intervalMs),
WithRemoteAddressPusher(remoteAddr),
WithJobNamePusher(jobName),
WithBasicAuthPusher(basicAuth),
WithZapLoggerEntryPusher(zapLoggerEntry),
WithEventLoggerEntryPusher(eventLoggerEntry))
assert.NotNil(t, pusher, "pusher should not be nil")
assert.Nil(t, err, "error should be nil")
assert.False(t, pusher.IsRunning())
// start periodic job
pusher.Start()
assert.True(t, pusher.IsRunning())
defer pusher.Stop()
}
func TestPushGatewayPusher_Stop(t *testing.T) {
pusher, err := NewPushGatewayPusher(
WithIntervalMSPusher(intervalMs),
WithRemoteAddressPusher(remoteAddr),
WithJobNamePusher(jobName),
WithBasicAuthPusher(basicAuth),
WithZapLoggerEntryPusher(zapLoggerEntry),
WithEventLoggerEntryPusher(eventLoggerEntry))
assert.NotNil(t, pusher, "pusher should not be nil")
assert.Nil(t, err, "error should be nil")
// start periodic job
pusher.Start()
assert.True(t, pusher.IsRunning())
pusher.Stop()
assert.False(t, pusher.IsRunning())
}
func TestPushGatewayPusher_GetPusher(t *testing.T) {
pusher, err := NewPushGatewayPusher(
WithIntervalMSPusher(intervalMs),
WithRemoteAddressPusher(remoteAddr),
WithJobNamePusher(jobName),
WithBasicAuthPusher(basicAuth),
WithZapLoggerEntryPusher(zapLoggerEntry),
WithEventLoggerEntryPusher(eventLoggerEntry))
assert.NotNil(t, pusher, "pusher should not be nil")
assert.Nil(t, err, "error should be nil")
assert.NotNil(t, pusher.GetPusher())
}