forked from iron-io/functions
/
mock.go
139 lines (118 loc) · 3.42 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
package datastore
import (
"context"
"github.com/iron-io/functions/api/datastore/internal/datastoreutil"
"github.com/iron-io/functions/api/models"
)
type mock struct {
Apps []*models.App
Routes []*models.Route
data map[string][]byte
}
func NewMock() models.Datastore {
return NewMockInit(nil, nil)
}
func NewMockInit(apps []*models.App, routes []*models.Route) models.Datastore {
if apps == nil {
apps = []*models.App{}
}
if routes == nil {
routes = []*models.Route{}
}
return datastoreutil.NewValidator(&mock{apps, routes, make(map[string][]byte)})
}
func (m *mock) GetApp(ctx context.Context, appName string) (app *models.App, err error) {
for _, a := range m.Apps {
if a.Name == appName {
return a, nil
}
}
return nil, models.ErrAppsNotFound
}
func (m *mock) GetApps(ctx context.Context, appFilter *models.AppFilter) ([]*models.App, error) {
return m.Apps, nil
}
func (m *mock) InsertApp(ctx context.Context, app *models.App) (*models.App, error) {
if a, _ := m.GetApp(ctx, app.Name); a != nil {
return nil, models.ErrAppsAlreadyExists
}
m.Apps = append(m.Apps, app)
return app, nil
}
func (m *mock) UpdateApp(ctx context.Context, app *models.App) (*models.App, error) {
a, err := m.GetApp(ctx, app.Name)
if err != nil {
return nil, err
}
a.UpdateConfig(app.Config)
return a.Clone(), nil
}
func (m *mock) RemoveApp(ctx context.Context, appName string) error {
for i, a := range m.Apps {
if a.Name == appName {
m.Apps = append(m.Apps[:i], m.Apps[i+1:]...)
return nil
}
}
return models.ErrAppsNotFound
}
func (m *mock) GetRoute(ctx context.Context, appName, routePath string) (*models.Route, error) {
for _, r := range m.Routes {
if r.AppName == appName && r.Path == routePath {
return r, nil
}
}
return nil, models.ErrRoutesNotFound
}
func (m *mock) GetRoutes(ctx context.Context, routeFilter *models.RouteFilter) (routes []*models.Route, err error) {
for _, r := range m.Routes {
routes = append(routes, r)
}
return
}
func (m *mock) GetRoutesByApp(ctx context.Context, appName string, routeFilter *models.RouteFilter) (routes []*models.Route, err error) {
for _, r := range m.Routes {
if r.AppName == appName && (routeFilter.Path == "" || r.Path == routeFilter.Path) && (routeFilter.AppName == "" || r.AppName == routeFilter.AppName) {
routes = append(routes, r)
}
}
return
}
func (m *mock) InsertRoute(ctx context.Context, route *models.Route) (*models.Route, error) {
if _, err := m.GetApp(ctx, route.AppName); err != nil {
return nil, err
}
if r, _ := m.GetRoute(ctx, route.AppName, route.Path); r != nil {
return nil, models.ErrRoutesAlreadyExists
}
m.Routes = append(m.Routes, route)
return route, nil
}
func (m *mock) UpdateRoute(ctx context.Context, route *models.Route) (*models.Route, error) {
r, err := m.GetRoute(ctx, route.AppName, route.Path)
if err != nil {
return nil, err
}
r.Update(route)
return r.Clone(), nil
}
func (m *mock) RemoveRoute(ctx context.Context, appName, routePath string) error {
for i, r := range m.Routes {
if r.AppName == appName && r.Path == routePath {
m.Routes = append(m.Routes[:i], m.Routes[i+1:]...)
return nil
}
}
return models.ErrRoutesNotFound
}
func (m *mock) Put(ctx context.Context, key, value []byte) error {
if len(value) == 0 {
delete(m.data, string(key))
} else {
m.data[string(key)] = value
}
return nil
}
func (m *mock) Get(ctx context.Context, key []byte) ([]byte, error) {
return m.data[string(key)], nil
}