atlaslocal

package
v0.40.0 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Container

type Container struct {
	testcontainers.Container
	// contains filtered or unexported fields
}

Container represents the MongoDBAtlasLocal container type used in the module.

func Run

func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*Container, error)

Run creates an instance of the MongoDBAtlasLocal container type.

Example
// runMongoDBAtlasLocalContainer {
ctx := context.Background()

atlaslocalContainer, err := atlaslocal.Run(ctx, "mongodb/mongodb-atlas-local:latest")
defer func() {
	if err := testcontainers.TerminateContainer(atlaslocalContainer); err != nil {
		log.Printf("failed to terminate container: %s", err)
	}
}()
if err != nil {
	log.Printf("failed to start container: %s", err)
	return
}
// }

state, err := atlaslocalContainer.State(ctx)
if err != nil {
	log.Printf("failed to get container state: %s", err)
	return
}

fmt.Println(state.Running)
Output:

true
Example (Connect)
// connectToMongo {
ctx := context.Background()

atlaslocalContainer, err := atlaslocal.Run(ctx, "mongodb/mongodb-atlas-local:latest")
defer func() {
	if err := testcontainers.TerminateContainer(atlaslocalContainer); err != nil {
		log.Printf("failed to terminate container: %s", err)
	}
}()
if err != nil {
	log.Printf("failed to start container: %s", err)
	return
}

connString, err := atlaslocalContainer.ConnectionString(ctx)
if err != nil {
	log.Printf("failed to get connection string: %s", err)
	return
}

mongoClient, err := mongo.Connect(options.Client().ApplyURI(connString))
if err != nil {
	log.Printf("failed to connect to MongoDB: %s", err)
	return
}
// }

err = mongoClient.Ping(ctx, nil)
if err != nil {
	log.Printf("failed to ping MongoDB: %s", err)
	return
}

fmt.Println(mongoClient.Database("test").Name())
Output:

test
Example (ReadMongotLogs)
// mongotLogsRead {
ctx := context.Background()

atlaslocalContainer, err := atlaslocal.Run(ctx, "mongodb/mongodb-atlas-local:latest",
	atlaslocal.WithMongotLogFile())

defer func() {
	if err := testcontainers.TerminateContainer(atlaslocalContainer); err != nil {
		log.Printf("failed to terminate container: %s", err)
	}
}()

if err != nil {
	log.Printf("failed to start container: %s", err)
	return
}

connString, err := atlaslocalContainer.ConnectionString(ctx)
if err != nil {
	log.Printf("failed to get connection string: %s", err)
	return
}

_, err = mongo.Connect(options.Client().ApplyURI(connString))
if err != nil {
	log.Printf("failed to connect to MongoDB: %s", err)
	return
}

reader, err := atlaslocalContainer.ReadMongotLogs(ctx)
if err != nil {
	log.Printf("failed to read mongot logs: %s", err)
	return
}
defer reader.Close()

if _, err := io.Copy(io.Discard, reader); err != nil {
	log.Printf("failed to write mongot logs: %s", err)
	return
}
// }
Example (ReadRunnerLogs)
// runnerLogsRead {
ctx := context.Background()

atlaslocalContainer, err := atlaslocal.Run(ctx, "mongodb/mongodb-atlas-local:latest",
	atlaslocal.WithRunnerLogFile())

defer func() {
	if err := testcontainers.TerminateContainer(atlaslocalContainer); err != nil {
		log.Printf("failed to terminate container: %s", err)
	}
}()

if err != nil {
	log.Printf("failed to start container: %s", err)
	return
}

connString, err := atlaslocalContainer.ConnectionString(ctx)
if err != nil {
	log.Printf("failed to get connection string: %s", err)
	return
}

_, err = mongo.Connect(options.Client().ApplyURI(connString))
if err != nil {
	log.Printf("failed to connect to MongoDB: %s", err)
	return
}

reader, err := atlaslocalContainer.ReadRunnerLogs(ctx)
if err != nil {
	log.Printf("failed to read runner logs: %s", err)
	return
}
defer reader.Close()

if _, err := io.Copy(io.Discard, reader); err != nil {
	log.Printf("failed to write runner logs: %s", err)
	return
}
// }

