mtest

package
v1.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 8, 2020 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ReplicaSet TopologyKind = "replicaset"
	Sharded                 = "sharded"
	Single                  = "single"
)

These constants specify valid values for TopologyKind

View Source
const (
	// TestDb specifies the name of default test database.
	TestDb = "test"
)

Variables

View Source
var (
	// Background is a no-op context.
	Background = context.Background()
	// MajorityWc is the majority write concern.
	MajorityWc = writeconcern.New(writeconcern.WMajority())
	// PrimaryRp is the primary read preference.
	PrimaryRp = readpref.Primary()
	// LocalRc is the local read concern
	LocalRc = readconcern.Local()
	// MajorityRc is the majority read concern
	MajorityRc = readconcern.Majority()
)

Functions

func CreateCommandErrorResponse

func CreateCommandErrorResponse(ce CommandError) bson.D

CreateCommandErrorResponse creates a response with a command error.

func CreateCursorResponse

func CreateCursorResponse(cursorID int64, ns string, identifier BatchIdentifier, batch ...bson.D) bson.D

CreateCursorResponse creates a response for a cursor command.

func CreateSuccessResponse

func CreateSuccessResponse(elems ...bson.E) bson.D

CreateSuccessResponse creates a response for a successful operation with the given elements.

func CreateWriteConcernErrorResponse

func CreateWriteConcernErrorResponse(wce WriteConcernError) bson.D

CreateWriteConcernErrorResponse creates a response with a write concern error.

func CreateWriteErrorsResponse

func CreateWriteErrorsResponse(writeErrorrs ...WriteError) bson.D

CreateWriteErrorsResponse creates a response with one or more write errors.

func Setup

func Setup() error

Setup initializes the current testing context. This function must only be called one time and must be called before any tests run.

func Teardown

func Teardown() error

Teardown cleans up resources initialized by Setup. This function must be called once after all tests have finished running.

Types

type BatchIdentifier

type BatchIdentifier string

BatchIdentifier specifies the keyword to identify the batch in a cursor response.

const (
	FirstBatch BatchIdentifier = "firstBatch"
	NextBatch  BatchIdentifier = "nextBatch"
)

These constants specify valid values for BatchIdentifier.

type ClientType

type ClientType int

ClientType specifies the type of Client that should be created for a test.

const (
	// Default specifies a client to the connection string in the MONGODB_URI env variable with command monitoring
	// enabled.
	Default ClientType = iota
	// Pinned specifies a client that is pinned to a single mongos in a sharded cluster.
	Pinned
	// Mock specifies a client that communicates with a mock deployment.
	Mock
)

These constants specify valid values for ClientType

type Collection

type Collection struct {
	Name       string
	DB         string        // defaults to mt.DB.Name() if not specified
	Client     *mongo.Client // defaults to mt.Client if not specified
	Opts       *options.CollectionOptions
	CreateOpts bson.D
}

Collection is used to configure a new collection created during a test.

type CommandError

type CommandError struct {
	Code    int32
	Message string
	Name    string
	Labels  []string
}

CommandError is a representation of a command error from the server.

type FailPoint

type FailPoint struct {
	ConfigureFailPoint string `bson:"configureFailPoint"`
	// Mode should be a string, FailPointMode, or map[string]interface{}
	Mode interface{}   `bson:"mode"`
	Data FailPointData `bson:"data"`
}

FailPoint is a representation of a server fail point. See https://github.com/mongodb/specifications/tree/master/source/transactions/tests#server-fail-point for more information regarding fail points.

type FailPointData

type FailPointData struct {
	FailCommands                  []string `bson:"failCommands,omitempty"`
	CloseConnection               bool     `bson:"closeConnection,omitempty"`
	ErrorCode                     int32    `bson:"errorCode,omitempty"`
	FailBeforeCommitExceptionCode int32    `bson:"failBeforeCommitExceptionCode,omitempty"`
	WriteConcernError             *struct {
		Code   int32  `bson:"code"`
		Name   string `bson:"codeName"`
		Errmsg string `bson:"errmsg"`
	} `bson:"writeConcernError,omitempty"`
}

FailPointData is a representation of the FailPoint.Data field.

type FailPointMode

type FailPointMode struct {
	Times int32 `bson:"times"`
	Skip  int32 `bson:"skip"`
}

FailPointMode is a representation of the Failpoint.Mode field.

type Options

type Options struct {
	// contains filtered or unexported fields
}

Options is the type used to configure a new T instance.

func NewOptions

