-
Notifications
You must be signed in to change notification settings - Fork 405
/
mock.go
460 lines (384 loc) · 10 KB
/
mock.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
package datastore
import (
"context"
"encoding/base64"
"sort"
"strings"
"time"
"github.com/fnproject/fn/api/common"
"github.com/fnproject/fn/api/datastore/internal/datastoreutil"
"github.com/fnproject/fn/api/id"
"github.com/fnproject/fn/api/logs"
"github.com/fnproject/fn/api/models"
"github.com/sirupsen/logrus"
)
type mock struct {
Apps []*models.App
Fns []*models.Fn
Triggers []*models.Trigger
models.LogStore
}
func NewMock() models.Datastore {
return NewMockInit()
}
var _ models.Datastore = &mock{}
func (m *mock) GetTriggerBySource(ctx context.Context, appId string, triggerType, source string) (*models.Trigger, error) {
for _, t := range m.Triggers {
if t.AppID == appId && t.Type == triggerType && t.Source == source {
return t, nil
}
}
return nil, models.ErrTriggerNotFound
}
// args helps break tests less if we change stuff
func NewMockInit(args ...interface{}) models.Datastore {
var mocker mock
for _, a := range args {
switch x := a.(type) {
case []*models.App:
mocker.Apps = x
case []*models.Fn:
mocker.Fns = x
case []*models.Trigger:
mocker.Triggers = x
default:
panic("not accounted for data type sent to mock init. add it")
}
}
mocker.LogStore = logs.NewMock()
return datastoreutil.NewValidator(&mocker)
}
func (m *mock) GetAppID(ctx context.Context, appName string) (string, error) {
for _, a := range m.Apps {
if a.Name == appName {
return a.ID, nil
}
}
return "", models.ErrAppsNotFound
}
func (m *mock) GetAppByID(ctx context.Context, appID string) (*models.App, error) {
for _, a := range m.Apps {
if a.ID == appID {
return a.Clone(), nil
}
}
return nil, models.ErrAppsNotFound
}
type sortA []*models.App
func (s sortA) Len() int { return len(s) }
func (s sortA) Less(i, j int) bool { return strings.Compare(s[i].Name, s[j].Name) < 0 }
func (s sortA) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (m *mock) GetApps(ctx context.Context, filter *models.AppFilter) (*models.AppList, error) {
// sort them all first for cursoring (this is for testing, n is small & mock is not concurrent..)
sort.Sort(sortA(m.Apps))
var cursor string
if filter.Cursor != "" {
s, err := base64.RawURLEncoding.DecodeString(filter.Cursor)
if err != nil {
return nil, err
}
logrus.Error(s)
cursor = string(s)
}
apps := []*models.App{}
for _, a := range m.Apps {
if len(apps) == filter.PerPage {
break
}
if strings.Compare(cursor, a.Name) < 0 {
if filter.Name != "" && filter.Name != a.Name {
continue
}
apps = append(apps, a.Clone())
}
}
var nextCursor string
if len(apps) > 0 && len(apps) == filter.PerPage {
last := []byte(apps[len(apps)-1].Name)
nextCursor = base64.RawURLEncoding.EncodeToString(last)
}
return &models.AppList{
NextCursor: nextCursor,
Items: apps,
}, nil
}
func (m *mock) InsertApp(ctx context.Context, newApp *models.App) (*models.App, error) {
for _, a := range m.Apps {
if newApp.Name == a.Name {
return nil, models.ErrAppsAlreadyExists
}
}
app := newApp.Clone()
app.CreatedAt = common.DateTime(time.Now())
app.UpdatedAt = app.CreatedAt
app.ID = id.New().String()
m.Apps = append(m.Apps, app)
return app.Clone(), nil
}
func (m *mock) UpdateApp(ctx context.Context, app *models.App) (*models.App, error) {
appID := app.ID
for idx, a := range m.Apps {
if a.ID == appID {
if app.Name != "" && app.Name != a.Name {
return nil, models.ErrAppsNameImmutable
}
c := a.Clone()
c.Update(app)
err := c.Validate()
if err != nil {
return nil, err
}
m.Apps[idx] = c
return c.Clone(), nil
}
}
return nil, models.ErrAppsNotFound
}
func (m *mock) RemoveApp(ctx context.Context, appID string) error {
for i, a := range m.Apps {
if a.ID == appID {
var newFns []*models.Fn
var newTriggers []*models.Trigger
newApps := append(m.Apps[0:i], m.Apps[i+1:]...)
for _, fn := range m.Fns {
if fn.AppID != appID {
newFns = append(newFns, fn)
}
}
for _, t := range m.Triggers {
if t.AppID != appID {
newTriggers = append(newTriggers, t)
}
}
m.Apps = newApps
m.Triggers = newTriggers
m.Fns = newFns
return nil
}
}
return models.ErrAppsNotFound
}
func (m *mock) InsertFn(ctx context.Context, fn *models.Fn) (*models.Fn, error) {
_, err := m.GetAppByID(ctx, fn.AppID)
if err != nil {
return nil, err
}
for _, f := range m.Fns {
if f.ID == fn.ID ||
(f.AppID == fn.AppID &&
f.Name == fn.Name) {
return nil, models.ErrFnsExists
}
}
cl := fn.Clone()
cl.ID = id.New().String()
cl.CreatedAt = common.DateTime(time.Now())
cl.UpdatedAt = cl.CreatedAt
err = fn.Validate()
if err != nil {
return nil, err
}
m.Fns = append(m.Fns, cl)
return cl.Clone(), nil
}
func (m *mock) UpdateFn(ctx context.Context, fn *models.Fn) (*models.Fn, error) {
// update if exists
for _, f := range m.Fns {
if f.ID == fn.ID {
clone := f.Clone()
clone.Update(fn)
err := clone.Validate()
if err != nil {
return nil, err
}
*f = *clone
return f, nil
}
}
return nil, models.ErrFnsNotFound
}
type sortF []*models.Fn
func (s sortF) Len() int { return len(s) }
func (s sortF) Less(i, j int) bool { return strings.Compare(s[i].Name, s[j].Name) < 0 }
func (s sortF) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (m *mock) GetFns(ctx context.Context, filter *models.FnFilter) (*models.FnList, error) {
// sort them all first for cursoring (this is for testing, n is small & mock is not concurrent..)
sort.Sort(sortF(m.Fns))
funcs := []*models.Fn{}
var cursor string
if filter.Cursor != "" {
s, err := base64.RawURLEncoding.DecodeString(filter.Cursor)
if err != nil {
return nil, err
}
cursor = string(s)
}
for _, f := range m.Fns {
if filter.PerPage > 0 && len(funcs) == filter.PerPage {
break
}
if strings.Compare(cursor, f.Name) < 0 &&
(filter.AppID == "" || filter.AppID == f.AppID) &&
(filter.Name == "" || filter.Name == f.Name) {
funcs = append(funcs, f)
}
}
var nextCursor string
if len(funcs) > 0 && len(funcs) == filter.PerPage {
last := []byte(funcs[len(funcs)-1].Name)
nextCursor = base64.RawURLEncoding.EncodeToString(last)
}
return &models.FnList{
NextCursor: nextCursor,
Items: funcs,
}, nil
}
func (m *mock) GetFnByID(ctx context.Context, fnID string) (*models.Fn, error) {
for _, f := range m.Fns {
if f.ID == fnID {
return f, nil
}
}
return nil, models.ErrFnsNotFound
}
func (m *mock) RemoveFn(ctx context.Context, fnID string) error {
for i, f := range m.Fns {
if f.ID == fnID {
m.Fns = append(m.Fns[:i], m.Fns[i+1:]...)
var newTriggers []*models.Trigger
for _, t := range m.Triggers {
if t.FnID != f.ID {
newTriggers = append(newTriggers, t)
}
}
m.Triggers = newTriggers
return nil
}
}
return models.ErrFnsNotFound
}
func (m *mock) InsertTrigger(ctx context.Context, trigger *models.Trigger) (*models.Trigger, error) {
_, err := m.GetAppByID(ctx, trigger.AppID)
if err != nil {
return nil, err
}
fn, err := m.GetFnByID(ctx, trigger.FnID)
if err != nil {
return nil, err
}
if fn.AppID != trigger.AppID {
return nil, models.ErrTriggerFnIDNotSameApp
}
for _, t := range m.Triggers {
if t.ID == trigger.ID ||
(t.AppID == trigger.AppID &&
t.FnID == trigger.FnID &&
t.Name == trigger.Name) {
return nil, models.ErrTriggerExists
}
if t.AppID == trigger.AppID &&
t.Source == trigger.Source &&
t.Type == trigger.Type {
return nil, models.ErrTriggerSourceExists
}
}
cl := trigger.Clone()
cl.CreatedAt = common.DateTime(time.Now())
cl.UpdatedAt = cl.CreatedAt
cl.ID = id.New().String()
err = trigger.Validate()
if err != nil {
return nil, err
}
m.Triggers = append(m.Triggers, cl)
return cl.Clone(), nil
}
func (m *mock) UpdateTrigger(ctx context.Context, trigger *models.Trigger) (*models.Trigger, error) {
for _, t := range m.Triggers {
if t.ID == trigger.ID {
cl := t.Clone()
cl.Update(trigger)
err := cl.Validate()
if err != nil {
return nil, err
}
*t = *cl
return cl.Clone(), nil
}
}
return nil, models.ErrTriggerNotFound
}
func (m *mock) GetTrigger(ctx context.Context, appId, fnId, triggerName string) (*models.Trigger, error) {
for _, t := range m.Triggers {
if t.AppID == appId && t.FnID == fnId && t.Name == triggerName {
return t.Clone(), nil
}
}
return nil, models.ErrTriggerNotFound
}
func (m *mock) GetTriggerByID(ctx context.Context, triggerId string) (*models.Trigger, error) {
for _, t := range m.Triggers {
if t.ID == triggerId {
return t.Clone(), nil
}
}
return nil, models.ErrTriggerNotFound
}
type sortT []*models.Trigger
func (s sortT) Len() int { return len(s) }
func (s sortT) Less(i, j int) bool { return strings.Compare(s[i].Name, s[j].Name) < 0 }
func (s sortT) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (m *mock) GetTriggers(ctx context.Context, filter *models.TriggerFilter) (*models.TriggerList, error) {
sort.Sort(sortT(m.Triggers))
var cursor string
if filter.Cursor != "" {
s, err := base64.RawURLEncoding.DecodeString(filter.Cursor)
if err != nil {
return nil, err
}
cursor = string(s)
}
res := []*models.Trigger{}
for _, t := range m.Triggers {
if filter.PerPage > 0 && len(res) == filter.PerPage {
break
}
matched := true
if filter.Cursor != "" && t.Name <= cursor {
matched = false
}
if t.AppID != filter.AppID {
matched = false
}
if filter.FnID != "" && filter.FnID != t.FnID {
matched = false
}
if filter.Name != "" && filter.Name != t.Name {
matched = false
}
if matched {
res = append(res, t)
}
}
var nextCursor string
if len(res) > 0 && len(res) == filter.PerPage {
last := []byte(res[len(res)-1].Name)
nextCursor = base64.RawURLEncoding.EncodeToString(last)
}
return &models.TriggerList{
NextCursor: nextCursor,
Items: res,
}, nil
}
func (m *mock) RemoveTrigger(ctx context.Context, triggerID string) error {
for i, t := range m.Triggers {
if t.ID == triggerID {
m.Triggers = append(m.Triggers[:i], m.Triggers[i+1:]...)
return nil
}
}
return models.ErrTriggerNotFound
}
func (m *mock) Close() error {
return nil
}