...
1
2
3
4
5
6
7 package main
8
9 import (
10 "context"
11 "errors"
12 "flag"
13 "fmt"
14 "time"
15
16 "go.mongodb.org/mongo-driver/bson"
17 "go.mongodb.org/mongo-driver/internal/handshake"
18 "go.mongodb.org/mongo-driver/mongo"
19 "go.mongodb.org/mongo-driver/mongo/options"
20 )
21
22 func main() {
23 flag.Parse()
24 uris := flag.Args()
25 ctx := context.Background()
26
27 fmt.Printf("Running atlas tests for %d uris\n", len(uris))
28
29 for idx, uri := range uris {
30 fmt.Printf("Running test %d\n", idx)
31
32
33 clientOpts := options.Client().
34 ApplyURI(uri).
35 SetServerSelectionTimeout(1 * time.Second)
36
37
38 if err := runTest(ctx, clientOpts); err != nil {
39 panic(fmt.Sprintf("error running test with TLS at index %d: %v", idx, err))
40 }
41
42
43
44 clientOpts.TLSConfig.InsecureSkipVerify = true
45 if err := runTest(ctx, clientOpts); err != nil {
46 panic(fmt.Sprintf("error running test with tlsInsecure at index %d: %v", idx, err))
47 }
48 }
49
50 fmt.Println("Finished!")
51 }
52
53 func runTest(ctx context.Context, clientOpts *options.ClientOptions) error {
54 client, err := mongo.Connect(ctx, clientOpts)
55 if err != nil {
56 return fmt.Errorf("Connect error: %w", err)
57 }
58
59 defer func() {
60 _ = client.Disconnect(ctx)
61 }()
62
63 db := client.Database("test")
64 cmd := bson.D{{handshake.LegacyHello, 1}}
65 err = db.RunCommand(ctx, cmd).Err()
66 if err != nil {
67 return fmt.Errorf("legacy hello error: %w", err)
68 }
69
70 coll := db.Collection("test")
71 if err = coll.FindOne(ctx, bson.D{{"x", 1}}).Err(); err != nil && !errors.Is(err, mongo.ErrNoDocuments) {
72 return fmt.Errorf("FindOne error: %w", err)
73 }
74 return nil
75 }
76
View as plain text