testcontainermongo

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2023 License: Apache-2.0, Unlicense Imports: 4 Imported by: 0

README

Mongo testcontainer

A mongo docker testcontainer for go

Example usage:

import (
	"context"
	"path/filepath"
	"regexp"
	"testing"

	testmongo "github.com/thlib/testcontainermongo"
)
initPath, err := filepath.Abs("./initdb")
if err != nil {
    log.Fatalf("%v", err)
}
ctx := context.Background()
c, conn, err := testmongo.New(ctx, "latest",
    testmongo.WithInit(initdb),
    testmongo.WithDb("test_db"),
    testmongo.WithAuth("root", "example"),
)
defer testmongo.Terminate(ctx, container)

fmt.Println(conn)
// Output: mongodb://root:example@localhost:49156/test_db

Run tests

go test

Documentation

Overview

Package testcontainermongo provides an easy way to start a mongo testcontainer using docker

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(ctx context.Context, tag string, opts ...Option) (testcontainers.Container, string, error)

New setup a mongo testcontainer

func Terminate

func Terminate(ctx context.Context, c testcontainers.Container)

Terminate terminates the container in a defer friendly way

Types

type Option added in v0.1.0

func WithAuth added in v0.1.0

func WithAuth(user, pass string) Option

WithAuth adds a username and password to the container request

func WithDb added in v0.1.0

func WithDb(db string) Option

WithDb adds a database name to the container request

func WithEnv added in v0.1.0

func WithEnv(env map[string]string) Option

WithEnv replaces the environment variables of the container request

func WithInit added in v0.1.0

func WithInit(init string) Option

WithInit adds a path to a folder containing sql files to be executed on startup

Jump to

Keyboard shortcuts

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