4
4
// not use this file except in compliance with the License. You may obtain
5
5
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
6
7
+ //go:build atlastest
8
+ // +build atlastest
9
+
7
10
package main
8
11
9
12
import (
10
13
"context"
14
+ "crypto/tls"
15
+ "encoding/base64"
11
16
"errors"
12
- "flag"
13
17
"fmt"
18
+ "net/url"
14
19
"os"
20
+ "path/filepath"
15
21
"testing"
16
22
"time"
17
23
18
24
"go.mongodb.org/mongo-driver/v2/bson"
25
+ "go.mongodb.org/mongo-driver/v2/internal/assert"
19
26
"go.mongodb.org/mongo-driver/v2/internal/handshake"
27
+ "go.mongodb.org/mongo-driver/v2/internal/require"
20
28
"go.mongodb.org/mongo-driver/v2/mongo"
21
29
"go.mongodb.org/mongo-driver/v2/mongo/options"
22
30
)
23
31
24
- func TestMain (m * testing.M ) {
25
- flag .Parse ()
26
- os .Exit (m .Run ())
27
- }
28
-
29
32
func TestAtlas (t * testing.T ) {
30
- uris := flag .Args ()
31
- ctx := context .Background ()
32
-
33
- t .Logf ("Running atlas tests for %d uris\n " , len (uris ))
34
-
35
- for idx , uri := range uris {
36
- t .Logf ("Running test %d\n " , idx )
37
-
38
- // Set a low server selection timeout so we fail fast if there are errors.
39
- clientOpts := options .Client ().
40
- ApplyURI (uri ).
41
- SetServerSelectionTimeout (1 * time .Second )
42
-
43
- // Run basic connectivity test.
44
- if err := runTest (ctx , clientOpts ); err != nil {
45
- t .Fatalf ("error running test with TLS at index %d: %v" , idx , err )
46
- }
47
-
48
- tlsConfigSkipVerify := clientOpts .TLSConfig
49
- tlsConfigSkipVerify .InsecureSkipVerify = true
50
-
51
- // Run the connectivity test with InsecureSkipVerify to ensure SNI is done correctly even if verification is
52
- // disabled.
53
- clientOpts .SetTLSConfig (tlsConfigSkipVerify )
54
-
55
- if err := runTest (ctx , clientOpts ); err != nil {
56
- t .Fatalf ("error running test with tlsInsecure at index %d: %v" , idx , err )
57
- }
33
+ cases := []struct {
34
+ name string
35
+ envVar string
36
+ certKeyFile string
37
+ wantErr string
38
+ }{
39
+ {
40
+ name : "Atlas with TLS" ,
41
+ envVar : "ATLAS_REPL" ,
42
+ certKeyFile : "" ,
43
+ wantErr : "" ,
44
+ },
45
+ {
46
+ name : "Atlas with TLS and shared cluster" ,
47
+ envVar : "ATLAS_SHRD" ,
48
+ certKeyFile : "" ,
49
+ wantErr : "" ,
50
+ },
51
+ {
52
+ name : "Atlas with free tier" ,
53
+ envVar : "ATLAS_FREE" ,
54
+ certKeyFile : "" ,
55
+ wantErr : "" ,
56
+ },
57
+ {
58
+ name : "Atlas with TLS 1.1" ,
59
+ envVar : "ATLAS_TLS11" ,
60
+ certKeyFile : "" ,
61
+ wantErr : "" ,
62
+ },
63
+ {
64
+ name : "Atlas with TLS 1.2" ,
65
+ envVar : "ATLAS_TLS12" ,
66
+ certKeyFile : "" ,
67
+ wantErr : "" ,
68
+ },
69
+ {
70
+ name : "Atlas with serverless" ,
71
+ envVar : "ATLAS_SERVERLESS" ,
72
+ certKeyFile : "" ,
73
+ wantErr : "" ,
74
+ },
75
+ {
76
+ name : "Atlas with srv file on replica set" ,
77
+ envVar : "ATLAS_SRV_REPL" ,
78
+ certKeyFile : "" ,
79
+ wantErr : "" ,
80
+ },
81
+ {
82
+ name : "Atlas with srv file on shared cluster" ,
83
+ envVar : "ATLAS_SRV_SHRD" ,
84
+ certKeyFile : "" ,
85
+ wantErr : "" ,
86
+ },
87
+ {
88
+ name : "Atlas with srv file on free tier" ,
89
+ envVar : "ATLAS_SRV_FREE" ,
90
+ certKeyFile : "" ,
91
+ wantErr : "" ,
92
+ },
93
+ {
94
+ name : "Atlas with srv file on TLS 1.1" ,
95
+ envVar : "ATLAS_SRV_TLS11" ,
96
+ certKeyFile : "" ,
97
+ wantErr : "" ,
98
+ },
99
+ {
100
+ name : "Atlas with srv file on TLS 1.2" ,
101
+ envVar : "ATLAS_SRV_TLS12" ,
102
+ certKeyFile : "" ,
103
+ wantErr : "" ,
104
+ },
105
+ {
106
+ name : "Atlas with srv file on serverless" ,
107
+ envVar : "ATLAS_SRV_SERVERLESS" ,
108
+ certKeyFile : "" ,
109
+ wantErr : "" ,
110
+ },
111
+ {
112
+ name : "Atlas with X509 Dev" ,
113
+ envVar : "ATLAS_X509_DEV" ,
114
+ certKeyFile : createAtlasX509DevCertKeyFile (t ),
115
+ wantErr : "" ,
116
+ },
117
+ {
118
+ name : "Atlas with X509 Dev no user" ,
119
+ envVar : "ATLAS_X509_DEV" ,
120
+ certKeyFile : createAtlasX509DevCertKeyFileNoUser (t ),
121
+ wantErr : "UserNotFound" ,
122
+ },
58
123
}
59
124
60
- t .Logf ("Finished!" )
125
+ for _ , tc := range cases {
126
+ t .Run (fmt .Sprintf ("%s (%s)" , tc .name , tc .envVar ), func (t * testing.T ) {
127
+ uri := os .Getenv (tc .envVar )
128
+ require .NotEmpty (t , uri , "Environment variable %s is not set" , tc .envVar )
129
+
130
+ if tc .certKeyFile != "" {
131
+ uri = addTLSCertKeyFile (t , tc .certKeyFile , uri )
132
+ }
133
+
134
+ // Set a low server selection timeout so we fail fast if there are errors.
135
+ clientOpts := options .Client ().
136
+ ApplyURI (uri ).
137
+ SetServerSelectionTimeout (1 * time .Second )
138
+
139
+ // Run basic connectivity test.
140
+ err := runTest (context .Background (), clientOpts )
141
+ if tc .wantErr != "" {
142
+ assert .ErrorContains (t , err , tc .wantErr , "expected error to contain %q" , tc .wantErr )
143
+
144
+ return
145
+ }
146
+ require .NoError (t , err , "error running test with TLS" )
147
+
148
+ orig := clientOpts .TLSConfig
149
+ if orig == nil {
150
+ orig = & tls.Config {}
151
+ }
152
+
153
+ insecure := orig .Clone ()
154
+ insecure .InsecureSkipVerify = true
155
+
156
+ // Run the connectivity test with InsecureSkipVerify to ensure SNI is done
157
+ // correctly even if verification is disabled.
158
+ insecureClientOpts := options .Client ().
159
+ ApplyURI (uri ).
160
+ SetServerSelectionTimeout (1 * time .Second ).
161
+ SetTLSConfig (insecure )
162
+
163
+ err = runTest (context .Background (), insecureClientOpts )
164
+ require .NoError (t , err , "error running test with tlsInsecure" )
165
+ })
166
+ }
61
167
}
62
168
63
169
func runTest (ctx context.Context , clientOpts * options.ClientOptions ) error {
@@ -83,3 +189,51 @@ func runTest(ctx context.Context, clientOpts *options.ClientOptions) error {
83
189
}
84
190
return nil
85
191
}
192
+
193
+ func createAtlasX509DevCertKeyFile (t * testing.T ) string {
194
+ t .Helper ()
195
+
196
+ b64 := os .Getenv ("ATLAS_X509_DEV_CERT_BASE64" )
197
+ assert .NotEmpty (t , b64 , "Environment variable ATLAS_X509_DEV_CERT_BASE64 is not set" )
198
+
199
+ certBytes , err := base64 .StdEncoding .DecodeString (b64 )
200
+ require .NoError (t , err , "failed to decode ATLAS_X509_DEV_CERT_BASE64" )
201
+
202
+ certFilePath := filepath .Join (t .TempDir (), "atlas_x509_dev_cert.pem" )
203
+
204
+ err = os .WriteFile (certFilePath , certBytes , 0600 )
205
+ require .NoError (t , err , "failed to write ATLAS_X509_DEV_CERT_BASE64 to file" )
206
+
207
+ return certFilePath
208
+ }
209
+
210
+ func createAtlasX509DevCertKeyFileNoUser (t * testing.T ) string {
211
+ t .Helper ()
212
+
213
+ b64 := os .Getenv ("ATLAS_X509_DEV_CERT_NOUSER_BASE64" )
214
+ assert .NotEmpty (t , b64 , "Environment variable ATLAS_X509_DEV_CERT_NOUSER_BASE64 is not set" )
215
+
216
+ keyBytes , err := base64 .StdEncoding .DecodeString (b64 )
217
+ require .NoError (t , err , "failed to decode ATLAS_X509_DEV_CERT_NOUSER_BASE64" )
218
+
219
+ keyFilePath := filepath .Join (t .TempDir (), "atlas_x509_dev_cert_no_user.pem" )
220
+
221
+ err = os .WriteFile (keyFilePath , keyBytes , 0600 )
222
+ require .NoError (t , err , "failed to write ATLAS_X509_DEV_CERT_NOUSER_BASE64 to file" )
223
+
224
+ return keyFilePath
225
+ }
226
+
227
+ func addTLSCertKeyFile (t * testing.T , certKeyFile , uri string ) string {
228
+ t .Helper ()
229
+
230
+ u , err := url .Parse (uri )
231
+ require .NoError (t , err , "failed to parse uri" )
232
+
233
+ q := u .Query ()
234
+ q .Set ("tlsCertificateKeyFile" , filepath .ToSlash (certKeyFile ))
235
+
236
+ u .RawQuery = q .Encode ()
237
+
238
+ return u .String ()
239
+ }
0 commit comments