Skip to content

Commit 67a5625

Browse files
kumarlokeshblink1073
authored andcommitted
GODRIVER-2117 - Check clientSession is not nil inside executeTestRunnerOperation (mongodb#1457)
(cherry picked from commit fd1ca35)
1 parent ea38d1c commit 67a5625

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

mongo/integration/unified_spec_test.go

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -458,46 +458,64 @@ func executeTestRunnerOperation(mt *mtest.T, testCase *testCase, op *operation,
458458

459459
var fp mtest.FailPoint
460460
if err := bson.Unmarshal(fpDoc.Document(), &fp); err != nil {
461-
return fmt.Errorf("Unmarshal error: %v", err)
461+
return fmt.Errorf("Unmarshal error: %w", err)
462462
}
463463

464+
if clientSession == nil {
465+
return errors.New("expected valid session, got nil")
466+
}
464467
targetHost := clientSession.PinnedServer.Addr.String()
465468
opts := options.Client().ApplyURI(mtest.ClusterURI()).SetHosts([]string{targetHost})
466469
integtest.AddTestServerAPIVersion(opts)
467470
client, err := mongo.Connect(context.Background(), opts)
468471
if err != nil {
469-
return fmt.Errorf("Connect error for targeted client: %v", err)
472+
return fmt.Errorf("Connect error for targeted client: %w", err)
470473
}
471474
defer func() { _ = client.Disconnect(context.Background()) }()
472475

473476
if err = client.Database("admin").RunCommand(context.Background(), fp).Err(); err != nil {
474-
return fmt.Errorf("error setting targeted fail point: %v", err)
477+
return fmt.Errorf("error setting targeted fail point: %w", err)
475478
}
476479
mt.TrackFailPoint(fp.ConfigureFailPoint)
477480
case "configureFailPoint":
478481
fp, err := op.Arguments.LookupErr("failPoint")
479-
assert.Nil(mt, err, "failPoint not found in arguments")
482+
if err != nil {
483+
return fmt.Errorf("unable to find 'failPoint' in arguments: %w", err)
484+
}
480485
mt.SetFailPointFromDocument(fp.Document())
481486
case "assertSessionTransactionState":
482487
stateVal, err := op.Arguments.LookupErr("state")
483-
assert.Nil(mt, err, "state not found in arguments")
488+
if err != nil {
489+
return fmt.Errorf("unable to find 'state' in arguments: %w", err)
490+
}
484491
expectedState, ok := stateVal.StringValueOK()
485-
assert.True(mt, ok, "state argument is not a string")
492+
if !ok {
493+
return errors.New("expected 'state' argument to be string")
494+
}
486495

487-
assert.NotNil(mt, clientSession, "expected valid session, got nil")
496+
if clientSession == nil {
497+
return errors.New("expected valid session, got nil")
498+
}
488499
actualState := clientSession.TransactionState.String()
489500

490501
// actualState should match expectedState, but "in progress" is the same as
491502
// "in_progress".
492503
stateMatch := actualState == expectedState ||
493504
actualState == "in progress" && expectedState == "in_progress"
494-
assert.True(mt, stateMatch, "expected transaction state %v, got %v",
495-
expectedState, actualState)
505+
if !stateMatch {
506+
return fmt.Errorf("expected transaction state %v, got %v", expectedState, actualState)
507+
}
496508
case "assertSessionPinned":
509+
if clientSession == nil {
510+
return errors.New("expected valid session, got nil")
511+
}
497512
if clientSession.PinnedServer == nil {
498513
return errors.New("expected pinned server, got nil")
499514
}
500515
case "assertSessionUnpinned":
516+
if clientSession == nil {
517+
return errors.New("expected valid session, got nil")
518+
}
501519
// We don't use a combined helper for assertSessionPinned and assertSessionUnpinned because the unpinned
502520
// case provides the pinned server address in the error msg for debugging.
503521
if clientSession.PinnedServer != nil {
@@ -540,7 +558,7 @@ func executeTestRunnerOperation(mt *mtest.T, testCase *testCase, op *operation,
540558
case "waitForThread":
541559
waitForThread(mt, testCase, op)
542560
default:
543-
mt.Fatalf("unrecognized testRunner operation %v", op.Name)
561+
return fmt.Errorf("unrecognized testRunner operation %v", op.Name)
544562
}
545563

546564
return nil
@@ -567,7 +585,7 @@ func indexExists(dbName, collName, indexName string) (bool, error) {
567585
iv := mtest.GlobalClient().Database(dbName).Collection(collName).Indexes()
568586
cursor, err := iv.List(context.Background())
569587
if err != nil {
570-
return false, fmt.Errorf("IndexView.List error: %v", err)
588+
return false, fmt.Errorf("IndexView.List error: %w", err)
571589
}
572590
defer cursor.Close(context.Background())
573591

@@ -602,7 +620,7 @@ func collectionExists(dbName, collName string) (bool, error) {
602620
// Use global client because listCollections cannot be executed inside a transaction.
603621
collections, err := mtest.GlobalClient().Database(dbName).ListCollectionNames(context.Background(), filter)
604622
if err != nil {
605-
return false, fmt.Errorf("ListCollectionNames error: %v", err)
623+
return false, fmt.Errorf("ListCollectionNames error: %w", err)
606624
}
607625

608626
return len(collections) > 0, nil
@@ -632,9 +650,8 @@ func executeSessionOperation(mt *mtest.T, op *operation, sess mongo.Session) err
632650
case "withTransaction":
633651
return executeWithTransaction(mt, sess, op.Arguments)
634652
default:
635-
mt.Fatalf("unrecognized session operation: %v", op.Name)
653+
return fmt.Errorf("unrecognized session operation: %v", op.Name)
636654
}
637-
return nil
638655
}
639656

640657
func executeCollectionOperation(mt *mtest.T, op *operation, sess mongo.Session) error {

0 commit comments

Comments
 (0)