@@ -10,6 +10,7 @@ import (
10
10
"context"
11
11
"flag"
12
12
"fmt"
13
+ "time"
13
14
14
15
"go.mongodb.org/mongo-driver/bson"
15
16
"go.mongodb.org/mongo-driver/mongo"
@@ -22,34 +23,45 @@ func main() {
22
23
ctx := context .Background ()
23
24
24
25
for idx , uri := range uris {
25
- client , err := mongo . Connect ( ctx , options . Client (). ApplyURI ( uri ))
26
- if err != nil {
27
- panic ( createErrorMessage ( idx , "Connect error: %v" , err ))
28
- }
26
+ // Set a low server selection timeout so we fail fast if there are errors.
27
+ clientOpts := options . Client ().
28
+ ApplyURI ( uri ).
29
+ SetServerSelectionTimeout ( 1 * time . Second )
29
30
30
- defer func () {
31
- if err = client .Disconnect (ctx ); err != nil {
32
- panic (createErrorMessage (idx , "Disconnect error: %v" , err ))
33
- }
34
- }()
35
-
36
- db := client .Database ("test" )
37
- err = db .RunCommand (
38
- ctx ,
39
- bson.D {{"isMaster" , 1 }},
40
- ).Err ()
41
- if err != nil {
42
- panic (createErrorMessage (idx , "isMaster error: %v" , err ))
31
+ // Run basic connectivity test.
32
+ if err := runTest (ctx , clientOpts ); err != nil {
33
+ panic (fmt .Sprintf ("error running test with TLS at index %d: %v" , idx , err ))
43
34
}
44
35
45
- coll := db .Collection ("test" )
46
- if err = coll .FindOne (ctx , bson.D {{"x" , 1 }}).Err (); err != nil && err != mongo .ErrNoDocuments {
47
- panic (createErrorMessage (idx , "FindOne error: %v" , err ))
36
+ // Run the connectivity test with InsecureSkipVerify to ensure SNI is done correctly even if verification is
37
+ // disabled.
38
+ clientOpts .TLSConfig .InsecureSkipVerify = true
39
+ if err := runTest (ctx , clientOpts ); err != nil {
40
+ panic (fmt .Sprintf ("error running test with tlsInsecure at index %d: %v" , idx , err ))
48
41
}
49
42
}
50
43
}
51
44
52
- func createErrorMessage (idx int , msg string , args ... interface {}) string {
53
- msg = fmt .Sprintf (msg , args ... )
54
- return fmt .Sprintf ("error for URI at index %d: %s" , idx , msg )
45
+ func runTest (ctx context.Context , clientOpts * options.ClientOptions ) error {
46
+ client , err := mongo .Connect (ctx , clientOpts )
47
+ if err != nil {
48
+ return fmt .Errorf ("Connect error: %v" , err )
49
+ }
50
+
51
+ defer func () {
52
+ _ = client .Disconnect (ctx )
53
+ }()
54
+
55
+ db := client .Database ("test" )
56
+ cmd := bson.D {{"isMaster" , 1 }}
57
+ err = db .RunCommand (ctx , cmd ).Err ()
58
+ if err != nil {
59
+ return fmt .Errorf ("isMaster error: %v" , err )
60
+ }
61
+
62
+ coll := db .Collection ("test" )
63
+ if err = coll .FindOne (ctx , bson.D {{"x" , 1 }}).Err (); err != nil && err != mongo .ErrNoDocuments {
64
+ return fmt .Errorf ("FindOne error: %v" , err )
65
+ }
66
+ return nil
55
67
}
0 commit comments