@@ -20,9 +20,9 @@ import (
20
20
"github.com/stretchr/testify/require"
21
21
"go.mongodb.org/mongo-driver/event"
22
22
"go.mongodb.org/mongo-driver/mongo/description"
23
+ "go.mongodb.org/mongo-driver/mongo/options"
23
24
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
24
25
"go.mongodb.org/mongo-driver/x/mongo/driver/connstring"
25
- "go.mongodb.org/mongo-driver/x/mongo/driver/ocsp"
26
26
"go.mongodb.org/mongo-driver/x/mongo/driver/operation"
27
27
"go.mongodb.org/mongo-driver/x/mongo/driver/topology"
28
28
)
@@ -77,28 +77,16 @@ func AddCompressorToURI(uri string) string {
77
77
78
78
// MonitoredTopology returns a new topology with the command monitor attached
79
79
func MonitoredTopology (t * testing.T , dbName string , monitor * event.CommandMonitor ) * topology.Topology {
80
- cs := ConnString (t )
81
- opts := []topology.Option {
82
- topology .WithConnString (func (connstring.ConnString ) connstring.ConnString { return cs }),
83
- topology .WithServerOptions (func (opts ... topology.ServerOption ) []topology.ServerOption {
84
- return append (
85
- opts ,
86
- topology .WithConnectionOptions (func (opts ... topology.ConnectionOption ) []topology.ConnectionOption {
87
- return append (
88
- opts ,
89
- topology .WithMonitor (func (* event.CommandMonitor ) * event.CommandMonitor {
90
- return monitor
91
- }),
92
- topology .WithOCSPCache (func (ocsp.Cache ) ocsp.Cache {
93
- return ocsp .NewCache ()
94
- }),
95
- )
96
- }),
97
- )
98
- }),
80
+ uri , err := MongoDBURI ()
81
+ if err != nil {
82
+ t .Fatal (err )
83
+ }
84
+ cfg , err := topology .NewConfig (options .Client ().ApplyURI (uri ).SetMonitor (monitor ), nil )
85
+ if err != nil {
86
+ t .Fatal (err )
99
87
}
100
88
101
- monitoredTopology , err := topology .New (opts ... )
89
+ monitoredTopology , err := topology .New (cfg )
102
90
if err != nil {
103
91
t .Fatal (err )
104
92
} else {
@@ -115,27 +103,14 @@ func MonitoredTopology(t *testing.T, dbName string, monitor *event.CommandMonito
115
103
116
104
// Topology gets the globally configured topology.
117
105
func Topology (t * testing.T ) * topology.Topology {
118
- cs := ConnString (t )
119
- opts := []topology.Option {
120
- topology .WithConnString (func (connstring.ConnString ) connstring.ConnString { return cs }),
121
- topology .WithServerOptions (func (opts ... topology.ServerOption ) []topology.ServerOption {
122
- return append (
123
- opts ,
124
- topology .WithConnectionOptions (func (opts ... topology.ConnectionOption ) []topology.ConnectionOption {
125
- return append (
126
- opts ,
127
- topology .WithOCSPCache (func (ocsp.Cache ) ocsp.Cache {
128
- return ocsp .NewCache ()
129
- }),
130
- )
131
- }),
132
- )
133
- }),
134
- }
106
+ uri , err := MongoDBURI ()
107
+ require .NoError (t , err , "error constructing mongodb URI: %v" , err )
108
+ cfg , err := topology .NewConfig (options .Client ().ApplyURI (uri ), nil )
109
+ require .NoError (t , err , "error constructing topology config: %v" , err )
135
110
136
111
liveTopologyOnce .Do (func () {
137
112
var err error
138
- liveTopology , err = topology .New (opts ... )
113
+ liveTopology , err = topology .New (cfg )
139
114
if err != nil {
140
115
liveTopologyErr = err
141
116
} else {
@@ -154,27 +129,17 @@ func Topology(t *testing.T) *topology.Topology {
154
129
return liveTopology
155
130
}
156
131
157
- // TopologyWithConnString takes a connection string and returns a connected
158
- // topology, or else bails out of testing
159
- func TopologyWithConnString (t * testing.T , cs connstring.ConnString ) * topology.Topology {
160
- opts := []topology.Option {
161
- topology .WithConnString (func (connstring.ConnString ) connstring.ConnString { return cs }),
162
- topology .WithServerOptions (func (opts ... topology.ServerOption ) []topology.ServerOption {
163
- return append (
164
- opts ,
165
- topology .WithConnectionOptions (func (opts ... topology.ConnectionOption ) []topology.ConnectionOption {
166
- return append (
167
- opts ,
168
- topology .WithOCSPCache (func (ocsp.Cache ) ocsp.Cache {
169
- return ocsp .NewCache ()
170
- }),
171
- )
172
- }),
173
- )
174
- }),
132
+ // TopologyWithCredential takes an "options.Credential" object and returns a connected topology.
133
+ func TopologyWithCredential (t * testing.T , credential options.Credential ) * topology.Topology {
134
+ uri , err := MongoDBURI ()
135
+ if err != nil {
136
+ t .Fatalf ("error constructing mongodb URI: %v" , err )
175
137
}
176
-
177
- topology , err := topology .New (opts ... )
138
+ cfg , err := topology .NewConfig (options .Client ().ApplyURI (uri ).SetAuth (credential ), nil )
139
+ if err != nil {
140
+ t .Fatalf ("error constructing topology config: %v" , err )
141
+ }
142
+ topology , err := topology .New (cfg )
178
143
if err != nil {
179
144
t .Fatal ("Could not construct topology" )
180
145
}
@@ -194,20 +159,55 @@ func ColName(t *testing.T) string {
194
159
return name .String ()
195
160
}
196
161
162
+ // MongoDBURI will construct the MongoDB URI from the MONGODB_URI environment variable for testing. The default host is
163
+ // "localhost" and the default port is "27017"
164
+ func MongoDBURI () (string , error ) {
165
+ uri := os .Getenv ("MONGODB_URI" )
166
+ if uri == "" {
167
+ uri = "mongodb://localhost:27017"
168
+ }
169
+
170
+ uri = AddTLSConfigToURI (uri )
171
+ uri = AddCompressorToURI (uri )
172
+ uri , err := AddServerlessAuthCredentials (uri )
173
+ return uri , err
174
+ }
175
+
176
+ // AddServerlessAuthCredentials will attempt to construct the serverless auth credentials for a URI.
177
+ func AddServerlessAuthCredentials (uri string ) (string , error ) {
178
+ if os .Getenv ("SERVERLESS" ) != "serverless" {
179
+ return uri , nil
180
+ }
181
+ user := os .Getenv ("SERVERLESS_ATLAS_USER" )
182
+ if user == "" {
183
+ return "" , fmt .Errorf ("serverless expects SERVERLESS_ATLAS_USER to be set" )
184
+ }
185
+ password := os .Getenv ("SERVERLESS_ATLAS_PASSWORD" )
186
+ if password == "" {
187
+ return "" , fmt .Errorf ("serverless expects SERVERLESS_ATLAS_PASSWORD to be set" )
188
+ }
189
+
190
+ var scheme string
191
+ // remove the scheme
192
+ if strings .HasPrefix (uri , "mongodb+srv://" ) {
193
+ scheme = "mongodb+srv://"
194
+ } else if strings .HasPrefix (uri , "mongodb://" ) {
195
+ scheme = "mongodb://"
196
+ } else {
197
+ return "" , fmt .Errorf ("scheme must be \" mongodb\" or \" mongodb+srv\" " )
198
+ }
199
+
200
+ uri = scheme + user + ":" + password + "@" + uri [len (scheme ):]
201
+ return uri , nil
202
+ }
203
+
197
204
// ConnString gets the globally configured connection string.
198
205
func ConnString (t * testing.T ) connstring.ConnString {
199
206
connectionStringOnce .Do (func () {
200
- connectionString , connectionStringErr = GetConnString ()
201
- mongodbURI := os .Getenv ("MONGODB_URI" )
202
- if mongodbURI == "" {
203
- mongodbURI = "mongodb://localhost:27017"
204
- }
205
-
206
- mongodbURI = AddTLSConfigToURI (mongodbURI )
207
- mongodbURI = AddCompressorToURI (mongodbURI )
207
+ uri , err := MongoDBURI ()
208
+ require .NoError (t , err , "error constructing mongodb URI: %v" , err )
208
209
209
- var err error
210
- connectionString , err = connstring .ParseAndValidate (mongodbURI )
210
+ connectionString , err = connstring .ParseAndValidate (uri )
211
211
if err != nil {
212
212
connectionStringErr = err
213
213
}
0 commit comments