Skip to content

Commit 195240c

Browse files
GODRIVER-1530 Deprecate topology.WithConnString, which duplicates logic (#1044)
1 parent 78f5014 commit 195240c

File tree

22 files changed

+640
-1194
lines changed

22 files changed

+640
-1194
lines changed

examples/documentation_examples/examples_test.go

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import (
1919
"go.mongodb.org/mongo-driver/mongo"
2020
"go.mongodb.org/mongo-driver/mongo/integration/mtest"
2121
"go.mongodb.org/mongo-driver/mongo/options"
22-
"go.mongodb.org/mongo-driver/x/mongo/driver/connstring"
23-
"go.mongodb.org/mongo-driver/x/mongo/driver/topology"
2422
)
2523

2624
func TestMain(m *testing.M) {
@@ -118,18 +116,18 @@ func TestDocumentationExamples(t *testing.T) {
118116
// Transaction examples can only run on replica sets on 4.0+.
119117
mtOpts = mtest.NewOptions().MinServerVersion("4.0").Topologies(mtest.ReplicaSet)
120118
mt.RunOpts("TransactionsExamples", mtOpts, func(mt *mtest.T) {
121-
mt.ResetClient(&options.ClientOptions{Deployment: createTopology(mt)})
119+
mt.ResetClient(options.Client().ApplyURI(mtest.ClusterURI()))
122120
documentation_examples.TransactionsExamples(ctx, mt.Client)
123121
})
124122
mt.RunOpts("WithTransactionExample", mtOpts, func(mt *mtest.T) {
125-
mt.ResetClient(&options.ClientOptions{Deployment: createTopology(mt)})
123+
mt.ResetClient(options.Client().ApplyURI(mtest.ClusterURI()))
126124
documentation_examples.WithTransactionExample(ctx)
127125
})
128126

129127
// Change stream examples can only run on replica sets on 3.6+.
130128
mtOpts = mtest.NewOptions().MinServerVersion("3.6").Topologies(mtest.ReplicaSet)
131129
mt.RunOpts("ChangeStreamExamples", mtOpts, func(mt *mtest.T) {
132-
mt.ResetClient(&options.ClientOptions{Deployment: createTopology(mt)})
130+
mt.ResetClient(options.Client().ApplyURI(mtest.ClusterURI()))
133131
csdb := client.Database("changestream_examples")
134132
documentation_examples.ChangeStreamExamples(mt.T, csdb)
135133
})
@@ -145,13 +143,3 @@ func TestDocumentationExamples(t *testing.T) {
145143
documentation_examples.CausalConsistencyExamples(mt.Client)
146144
})
147145
}
148-
149-
func createTopology(mt *mtest.T) *topology.Topology {
150-
topo, err := topology.New(topology.WithConnString(func(connstring.ConnString) connstring.ConnString {
151-
return mtest.ClusterConnString()
152-
}))
153-
if err != nil {
154-
mt.Fatalf("topology.New error: %v", err)
155-
}
156-
return topo
157-
}

internal/testutil/config.go

Lines changed: 69 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ import (
2020
"github.com/stretchr/testify/require"
2121
"go.mongodb.org/mongo-driver/event"
2222
"go.mongodb.org/mongo-driver/mongo/description"
23+
"go.mongodb.org/mongo-driver/mongo/options"
2324
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
2425
"go.mongodb.org/mongo-driver/x/mongo/driver/connstring"
25-
"go.mongodb.org/mongo-driver/x/mongo/driver/ocsp"
2626
"go.mongodb.org/mongo-driver/x/mongo/driver/operation"
2727
"go.mongodb.org/mongo-driver/x/mongo/driver/topology"
2828
)
@@ -77,28 +77,16 @@ func AddCompressorToURI(uri string) string {
7777

7878
// MonitoredTopology returns a new topology with the command monitor attached
7979
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)
9987
}
10088

101-
monitoredTopology, err := topology.New(opts...)
89+
monitoredTopology, err := topology.New(cfg)
10290
if err != nil {
10391
t.Fatal(err)
10492
} else {
@@ -115,27 +103,14 @@ func MonitoredTopology(t *testing.T, dbName string, monitor *event.CommandMonito
115103

116104
// Topology gets the globally configured topology.
117105
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)
135110

136111
liveTopologyOnce.Do(func() {
137112
var err error
138-
liveTopology, err = topology.New(opts...)
113+
liveTopology, err = topology.New(cfg)
139114
if err != nil {
140115
liveTopologyErr = err
141116
} else {
@@ -154,27 +129,17 @@ func Topology(t *testing.T) *topology.Topology {
154129
return liveTopology
155130
}
156131

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)
175137
}
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)
178143
if err != nil {
179144
t.Fatal("Could not construct topology")
180145
}
@@ -194,20 +159,55 @@ func ColName(t *testing.T) string {
194159
return name.String()
195160
}
196161

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+
197204
// ConnString gets the globally configured connection string.
198205
func ConnString(t *testing.T) connstring.ConnString {
199206
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)
208209

209-
var err error
210-
connectionString, err = connstring.ParseAndValidate(mongodbURI)
210+
connectionString, err = connstring.ParseAndValidate(uri)
211211
if err != nil {
212212
connectionStringErr = err
213213
}

0 commit comments

Comments
 (0)