...

Source file src/go.mongodb.org/mongo-driver/mongo/integration/data_lake_test.go

Documentation: go.mongodb.org/mongo-driver/mongo/integration

     1  // Copyright (C) MongoDB, Inc. 2017-present.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"); you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
     6  
     7  package integration
     8  
     9  import (
    10  	"context"
    11  	"fmt"
    12  	"testing"
    13  
    14  	"go.mongodb.org/mongo-driver/bson"
    15  	"go.mongodb.org/mongo-driver/internal/assert"
    16  	"go.mongodb.org/mongo-driver/mongo/integration/mtest"
    17  	"go.mongodb.org/mongo-driver/mongo/options"
    18  )
    19  
    20  func TestAtlasDataLake(t *testing.T) {
    21  	// Prose tests against Atlas Data Lake.
    22  
    23  	mt := mtest.New(t, mtest.NewOptions().AtlasDataLake(true).CreateClient(false))
    24  	getMtOpts := func() *mtest.Options {
    25  		return mtest.NewOptions().CollectionName("driverdata")
    26  	}
    27  
    28  	mt.RunOpts("killCursors", getMtOpts(), func(mt *mtest.T) {
    29  		// Test that the killCursors sent internally when closing a cursor uses the correct namespace and cursor ID.
    30  
    31  		// Run a Find and get the cursor ID and namespace returned by the server. Use a batchSize of 2 to force the
    32  		// server to keep the cursor open.
    33  		cursor, err := mt.Coll.Find(context.Background(), bson.D{}, options.Find().SetBatchSize(2))
    34  		assert.Nil(mt, err, "Find error: %v", err)
    35  		findEvt := mt.GetSucceededEvent()
    36  		assert.Equal(mt, "find", findEvt.CommandName, "expected command name %q, got %q", "find", findEvt.CommandName)
    37  		expectedID := findEvt.Reply.Lookup("cursor", "id").Int64()
    38  		expectedNS := findEvt.Reply.Lookup("cursor", "ns").StringValue()
    39  
    40  		// Close the cursor, forcing a killCursors command.
    41  		mt.ClearEvents()
    42  		err = cursor.Close(context.Background())
    43  		assert.Nil(mt, err, "Close error: %v", err)
    44  
    45  		// Extract information from the killCursors started event and assert that it sent the right cursor ID and ns.
    46  		killCursorsEvt := mt.GetStartedEvent()
    47  		assert.Equal(mt, "killCursors", killCursorsEvt.CommandName, "expected command name %q, got %q", "killCursors",
    48  			killCursorsEvt.CommandName)
    49  		actualID := killCursorsEvt.Command.Lookup("cursors", "0").Int64()
    50  		killCursorsDB := killCursorsEvt.Command.Lookup("$db").StringValue()
    51  		killCursorsColl := killCursorsEvt.Command.Lookup("killCursors").StringValue()
    52  		actualNS := fmt.Sprintf("%s.%s", killCursorsDB, killCursorsColl)
    53  
    54  		assert.Equal(mt, expectedID, actualID, "expected cursor ID %v, got %v; find event %v, killCursors event %v",
    55  			expectedID, actualID, findEvt, killCursorsEvt)
    56  		assert.Equal(mt, expectedNS, actualNS, "expected namespace %q, got %q; find event %v, killCursors event %v",
    57  			expectedNS, actualNS, findEvt, killCursorsEvt)
    58  
    59  		// Extract information from the killCursors succeeded event and assert that the right cursor was killed.
    60  		var killCursorsResponse struct {
    61  			CursorsKilled []int64
    62  		}
    63  		err = bson.Unmarshal(mt.GetSucceededEvent().Reply, &killCursorsResponse)
    64  		assert.Nil(mt, err, "error unmarshalling killCursors response: %v", err)
    65  		expectedCursorsKilled := []int64{expectedID}
    66  		assert.Equal(mt, expectedCursorsKilled, killCursorsResponse.CursorsKilled,
    67  			"expected cursorsKilled array %v, got %v", expectedCursorsKilled, killCursorsResponse.CursorsKilled)
    68  	})
    69  
    70  	mt.RunOpts("auth settings", noClientOpts, func(mt *mtest.T) {
    71  		// Test connectivity using different auth settings.
    72  
    73  		testCases := []struct {
    74  			name          string
    75  			authMechanism string // No auth will be used if this is "".
    76  		}{
    77  			{"no auth", ""},
    78  			{"scram-sha-1", "SCRAM-SHA-1"},
    79  			{"scram-sha-256", "SCRAM-SHA-256"},
    80  		}
    81  		for _, tc := range testCases {
    82  			clientOpts := getBaseClientOptions()
    83  			if tc.authMechanism != "" {
    84  				cred := getBaseCredential(mt)
    85  				cred.AuthMechanism = tc.authMechanism
    86  				clientOpts.SetAuth(cred)
    87  			}
    88  			mtOpts := getMtOpts().ClientOptions(clientOpts)
    89  
    90  			mt.RunOpts(tc.name, mtOpts, func(mt *mtest.T) {
    91  				err := mt.Client.Ping(context.Background(), mtest.PrimaryRp)
    92  				assert.Nil(mt, err, "Ping error: %v", err)
    93  			})
    94  		}
    95  	})
    96  }
    97  
    98  func getBaseClientOptions() *options.ClientOptions {
    99  	opts := options.Client().ApplyURI(mtest.ClusterURI())
   100  	return options.Client().SetHosts(opts.Hosts)
   101  }
   102  
   103  func getBaseCredential(mt *mtest.T) options.Credential {
   104  	mt.Helper()
   105  
   106  	cred := options.Client().ApplyURI(mtest.ClusterURI()).Auth
   107  	assert.NotNil(mt, cred, "expected options for URI %q to have a non-nil Auth field", mtest.ClusterURI())
   108  	return *cred
   109  }
   110  

View as plain text