diff --git a/docs/modules/mongodb.md b/docs/modules/mongodb.md index 58409e6449..31cb5dc38e 100644 --- a/docs/modules/mongodb.md +++ b/docs/modules/mongodb.md @@ -17,7 +17,7 @@ go get github.com/testcontainers/testcontainers-go/modules/mongodb ## Usage example -[Creating a MongoDB container](../../modules/mongodb/mongodb_test.go) inside_block:runMongoDBContainer +[Creating a MongoDB container](../../modules/mongodb/examples_test.go) inside_block:runMongoDBContainer ## Module reference @@ -72,5 +72,5 @@ It returns a string with the format `mongodb://:`. It can be use to configure a MongoDB client (`go.mongodb.org/mongo-driver/mongo`), e.g.: -[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 diff --git a/modules/mongodb/examples_test.go b/modules/mongodb/examples_test.go new file mode 100644 index 0000000000..8e936a7c64 --- /dev/null +++ b/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 +} diff --git a/modules/mongodb/mongodb_test.go b/modules/mongodb/mongodb_test.go index 8e936a7c64..9326509f09 100644 --- a/modules/mongodb/mongodb_test.go +++ b/modules/mongodb/mongodb_test.go @@ -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 }