forked from smartcontractkit/chainlink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mocks.go
484 lines (406 loc) · 12.1 KB
/
mocks.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
package cltest
import (
"context"
"fmt"
"io"
"io/ioutil"
"math/big"
"net/http"
"net/http/httptest"
"runtime/debug"
"sync"
"testing"
"time"
"github.com/DCMMC/chainlink/core/chains/evm"
evmconfig "github.com/DCMMC/chainlink/core/chains/evm/config"
evmmocks "github.com/DCMMC/chainlink/core/chains/evm/mocks"
"github.com/DCMMC/chainlink/core/cmd"
"github.com/DCMMC/chainlink/core/gracefulpanic"
"github.com/DCMMC/chainlink/core/logger"
"github.com/DCMMC/chainlink/core/services/chainlink"
"github.com/DCMMC/chainlink/core/services/eth"
"github.com/DCMMC/chainlink/core/sessions"
"github.com/DCMMC/chainlink/core/store/config"
"github.com/DCMMC/chainlink/core/web"
"go.uber.org/atomic"
gethTypes "github.com/ethereum/go-ethereum/core/types"
"github.com/robfig/cron/v3"
"github.com/stretchr/testify/assert"
)
// MockSubscription a mock subscription
type MockSubscription struct {
mut sync.Mutex
channel interface{}
unsubscribed bool
Errors chan error
}
// EmptyMockSubscription return empty MockSubscription
func EmptyMockSubscription() *MockSubscription {
return &MockSubscription{Errors: make(chan error, 1), channel: make(chan struct{})}
}
// Err returns error channel from mes
func (mes *MockSubscription) Err() <-chan error { return mes.Errors }
// Unsubscribe closes the subscription
func (mes *MockSubscription) Unsubscribe() {
mes.mut.Lock()
defer mes.mut.Unlock()
if mes.unsubscribed {
return
}
mes.unsubscribed = true
switch mes.channel.(type) {
case chan struct{}:
close(mes.channel.(chan struct{}))
case chan gethTypes.Log:
close(mes.channel.(chan gethTypes.Log))
case chan *eth.Head:
close(mes.channel.(chan *eth.Head))
default:
logger.Fatal(fmt.Sprintf("Unable to close MockSubscription channel of type %T", mes.channel))
}
close(mes.Errors)
}
// InstantClock an InstantClock
type InstantClock struct{}
// Now returns the current local time
func (InstantClock) Now() time.Time {
return time.Now()
}
// After return channel of time
func (InstantClock) After(_ time.Duration) <-chan time.Time {
c := make(chan time.Time, 100)
c <- time.Now()
return c
}
// TriggerClock implements the AfterNower interface, but must be manually triggered
// to resume computation on After.
type TriggerClock struct {
triggers chan time.Time
t testing.TB
}
// NewTriggerClock returns a new TriggerClock, that a test can manually fire
// to continue processing in a Clock dependency.
func NewTriggerClock(t testing.TB) *TriggerClock {
return &TriggerClock{
triggers: make(chan time.Time),
t: t,
}
}
// Trigger sends a time to unblock the After call.
func (t *TriggerClock) Trigger() {
select {
case t.triggers <- time.Now():
case <-time.After(60 * time.Second):
t.t.Error("timed out while trying to trigger clock")
}
}
// TriggerWithoutTimeout is a special case where we know the trigger might
// block but don't care
func (t *TriggerClock) TriggerWithoutTimeout() {
t.triggers <- time.Now()
}
// Now returns the current local time
func (t TriggerClock) Now() time.Time {
return time.Now()
}
// After waits on a manual trigger.
func (t *TriggerClock) After(_ time.Duration) <-chan time.Time {
return t.triggers
}
// RendererMock a mock renderer
type RendererMock struct {
Renders []interface{}
}
// Render appends values to renderer mock
func (rm *RendererMock) Render(v interface{}, headers ...string) error {
rm.Renders = append(rm.Renders, v)
return nil
}
// InstanceAppFactory is an InstanceAppFactory
type InstanceAppFactory struct {
App chainlink.Application
}
// NewApplication creates a new application with specified config
func (f InstanceAppFactory) NewApplication(config config.GeneralConfig) (chainlink.Application, error) {
return f.App, nil
}
type seededAppFactory struct {
Application chainlink.Application
}
func (s seededAppFactory) NewApplication(config config.GeneralConfig) (chainlink.Application, error) {
return noopStopApplication{s.Application}, nil
}
type noopStopApplication struct {
chainlink.Application
}
// FIXME: Why bother with this wrapper?
func (a noopStopApplication) Stop() error {
return nil
}
// BlockedRunner is a Runner that blocks until its channel is posted to
type BlockedRunner struct {
Done chan struct{}
}
// Run runs the blocked runner, doesn't return until the channel is signalled
func (r BlockedRunner) Run(app chainlink.Application) error {
<-r.Done
return nil
}
// EmptyRunner is an EmptyRunner
type EmptyRunner struct{}
// Run runs the empty runner
func (r EmptyRunner) Run(app chainlink.Application) error {
return nil
}
// MockCountingPrompter is a mock counting prompt
type MockCountingPrompter struct {
T *testing.T
EnteredStrings []string
Count int
NotTerminal bool
}
// Prompt returns an entered string
func (p *MockCountingPrompter) Prompt(string) string {
i := p.Count
p.Count++
if len(p.EnteredStrings)-1 < i {
p.T.Errorf("Not enough passwords supplied to MockCountingPrompter, wanted %d", i)
p.T.FailNow()
}
return p.EnteredStrings[i]
}
// PasswordPrompt returns an entered string
func (p *MockCountingPrompter) PasswordPrompt(string) string {
i := p.Count
p.Count++
if len(p.EnteredStrings)-1 < i {
p.T.Errorf("Not enough passwords supplied to MockCountingPrompter, wanted %d", i)
p.T.FailNow()
}
return p.EnteredStrings[i]
}
// IsTerminal always returns true in tests
func (p *MockCountingPrompter) IsTerminal() bool {
return !p.NotTerminal
}
// NewHTTPMockServer create http test server with passed in parameters
func NewHTTPMockServer(
t *testing.T,
status int,
wantMethod string,
response string,
callback ...func(http.Header, string),
) *httptest.Server {
called := false
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
assert.NoError(t, err)
assert.Equal(t, wantMethod, r.Method)
if len(callback) > 0 {
callback[0](r.Header, string(b))
}
called = true
w.WriteHeader(status)
_, _ = io.WriteString(w, response) // Assignment for errcheck. Only used in tests so we can ignore.
})
server := httptest.NewServer(handler)
t.Cleanup(func() {
server.Close()
assert.True(t, called, "expected call Mock HTTP endpoint '%s'", server.URL)
})
return server
}
// NewHTTPMockServerWithRequest creates http test server that makes the request
// available in the callback
func NewHTTPMockServerWithRequest(
t *testing.T,
status int,
response string,
callback func(r *http.Request),
) *httptest.Server {
called := false
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
callback(r)
called = true
w.WriteHeader(status)
_, _ = io.WriteString(w, response) // Assignment for errcheck. Only used in tests so we can ignore.
})
server := httptest.NewServer(handler)
t.Cleanup(func() {
server.Close()
assert.True(t, called, "expected call Mock HTTP endpoint '%s'", server.URL)
})
return server
}
func NewHTTPMockServerWithAlterableResponse(
t *testing.T, response func() string) (server *httptest.Server) {
server = httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = io.WriteString(w, response())
}))
return server
}
func NewHTTPMockServerWithAlterableResponseAndRequest(t *testing.T, response func() string, callback func(r *http.Request)) (server *httptest.Server) {
server = httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
callback(r)
w.WriteHeader(http.StatusOK)
_, _ = io.WriteString(w, response())
}))
return server
}
// MockCron represents a mock cron
type MockCron struct {
Entries []MockCronEntry
nextID cron.EntryID
}
// NewMockCron returns a new mock cron
func NewMockCron() *MockCron {
return &MockCron{}
}
// Start starts the mockcron
func (*MockCron) Start() {}
// Stop stops the mockcron
func (*MockCron) Stop() context.Context {
ctx, cancel := context.WithCancel(context.Background())
cancel()
return ctx
}
// AddFunc appends a schedule to mockcron entries
func (mc *MockCron) AddFunc(schd string, fn func()) (cron.EntryID, error) {
mc.Entries = append(mc.Entries, MockCronEntry{
Schedule: schd,
Function: fn,
})
mc.nextID++
return mc.nextID, nil
}
// RunEntries run every function for each mockcron entry
func (mc *MockCron) RunEntries() {
for _, entry := range mc.Entries {
entry.Function()
}
}
// MockCronEntry a cron schedule and function
type MockCronEntry struct {
Schedule string
Function func()
}
// MockHeadTrackable allows you to mock HeadTrackable
type MockHeadTrackable struct {
onNewHeadCount atomic.Int32
}
// OnNewLongestChain increases the OnNewLongestChainCount count by one
func (m *MockHeadTrackable) OnNewLongestChain(context.Context, eth.Head) {
m.onNewHeadCount.Inc()
}
// OnNewLongestChainCount returns the count of new heads, safely.
func (m *MockHeadTrackable) OnNewLongestChainCount() int32 {
return m.onNewHeadCount.Load()
}
// NeverSleeper is a struct that never sleeps
type NeverSleeper struct{}
// Reset resets the never sleeper
func (ns NeverSleeper) Reset() {}
// Sleep puts the never sleeper to sleep
func (ns NeverSleeper) Sleep() {}
// After returns a duration
func (ns NeverSleeper) After() time.Duration { return 0 * time.Microsecond }
// Duration returns a duration
func (ns NeverSleeper) Duration() time.Duration { return 0 * time.Microsecond }
func MustRandomUser(t testing.TB) sessions.User {
email := fmt.Sprintf("user-%v@chainlink.test", NewRandomInt64())
r, err := sessions.NewUser(email, Password)
if err != nil {
logger.TestLogger(t).Panic(err)
}
return r
}
func MustNewUser(t *testing.T, email, password string) sessions.User {
r, err := sessions.NewUser(email, password)
if err != nil {
t.Fatal(err)
}
return r
}
type MockAPIInitializer struct {
t testing.TB
Count int
}
func NewMockAPIInitializer(t testing.TB) *MockAPIInitializer {
return &MockAPIInitializer{t: t}
}
func (m *MockAPIInitializer) Initialize(orm sessions.ORM) (sessions.User, error) {
if user, err := orm.FindUser(); err == nil {
return user, err
}
m.Count++
user := MustRandomUser(m.t)
return user, orm.CreateUser(&user)
}
func NewMockAuthenticatedHTTPClient(cfg cmd.HTTPClientConfig, sessionID string) cmd.HTTPClient {
return cmd.NewAuthenticatedHTTPClient(cfg, MockCookieAuthenticator{SessionID: sessionID}, sessions.SessionRequest{})
}
type MockCookieAuthenticator struct {
t testing.TB
SessionID string
Error error
}
func (m MockCookieAuthenticator) Cookie() (*http.Cookie, error) {
return MustGenerateSessionCookie(m.t, m.SessionID), m.Error
}
func (m MockCookieAuthenticator) Authenticate(sessions.SessionRequest) (*http.Cookie, error) {
return MustGenerateSessionCookie(m.t, m.SessionID), m.Error
}
type MockSessionRequestBuilder struct {
Count int
Error error
}
func (m *MockSessionRequestBuilder) Build(string) (sessions.SessionRequest, error) {
m.Count++
if m.Error != nil {
return sessions.SessionRequest{}, m.Error
}
return sessions.SessionRequest{Email: APIEmail, Password: Password}, nil
}
type MockSecretGenerator struct{}
func (m MockSecretGenerator) Generate(string) ([]byte, error) {
return []byte(SessionSecret), nil
}
type MockChangePasswordPrompter struct {
web.UpdatePasswordRequest
err error
}
func (m MockChangePasswordPrompter) Prompt() (web.UpdatePasswordRequest, error) {
return m.UpdatePasswordRequest, m.err
}
type MockPasswordPrompter struct {
Password string
}
func (m MockPasswordPrompter) Prompt() string {
return m.Password
}
var _ gracefulpanic.Signal = &testShutdownSignal{}
type testShutdownSignal struct {
t testing.TB
}
func (tss *testShutdownSignal) Panic() {
tss.t.Errorf("panic: %s", debug.Stack())
panic("panic")
}
func (tss *testShutdownSignal) Wait() <-chan struct{} {
return make(chan struct{})
}
func NewChainSetMockWithOneChain(t testing.TB, ethClient eth.Client, cfg evmconfig.ChainScopedConfig) evm.ChainSet {
cc := new(evmmocks.ChainSet)
ch := new(evmmocks.Chain)
ch.On("Client").Return(ethClient)
ch.On("Config").Return(cfg)
ch.On("Logger").Return(logger.TestLogger(t))
ch.On("ID").Return(cfg.ChainID())
cc.On("Default").Return(ch, nil)
cc.On("Get", (*big.Int)(nil)).Return(ch, nil)
cc.On("Chains").Return([]evm.Chain{ch})
return cc
}