Copied mongo repo (to patch it)
This commit is contained in:
137
mongo/internal/testutil/assert/assert.go
Normal file
137
mongo/internal/testutil/assert/assert.go
Normal file
@@ -0,0 +1,137 @@
|
||||
// Copyright (C) MongoDB, Inc. 2017-present.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
// not use this file except in compliance with the License. You may obtain
|
||||
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
package assert
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
)
|
||||
|
||||
var cmpOpts sync.Map
|
||||
var errorCompareFn = func(e1, e2 error) bool {
|
||||
if e1 == nil || e2 == nil {
|
||||
return e1 == nil && e2 == nil
|
||||
}
|
||||
|
||||
return e1.Error() == e2.Error()
|
||||
}
|
||||
var errorCompareOpts = cmp.Options{cmp.Comparer(errorCompareFn)}
|
||||
|
||||
// RegisterOpts registers go-cmp options for a type. These options will be used when comparing two objects for equality.
|
||||
func RegisterOpts(t reflect.Type, opts ...cmp.Option) {
|
||||
cmpOpts.Store(t, cmp.Options(opts))
|
||||
}
|
||||
|
||||
// Equal compares first and second for equality. The objects must be of the same type.
|
||||
// If the objects are not equal, the test will be failed with an error message containing msg and args.
|
||||
func Equal(t testing.TB, first, second interface{}, msg string, args ...interface{}) {
|
||||
t.Helper()
|
||||
if !cmp.Equal(first, second, getCmpOpts(first)...) {
|
||||
t.Fatalf(msg, args...)
|
||||
}
|
||||
}
|
||||
|
||||
// NotEqual compares first and second for inequality. The objects must be of the same type.
|
||||
func NotEqual(t testing.TB, first, second interface{}, msg string, args ...interface{}) {
|
||||
t.Helper()
|
||||
if cmp.Equal(first, second, getCmpOpts(first)...) {
|
||||
t.Fatalf(msg, args...)
|
||||
}
|
||||
}
|
||||
|
||||
// True asserts that the obj parameter is a boolean with value true.
|
||||
func True(t testing.TB, obj interface{}, msg string, args ...interface{}) {
|
||||
t.Helper()
|
||||
b, ok := obj.(bool)
|
||||
if !ok || !b {
|
||||
t.Fatalf(msg, args...)
|
||||
}
|
||||
}
|
||||
|
||||
// False asserts that the obj parameter is a boolean with value false.
|
||||
func False(t testing.TB, obj interface{}, msg string, args ...interface{}) {
|
||||
t.Helper()
|
||||
b, ok := obj.(bool)
|
||||
if !ok || b {
|
||||
t.Fatalf(msg, args...)
|
||||
}
|
||||
}
|
||||
|
||||
// Nil asserts that the obj parameter is nil.
|
||||
func Nil(t testing.TB, obj interface{}, msg string, args ...interface{}) {
|
||||
t.Helper()
|
||||
if !isNil(obj) {
|
||||
t.Fatalf(msg, args...)
|
||||
}
|
||||
}
|
||||
|
||||
// NotNil asserts that the obj parameter is not nil.
|
||||
func NotNil(t testing.TB, obj interface{}, msg string, args ...interface{}) {
|
||||
t.Helper()
|
||||
if isNil(obj) {
|
||||
t.Fatalf(msg, args...)
|
||||
}
|
||||
}
|
||||
|
||||
// Soon runs the provided callback for a maximum of timeoutMS milliseconds. The provided callback
|
||||
// should respect the passed-in context and cease execution when it has expired.
|
||||
func Soon(t testing.TB, callback func(ctx context.Context), timeout time.Duration) {
|
||||
t.Helper()
|
||||
|
||||
// Create context to manually cancel callback after Soon assertion.
|
||||
callbackCtx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
done := make(chan struct{})
|
||||
fullCallback := func() {
|
||||
callback(callbackCtx)
|
||||
done <- struct{}{}
|
||||
}
|
||||
|
||||
timer := time.NewTimer(timeout)
|
||||
defer timer.Stop()
|
||||
|
||||
go fullCallback()
|
||||
|
||||
select {
|
||||
case <-done:
|
||||
return
|
||||
case <-timer.C:
|
||||
t.Fatalf("timed out in %s waiting for callback", timeout)
|
||||
}
|
||||
}
|
||||
|
||||
func getCmpOpts(obj interface{}) cmp.Options {
|
||||
opts, ok := cmpOpts.Load(reflect.TypeOf(obj))
|
||||
if ok {
|
||||
return opts.(cmp.Options)
|
||||
}
|
||||
|
||||
if _, ok := obj.(error); ok {
|
||||
return errorCompareOpts
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func isNil(object interface{}) bool {
|
||||
if object == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
val := reflect.ValueOf(object)
|
||||
switch val.Kind() {
|
||||
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
|
||||
return val.IsNil()
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
275
mongo/internal/testutil/config.go
Normal file
275
mongo/internal/testutil/config.go
Normal file
@@ -0,0 +1,275 @@
|
||||
// Copyright (C) MongoDB, Inc. 2017-present.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
// not use this file except in compliance with the License. You may obtain
|
||||
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
package testutil
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math"
|
||||
"os"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.mongodb.org/mongo-driver/event"
|
||||
"go.mongodb.org/mongo-driver/mongo/description"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
|
||||
"go.mongodb.org/mongo-driver/x/mongo/driver/connstring"
|
||||
"go.mongodb.org/mongo-driver/x/mongo/driver/operation"
|
||||
"go.mongodb.org/mongo-driver/x/mongo/driver/topology"
|
||||
)
|
||||
|
||||
var connectionString connstring.ConnString
|
||||
var connectionStringOnce sync.Once
|
||||
var connectionStringErr error
|
||||
var liveTopology *topology.Topology
|
||||
var liveTopologyOnce sync.Once
|
||||
var liveTopologyErr error
|
||||
|
||||
// AddOptionsToURI appends connection string options to a URI.
|
||||
func AddOptionsToURI(uri string, opts ...string) string {
|
||||
if !strings.ContainsRune(uri, '?') {
|
||||
if uri[len(uri)-1] != '/' {
|
||||
uri += "/"
|
||||
}
|
||||
|
||||
uri += "?"
|
||||
} else {
|
||||
uri += "&"
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
uri += opt
|
||||
}
|
||||
|
||||
return uri
|
||||
}
|
||||
|
||||
// AddTLSConfigToURI checks for the environmental variable indicating that the tests are being run
|
||||
// on an SSL-enabled server, and if so, returns a new URI with the necessary configuration.
|
||||
func AddTLSConfigToURI(uri string) string {
|
||||
caFile := os.Getenv("MONGO_GO_DRIVER_CA_FILE")
|
||||
if len(caFile) == 0 {
|
||||
return uri
|
||||
}
|
||||
|
||||
return AddOptionsToURI(uri, "ssl=true&sslCertificateAuthorityFile=", caFile)
|
||||
}
|
||||
|
||||
// AddCompressorToURI checks for the environment variable indicating that the tests are being run with compression
|
||||
// enabled. If so, it returns a new URI with the necessary configuration
|
||||
func AddCompressorToURI(uri string) string {
|
||||
comp := os.Getenv("MONGO_GO_DRIVER_COMPRESSOR")
|
||||
if len(comp) == 0 {
|
||||
return uri
|
||||
}
|
||||
|
||||
return AddOptionsToURI(uri, "compressors=", comp)
|
||||
}
|
||||
|
||||
// MonitoredTopology returns a new topology with the command monitor attached
|
||||
func MonitoredTopology(t *testing.T, dbName string, monitor *event.CommandMonitor) *topology.Topology {
|
||||
uri, err := MongoDBURI()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg, err := topology.NewConfig(options.Client().ApplyURI(uri).SetMonitor(monitor), nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
monitoredTopology, err := topology.New(cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
} else {
|
||||
_ = monitoredTopology.Connect()
|
||||
|
||||
err = operation.NewCommand(bsoncore.BuildDocument(nil, bsoncore.AppendInt32Element(nil, "dropDatabase", 1))).
|
||||
Database(dbName).ServerSelector(description.WriteSelector()).Deployment(monitoredTopology).Execute(context.Background())
|
||||
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
return monitoredTopology
|
||||
}
|
||||
|
||||
// Topology gets the globally configured topology.
|
||||
func Topology(t *testing.T) *topology.Topology {
|
||||
uri, err := MongoDBURI()
|
||||
require.NoError(t, err, "error constructing mongodb URI: %v", err)
|
||||
cfg, err := topology.NewConfig(options.Client().ApplyURI(uri), nil)
|
||||
require.NoError(t, err, "error constructing topology config: %v", err)
|
||||
|
||||
liveTopologyOnce.Do(func() {
|
||||
var err error
|
||||
liveTopology, err = topology.New(cfg)
|
||||
if err != nil {
|
||||
liveTopologyErr = err
|
||||
} else {
|
||||
_ = liveTopology.Connect()
|
||||
|
||||
err = operation.NewCommand(bsoncore.BuildDocument(nil, bsoncore.AppendInt32Element(nil, "dropDatabase", 1))).
|
||||
Database(DBName(t)).ServerSelector(description.WriteSelector()).Deployment(liveTopology).Execute(context.Background())
|
||||
require.NoError(t, err)
|
||||
}
|
||||
})
|
||||
|
||||
if liveTopologyErr != nil {
|
||||
t.Fatal(liveTopologyErr)
|
||||
}
|
||||
|
||||
return liveTopology
|
||||
}
|
||||
|
||||
// TopologyWithCredential takes an "options.Credential" object and returns a connected topology.
|
||||
func TopologyWithCredential(t *testing.T, credential options.Credential) *topology.Topology {
|
||||
uri, err := MongoDBURI()
|
||||
if err != nil {
|
||||
t.Fatalf("error constructing mongodb URI: %v", err)
|
||||
}
|
||||
cfg, err := topology.NewConfig(options.Client().ApplyURI(uri).SetAuth(credential), nil)
|
||||
if err != nil {
|
||||
t.Fatalf("error constructing topology config: %v", err)
|
||||
}
|
||||
topology, err := topology.New(cfg)
|
||||
if err != nil {
|
||||
t.Fatal("Could not construct topology")
|
||||
}
|
||||
err = topology.Connect()
|
||||
if err != nil {
|
||||
t.Fatal("Could not start topology connection")
|
||||
}
|
||||
return topology
|
||||
}
|
||||
|
||||
// ColName gets a collection name that should be unique
|
||||
// to the currently executing test.
|
||||
func ColName(t *testing.T) string {
|
||||
// Get this indirectly to avoid copying a mutex
|
||||
v := reflect.Indirect(reflect.ValueOf(t))
|
||||
name := v.FieldByName("name")
|
||||
return name.String()
|
||||
}
|
||||
|
||||
// MongoDBURI will construct the MongoDB URI from the MONGODB_URI environment variable for testing. The default host is
|
||||
// "localhost" and the default port is "27017"
|
||||
func MongoDBURI() (string, error) {
|
||||
uri := os.Getenv("MONGODB_URI")
|
||||
if uri == "" {
|
||||
uri = "mongodb://localhost:27017"
|
||||
}
|
||||
|
||||
uri = AddTLSConfigToURI(uri)
|
||||
uri = AddCompressorToURI(uri)
|
||||
uri, err := AddServerlessAuthCredentials(uri)
|
||||
return uri, err
|
||||
}
|
||||
|
||||
// AddServerlessAuthCredentials will attempt to construct the serverless auth credentials for a URI.
|
||||
func AddServerlessAuthCredentials(uri string) (string, error) {
|
||||
if os.Getenv("SERVERLESS") != "serverless" {
|
||||
return uri, nil
|
||||
}
|
||||
user := os.Getenv("SERVERLESS_ATLAS_USER")
|
||||
if user == "" {
|
||||
return "", fmt.Errorf("serverless expects SERVERLESS_ATLAS_USER to be set")
|
||||
}
|
||||
password := os.Getenv("SERVERLESS_ATLAS_PASSWORD")
|
||||
if password == "" {
|
||||
return "", fmt.Errorf("serverless expects SERVERLESS_ATLAS_PASSWORD to be set")
|
||||
}
|
||||
|
||||
var scheme string
|
||||
// remove the scheme
|
||||
if strings.HasPrefix(uri, "mongodb+srv://") {
|
||||
scheme = "mongodb+srv://"
|
||||
} else if strings.HasPrefix(uri, "mongodb://") {
|
||||
scheme = "mongodb://"
|
||||
} else {
|
||||
return "", fmt.Errorf("scheme must be \"mongodb\" or \"mongodb+srv\"")
|
||||
}
|
||||
|
||||
uri = scheme + user + ":" + password + "@" + uri[len(scheme):]
|
||||
return uri, nil
|
||||
}
|
||||
|
||||
// ConnString gets the globally configured connection string.
|
||||
func ConnString(t *testing.T) connstring.ConnString {
|
||||
connectionStringOnce.Do(func() {
|
||||
uri, err := MongoDBURI()
|
||||
require.NoError(t, err, "error constructing mongodb URI: %v", err)
|
||||
|
||||
connectionString, err = connstring.ParseAndValidate(uri)
|
||||
if err != nil {
|
||||
connectionStringErr = err
|
||||
}
|
||||
})
|
||||
if connectionStringErr != nil {
|
||||
t.Fatal(connectionStringErr)
|
||||
}
|
||||
|
||||
return connectionString
|
||||
}
|
||||
|
||||
func GetConnString() (connstring.ConnString, error) {
|
||||
mongodbURI := os.Getenv("MONGODB_URI")
|
||||
if mongodbURI == "" {
|
||||
mongodbURI = "mongodb://localhost:27017"
|
||||
}
|
||||
|
||||
mongodbURI = AddTLSConfigToURI(mongodbURI)
|
||||
|
||||
cs, err := connstring.ParseAndValidate(mongodbURI)
|
||||
if err != nil {
|
||||
return connstring.ConnString{}, err
|
||||
}
|
||||
|
||||
return cs, nil
|
||||
}
|
||||
|
||||
// DBName gets the globally configured database name.
|
||||
func DBName(t *testing.T) string {
|
||||
return GetDBName(ConnString(t))
|
||||
}
|
||||
|
||||
func GetDBName(cs connstring.ConnString) string {
|
||||
if cs.Database != "" {
|
||||
return cs.Database
|
||||
}
|
||||
|
||||
return fmt.Sprintf("mongo-go-driver-%d", os.Getpid())
|
||||
}
|
||||
|
||||
// CompareVersions compares two version number strings (i.e. positive integers separated by
|
||||
// periods). Comparisons are done to the lesser precision of the two versions. For example, 3.2 is
|
||||
// considered equal to 3.2.11, whereas 3.2.0 is considered less than 3.2.11.
|
||||
//
|
||||
// Returns a positive int if version1 is greater than version2, a negative int if version1 is less
|
||||
// than version2, and 0 if version1 is equal to version2.
|
||||
func CompareVersions(t *testing.T, v1 string, v2 string) int {
|
||||
n1 := strings.Split(v1, ".")
|
||||
n2 := strings.Split(v2, ".")
|
||||
|
||||
for i := 0; i < int(math.Min(float64(len(n1)), float64(len(n2)))); i++ {
|
||||
i1, err := strconv.Atoi(n1[i])
|
||||
require.NoError(t, err)
|
||||
|
||||
i2, err := strconv.Atoi(n2[i])
|
||||
require.NoError(t, err)
|
||||
|
||||
difference := i1 - i2
|
||||
if difference != 0 {
|
||||
return difference
|
||||
}
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
61
mongo/internal/testutil/helpers/helpers.go
Normal file
61
mongo/internal/testutil/helpers/helpers.go
Normal file
@@ -0,0 +1,61 @@
|
||||
// Copyright (C) MongoDB, Inc. 2017-present.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
// not use this file except in compliance with the License. You may obtain
|
||||
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
package helpers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"path"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
)
|
||||
|
||||
// FindJSONFilesInDir finds the JSON files in a directory.
|
||||
func FindJSONFilesInDir(t *testing.T, dir string) []string {
|
||||
t.Helper()
|
||||
|
||||
files := make([]string, 0)
|
||||
|
||||
entries, err := ioutil.ReadDir(dir)
|
||||
require.NoError(t, err)
|
||||
|
||||
for _, entry := range entries {
|
||||
if entry.IsDir() || path.Ext(entry.Name()) != ".json" {
|
||||
continue
|
||||
}
|
||||
|
||||
files = append(files, entry.Name())
|
||||
}
|
||||
|
||||
return files
|
||||
}
|
||||
|
||||
// RawToDocuments converts a bson.Raw that is internally an array of documents to []bson.Raw.
|
||||
func RawToDocuments(doc bson.Raw) []bson.Raw {
|
||||
values, err := doc.Values()
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("error converting BSON document to values: %v", err))
|
||||
}
|
||||
|
||||
out := make([]bson.Raw, len(values))
|
||||
for i := range values {
|
||||
out[i] = values[i].Document()
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
// RawToInterfaces takes one or many bson.Raw documents and returns them as a []interface{}.
|
||||
func RawToInterfaces(docs ...bson.Raw) []interface{} {
|
||||
out := make([]interface{}, len(docs))
|
||||
for i := range docs {
|
||||
out[i] = docs[i]
|
||||
}
|
||||
return out
|
||||
}
|
||||
14
mongo/internal/testutil/israce/norace.go
Normal file
14
mongo/internal/testutil/israce/norace.go
Normal file
@@ -0,0 +1,14 @@
|
||||
// Copyright (C) MongoDB, Inc. 2017-present.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
// not use this file except in compliance with the License. You may obtain
|
||||
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
//go:build !race
|
||||
// +build !race
|
||||
|
||||
// Package israce reports if the Go race detector is enabled.
|
||||
package israce
|
||||
|
||||
// Enabled reports if the race detector is enabled.
|
||||
const Enabled = false
|
||||
14
mongo/internal/testutil/israce/race.go
Normal file
14
mongo/internal/testutil/israce/race.go
Normal file
@@ -0,0 +1,14 @@
|
||||
// Copyright (C) MongoDB, Inc. 2017-present.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
// not use this file except in compliance with the License. You may obtain
|
||||
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
//go:build race
|
||||
// +build race
|
||||
|
||||
// Package israce reports if the Go race detector is enabled.
|
||||
package israce
|
||||
|
||||
// Enabled reports if the race detector is enabled.
|
||||
const Enabled = true
|
||||
78
mongo/internal/testutil/monitor/monitor.go
Normal file
78
mongo/internal/testutil/monitor/monitor.go
Normal file
@@ -0,0 +1,78 @@
|
||||
// Copyright (C) MongoDB, Inc. 2022-present.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
// not use this file except in compliance with the License. You may obtain
|
||||
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
// Package monitor provides test types that are used to monitor client state and actions via the
|
||||
// various monitor types supported by the driver.
|
||||
package monitor
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"go.mongodb.org/mongo-driver/event"
|
||||
)
|
||||
|
||||
// TestPoolMonitor exposes an *event.TestPoolMonitor and collects all events logged to that
|
||||
// *event.TestPoolMonitor. It is safe to use from multiple concurrent goroutines.
|
||||
type TestPoolMonitor struct {
|
||||
*event.PoolMonitor
|
||||
|
||||
events []*event.PoolEvent
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
func NewTestPoolMonitor() *TestPoolMonitor {
|
||||
tpm := &TestPoolMonitor{
|
||||
events: make([]*event.PoolEvent, 0),
|
||||
}
|
||||
tpm.PoolMonitor = &event.PoolMonitor{
|
||||
Event: func(evt *event.PoolEvent) {
|
||||
tpm.mu.Lock()
|
||||
defer tpm.mu.Unlock()
|
||||
tpm.events = append(tpm.events, evt)
|
||||
},
|
||||
}
|
||||
return tpm
|
||||
}
|
||||
|
||||
// Events returns a copy of the events collected by the testPoolMonitor. Filters can optionally be
|
||||
// applied to the returned events set and are applied using AND logic (i.e. all filters must return
|
||||
// true to include the event in the result).
|
||||
func (tpm *TestPoolMonitor) Events(filters ...func(*event.PoolEvent) bool) []*event.PoolEvent {
|
||||
tpm.mu.RLock()
|
||||
defer tpm.mu.RUnlock()
|
||||
|
||||
filtered := make([]*event.PoolEvent, 0, len(tpm.events))
|
||||
for _, evt := range tpm.events {
|
||||
keep := true
|
||||
for _, filter := range filters {
|
||||
if !filter(evt) {
|
||||
keep = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if keep {
|
||||
filtered = append(filtered, evt)
|
||||
}
|
||||
}
|
||||
|
||||
return filtered
|
||||
}
|
||||
|
||||
// ClearEvents will reset the events collected by the testPoolMonitor.
|
||||
func (tpm *TestPoolMonitor) ClearEvents() {
|
||||
tpm.mu.Lock()
|
||||
defer tpm.mu.Unlock()
|
||||
tpm.events = tpm.events[:0]
|
||||
}
|
||||
|
||||
// IsPoolCleared returns true if there are any events of type "event.PoolCleared" in the events
|
||||
// recorded by the testPoolMonitor.
|
||||
func (tpm *TestPoolMonitor) IsPoolCleared() bool {
|
||||
poolClearedEvents := tpm.Events(func(evt *event.PoolEvent) bool {
|
||||
return evt.Type == event.PoolCleared
|
||||
})
|
||||
return len(poolClearedEvents) > 0
|
||||
}
|
||||
59
mongo/internal/testutil/ops.go
Normal file
59
mongo/internal/testutil/ops.go
Normal file
@@ -0,0 +1,59 @@
|
||||
// Copyright (C) MongoDB, Inc. 2017-present.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
// not use this file except in compliance with the License. You may obtain
|
||||
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
package testutil // import "go.mongodb.org/mongo-driver/internal/testutil"
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.mongodb.org/mongo-driver/mongo/description"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"go.mongodb.org/mongo-driver/mongo/writeconcern"
|
||||
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
|
||||
"go.mongodb.org/mongo-driver/x/mongo/driver"
|
||||
"go.mongodb.org/mongo-driver/x/mongo/driver/operation"
|
||||
)
|
||||
|
||||
// DropCollection drops the collection in the test cluster.
|
||||
func DropCollection(t *testing.T, dbname, colname string) {
|
||||
err := operation.NewCommand(bsoncore.BuildDocument(nil, bsoncore.AppendStringElement(nil, "drop", colname))).
|
||||
Database(dbname).ServerSelector(description.WriteSelector()).Deployment(Topology(t)).
|
||||
Execute(context.Background())
|
||||
if de, ok := err.(driver.Error); err != nil && !(ok && de.NamespaceNotFound()) {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
}
|
||||
|
||||
// AutoInsertDocs inserts the docs into the test cluster.
|
||||
func AutoInsertDocs(t *testing.T, writeConcern *writeconcern.WriteConcern, docs ...bsoncore.Document) {
|
||||
InsertDocs(t, DBName(t), ColName(t), writeConcern, docs...)
|
||||
}
|
||||
|
||||
// InsertDocs inserts the docs into the test cluster.
|
||||
func InsertDocs(t *testing.T, dbname, colname string, writeConcern *writeconcern.WriteConcern, docs ...bsoncore.Document) {
|
||||
err := operation.NewInsert(docs...).Collection(colname).Database(dbname).
|
||||
Deployment(Topology(t)).ServerSelector(description.WriteSelector()).Execute(context.Background())
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
// RunCommand runs an arbitrary command on a given database of target server
|
||||
func RunCommand(t *testing.T, s driver.Server, db string, cmd bsoncore.Document) (bsoncore.Document, error) {
|
||||
op := operation.NewCommand(cmd).
|
||||
Database(db).Deployment(driver.SingleServerDeployment{Server: s})
|
||||
err := op.Execute(context.Background())
|
||||
res := op.Result()
|
||||
return res, err
|
||||
}
|
||||
|
||||
// AddTestServerAPIVersion adds the latest server API version in a ServerAPIOptions to passed-in opts.
|
||||
func AddTestServerAPIVersion(opts *options.ClientOptions) {
|
||||
if os.Getenv("REQUIRE_API_VERSION") == "true" {
|
||||
opts.SetServerAPIOptions(options.ServerAPI(driver.TestServerAPIVersion))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user