Documentation
¶
Index ¶
- type Container
- type Option
- func WithInitDatabase(database string) Option
- func WithInitScripts(scriptsDir string) Option
- func WithMongotLogFile() Option
- func WithMongotLogToStderr() Option
- func WithMongotLogToStdout() Option
- func WithNoTelemetry() Option
- func WithPassword(password string) Option
- func WithPasswordFile(passwordFile string) Option
- func WithRunnerLogFile() Option
- func WithRunnerLogToStderr() Option
- func WithRunnerLogToStdout() Option
- func WithUsername(username string) Option
- func WithUsernameFile(usernameFile string) Option
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
WithPassword sets the MongoDB root password by setting the the MONGODB_INITDB_ROOT_PASSWORD environment variable.
func WithPasswordFile ¶
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 ¶
WithUsername sets the MongoDB root username by setting the MONGODB_INITDB_ROOT_USERNAME environment variable.
func WithUsernameFile ¶
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.