fix datastore Put and added tests (#402)

This commit is contained in:
Pedro Nasser
2016-12-07 14:59:54 -02:00
committed by Travis Reeder
parent 0baf03841f
commit 9382f0b133
6 changed files with 132 additions and 37 deletions

View File

@@ -505,6 +505,10 @@ func (ds *BoltDatastore) GetRoutes(ctx context.Context, filter *models.RouteFilt
} }
func (ds *BoltDatastore) Put(ctx context.Context, key, value []byte) error { func (ds *BoltDatastore) Put(ctx context.Context, key, value []byte) error {
if key == nil || len(key) == 0 {
return models.ErrDatastoreEmptyKey
}
ds.db.Update(func(tx *bolt.Tx) error { ds.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket(ds.extrasBucket) // todo: maybe namespace by app? b := tx.Bucket(ds.extrasBucket) // todo: maybe namespace by app?
err := b.Put(key, value) err := b.Put(key, value)
@@ -514,6 +518,10 @@ func (ds *BoltDatastore) Put(ctx context.Context, key, value []byte) error {
} }
func (ds *BoltDatastore) Get(ctx context.Context, key []byte) ([]byte, error) { func (ds *BoltDatastore) Get(ctx context.Context, key []byte) ([]byte, error) {
if key == nil || len(key) == 0 {
return nil, models.ErrDatastoreEmptyKey
}
var ret []byte var ret []byte
ds.db.View(func(tx *bolt.Tx) error { ds.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket(ds.extrasBucket) b := tx.Bucket(ds.extrasBucket)

View File

@@ -1,27 +1,13 @@
package datastore package datastore
import ( import (
"bytes"
"context" "context"
"log"
"os" "os"
"testing" "testing"
"github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin"
"github.com/iron-io/functions/api/models" "github.com/iron-io/functions/api/models"
) )
func setLogBuffer() *bytes.Buffer {
var buf bytes.Buffer
buf.WriteByte('\n')
logrus.SetOutput(&buf)
gin.DefaultErrorWriter = &buf
gin.DefaultWriter = &buf
log.SetOutput(&buf)
return &buf
}
const tmpBolt = "/tmp/func_test_bolt.db" const tmpBolt = "/tmp/func_test_bolt.db"
func TestBolt(t *testing.T) { func TestBolt(t *testing.T) {
@@ -35,16 +21,6 @@ func TestBolt(t *testing.T) {
t.Fatalf("Error when creating datastore: %v", err) t.Fatalf("Error when creating datastore: %v", err)
} }
testApp := &models.App{
Name: "Test",
}
testRoute := &models.Route{
AppName: testApp.Name,
Path: "/test",
Image: "iron/hello",
}
// Testing insert app // Testing insert app
_, err = ds.InsertApp(ctx, nil) _, err = ds.InsertApp(ctx, nil)
if err != models.ErrDatastoreEmptyApp { if err != models.ErrDatastoreEmptyApp {
@@ -272,4 +248,43 @@ func TestBolt(t *testing.T) {
t.Log(buf.String()) t.Log(buf.String())
t.Fatalf("Test RemoveApp: failed to remove the route") t.Fatalf("Test RemoveApp: failed to remove the route")
} }
// Testing Put/Get
err = ds.Put(ctx, nil, nil)
if err != models.ErrDatastoreEmptyKey {
t.Log(buf.String())
t.Fatalf("Test Put(nil,nil): expected error `%v`, but it was `%v`", models.ErrDatastoreEmptyKey, err)
}
err = ds.Put(ctx, []byte("test"), []byte("success"))
if err != nil {
t.Log(buf.String())
t.Fatalf("Test Put: unexpected error: %v", err)
}
val, err := ds.Get(ctx, []byte("test"))
if err != nil {
t.Log(buf.String())
t.Fatalf("Test Put: unexpected error: %v", err)
}
if string(val) != "success" {
t.Log(buf.String())
t.Fatalf("Test Get: expected value to be `%v`, but it was `%v`", "success", string(val))
}
err = ds.Put(ctx, []byte("test"), nil)
if err != nil {
t.Log(buf.String())
t.Fatalf("Test Put: unexpected error: %v", err)
}
val, err = ds.Get(ctx, []byte("test"))
if err != nil {
t.Log(buf.String())
t.Fatalf("Test Put: unexpected error: %v", err)
}
if string(val) != "" {
t.Log(buf.String())
t.Fatalf("Test Get: expected value to be `%v`, but it was `%v`", "", string(val))
}
} }

View File

@@ -0,0 +1,31 @@
package datastore
import (
"bytes"
"github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin"
"github.com/iron-io/functions/api/models"
"log"
)
func setLogBuffer() *bytes.Buffer {
var buf bytes.Buffer
buf.WriteByte('\n')
logrus.SetOutput(&buf)
gin.DefaultErrorWriter = &buf
gin.DefaultWriter = &buf
log.SetOutput(&buf)
return &buf
}
var testApp = &models.App{
Name: "Test",
}
var testRoute = &models.Route{
AppName: testApp.Name,
Path: "/test",
Image: "iron/hello",
Type: "sync",
Format: "http",
}

View File

@@ -524,6 +524,10 @@ func buildFilterRouteQuery(filter *models.RouteFilter) string {
} }
func (ds *PostgresDatastore) Put(ctx context.Context, key, value []byte) error { func (ds *PostgresDatastore) Put(ctx context.Context, key, value []byte) error {
if key == nil || len(key) == 0 {
return models.ErrDatastoreEmptyKey
}
_, err := ds.db.Exec(` _, err := ds.db.Exec(`
INSERT INTO extras ( INSERT INTO extras (
key, key,
@@ -531,7 +535,7 @@ func (ds *PostgresDatastore) Put(ctx context.Context, key, value []byte) error {
) )
VALUES ($1, $2) VALUES ($1, $2)
ON CONFLICT (key) DO UPDATE SET ON CONFLICT (key) DO UPDATE SET
value = $1; value = $2;
`, string(key), string(value)) `, string(key), string(value))
if err != nil { if err != nil {
@@ -542,6 +546,10 @@ func (ds *PostgresDatastore) Put(ctx context.Context, key, value []byte) error {
} }
func (ds *PostgresDatastore) Get(ctx context.Context, key []byte) ([]byte, error) { func (ds *PostgresDatastore) Get(ctx context.Context, key []byte) ([]byte, error) {
if key == nil || len(key) == 0 {
return nil, models.ErrDatastoreEmptyKey
}
row := ds.db.QueryRow("SELECT value FROM extras WHERE key=$1", key) row := ds.db.QueryRow("SELECT value FROM extras WHERE key=$1", key)
var value string var value string

View File

@@ -49,18 +49,6 @@ func TestPostgres(t *testing.T) {
t.Fatalf("Error when creating datastore: %v", err) t.Fatalf("Error when creating datastore: %v", err)
} }
testApp := &models.App{
Name: "Test",
}
testRoute := &models.Route{
AppName: testApp.Name,
Path: "/test",
Image: "iron/hello",
Type: "sync",
Format: "http",
}
// Testing insert app // Testing insert app
_, err = ds.InsertApp(ctx, nil) _, err = ds.InsertApp(ctx, nil)
if err != models.ErrDatastoreEmptyApp { if err != models.ErrDatastoreEmptyApp {
@@ -288,4 +276,48 @@ func TestPostgres(t *testing.T) {
t.Log(buf.String()) t.Log(buf.String())
t.Fatalf("Test RemoveApp: failed to remove the route") t.Fatalf("Test RemoveApp: failed to remove the route")
} }
// Testing Put/Get
err = ds.Put(ctx, nil, nil)
if err != models.ErrDatastoreEmptyKey {
t.Log(buf.String())
t.Fatalf("Test Put(nil,nil): expected error `%v`, but it was `%v`", models.ErrDatastoreEmptyKey, err)
}
err = ds.Put(ctx, []byte("test"), []byte("success"))
if err != nil {
t.Log(buf.String())
t.Fatalf("Test Put: unexpected error: %v", err)
}
val, err := ds.Get(ctx, []byte("test"))
if err != nil {
t.Log(buf.String())
t.Fatalf("Test Put: unexpected error: %v", err)
}
if string(val) != "success" {
t.Log(buf.String())
t.Fatalf("Test Get: expected value to be `%v`, but it was `%v`", "success", string(val))
}
err = ds.Put(ctx, []byte("test"), nil)
if err != nil {
t.Log(buf.String())
t.Fatalf("Test Put: unexpected error: %v", err)
}
val, err = ds.Get(ctx, []byte("test"))
if err != nil {
t.Log(buf.String())
t.Fatalf("Test Put: unexpected error: %v", err)
}
if string(val) != "" {
t.Log(buf.String())
t.Fatalf("Test Get: expected value to be `%v`, but it was `%v`", "", string(val))
}
}
func testPostgresInsert(t *testing.T, ctx context.Context, ds models.Datastore) {
} }

View File

@@ -30,4 +30,5 @@ var (
ErrDatastoreEmptyRoutePath = errors.New("Missing route name") ErrDatastoreEmptyRoutePath = errors.New("Missing route name")
ErrDatastoreEmptyApp = errors.New("Missing app") ErrDatastoreEmptyApp = errors.New("Missing app")
ErrDatastoreEmptyRoute = errors.New("Missing route") ErrDatastoreEmptyRoute = errors.New("Missing route")
ErrDatastoreEmptyKey = errors.New("Missing key")
) )