From e50daed19bbaec979a91f2677289f1c2e6e0d0d9 Mon Sep 17 00:00:00 2001 From: Kiran Kamineni Date: Thu, 14 Mar 2019 15:38:13 -0700 Subject: 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 --- src/k8splugin/internal/db/mongo.go | 38 ++++++++++++------- src/k8splugin/internal/db/mongo_test.go | 66 ++++++++++++++------------------- 2 files changed, 52 insertions(+), 52 deletions(-) (limited to 'src/k8splugin/internal') 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)) -- cgit 1.2.3-korg