func NewOptions() *Options

NewOptions creates an empty Options instance.

func (*Options) Auth

func (op *Options) Auth(auth bool) *Options

Auth specifies whether or not auth should be enabled for this test to run. By default, a test will run regardless of whether or not auth is enabled.

func (*Options) ClientOptions

func (op *Options) ClientOptions(opts *options.ClientOptions) *Options

ClientOptions sets the options to use when creating a client for a test.

func (*Options) ClientType

func (op *Options) ClientType(ct ClientType) *Options

ClientType specifies the type of client that should be created for a test. This option will be propagated to all sub-tests.

func (*Options) CollectionCreateOptions

func (op *Options) CollectionCreateOptions(opts bson.D) *Options

CollectionCreateOptions sets the options to pass to the create command when creating a collection for a test. For example, if opts = {"capped": "true"}, the create command sent to the server will be {create: <collectionName>, foo: bar}.

func (*Options) CollectionName

func (op *Options) CollectionName(collName string) *Options

CollectionName specifies the name for the collection for the test.

func (*Options) CollectionOptions

func (op *Options) CollectionOptions(opts *options.CollectionOptions) *Options

CollectionOptions sets the options to use when creating a collection for a test.

func (*Options) CreateClient

func (op *Options) CreateClient(create bool) *Options

CreateClient specifies whether or not a client and collection should be created for a test. This should be set to false when running a test that only runs other tests.

func (*Options) DatabaseName

func (op *Options) DatabaseName(dbName string) *Options

DatabaseName specifies the name of the database for the test.

func (*Options) Enterprise

func (op *Options) Enterprise(ent bool) *Options

Enterprise specifies whether or not this test should only be run on enterprise server variants. Defaults to false.

func (*Options) MaxServerVersion

func (op *Options) MaxServerVersion(version string) *Options

MaxServerVersion specifies the maximum server version for the test.

func (*Options) MinServerVersion

func (op *Options) MinServerVersion(version string) *Options

MinServerVersion specifies the minimum server version for the test.

func (*Options) MockResponses

func (op *Options) MockResponses(responses ...bson.D) *Options

MockResponses specifies the responses returned by a mock deployment. This should only be used if the current test is being run with MockDeployment(true). Responses can also be added after a sub-test has already been created.

func (*Options) RunOn

func (op *Options) RunOn(blocks ...RunOnBlock) *Options

RunOn specifies run-on blocks used to determine if a test should run. If a test's environment meets at least one of the given constraints, it will be run. Otherwise, it will be skipped.

func (*Options) ShareClient

func (op *Options) ShareClient(share bool) *Options

ShareClient specifies whether or not a test should pass its client down to sub-tests. This should be set when calling New() if the inheriting behavior is desired. This option must not be used if the test accesses command monitoring events.

func (*Options) Topologies

func (op *Options) Topologies(topos ...TopologyKind) *Options

Topologies specifies a list of topologies that the test can run on.

type RunOnBlock

type RunOnBlock struct {
	MinServerVersion string         `bson:"minServerVersion"`
	MaxServerVersion string         `bson:"maxServerVersion"`
	Topology         []TopologyKind `bson:"topology"`
}

RunOnBlock describes a constraint for a test.

type T

type T struct {
	*testing.T

	Client *mongo.Client
	DB     *mongo.Database
	Coll   *mongo.Collection
	// contains filtered or unexported fields
}

T is a wrapper around testing.T.

func New

func New(wrapped *testing.T, opts ...*Options) *T

New creates a new T instance with the given options. If the current environment does not satisfy constraints specified in the options, the test will be skipped automatically.

func (*T) AddMockResponses

func (t *T) AddMockResponses(responses ...bson.D)

AddMockResponses adds responses to be returned by the mock deployment. This should only be used if T is being run against a mock deployment.

func (*T) AuthEnabled

func (t *T) AuthEnabled() bool

AuthEnabled returns whether or not this test is running in an environment with auth.

func (*T) ClearCollections

func (t *T) ClearCollections()

ClearCollections drops all collections previously created by this test.

func (*T) ClearEvents

func (t *T) ClearEvents()

ClearEvents clears the existing command monitoring events.

func (*T) ClearFailPoints

func (t *T) ClearFailPoints()

ClearFailPoints disables all previously set failpoints for this test.

func (*T) ClearMockResponses

func (t *T) ClearMockResponses()

ClearMockResponses clears all responses in the mock deployment.

func (*T) CloneCollection

func (t *T) CloneCollection(opts *options.CollectionOptions)

