...

Source file src/go.mongodb.org/mongo-driver/x/mongo/driver/integration/main_test.go

Documentation: go.mongodb.org/mongo-driver/x/mongo/driver/integration

     1  // Copyright (C) MongoDB, Inc. 2022-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 integration
     8  
     9  import (
    10  	"context"
    11  	"flag"
    12  	"fmt"
    13  	"os"
    14  	"strings"
    15  	"testing"
    16  
    17  	"go.mongodb.org/mongo-driver/internal/integtest"
    18  	"go.mongodb.org/mongo-driver/internal/require"
    19  	"go.mongodb.org/mongo-driver/mongo/description"
    20  	"go.mongodb.org/mongo-driver/mongo/writeconcern"
    21  	"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
    22  	"go.mongodb.org/mongo-driver/x/mongo/driver"
    23  	"go.mongodb.org/mongo-driver/x/mongo/driver/auth"
    24  	"go.mongodb.org/mongo-driver/x/mongo/driver/connstring"
    25  	"go.mongodb.org/mongo-driver/x/mongo/driver/operation"
    26  	"go.mongodb.org/mongo-driver/x/mongo/driver/topology"
    27  )
    28  
    29  var host *string
    30  var connectionString *connstring.ConnString
    31  var dbName string
    32  
    33  func TestMain(m *testing.M) {
    34  	flag.Parse()
    35  
    36  	mongodbURI := os.Getenv("MONGODB_URI")
    37  	if mongodbURI == "" {
    38  		mongodbURI = "mongodb://localhost:27017"
    39  	}
    40  
    41  	mongodbURI = addTLSConfigToURI(mongodbURI)
    42  	mongodbURI = addCompressorToURI(mongodbURI)
    43  
    44  	var err error
    45  	connectionString, err = connstring.ParseAndValidate(mongodbURI)
    46  	if err != nil {
    47  		fmt.Printf("Could not parse connection string: %v\n", err)
    48  		os.Exit(1)
    49  	}
    50  
    51  	host = &connectionString.Hosts[0]
    52  
    53  	dbName = fmt.Sprintf("mongo-go-driver-%d", os.Getpid())
    54  	if connectionString.Database != "" {
    55  		dbName = connectionString.Database
    56  	}
    57  	os.Exit(m.Run())
    58  }
    59  
    60  func noerr(t *testing.T, err error) {
    61  	if err != nil {
    62  		t.Helper()
    63  		t.Errorf("Unexpected error: %v", err)
    64  		t.FailNow()
    65  	}
    66  }
    67  
    68  func autherr(t *testing.T, err error) {
    69  	t.Helper()
    70  	switch e := err.(type) {
    71  	case topology.ConnectionError:
    72  		if _, ok := e.Wrapped.(*auth.Error); !ok {
    73  			t.Fatal("Expected auth error and didn't get one")
    74  		}
    75  	case *auth.Error:
    76  		return
    77  	default:
    78  		t.Fatal("Expected auth error and didn't get one")
    79  	}
    80  }
    81  
    82  // addTLSConfigToURI checks for the environmental variable indicating that the tests are being run
    83  // on an SSL-enabled server, and if so, returns a new URI with the necessary configuration.
    84  func addTLSConfigToURI(uri string) string {
    85  	caFile := os.Getenv("MONGO_GO_DRIVER_CA_FILE")
    86  	if len(caFile) == 0 {
    87  		return uri
    88  	}
    89  
    90  	if !strings.ContainsRune(uri, '?') {
    91  		if uri[len(uri)-1] != '/' {
    92  			uri += "/"
    93  		}
    94  
    95  		uri += "?"
    96  	} else {
    97  		uri += "&"
    98  	}
    99  
   100  	return uri + "ssl=true&sslCertificateAuthorityFile=" + caFile
   101  }
   102  
   103  func addCompressorToURI(uri string) string {
   104  	comp := os.Getenv("MONGO_GO_DRIVER_COMPRESSOR")
   105  	if len(comp) == 0 {
   106  		return uri
   107  	}
   108  
   109  	if !strings.ContainsRune(uri, '?') {
   110  		if uri[len(uri)-1] != '/' {
   111  			uri += "/"
   112  		}
   113  
   114  		uri += "?"
   115  	} else {
   116  		uri += "&"
   117  	}
   118  
   119  	return uri + "compressors=" + comp
   120  }
   121  
   122  // runCommand runs an arbitrary command on a given database of target server
   123  func runCommand(s driver.Server, db string, cmd bsoncore.Document) (bsoncore.Document, error) {
   124  	op := operation.NewCommand(cmd).
   125  		Database(db).Deployment(driver.SingleServerDeployment{Server: s})
   126  	err := op.Execute(context.Background())
   127  	res := op.Result()
   128  	return res, err
   129  }
   130  
   131  // dropCollection drops the collection in the test cluster.
   132  func dropCollection(t *testing.T, dbname, colname string) {
   133  	err := operation.NewCommand(bsoncore.BuildDocument(nil, bsoncore.AppendStringElement(nil, "drop", colname))).
   134  		Database(dbname).ServerSelector(description.WriteSelector()).Deployment(integtest.Topology(t)).
   135  		Execute(context.Background())
   136  	if de, ok := err.(driver.Error); err != nil && !(ok && de.NamespaceNotFound()) {
   137  		require.NoError(t, err)
   138  	}
   139  }
   140  
   141  // autoInsertDocs inserts the docs into the test cluster.
   142  func autoInsertDocs(t *testing.T, writeConcern *writeconcern.WriteConcern, docs ...bsoncore.Document) {
   143  	insertDocs(t, integtest.DBName(t), integtest.ColName(t), writeConcern, docs...)
   144  }
   145  
   146  // insertDocs inserts the docs into the test cluster.
   147  func insertDocs(t *testing.T, dbname, colname string, writeConcern *writeconcern.WriteConcern, docs ...bsoncore.Document) {
   148  	err := operation.NewInsert(docs...).
   149  		Collection(colname).
   150  		Database(dbname).
   151  		Deployment(integtest.Topology(t)).
   152  		ServerSelector(description.WriteSelector()).
   153  		WriteConcern(writeConcern).
   154  		Execute(context.Background())
   155  	require.NoError(t, err)
   156  }
   157  

View as plain text