@@ -21,6 +21,8 @@ import (
21
21
"github.com/mongodb/mongo-go-driver/mongo/readpref"
22
22
"github.com/mongodb/mongo-go-driver/mongo/writeconcern"
23
23
"github.com/mongodb/mongo-go-driver/x/bsonx"
24
+ "github.com/mongodb/mongo-go-driver/x/network/connstring"
25
+ "github.com/mongodb/mongo-go-driver/x/network/description"
24
26
"github.com/stretchr/testify/require"
25
27
)
26
28
@@ -206,7 +208,7 @@ func verifyListCollections(cursor Cursor, uncappedName string, cappedName string
206
208
var cappedFound bool
207
209
208
210
for cursor .Next (context .Background ()) {
209
- next := bsonx.Doc {}
211
+ next := & bsonx.Doc {}
210
212
err = cursor .Decode (next )
211
213
if err != nil {
212
214
return err
@@ -223,9 +225,9 @@ func verifyListCollections(cursor Cursor, uncappedName string, cappedName string
223
225
224
226
elemName := elem .StringValue ()
225
227
226
- if elemName != uncappedName && elemName != cappedName {
227
- return fmt . Errorf ( "incorrect collection name. got: %s. wanted: %s or %s" , elemName , uncappedName ,
228
- cappedName )
228
+ // legacy servers can return an indexes collection that shouldn't be considered here
229
+ if elemName != cappedName && elemName != uncappedName {
230
+ continue
229
231
}
230
232
231
233
if elemName == uncappedName && ! uncappedFound {
@@ -257,19 +259,16 @@ func verifyListCollections(cursor Cursor, uncappedName string, cappedName string
257
259
return nil
258
260
}
259
261
260
- func listCollectionsTest (db * Database , cappedOnly bool ) error {
261
- uncappedName , cappedName , err := setupListCollectionsDb (db )
262
- if err != nil {
263
- return err
264
- }
265
-
262
+ func listCollectionsTest (db * Database , cappedOnly bool , cappedName , uncappedName string ) error {
266
263
var filter bsonx.Doc
267
264
if cappedOnly {
268
265
filter = bsonx.Doc {{"options.capped" , bsonx .Boolean (true )}}
269
266
}
270
267
268
+ var cursor Cursor
269
+ var err error
271
270
for i := 0 ; i < 10 ; i ++ {
272
- cursor , err : = db .ListCollections (context .Background (), filter )
271
+ cursor , err = db .ListCollections (context .Background (), filter )
273
272
if err != nil {
274
273
return err
275
274
}
@@ -283,43 +282,69 @@ func listCollectionsTest(db *Database, cappedOnly bool) error {
283
282
return err // all tests failed
284
283
}
285
284
286
- func TestDatabase_ListCollections (t * testing.T ) {
287
- // TODO(GODRIVER-492) - implement legacy list collections
288
- skipIfBelow32 (t )
285
+ // get the connection string for a direct connection to a secondary in a replica set
286
+ func getSecondaryConnString (t * testing.T ) connstring.ConnString {
287
+ topo := testutil .Topology (t )
288
+ for _ , server := range topo .Description ().Servers {
289
+ if server .Kind != description .RSSecondary {
290
+ continue
291
+ }
289
292
290
- rpPrimary := readpref .Primary ()
291
- rpSecondary := readpref .Secondary ()
293
+ fullAddr := "mongodb://" + server .Addr .String () + "/?connect=direct"
294
+ cs , err := connstring .Parse (fullAddr )
295
+ require .NoError (t , err )
296
+ return cs
297
+ }
292
298
299
+ t .Fatalf ("no secondary found for %s" , t .Name ())
300
+ return connstring.ConnString {}
301
+ }
302
+
303
+ func TestDatabase_ListCollections (t * testing.T ) {
293
304
var listCollectionsTable = []struct {
294
305
name string
295
306
expectedTopology string
296
307
cappedOnly bool
297
- rp * readpref. ReadPref
308
+ direct bool
298
309
}{
299
- {"standalone_nofilter" , "server" , false , rpPrimary },
300
- {"standalone_filter" , "server" , true , rpPrimary },
301
- {"replicaset_nofilter" , "replica_set" , false , rpPrimary },
302
- {"replicaset_filter" , "replica_set" , true , rpPrimary },
303
- {"replicaset_secondary_nofilter" , "replica_set" , false , rpSecondary },
304
- {"replicaset_secondary_filter" , "replica_set" , true , rpSecondary },
305
- {"sharded_nofilter" , "sharded_cluster" , false , rpPrimary },
306
- {"sharded_filter" , "sharded_cluster" , true , rpPrimary },
310
+ {"standalone_nofilter" , "server" , false , false },
311
+ {"standalone_filter" , "server" , true , false },
312
+ {"replicaset_nofilter" , "replica_set" , false , false },
313
+ {"replicaset_filter" , "replica_set" , true , false },
314
+ {"replicaset_secondary_nofilter" , "replica_set" , false , true },
315
+ {"replicaset_secondary_filter" , "replica_set" , true , true },
316
+ {"sharded_nofilter" , "sharded_cluster" , false , false },
317
+ {"sharded_filter" , "sharded_cluster" , true , false },
307
318
}
308
319
309
320
for _ , tt := range listCollectionsTable {
310
321
t .Run (tt .name , func (t * testing.T ) {
311
- if os .Getenv ("topology " ) != tt .expectedTopology {
322
+ if os .Getenv ("TOPOLOGY " ) != tt .expectedTopology {
312
323
t .Skip ()
313
324
}
314
- dbName := tt .name
315
- db := createTestDatabase (t , & dbName , options .Database ().SetReadPreference (tt .rp ))
316
325
326
+ createDb := createTestDatabase (t , & tt .name , options .Database ().SetWriteConcern (wcMajority ))
317
327
defer func () {
318
- err := db .Drop (context .Background ())
328
+ err := createDb .Drop (context .Background ())
319
329
require .NoError (t , err )
320
330
}()
321
331
322
- err := listCollectionsTest (db , tt .cappedOnly )
332
+ uncappedName , cappedName , err := setupListCollectionsDb (createDb )
333
+ require .NoError (t , err )
334
+
335
+ var cs connstring.ConnString
336
+ if tt .direct {
337
+ // TODO(GODRIVER-641) - correctly set read preference on direct connections for OP_MSG
338
+ t .Skip ()
339
+ cs = getSecondaryConnString (t )
340
+ } else {
341
+ cs = testutil .ConnString (t )
342
+ }
343
+
344
+ client := createTestClientWithConnstring (t , cs )
345
+ db := client .Database (tt .name )
346
+
347
+ err = listCollectionsTest (db , tt .cappedOnly , cappedName , uncappedName )
323
348
require .NoError (t , err )
324
349
})
325
350
}
0 commit comments