func (*Container) ConnectionString

func (ctr *Container) ConnectionString(ctx context.Context) (string, error)

ConnectionString returns the connection string for the MongoDB Atlas Local container. If you provide a username and a password, the connection string will also include them.

func (*Container) ReadMongotLogs

func (ctr *Container) ReadMongotLogs(ctx context.Context) (io.ReadCloser, error)

ReadMongotLogs returns a reader for mongot logs in the container. Reads from stdout/stderr or /tmp/mongot.log if configured.

This method return the os.ErrNotExist sentinel error if it is called with no log file configured.

func (*Container) ReadRunnerLogs

func (ctr *Container) ReadRunnerLogs(ctx context.Context) (io.ReadCloser, error)

ReadRunnerLogs() returns a reader for runner logs in the container. Reads from stdout/stderr or /tmp/runner.log if configured.

This method return the os.ErrNotExist sentinel error if it is called with no log file configured.

type Option

type Option func(*options) error

Option is an option for the Atlas Local container.

func WithInitDatabase

func WithInitDatabase(database string) Option

WithInitDatabase sets MONGODB_INITDB_DATABASE environment variable so the init scripts and the default connection string target the specified database instead of the default "test" database.

func WithInitScripts

func WithInitScripts(scriptsDir string) Option

WithInitScripts mounts a directory containing .sh/.js init scripts into /docker-entrypoint-initdb.d so they run in alphabetical order on startup. If called multiple times, this function removes any prior init-scripts bind and uses only the latest on specified.

func WithMongotLogFile

func WithMongotLogFile() Option

WithMongotLogFile writes the mongot logs to /tmp/mongot.log inside the container. See (*Container).ReadMongotLogs to read the logs locally.

func WithMongotLogToStderr

func WithMongotLogToStderr() Option

WithMongotLogToStderr writes to /dev/stderr inside the container. See (*Container).ReadMongotLogs to read the logs locally.

func WithMongotLogToStdout

func WithMongotLogToStdout() Option

WithMongotLogStdout writes to /dev/stdout inside the container. See (*Container).ReadMongotLogs to read the logs locally.

func WithNoTelemetry

func WithNoTelemetry() Option

WithNoTelemetry opts out of telemetry for the MongoDB Atlas Local container by setting the DO_NOT_TRACK environment variable to 1.

func WithPassword

func WithPassword(password string) Option

WithPassword sets the MongoDB root password by setting the the MONGODB_INITDB_ROOT_PASSWORD environment variable.

func WithPasswordFile

func WithPasswordFile(passwordFile string) Option

WithPasswordFile mounts a local file as the MongoDB root password secret at /run/secrets/mongo-root-password and sets MONGODB_INITDB_ROOT_PASSWORD_FILE. Path must be absolute and an existing file; no-op if empty.

func WithRunnerLogFile

func WithRunnerLogFile() Option

WithRunnerLogFile writes the runner logs to /tmp/runner.log inside the container. See (*Container).ReadRunnerLogs to read the logs locally.

func WithRunnerLogToStderr

func WithRunnerLogToStderr() Option

WithRunnerLogToStderr writes to /dev/stderr inside the container. See (*Container).ReadRunnerLogs to read the logs locally.

func WithRunnerLogToStdout

func WithRunnerLogToStdout() Option

WithRunnerLogToStdout writes to /dev/stdout inside the container. See (*Container).ReadRunnerLogs to read the logs locally.

func WithUsername

func WithUsername(username string) Option

WithUsername sets the MongoDB root username by setting the MONGODB_INITDB_ROOT_USERNAME environment variable.

func WithUsernameFile

func WithUsernameFile(usernameFile string) Option

WithUsernameFile mounts a local file as the MongoDB root username secret at /run/secrets/mongo-root-username and sets MONGODB_INITDB_ROOT_USERNAME_FILE. The path must be absolute and exist; no-op if empty.

func (Option) Customize

func (o Option) Customize(*testcontainers.GenericContainerRequest) error

Customize is a NOOP. It's defined to satisfy the testcontainers.ContainerCustomizer interface.

Jump to

Keyboard shortcuts

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