From a2de2b10d6d45a2a78f40e1746833dcba1aa60cc Mon Sep 17 00:00:00 2001 From: Saghm Rossi Date: Tue, 13 Feb 2018 13:38:16 -0500 Subject: [PATCH] GODRIVER-234 Specify write concern in listDatabases test to fix failures on sharded clusters Change-Id: I14173c603a9bd7a04ed64b266d3108ffaa945851 --- mongo/internal/testutil/ops.go | 14 +++++++++++--- mongo/private/ops/aggregate_test.go | 6 ++++-- mongo/private/ops/cursor_test.go | 7 ++++--- mongo/private/ops/list_collections_test.go | 12 ++++++------ mongo/private/ops/list_databases_test.go | 3 ++- mongo/writeconcern/writeconcern.go | 2 +- 6 files changed, 28 insertions(+), 16 deletions(-) diff --git a/mongo/internal/testutil/ops.go b/mongo/internal/testutil/ops.go index 587dbf242d..36ca738699 100644 --- a/mongo/internal/testutil/ops.go +++ b/mongo/internal/testutil/ops.go @@ -17,6 +17,7 @@ import ( "github.com/mongodb/mongo-go-driver/mongo/private/conn" "github.com/mongodb/mongo-go-driver/mongo/private/msg" "github.com/mongodb/mongo-go-driver/mongo/readpref" + "github.com/mongodb/mongo-go-driver/mongo/writeconcern" "github.com/stretchr/testify/require" ) @@ -102,12 +103,12 @@ func autoDropDB(t *testing.T, clstr *cluster.Cluster) { } // AutoInsertDocs inserts the docs into the test cluster. -func AutoInsertDocs(t *testing.T, docs ...*bson.Document) { - InsertDocs(t, DBName(t), ColName(t), docs...) +func AutoInsertDocs(t *testing.T, writeConcern *writeconcern.WriteConcern, docs ...*bson.Document) { + InsertDocs(t, DBName(t), ColName(t), writeConcern, docs...) } // InsertDocs inserts the docs into the test cluster. -func InsertDocs(t *testing.T, dbname, colname string, docs ...*bson.Document) { +func InsertDocs(t *testing.T, dbname, colname string, writeConcern *writeconcern.WriteConcern, docs ...*bson.Document) { arrDocs := make([]*bson.Value, 0, len(docs)) for _, doc := range docs { arrDocs = append(arrDocs, bson.AC.Document(doc)) @@ -116,6 +117,13 @@ func InsertDocs(t *testing.T, dbname, colname string, docs ...*bson.Document) { bson.C.String("insert", colname), bson.C.ArrayFromElements("documents", arrDocs...)) + if writeConcern != nil { + wc, err := writeConcern.MarshalBSONElement() + require.NoError(t, err) + + insertCommand.Append(wc) + } + request := msg.NewCommand( msg.NextRequestID(), dbname, diff --git a/mongo/private/ops/aggregate_test.go b/mongo/private/ops/aggregate_test.go index 4f464cb693..1d53f8d5ac 100644 --- a/mongo/private/ops/aggregate_test.go +++ b/mongo/private/ops/aggregate_test.go @@ -44,7 +44,7 @@ func TestAggregateWithMultipleBatches(t *testing.T) { bson.NewDocument(bson.C.Int32("_id", 4)), bson.NewDocument(bson.C.Int32("_id", 5)), } - testutil.AutoInsertDocs(t, documents...) + testutil.AutoInsertDocs(t, nil, documents...) readers := make([]bson.Reader, 0, len(documents)) for _, doc := range documents { @@ -100,6 +100,7 @@ func TestAggregateWithAllowDiskUse(t *testing.T) { t.Parallel() testutil.Integration(t) testutil.AutoInsertDocs(t, + nil, bson.NewDocument(bson.C.Int32("_id", 1)), bson.NewDocument(bson.C.Int32("_id", 2)), ) @@ -161,7 +162,7 @@ func TestLegacyAggregateWithMultipleBatches(t *testing.T) { bson.NewDocument(bson.C.Int32("_id", 4)), bson.NewDocument(bson.C.Int32("_id", 5)), } - testutil.AutoInsertDocs(t, documents...) + testutil.AutoInsertDocs(t, nil, documents...) readers := make([]bson.Reader, 0, len(documents)) for _, doc := range documents { @@ -225,6 +226,7 @@ func TestLegacyAggregateWithAllowDiskUse(t *testing.T) { t.Parallel() testutil.Integration(t) testutil.AutoInsertDocs(t, + nil, bson.NewDocument(bson.C.Int32("_id", 1)), bson.NewDocument(bson.C.Int32("_id", 2)), ) diff --git a/mongo/private/ops/cursor_test.go b/mongo/private/ops/cursor_test.go index 3a59228fe5..a5561d99d4 100644 --- a/mongo/private/ops/cursor_test.go +++ b/mongo/private/ops/cursor_test.go @@ -56,7 +56,7 @@ func TestCursorSingleBatch(t *testing.T) { bson.NewDocument(bson.C.Int32("_id", 1)), bson.NewDocument(bson.C.Int32("_id", 2)), } - testutil.AutoInsertDocs(t, documents...) + testutil.AutoInsertDocs(t, nil, documents...) readers := make([]bson.Reader, 0, len(documents)) for _, doc := range documents { @@ -98,7 +98,7 @@ func TestCursorMultipleBatches(t *testing.T) { bson.NewDocument(bson.C.Int32("_id", 4)), bson.NewDocument(bson.C.Int32("_id", 5)), } - testutil.AutoInsertDocs(t, documents...) + testutil.AutoInsertDocs(t, nil, documents...) readers := make([]bson.Reader, 0, len(documents)) for _, doc := range documents { @@ -159,7 +159,7 @@ func TestCursorClose(t *testing.T) { bson.NewDocument(bson.C.Int32("_id", 4)), bson.NewDocument(bson.C.Int32("_id", 5)), } - testutil.AutoInsertDocs(t, documents...) + testutil.AutoInsertDocs(t, nil, documents...) s := getServer(t) cursorResult := find(t, s, 2) @@ -177,6 +177,7 @@ func TestCursorError(t *testing.T) { testutil.Integration(t) testutil.AutoDropCollection(t) testutil.AutoInsertDocs(t, + nil, bson.NewDocument(bson.C.Int32("_id", 1)), bson.NewDocument(bson.C.Int32("_id", 2)), bson.NewDocument(bson.C.Int32("_id", 3)), diff --git a/mongo/private/ops/list_collections_test.go b/mongo/private/ops/list_collections_test.go index f6e899611c..853ff4a50f 100644 --- a/mongo/private/ops/list_collections_test.go +++ b/mongo/private/ops/list_collections_test.go @@ -37,9 +37,9 @@ func TestListCollections(t *testing.T) { testutil.DropCollection(t, dbname, collectionNameOne) testutil.DropCollection(t, dbname, collectionNameTwo) testutil.DropCollection(t, dbname, collectionNameThree) - testutil.InsertDocs(t, dbname, collectionNameOne, bson.NewDocument(bson.C.Int32("_id", 1))) - testutil.InsertDocs(t, dbname, collectionNameTwo, bson.NewDocument(bson.C.Int32("_id", 1))) - testutil.InsertDocs(t, dbname, collectionNameThree, bson.NewDocument(bson.C.Int32("_id", 1))) + testutil.InsertDocs(t, dbname, collectionNameOne, nil, bson.NewDocument(bson.C.Int32("_id", 1))) + testutil.InsertDocs(t, dbname, collectionNameTwo, nil, bson.NewDocument(bson.C.Int32("_id", 1))) + testutil.InsertDocs(t, dbname, collectionNameThree, nil, bson.NewDocument(bson.C.Int32("_id", 1))) s := getServer(t) cursor, err := ListCollections(context.Background(), s, dbname, ListCollectionsOptions{}) @@ -74,9 +74,9 @@ func TestListCollectionsMultipleBatches(t *testing.T) { testutil.DropCollection(t, dbname, collectionNameOne) testutil.DropCollection(t, dbname, collectionNameTwo) testutil.DropCollection(t, dbname, collectionNameThree) - testutil.InsertDocs(t, dbname, collectionNameOne, bson.NewDocument(bson.C.Int32("_id", 1))) - testutil.InsertDocs(t, dbname, collectionNameTwo, bson.NewDocument(bson.C.Int32("_id", 1))) - testutil.InsertDocs(t, dbname, collectionNameThree, bson.NewDocument(bson.C.Int32("_id", 1))) + testutil.InsertDocs(t, dbname, collectionNameOne, nil, bson.NewDocument(bson.C.Int32("_id", 1))) + testutil.InsertDocs(t, dbname, collectionNameTwo, nil, bson.NewDocument(bson.C.Int32("_id", 1))) + testutil.InsertDocs(t, dbname, collectionNameThree, nil, bson.NewDocument(bson.C.Int32("_id", 1))) s := getServer(t) cursor, err := ListCollections(context.Background(), s, dbname, ListCollectionsOptions{ diff --git a/mongo/private/ops/list_databases_test.go b/mongo/private/ops/list_databases_test.go index cdbe027480..a113e9b9df 100644 --- a/mongo/private/ops/list_databases_test.go +++ b/mongo/private/ops/list_databases_test.go @@ -14,6 +14,7 @@ import ( "github.com/mongodb/mongo-go-driver/bson" "github.com/mongodb/mongo-go-driver/mongo/internal/testutil" . "github.com/mongodb/mongo-go-driver/mongo/private/ops" + "github.com/mongodb/mongo-go-driver/mongo/writeconcern" "github.com/stretchr/testify/require" ) @@ -21,7 +22,7 @@ func TestListDatabases(t *testing.T) { t.Parallel() testutil.Integration(t) testutil.AutoDropCollection(t) - testutil.AutoInsertDocs(t, bson.NewDocument(bson.C.Int32("_id", 1))) + testutil.AutoInsertDocs(t, writeconcern.New(writeconcern.WMajority()), bson.NewDocument(bson.C.Int32("_id", 1))) s := getServer(t) cursor, err := ListDatabases(context.Background(), s, ListDatabasesOptions{}) diff --git a/mongo/writeconcern/writeconcern.go b/mongo/writeconcern/writeconcern.go index 8c9a959dc3..dee75c26a2 100644 --- a/mongo/writeconcern/writeconcern.go +++ b/mongo/writeconcern/writeconcern.go @@ -54,7 +54,7 @@ func W(w int) Option { // WMajority requests acknowledgement that write operations propagate to the majority of mongod // instances. -func WMajority(w int) Option { +func WMajority() Option { return func(concern *WriteConcern) { concern.w = "majority" }