-
Notifications
You must be signed in to change notification settings - Fork 883
/
integtest.go
284 lines (239 loc) · 8.03 KB
/
integtest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Copyright (C) MongoDB, Inc. 2017-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package integtest
import (
"context"
"errors"
"fmt"
"math"
"os"
"reflect"
"strconv"
"strings"
"sync"
"testing"
"go.mongodb.org/mongo-driver/event"
"go.mongodb.org/mongo-driver/internal/require"
"go.mongodb.org/mongo-driver/mongo/description"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
"go.mongodb.org/mongo-driver/x/mongo/driver"
"go.mongodb.org/mongo-driver/x/mongo/driver/connstring"
"go.mongodb.org/mongo-driver/x/mongo/driver/operation"
"go.mongodb.org/mongo-driver/x/mongo/driver/topology"
)
var connectionString *connstring.ConnString
var connectionStringOnce sync.Once
var connectionStringErr error
var liveTopology *topology.Topology
var liveTopologyOnce sync.Once
var liveTopologyErr error
// AddOptionsToURI appends connection string options to a URI.
func AddOptionsToURI(uri string, opts ...string) string {
if !strings.ContainsRune(uri, '?') {
if uri[len(uri)-1] != '/' {
uri += "/"
}
uri += "?"
} else {
uri += "&"
}
for _, opt := range opts {
uri += opt
}
return uri
}
// AddTLSConfigToURI checks for the environmental variable indicating that the tests are being run
// on an SSL-enabled server, and if so, returns a new URI with the necessary configuration.
func AddTLSConfigToURI(uri string) string {
caFile := os.Getenv("MONGO_GO_DRIVER_CA_FILE")
if len(caFile) == 0 {
return uri
}
return AddOptionsToURI(uri, "ssl=true&sslCertificateAuthorityFile=", caFile)
}
// AddCompressorToURI checks for the environment variable indicating that the tests are being run with compression
// enabled. If so, it returns a new URI with the necessary configuration
func AddCompressorToURI(uri string) string {
comp := os.Getenv("MONGO_GO_DRIVER_COMPRESSOR")
if len(comp) == 0 {
return uri
}
return AddOptionsToURI(uri, "compressors=", comp)
}
// AddTestServerAPIVersion adds the latest server API version in a ServerAPIOptions to passed-in opts.
func AddTestServerAPIVersion(opts *options.ClientOptions) {
if os.Getenv("REQUIRE_API_VERSION") == "true" {
opts.SetServerAPIOptions(options.ServerAPI(driver.TestServerAPIVersion))
}
}
// MonitoredTopology returns a new topology with the command monitor attached
func MonitoredTopology(t *testing.T, dbName string, monitor *event.CommandMonitor) *topology.Topology {
uri, err := MongoDBURI()
if err != nil {
t.Fatal(err)
}
cfg, err := topology.NewConfig(options.Client().ApplyURI(uri).SetMonitor(monitor), nil)
if err != nil {
t.Fatal(err)
}
monitoredTopology, err := topology.New(cfg)
if err != nil {
t.Fatal(err)
} else {
_ = monitoredTopology.Connect()
err = operation.NewCommand(bsoncore.BuildDocument(nil, bsoncore.AppendInt32Element(nil, "dropDatabase", 1))).
Database(dbName).ServerSelector(description.WriteSelector()).Deployment(monitoredTopology).Execute(context.Background())
require.NoError(t, err)
}
return monitoredTopology
}
// Topology gets the globally configured topology.
func Topology(t *testing.T) *topology.Topology {
uri, err := MongoDBURI()
require.NoError(t, err, "error constructing mongodb URI: %v", err)
cfg, err := topology.NewConfig(options.Client().ApplyURI(uri), nil)
require.NoError(t, err, "error constructing topology config: %v", err)
liveTopologyOnce.Do(func() {
var err error
liveTopology, err = topology.New(cfg)
if err != nil {
liveTopologyErr = err
} else {
_ = liveTopology.Connect()
err = operation.NewCommand(bsoncore.BuildDocument(nil, bsoncore.AppendInt32Element(nil, "dropDatabase", 1))).
Database(DBName(t)).ServerSelector(description.WriteSelector()).Deployment(liveTopology).Execute(context.Background())
require.NoError(t, err)
}
})
if liveTopologyErr != nil {
t.Fatal(liveTopologyErr)
}
return liveTopology
}
// TopologyWithCredential takes an "options.Credential" object and returns a connected topology.
func TopologyWithCredential(t *testing.T, credential options.Credential) *topology.Topology {
uri, err := MongoDBURI()
if err != nil {
t.Fatalf("error constructing mongodb URI: %v", err)
}
cfg, err := topology.NewConfig(options.Client().ApplyURI(uri).SetAuth(credential), nil)
if err != nil {
t.Fatalf("error constructing topology config: %v", err)
}
topology, err := topology.New(cfg)
if err != nil {
t.Fatal("Could not construct topology")
}
err = topology.Connect()
if err != nil {
t.Fatal("Could not start topology connection")
}
return topology
}
// ColName gets a collection name that should be unique
// to the currently executing test.
func ColName(t *testing.T) string {
// Get this indirectly to avoid copying a mutex
v := reflect.Indirect(reflect.ValueOf(t))
name := v.FieldByName("name")
return name.String()
}
// MongoDBURI will construct the MongoDB URI from the MONGODB_URI environment variable for testing. The default host is
// "localhost" and the default port is "27017"
func MongoDBURI() (string, error) {
uri := os.Getenv("MONGODB_URI")
if uri == "" {
uri = "mongodb://localhost:27017"
}
uri = AddTLSConfigToURI(uri)
uri = AddCompressorToURI(uri)
uri, err := AddServerlessAuthCredentials(uri)
return uri, err
}
// AddServerlessAuthCredentials will attempt to construct the serverless auth credentials for a URI.
func AddServerlessAuthCredentials(uri string) (string, error) {
if os.Getenv("SERVERLESS") != "serverless" {
return uri, nil
}
user := os.Getenv("SERVERLESS_ATLAS_USER")
if user == "" {
return "", fmt.Errorf("serverless expects SERVERLESS_ATLAS_USER to be set")
}
password := os.Getenv("SERVERLESS_ATLAS_PASSWORD")
if password == "" {
return "", fmt.Errorf("serverless expects SERVERLESS_ATLAS_PASSWORD to be set")
}
var scheme string
// remove the scheme
if strings.HasPrefix(uri, "mongodb+srv://") {
scheme = "mongodb+srv://"
} else if strings.HasPrefix(uri, "mongodb://") {
scheme = "mongodb://"
} else {
return "", errors.New(`scheme must be "mongodb" or "mongodb+srv"`)
}
uri = scheme + user + ":" + password + "@" + uri[len(scheme):]
return uri, nil
}
// ConnString gets the globally configured connection string.
func ConnString(t *testing.T) *connstring.ConnString {
connectionStringOnce.Do(func() {
uri, err := MongoDBURI()
require.NoError(t, err, "error constructing mongodb URI: %v", err)
connectionString, err = connstring.ParseAndValidate(uri)
if err != nil {
connectionStringErr = err
}
})
if connectionStringErr != nil {
t.Fatal(connectionStringErr)
}
return connectionString
}
func GetConnString() (*connstring.ConnString, error) {
mongodbURI := os.Getenv("MONGODB_URI")
if mongodbURI == "" {
mongodbURI = "mongodb://localhost:27017"
}
mongodbURI = AddTLSConfigToURI(mongodbURI)
cs, err := connstring.ParseAndValidate(mongodbURI)
if err != nil {
return nil, err
}
return cs, nil
}
// DBName gets the globally configured database name.
func DBName(t *testing.T) string {
return GetDBName(ConnString(t))
}
func GetDBName(cs *connstring.ConnString) string {
if cs.Database != "" {
return cs.Database
}
return fmt.Sprintf("mongo-go-driver-%d", os.Getpid())
}
// CompareVersions compares two version number strings (i.e. positive integers separated by
// periods). Comparisons are done to the lesser precision of the two versions. For example, 3.2 is
// considered equal to 3.2.11, whereas 3.2.0 is considered less than 3.2.11.
//
// Returns a positive int if version1 is greater than version2, a negative int if version1 is less
// than version2, and 0 if version1 is equal to version2.
func CompareVersions(t *testing.T, v1 string, v2 string) int {
n1 := strings.Split(v1, ".")
n2 := strings.Split(v2, ".")
for i := 0; i < int(math.Min(float64(len(n1)), float64(len(n2)))); i++ {
i1, err := strconv.Atoi(n1[i])
require.NoError(t, err)
i2, err := strconv.Atoi(n2[i])
require.NoError(t, err)
difference := i1 - i2
if difference != 0 {
return difference
}
}
return 0
}