-
Notifications
You must be signed in to change notification settings - Fork 405
/
mock.go
115 lines (95 loc) · 2.65 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
package logs
import (
"bytes"
"context"
"encoding/base64"
"io"
"io/ioutil"
"sort"
"strings"
"time"
"github.com/fnproject/fn/api/models"
)
type mock struct {
Logs map[string][]byte
Calls []*models.Call
}
func NewMock(args ...interface{}) models.LogStore {
var mocker mock
for _, a := range args {
switch x := a.(type) {
case []*models.Call:
mocker.Calls = x
default:
panic("unknown type handed to mocker. add me")
}
}
mocker.Logs = make(map[string][]byte)
return &mocker
}
func (m *mock) InsertLog(ctx context.Context, call *models.Call, callLog io.Reader) error {
bytes, err := ioutil.ReadAll(callLog)
m.Logs[call.ID] = bytes
return err
}
func (m *mock) GetLog(ctx context.Context, fnID, callID string) (io.Reader, error) {
logEntry, ok := m.Logs[callID]
if !ok {
return nil, models.ErrCallLogNotFound
}
return bytes.NewReader(logEntry), nil
}
func (m *mock) InsertCall(ctx context.Context, call *models.Call) error {
m.Calls = append(m.Calls, call)
return nil
}
func (m *mock) GetCall(ctx context.Context, fnID, callID string) (*models.Call, error) {
for _, t := range m.Calls {
if t.ID == callID &&
t.FnID == fnID {
return t, nil
}
}
return nil, models.ErrCallNotFound
}
type sortC []*models.Call
func (s sortC) Len() int { return len(s) }
func (s sortC) Less(i, j int) bool { return strings.Compare(s[i].ID, s[j].ID) < 0 }
func (s sortC) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (m *mock) GetCalls(ctx context.Context, filter *models.CallFilter) (*models.CallList, error) {
// sort them all first for cursoring (this is for testing, n is small & mock is not concurrent..)
// calls are in DESC order so use sort.Reverse
sort.Sort(sort.Reverse(sortC(m.Calls)))
var calls []*models.Call
var cursor = ""
if filter.Cursor != "" {
s, err := base64.RawURLEncoding.DecodeString(filter.Cursor)
if err != nil {
return nil, err
}
cursor = string(s)
}
for _, c := range m.Calls {
if filter.PerPage > 0 && len(calls) == filter.PerPage {
break
}
if (cursor == "" || strings.Compare(cursor, c.ID) > 0) &&
(filter.FnID == "" || c.FnID == filter.FnID) &&
(time.Time(filter.FromTime).IsZero() || time.Time(filter.FromTime).Before(time.Time(c.CreatedAt))) &&
(time.Time(filter.ToTime).IsZero() || time.Time(c.CreatedAt).Before(time.Time(filter.ToTime))) {
calls = append(calls, c)
}
}
var nextCursor string
if len(calls) > 0 && len(calls) == filter.PerPage {
last := []byte(calls[len(calls)-1].ID)
nextCursor = base64.RawURLEncoding.EncodeToString(last)
}
return &models.CallList{
NextCursor: nextCursor,
Items: calls,
}, nil
}
func (m *mock) Close() error {
return nil
}