CloneCollection modifies the default collection for this test to match the given options.

func (*T) CloneDatabase

func (t *T) CloneDatabase(opts *options.DatabaseOptions)

CloneDatabase modifies the default database for this test to match the given options.

func (*T) Close

func (t *T) Close()

Close cleans up any resources associated with a T. There should be one Close corresponding to every New.

func (*T) ConnString

func (t *T) ConnString() string

ConnString returns the connection string used to create the client for this test.

func (*T) CreateCollection

func (t *T) CreateCollection(coll Collection, createOnServer bool) *mongo.Collection

CreateCollection creates a new collection with the given configuration. The collection will be dropped after the test finishes running. If createOnServer is true, the function ensures that the collection has been created server-side by running the create command. The create command will appear in command monitoring channels.

func (*T) GetAllFailedEvents

func (t *T) GetAllFailedEvents() []*event.CommandFailedEvent

GetAllFailedEvents returns a slice of all CommandFailedEvent instances for this test. This can be called multiple times.

func (*T) GetAllStartedEvents

func (t *T) GetAllStartedEvents() []*event.CommandStartedEvent

GetAllStartedEvents returns a slice of all CommandStartedEvent instances for this test. This can be called multiple times.

func (*T) GetAllSucceededEvents

func (t *T) GetAllSucceededEvents() []*event.CommandSucceededEvent

GetAllSucceededEvents returns a slice of all CommandSucceededEvent instances for this test. This can be called multiple times.

func (*T) GetFailedEvent

func (t *T) GetFailedEvent() *event.CommandFailedEvent

GetFailedEvent returns the most recent CommandFailedEvent, or nil if one is not present. This can only be called once per event.

func (*T) GetStartedEvent

func (t *T) GetStartedEvent() *event.CommandStartedEvent

GetStartedEvent returns the most recent CommandStartedEvent, or nil if one is not present. This can only be called once per event.

func (*T) GetSucceededEvent

func (t *T) GetSucceededEvent() *event.CommandSucceededEvent

GetSucceededEvent returns the most recent CommandSucceededEvent, or nil if one is not present. This can only be called once per event.

func (T) GlobalClient

func (T) GlobalClient() *mongo.Client

GlobalClient returns a client configured with read concern majority, write concern majority, and read preference primary. The returned client is not tied to the receiver and is valid outside the lifetime of the receiver.

func (*T) ResetClient

func (t *T) ResetClient(opts *options.ClientOptions)

ResetClient resets the existing client with the given options. If opts is nil, the existing options will be used. If t.Coll is not-nil, it will be reset to use the new client. Should only be called if the existing client is not nil. This will Disconnect the existing client but will not drop existing collections. To do so, ClearCollections must be called before calling ResetClient.

func (*T) Run

func (t *T) Run(name string, callback func(*T))

Run creates a new T instance for a sub-test and runs the given callback. It also creates a new collection using the given name which is available to the callback through the T.Coll variable and is dropped after the callback returns.

func (*T) RunOpts

func (t *T) RunOpts(name string, opts *Options, callback func(*T))

RunOpts creates a new T instance for a sub-test with the given options. If the current environment does not satisfy constraints specified in the options, the new sub-test will be skipped automatically. If the test is not skipped, the callback will be run with the new T instance. RunOpts creates a new collection with the given name which is available to the callback through the T.Coll variable and is dropped after the callback returns.

func (*T) SetFailPoint

func (t *T) SetFailPoint(fp FailPoint)

SetFailPoint sets a fail point for the client associated with T. Commands to create the failpoint will appear in command monitoring channels. The fail point will automatically be disabled after this test has run.

func (*T) TopologyKind

func (t *T) TopologyKind() TopologyKind

TopologyKind returns the topology kind of the environment

func (*T) TrackFailPoint

func (t *T) TrackFailPoint(fpName string)

TrackFailPoint adds the given fail point to the list of fail points to be disabled when the current test finishes. This function does not create a fail point on the server.

type TopologyKind

type TopologyKind string

TopologyKind describes the topology that a test is run on.

type WriteConcernError

type WriteConcernError struct {
	Name    string   `bson:"codeName"`
	Code    int      `bson:"code"`
	Message string   `bson:"errmsg"`
	Details bson.Raw `bson:"errInfo"`
}

WriteConcernError is a representation of a write concern error from the server.

type WriteError

type WriteError struct {
	Index   int
	Code    int
	Message string
}

WriteError is a representation of a write error from the server.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL