...

Source file src/go.mongodb.org/mongo-driver/cmd/testatlas/main.go

Documentation: go.mongodb.org/mongo-driver/cmd/testatlas

     1  // Copyright (C) MongoDB, Inc. 2017-present.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"); you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
     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  		// Set a low server selection timeout so we fail fast if there are errors.
    33  		clientOpts := options.Client().
    34  			ApplyURI(uri).
    35  			SetServerSelectionTimeout(1 * time.Second)
    36  
    37  		// Run basic connectivity test.
    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  		// Run the connectivity test with InsecureSkipVerify to ensure SNI is done correctly even if verification is
    43  		// disabled.
    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