summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorKiran Kamineni <kiran.k.kamineni@intel.com>2019-03-14 15:38:13 -0700
committerKiran Kamineni <kiran.k.kamineni@intel.com>2019-03-14 15:38:16 -0700
commite50daed19bbaec979a91f2677289f1c2e6e0d0d9 (patch)
treedd917440a69e92cd6e6668cdc18952864a1ac462 /src
parentf379cb8c8a5d8cf4290fd6e105f66e4fd4cdabe6 (diff)
Upgrade mongo driver to 1.0
Mongo driver 1.0 was released recently Upgrading from 0.2.0 which we were on right now. Issue-ID: MULTICLOUD-530 Change-Id: I6d968e99b129d4307414ec31410505200b7bd47d Signed-off-by: Kiran Kamineni <kiran.k.kamineni@intel.com>
Diffstat (limited to 'src')
-rw-r--r--src/k8splugin/go.mod3
-rw-r--r--src/k8splugin/go.sum4
-rw-r--r--src/k8splugin/internal/db/mongo.go38
-rw-r--r--src/k8splugin/internal/db/mongo_test.go66
4 files changed, 58 insertions, 53 deletions
diff --git a/src/k8splugin/go.mod b/src/k8splugin/go.mod
index 606eb4da..29a10ecb 100644
--- a/src/k8splugin/go.mod
+++ b/src/k8splugin/go.mod
@@ -51,7 +51,7 @@ require (
github.com/mitchellh/mapstructure v1.1.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v0.0.0-20180228065516-1df9eeb2bb81 // indirect
- github.com/mongodb/mongo-go-driver v0.2.0
+ github.com/mongodb/mongo-go-driver v1.0.0
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
@@ -66,6 +66,7 @@ require (
github.com/tidwall/pretty v0.0.0-20180105212114-65a9db5fad51 // indirect
github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c // indirect
github.com/xdg/stringprep v1.0.0 // indirect
+ go.mongodb.org/mongo-driver v1.0.0
golang.org/x/net v0.0.0-20181201002055-351d144fa1fc
golang.org/x/oauth2 v0.0.0-20181203162652-d668ce993890 // indirect
golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2 // indirect
diff --git a/src/k8splugin/go.sum b/src/k8splugin/go.sum
index 179c7784..f22fe8da 100644
--- a/src/k8splugin/go.sum
+++ b/src/k8splugin/go.sum
@@ -134,6 +134,8 @@ github.com/modern-go/reflect2 v0.0.0-20180228065516-1df9eeb2bb81 h1:ImOHKpmdLPXW
github.com/modern-go/reflect2 v0.0.0-20180228065516-1df9eeb2bb81/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/mongodb/mongo-go-driver v0.2.0 h1:MBI/hnb0LiACJRVAlT+nL5wdtV4EFKTjJEhQdapZFB0=
github.com/mongodb/mongo-go-driver v0.2.0/go.mod h1:NK/HWDIIZkaYsnYa0hmtP443T5ELr0KDecmIioVuuyU=
+github.com/mongodb/mongo-go-driver v1.0.0 h1:aq055NT+Xu6ta/f7D51gIbLHIZwM0Gwzt9RHfmrzs6A=
+github.com/mongodb/mongo-go-driver v1.0.0/go.mod h1:NK/HWDIIZkaYsnYa0hmtP443T5ELr0KDecmIioVuuyU=
github.com/opencontainers/go-digest v1.0.0-rc1 h1:WzifXhOVOEOuFYOJAW6aQqW0TooG2iki3E3Ii+WN7gQ=
github.com/opencontainers/go-digest v1.0.0-rc1/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s=
github.com/opencontainers/image-spec v1.0.1 h1:JMemWkRwHx4Zj+fVxWoMCFm/8sYGGrUVojFA6h/TRcI=
@@ -175,6 +177,8 @@ github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c h1:u40Z8hqBAAQyv+vATcGgV
github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c/go.mod h1:lB8K/P019DLNhemzwFU4jHLhdvlE6uDZjXFejJXr49I=
github.com/xdg/stringprep v1.0.0 h1:d9X0esnoa3dFsV0FG35rAT0RIhYFlPq7MiP+DW89La0=
github.com/xdg/stringprep v1.0.0/go.mod h1:Jhud4/sHMO4oL310DaZAKk9ZaJ08SJfe+sJh0HrGL1Y=
+go.mongodb.org/mongo-driver v1.0.0 h1:KxPRDyfB2xXnDE2My8acoOWBQkfv3tz0SaWTRZjJR0c=
+go.mongodb.org/mongo-driver v1.0.0/go.mod h1:u7ryQJ+DOzQmeO7zB6MHyr8jkEQvC8vH7qLUO4lqsUM=
golang.org/x/crypto v0.0.0-20180608092829-8ac0e0d97ce4 h1:wviDUSmtheHRBfoY8B9U8ELl2USoXi2YFwdGdpIIkzI=
golang.org/x/crypto v0.0.0-20180608092829-8ac0e0d97ce4/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 h1:u+LnwYTOOW7Ukr/fppxEb1Nwz0AtPflrblfvUudpo+I=
diff --git a/src/k8splugin/internal/db/mongo.go b/src/k8splugin/internal/db/mongo.go
index 05976b12..d414f543 100644
--- a/src/k8splugin/internal/db/mongo.go
+++ b/src/k8splugin/internal/db/mongo.go
@@ -17,14 +17,15 @@
package db
import (
- "github.com/mongodb/mongo-go-driver/bson"
- "github.com/mongodb/mongo-go-driver/bson/primitive"
- "github.com/mongodb/mongo-go-driver/mongo"
- "github.com/mongodb/mongo-go-driver/mongo/options"
- pkgerrors "github.com/pkg/errors"
"golang.org/x/net/context"
"log"
"os"
+
+ pkgerrors "github.com/pkg/errors"
+ "go.mongodb.org/mongo-driver/bson"
+ "go.mongodb.org/mongo-driver/bson/primitive"
+ "go.mongodb.org/mongo-driver/mongo"
+ "go.mongodb.org/mongo-driver/mongo/options"
)
// MongoCollection defines the a subset of MongoDB operations
@@ -39,7 +40,7 @@ type MongoCollection interface {
DeleteOne(ctx context.Context, filter interface{},
opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
Find(ctx context.Context, filter interface{},
- opts ...*options.FindOptions) (mongo.Cursor, error)
+ opts ...*options.FindOptions) (*mongo.Cursor, error)
}
// MongoStore is an implementation of the db.Store interface
@@ -60,12 +61,25 @@ var decodeBytes = func(sr *mongo.SingleResult) (bson.Raw, error) {
return sr.DecodeBytes()
}
+// These exists only for allowing us to mock the cursor.Next function
+// Mainly because we cannot construct a mongo.Cursor struct from our
+// tests. All fields in that struct are private and there is no public
+// constructor method.
+var cursorNext = func(ctx context.Context, cursor *mongo.Cursor) bool {
+ return cursor.Next(ctx)
+}
+var cursorClose = func(ctx context.Context, cursor *mongo.Cursor) error {
+ return cursor.Close(ctx)
+}
+
// NewMongoStore initializes a Mongo Database with the name provided
// If a database with that name exists, it will be returned
func NewMongoStore(name string, store *mongo.Database) (Store, error) {
if store == nil {
ip := "mongodb://" + os.Getenv("DATABASE_IP") + ":27017"
- mongoClient, err := mongo.NewClient(ip)
+ clientOptions := options.Client()
+ clientOptions.ApplyURI(ip)
+ mongoClient, err := mongo.NewClient(clientOptions)
if err != nil {
return nil, err
}
@@ -292,16 +306,12 @@ func (m *MongoStore) ReadAll(coll, tag string) (map[string][]byte, error) {
if err != nil {
return nil, pkgerrors.Errorf("Error reading from database: %s", err.Error())
}
- defer cursor.Close(ctx)
+ defer cursorClose(ctx, cursor)
//Iterate over all the master tables
result := make(map[string][]byte)
- for cursor.Next(ctx) {
- d, err := cursor.DecodeBytes()
- if err != nil {
- log.Printf("Unable to decode data in Readall: %s", err.Error())
- continue
- }
+ for cursorNext(ctx, cursor) {
+ d := cursor.Current
//Read key of each master table
key, ok := d.Lookup("key").StringValueOK()
diff --git a/src/k8splugin/internal/db/mongo_test.go b/src/k8splugin/internal/db/mongo_test.go
index 1663e774..973921c3 100644
--- a/src/k8splugin/internal/db/mongo_test.go
+++ b/src/k8splugin/internal/db/mongo_test.go
@@ -21,43 +21,21 @@ package db
import (
"bytes"
"context"
- "github.com/mongodb/mongo-go-driver/bson"
- "github.com/mongodb/mongo-go-driver/mongo"
- "github.com/mongodb/mongo-go-driver/mongo/options"
- pkgerrors "github.com/pkg/errors"
"reflect"
"strings"
"testing"
-)
-
-// Implements the mongo.Cursor interface
-type mockCursor struct {
- mongo.Cursor
- err error
- bson bson.Raw
- count int
-}
-
-func (mc *mockCursor) Next(ctx context.Context) bool {
- if mc.count > 0 {
- mc.count = mc.count - 1
- return true
- }
- return false
-}
-
-func (mc *mockCursor) DecodeBytes() (bson.Raw, error) {
- return mc.bson, mc.err
-}
-func (mc *mockCursor) Close(ctx context.Context) error {
- return nil
-}
+ pkgerrors "github.com/pkg/errors"
+ "go.mongodb.org/mongo-driver/bson"
+ "go.mongodb.org/mongo-driver/mongo"
+ "go.mongodb.org/mongo-driver/mongo/options"
+)
//Implements the functions used currently in mongo.go
type mockCollection struct {
- Err error
- mCursor mongo.Cursor
+ Err error
+ mCursor *mongo.Cursor
+ mCursorCount int
}
func (c *mockCollection) InsertOne(ctx context.Context, document interface{},
@@ -89,7 +67,7 @@ func (c *mockCollection) DeleteOne(ctx context.Context, filter interface{},
}
func (c *mockCollection) Find(ctx context.Context, filter interface{},
- opts ...*options.FindOptions) (mongo.Cursor, error) {
+ opts ...*options.FindOptions) (*mongo.Cursor, error) {
return c.mCursor, c.Err
}
@@ -415,14 +393,14 @@ func TestReadAll(t *testing.T) {
"tag": "metadata",
},
mockColl: &mockCollection{
- mCursor: &mockCursor{
+ mCursor: &mongo.Cursor{
// Binary form of
// {
// "_id" : ObjectId("5c115156777ff85654248ae1"),
// "key" : "b82c4bb1-09ff-6093-4d58-8327b94e1e20",
// "metadata" : ObjectId("5c115156c9755047e318bbfd")
// }
- bson: bson.Raw{
+ Current: bson.Raw{
'\x5a', '\x00', '\x00', '\x00', '\x07', '\x5f', '\x69', '\x64',
'\x00', '\x5c', '\x11', '\x51', '\x56', '\x77', '\x7f', '\xf8',
'\x56', '\x54', '\x24', '\x8a', '\xe1', '\x02', '\x6b', '\x65',
@@ -436,8 +414,8 @@ func TestReadAll(t *testing.T) {
'\x56', '\xc9', '\x75', '\x50', '\x47', '\xe3', '\x18', '\xbb',
'\xfd', '\x00',
},
- count: 1,
},
+ mCursorCount: 1,
},
expected: map[string][]byte{
"b82c4bb1-09ff-6093-4d58-8327b94e1e20": []byte{
@@ -462,14 +440,14 @@ func TestReadAll(t *testing.T) {
"tag": "tagName",
},
mockColl: &mockCollection{
- mCursor: &mockCursor{
+ mCursor: &mongo.Cursor{
// Binary form of
// {
// "_id" : ObjectId("5c115156777ff85654248ae1"),
// "key" : "b82c4bb1-09ff-6093-4d58-8327b94e1e20",
// "metadata" : ObjectId("5c115156c9755047e318bbfd")
// }
- bson: bson.Raw{
+ Current: bson.Raw{
'\x5a', '\x00', '\x00', '\x00', '\x07', '\x5f', '\x69', '\x64',
'\x00', '\x5c', '\x11', '\x51', '\x56', '\x77', '\x7f', '\xf8',
'\x56', '\x54', '\x24', '\x8a', '\xe1', '\x02', '\x6b', '\x65',
@@ -483,8 +461,8 @@ func TestReadAll(t *testing.T) {
'\x56', '\xc9', '\x75', '\x50', '\x47', '\xe3', '\x18', '\xbb',
'\xfd', '\x00',
},
- count: 1,
},
+ mCursorCount: 1,
},
expectedError: "Did not find any objects with tag",
},
@@ -508,7 +486,19 @@ func TestReadAll(t *testing.T) {
}
decodeBytes = func(sr *mongo.SingleResult) (bson.Raw, error) {
- return testCase.mockColl.mCursor.DecodeBytes()
+ return testCase.mockColl.mCursor.Current, testCase.mockColl.Err
+ }
+
+ cursorNext = func(ctx context.Context, cursor *mongo.Cursor) bool {
+ if testCase.mockColl.mCursorCount > 0 {
+ testCase.mockColl.mCursorCount -= 1
+ return true
+ }
+ return false
+ }
+
+ cursorClose = func(ctx context.Context, cursor *mongo.Cursor) error {
+ return nil
}
got, err := m.ReadAll(testCase.input["coll"].(string), testCase.input["tag"].(string))