forked from mongodb/mongo-go-driver
/
config.go
121 lines (99 loc) · 2.8 KB
/
config.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
// 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 testutil
import (
"fmt"
"os"
"reflect"
"strings"
"sync"
"testing"
"github.com/mongodb/mongo-go-driver/mongo/connstring"
"github.com/mongodb/mongo-go-driver/mongo/private/cluster"
)
var connectionString connstring.ConnString
var connectionStringOnce sync.Once
var connectionStringErr error
var liveCluster *cluster.Cluster
var liveClusterOnce sync.Once
var liveClusterErr error
// 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
}
if !strings.ContainsRune(uri, '?') {
if uri[len(uri)-1] != '/' {
uri += "/"
}
uri += "?"
} else {
uri += "&"
}
return uri + "ssl=true&sslCertificateAuthorityFile=" + caFile
}
// Cluster gets the globally configured cluster.
func Cluster(t *testing.T) *cluster.Cluster {
cs := ConnString(t)
liveClusterOnce.Do(func() {
var err error
liveCluster, err = cluster.New(cluster.WithConnString(cs))
if err != nil {
liveClusterErr = err
} else {
autoDropDB(t, liveCluster)
}
})
if liveClusterErr != nil {
t.Fatal(liveClusterErr)
}
return liveCluster
}
// 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()
}
// ConnString gets the globally configured connection string.
func ConnString(t *testing.T) connstring.ConnString {
connectionStringOnce.Do(func() {
mongodbURI := os.Getenv("MONGODB_URI")
if mongodbURI == "" {
mongodbURI = "mongodb://localhost:27017"
}
mongodbURI = AddTLSConfigToURI(mongodbURI)
var err error
connectionString, err = connstring.Parse(mongodbURI)
if err != nil {
connectionStringErr = err
}
})
if connectionStringErr != nil {
t.Fatal(connectionStringErr)
}
return connectionString
}
// DBName gets the globally configured database name.
func DBName(t *testing.T) string {
cs := ConnString(t)
if cs.Database != "" {
return cs.Database
}
return fmt.Sprintf("mongo-go-driver-%d", os.Getpid())
}
// Integration should be called at the beginning of integration
// tests to ensure that they are skipped if integration testing is
// turned off.
func Integration(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test in short mode")
}
}