...

Source file src/go.mongodb.org/mongo-driver/mongo/integration/mtest/global_state.go

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

     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 mtest
     8  
     9  import (
    10  	"context"
    11  	"fmt"
    12  
    13  	"go.mongodb.org/mongo-driver/bson"
    14  	"go.mongodb.org/mongo-driver/mongo"
    15  	"go.mongodb.org/mongo-driver/x/mongo/driver/connstring"
    16  	"go.mongodb.org/mongo-driver/x/mongo/driver/topology"
    17  )
    18  
    19  // AuthEnabled returns whether or not the cluster requires auth.
    20  func AuthEnabled() bool {
    21  	return testContext.authEnabled
    22  }
    23  
    24  // SSLEnabled returns whether or not the cluster requires SSL.
    25  func SSLEnabled() bool {
    26  	return testContext.sslEnabled
    27  }
    28  
    29  // ClusterTopologyKind returns the topology kind of the cluster under test.
    30  func ClusterTopologyKind() TopologyKind {
    31  	return testContext.topoKind
    32  }
    33  
    34  // ClusterURI returns the connection string for the cluster.
    35  func ClusterURI() string {
    36  	return testContext.connString.Original
    37  }
    38  
    39  // Serverless returns whether the test is running against a serverless instance.
    40  func Serverless() bool {
    41  	return testContext.serverless
    42  }
    43  
    44  // SingleMongosLoadBalancerURI returns the URI for a load balancer fronting a single mongos. This will only be set
    45  // if the cluster is load balanced.
    46  func SingleMongosLoadBalancerURI() string {
    47  	return testContext.singleMongosLoadBalancerURI
    48  }
    49  
    50  // MultiMongosLoadBalancerURI returns the URI for a load balancer fronting multiple mongoses. This will only be set
    51  // if the cluster is load balanced.
    52  func MultiMongosLoadBalancerURI() string {
    53  	return testContext.multiMongosLoadBalancerURI
    54  }
    55  
    56  // ClusterConnString returns the parsed ConnString for the cluster.
    57  func ClusterConnString() *connstring.ConnString {
    58  	return testContext.connString
    59  }
    60  
    61  // GlobalClient returns a Client connected to the cluster configured with read concern majority, write concern majority,
    62  // and read preference primary.
    63  func GlobalClient() *mongo.Client {
    64  	return testContext.client
    65  }
    66  
    67  // GlobalTopology returns a Topology that's connected to the cluster.
    68  func GlobalTopology() *topology.Topology {
    69  	return testContext.topo
    70  }
    71  
    72  // ServerVersion returns the server version of the cluster. This assumes that all nodes in the cluster have the same
    73  // version.
    74  func ServerVersion() string {
    75  	return testContext.serverVersion
    76  }
    77  
    78  // SetFailPoint configures the provided fail point on the cluster under test using the provided Client.
    79  func SetFailPoint(fp FailPoint, client *mongo.Client) error {
    80  	admin := client.Database("admin")
    81  	if err := admin.RunCommand(context.Background(), fp).Err(); err != nil {
    82  		return fmt.Errorf("error creating fail point: %w", err)
    83  	}
    84  	return nil
    85  }
    86  
    87  // SetRawFailPoint configures the fail point represented by the fp parameter on the cluster under test using the
    88  // provided Client
    89  func SetRawFailPoint(fp bson.Raw, client *mongo.Client) error {
    90  	admin := client.Database("admin")
    91  	if err := admin.RunCommand(context.Background(), fp).Err(); err != nil {
    92  		return fmt.Errorf("error creating fail point: %w", err)
    93  	}
    94  	return nil
    95  }
    96  

View as plain text