Skip to content

Commit

Permalink
chore: confirm support for new mongo images (#2326)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdelapenya committed Mar 6, 2024
1 parent 85361fe commit cfd60e9
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 105 deletions.
4 changes: 2 additions & 2 deletions docs/modules/mongodb.md
Expand Up @@ -17,7 +17,7 @@ go get github.com/testcontainers/testcontainers-go/modules/mongodb
## Usage example

<!--codeinclude-->
[Creating a MongoDB container](../../modules/mongodb/mongodb_test.go) inside_block:runMongoDBContainer
[Creating a MongoDB container](../../modules/mongodb/examples_test.go) inside_block:runMongoDBContainer
<!--/codeinclude-->

## Module reference
Expand Down Expand Up @@ -72,5 +72,5 @@ It returns a string with the format `mongodb://<host>:<port>`.
It can be use to configure a MongoDB client (`go.mongodb.org/mongo-driver/mongo`), e.g.:

<!--codeinclude-->
[Using ConnectionString with the MongoDB client](../../modules/mongodb/mongodb_test.go) inside_block:connectToMongo
[Using ConnectionString with the MongoDB client](../../modules/mongodb/examples_test.go) inside_block:connectToMongo
<!--/codeinclude-->
121 changes: 121 additions & 0 deletions modules/mongodb/examples_test.go
@@ -0,0 +1,121 @@
package mongodb_test

import (
"context"
"fmt"
"log"
"strings"

"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"

"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/mongodb"
"github.com/testcontainers/testcontainers-go/wait"
)

func ExampleRunContainer() {
// 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
}

func ExampleRunContainer_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
}

func ExampleRunContainer_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
}
157 changes: 54 additions & 103 deletions modules/mongodb/mongodb_test.go
Expand Up @@ -2,120 +2,71 @@ package mongodb_test

import (
"context"
"fmt"
"log"
"strings"
"testing"

"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"

"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/mongodb"
"github.com/testcontainers/testcontainers-go/wait"
)

func ExampleRunContainer() {
// 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
}

func ExampleRunContainer_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
}

func ExampleRunContainer_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)
func TestMongoDB(t *testing.T) {
type tests struct {
name string
image string
}

// 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
testCases := []tests{
{
name: "From Docker Hub",
image: "mongo:6",
},
{
name: "Community Server",
image: "mongodb/mongodb-community-server:7.0.2-ubi8",
},
{
name: "Enterprise Server",
image: "mongodb/mongodb-enterprise-server:7.0.0-ubi8",
},
}

mongoClient, err := mongo.Connect(ctx, options.Client().ApplyURI(connStr))
if err != nil {
log.Fatalf("failed to connect to MongoDB: %s", err)
for _, tc := range testCases {
t.Run(tc.image, func(t *testing.T) {
t.Parallel()

ctx := context.Background()

mongodbContainer, err := mongodb.RunContainer(ctx, testcontainers.WithImage(tc.image))
if err != nil {
t.Fatalf("failed to start container: %s", err)
}

defer func() {
if err := mongodbContainer.Terminate(ctx); err != nil {
t.Fatalf("failed to terminate container: %s", err)
}
}()

endpoint, err := mongodbContainer.ConnectionString(ctx)
if err != nil {
t.Fatalf("failed to get connection string: %s", err)
}

mongoClient, err := mongo.Connect(ctx, options.Client().ApplyURI(endpoint))
if err != nil {
t.Fatalf("failed to connect to MongoDB: %s", err)
}

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

if mongoClient.Database("test").Name() != "test" {
t.Fatalf("failed to connect to the correct database")
}
})
}

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
}

0 comments on commit cfd60e9

Please sign in to comment.