Skip to content

Commit 7aa1981

Browse files
authored
Datastore refactor and added postgres tests (#259)
* fix apps & routes creation/update * refactor datastore and added postgres tests * added test-datastore and fixed circleci test
1 parent ff8c553 commit 7aa1981

19 files changed

+775
-109
lines changed

Makefile

+5-2
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@ build-docker:
1414
docker build -t iron/functions:latest .
1515

1616
test:
17-
go test -v $(shell glide nv | grep -v examples | grep -v tool | grep -v fnctl)
17+
go test -v $(shell go list ./... | grep -v vendor | grep -v examples | grep -v tool | grep -v fnctl)
1818
cd fnctl && $(MAKE) test
1919

20+
test-datastore:
21+
cd api/datastore && go test -v
22+
2023
test-docker:
2124
docker run -ti --privileged --rm -e LOG_LEVEL=debug \
2225
-v /var/run/docker.sock:/var/run/docker.sock \
2326
-v $(DIR):/go/src/github.com/iron-io/functions \
2427
-w /go/src/github.com/iron-io/functions iron/go:dev go test \
25-
-v $(shell glide nv | grep -v examples | grep -v tool | grep -v fnctl)
28+
-v $(shell go list ./... | grep -v vendor | grep -v examples | grep -v tool | grep -v fnctl | grep -v datastore)
2629

2730
run:
2831
./functions

api/datastore/bolt/bolt.go

+164-4
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,31 @@ func New(url *url.URL) (models.Datastore, error) {
7272
return ds, nil
7373
}
7474

75-
func (ds *BoltDatastore) StoreApp(app *models.App) (*models.App, error) {
75+
func (ds *BoltDatastore) InsertApp(app *models.App) (*models.App, error) {
7676
if app == nil {
7777
return nil, models.ErrDatastoreEmptyApp
7878
}
7979

80+
if app.Name == "" {
81+
return nil, models.ErrDatastoreEmptyAppName
82+
}
83+
84+
appname := []byte(app.Name)
85+
8086
err := ds.db.Update(func(tx *bolt.Tx) error {
8187
bIm := tx.Bucket(ds.appsBucket)
88+
89+
v := bIm.Get(appname)
90+
if v != nil {
91+
return models.ErrAppsAlreadyExists
92+
}
93+
8294
buf, err := json.Marshal(app)
8395
if err != nil {
8496
return err
8597
}
86-
err = bIm.Put([]byte(app.Name), buf)
98+
99+
err = bIm.Put(appname, buf)
87100
if err != nil {
88101
return err
89102
}
@@ -94,6 +107,62 @@ func (ds *BoltDatastore) StoreApp(app *models.App) (*models.App, error) {
94107
}
95108
return nil
96109
})
110+
111+
return app, err
112+
}
113+
114+
func (ds *BoltDatastore) UpdateApp(newapp *models.App) (*models.App, error) {
115+
if newapp == nil {
116+
return nil, models.ErrDatastoreEmptyApp
117+
}
118+
119+
if newapp.Name == "" {
120+
return nil, models.ErrDatastoreEmptyAppName
121+
}
122+
123+
var app *models.App
124+
appname := []byte(newapp.Name)
125+
126+
err := ds.db.Update(func(tx *bolt.Tx) error {
127+
bIm := tx.Bucket(ds.appsBucket)
128+
129+
v := bIm.Get(appname)
130+
if v == nil {
131+
return models.ErrAppsNotFound
132+
}
133+
134+
err := json.Unmarshal(v, &app)
135+
if err != nil {
136+
return err
137+
}
138+
139+
// Update app fields
140+
if newapp.Config != nil {
141+
if app.Config == nil {
142+
app.Config = map[string]string{}
143+
}
144+
for k, v := range newapp.Config {
145+
app.Config[k] = v
146+
}
147+
}
148+
149+
buf, err := json.Marshal(app)
150+
if err != nil {
151+
return err
152+
}
153+
154+
err = bIm.Put(appname, buf)
155+
if err != nil {
156+
return err
157+
}
158+
bjParent := tx.Bucket(ds.routesBucket)
159+
_, err = bjParent.CreateBucketIfNotExists([]byte(app.Name))
160+
if err != nil {
161+
return err
162+
}
163+
return nil
164+
})
165+
97166
return app, err
98167
}
99168

@@ -181,23 +250,114 @@ func (ds *BoltDatastore) getRouteBucketForApp(tx *bolt.Tx, appName string) (*bol
181250
return b, nil
182251
}
183252

184-
func (ds *BoltDatastore) StoreRoute(route *models.Route) (*models.Route, error) {
253+
func (ds *BoltDatastore) InsertRoute(route *models.Route) (*models.Route, error) {
185254
if route == nil {
186255
return nil, models.ErrDatastoreEmptyApp
187256
}
188257

258+
if route.AppName == "" {
259+
return nil, models.ErrDatastoreEmptyAppName
260+
}
261+
262+
if route.Path == "" {
263+
return nil, models.ErrDatastoreEmptyRoutePath
264+
}
265+
266+
routePath := []byte(route.Path)
267+
189268
err := ds.db.Update(func(tx *bolt.Tx) error {
190269
b, err := ds.getRouteBucketForApp(tx, route.AppName)
191270
if err != nil {
192271
return err
193272
}
194273

274+
v := b.Get(routePath)
275+
if v != nil {
276+
return models.ErrRoutesAlreadyExists
277+
}
278+
279+
buf, err := json.Marshal(route)
280+
if err != nil {
281+
return err
282+
}
283+
284+
err = b.Put(routePath, buf)
285+
if err != nil {
286+
return err
287+
}
288+
return nil
289+
})
290+
if err != nil {
291+
return nil, err
292+
}
293+
return route, nil
294+
}
295+
296+
func (ds *BoltDatastore) UpdateRoute(newroute *models.Route) (*models.Route, error) {
297+
if newroute == nil {
298+
return nil, models.ErrDatastoreEmptyRoute
299+
}
300+
301+
if newroute.AppName == "" {
302+
return nil, models.ErrDatastoreEmptyAppName
303+
}
304+
305+
if newroute.Path == "" {
306+
return nil, models.ErrDatastoreEmptyRoutePath
307+
}
308+
309+
routePath := []byte(newroute.Path)
310+
311+
var route *models.Route
312+
313+
err := ds.db.Update(func(tx *bolt.Tx) error {
314+
b, err := ds.getRouteBucketForApp(tx, newroute.AppName)
315+
if err != nil {
316+
return err
317+
}
318+
319+
v := b.Get(routePath)
320+
if v == nil {
321+
return models.ErrRoutesNotFound
322+
}
323+
324+
err = json.Unmarshal(v, &route)
325+
if err != nil {
326+
return err
327+
}
328+
// Update route fields
329+
if newroute.Image != "" {
330+
route.Image = newroute.Image
331+
}
332+
if route.Memory != 0 {
333+
route.Memory = newroute.Memory
334+
}
335+
if route.Type != "" {
336+
route.Type = newroute.Type
337+
}
338+
if newroute.Headers != nil {
339+
if route.Config == nil {
340+
route.Config = map[string]string{}
341+
}
342+
for k, v := range newroute.Headers {
343+
route.Headers[k] = v
344+
}
345+
}
346+
if newroute.Config != nil {
347+
if route.Config == nil {
348+
route.Config = map[string]string{}
349+
}
350+
for k, v := range newroute.Config {
351+
route.Config[k] = v
352+
}
353+
}
354+
195355
buf, err := json.Marshal(route)
196356
if err != nil {
197357
return err
198358
}
199359

200-
err = b.Put([]byte(route.Path), buf)
360+
err = b.Put(routePath, buf)
201361
if err != nil {
202362
return err
203363
}

0 commit comments

Comments
 (0)