Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WithPassword ¶ added in v0.27.0
func WithPassword(password string) testcontainers.CustomizeRequestOption
WithPassword sets the initial password of the user to be created when the container starts It is used in conjunction with WithUsername to set a username and its password. It will set the superuser password for MongoDB.
func WithUsername ¶ added in v0.27.0
func WithUsername(username string) testcontainers.CustomizeRequestOption
WithUsername sets the initial username to be created when the container starts It is used in conjunction with WithPassword to set a username and its password. It will create the specified user with superuser power.
Types ¶
type MongoDBContainer ¶
type MongoDBContainer struct { testcontainers.Container // contains filtered or unexported fields }
MongoDBContainer represents the MongoDB container type used in the module
func RunContainer ¶
func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*MongoDBContainer, error)
RunContainer creates an instance of the MongoDB container type
Example ¶
// runMongoDBContainer { ctx := context.Background() mongodbContainer, err := mongodb.RunContainer(ctx, testcontainers.WithImage("mongo:6")) if err != nil { log.Fatalf("failed to start container: %s", err) } // Clean up the container defer func() { if err := mongodbContainer.Terminate(ctx); err != nil { log.Fatalf("failed to terminate container: %s", err) } }() // } state, err := mongodbContainer.State(ctx) if err != nil { log.Fatalf("failed to get container state: %s", err) // nolint:gocritic } fmt.Println(state.Running)
Output: true
Example (Connect) ¶
// connectToMongo { ctx := context.Background() mongodbContainer, err := mongodb.RunContainer(ctx, testcontainers.WithImage("mongo:6")) if err != nil { log.Fatalf("failed to start container: %s", err) } // Clean up the container defer func() { if err := mongodbContainer.Terminate(ctx); err != nil { log.Fatalf("failed to terminate container: %s", err) } }() endpoint, err := mongodbContainer.ConnectionString(ctx) if err != nil { log.Fatalf("failed to get connection string: %s", err) // nolint:gocritic } mongoClient, err := mongo.Connect(ctx, options.Client().ApplyURI(endpoint)) if err != nil { log.Fatalf("failed to connect to MongoDB: %s", err) } // } err = mongoClient.Ping(ctx, nil) if err != nil { log.Fatalf("failed to ping MongoDB: %s", err) } fmt.Println(mongoClient.Database("test").Name())
Output: test
Example (WithCredentials) ¶
ctx := context.Background() container, err := mongodb.RunContainer(ctx, testcontainers.WithImage("mongo:6"), mongodb.WithUsername("root"), mongodb.WithPassword("password"), testcontainers.WithWaitStrategy(wait.ForLog("Waiting for connections")), ) if err != nil { log.Fatalf("failed to start container: %s", err) } // Clean up the container defer func() { if err := container.Terminate(ctx); err != nil { log.Fatalf("failed to terminate container: %s", err) } }() connStr, err := container.ConnectionString(ctx) if err != nil { log.Fatalf("failed to get connection string: %s", err) // nolint:gocritic } mongoClient, err := mongo.Connect(ctx, options.Client().ApplyURI(connStr)) if err != nil { log.Fatalf("failed to connect to MongoDB: %s", err) } err = mongoClient.Ping(ctx, nil) if err != nil { log.Fatalf("failed to ping MongoDB: %s", err) } fmt.Println(strings.Split(connStr, "@")[0])
Output: mongodb://root:password
func (*MongoDBContainer) ConnectionString ¶
func (c *MongoDBContainer) ConnectionString(ctx context.Context) (string, error)
ConnectionString returns the connection string for the MongoDB container. If you provide a username and a password, the connection string will also include them.