Documentation
¶
Index ¶
Constants ¶
const DBNameChars = "abcdefghijklmnopqrstuvwxyz"
DBNameChars is the set of characters used by RandomDatabase(). It's OK to change this, but not concurrently with calls to RandomDatabase.
const DBNameLen = 15
DBNameLen is the length of a database name generated by RandomDatabase(). It's OK to change this, but not concurrently with calls to RandomDatabase.
Variables ¶
This section is empty.
Functions ¶
func RandomDatabase ¶
func RandomDatabase() string
RandomDatabase returns a random valid mongo database name. You can use to to pick a new database name for each test to isolate tests from each other without having to tear down the whole server.
This function will panic if it cannot generate a random number.
Types ¶
type Options ¶
type Options struct {
// ShouldUseReplica indicates whether a replica should be used. If this is not specified,
// no replica will be used and mongo server will be run as standalone.
ShouldUseReplica bool
// ReplicaSetName is the name of the replica set. Defaults to "rs0".
// Only used when ShouldUseReplica is true.
ReplicaSetName string
// Port to run MongoDB on. If this is not specified, a random (OS-assigned)
// port will be used
Port int
// Path to the cache for downloaded mongod binaries. Defaults to the
// system cache location.
CachePath string
// If DownloadURL and MongodBin are not given, this version of MongoDB will
// be downloaded
MongoVersion string
// If given, mongod will be downloaded from this URL instead of the
// auto-detected URL based on the current platform and MongoVersion
DownloadURL string
// If given, this binary will be run instead of downloading a mongod binary
MongodBin string
// Logger for printing messages. Defaults to printing to stdout.
Logger *log.Logger
// A LogLevel to log at. Defaults to LogLevelInfo.
LogLevel memongolog.LogLevel
// How long to wait for mongod to start up and report a port number. Does
// not include download time, only startup time. Defaults to 10 seconds.
StartupTimeout time.Duration
// If set, pass the --auth flag to mongod. This will allow tests to setup
// authentication.
Auth bool
// WiredTigerCacheSizeGB sets the maximum size of the WiredTiger cache in GB.
// This is useful to limit memory usage in test environments.
// Only applies when using WiredTiger storage engine (MongoDB 7.0+ or replica sets).
// If not set, MongoDB uses its default (typically 50% of RAM minus 1GB).
WiredTigerCacheSizeGB float64
}
Options is the configuration options for a launched MongoDB binary
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server represents a running MongoDB server
func Start ¶
Start runs a MongoDB server at a given MongoDB version using default options and returns the Server.
func StartWithOptions ¶
StartWithOptions is like Start(), but accepts options.
func (*Server) DBPath ¶
DBPath returns the path to the database directory. This can be useful for debugging or diagnostics.
func (*Server) IsReplicaSet ¶
IsReplicaSet returns true if the server was started as a replica set.
func (*Server) Ping ¶
Ping checks if the MongoDB server is responsive. It returns nil if the server is healthy, or an error if not.
func (*Server) ReplicaSetName ¶
ReplicaSetName returns the name of the replica set if the server was started as a replica set, or an empty string otherwise.
func (*Server) URIWithRandomDB ¶
URIWithRandomDB returns a mongodb:// URI to connect to, with a random database name (e.g. mongodb://localhost:1234/somerandomname)
