diff --git a/.github/dependabot.yml b/.github/dependabot.yml index afe597e259..2df814702f 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -81,17 +81,6 @@ updates: all: patterns: - '*' - - package-ecosystem: gomod - directory: /examples/mongodb - schedule: - interval: monthly - day: sunday - open-pull-requests-limit: 3 - rebase-strategy: disabled - groups: - all: - patterns: - - '*' - package-ecosystem: gomod directory: /examples/nats schedule: @@ -165,6 +154,10 @@ updates: day: sunday open-pull-requests-limit: 3 rebase-strategy: disabled + groups: + all: + patterns: + - '*' - package-ecosystem: gomod directory: /modules/compose schedule: @@ -209,6 +202,17 @@ updates: all: patterns: - '*' + - package-ecosystem: gomod + directory: /modules/mongodb + schedule: + interval: monthly + day: sunday + open-pull-requests-limit: 3 + rebase-strategy: disabled + groups: + all: + patterns: + - '*' - package-ecosystem: gomod directory: /modules/mysql schedule: diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fa733e8e07..875ce0735f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -82,7 +82,7 @@ jobs: matrix: go-version: [1.19.x, 1.x] platform: [ubuntu-latest, macos-latest] - module: [clickhouse, compose, couchbase, k3s, localstack, mysql, neo4j, postgres, pulsar, redis, redpanda, vault] + module: [clickhouse, compose, couchbase, k3s, localstack, mongodb, mysql, neo4j, postgres, pulsar, redis, redpanda, vault] exclude: - module: compose go-version: 1.19.x @@ -100,7 +100,7 @@ jobs: matrix: go-version: [1.19.x, 1.x] platform: [ubuntu-latest, macos-latest] - module: [bigtable, cockroachdb, consul, datastore, firestore, mongodb, nats, nginx, pubsub, spanner, toxiproxy] + module: [bigtable, cockroachdb, consul, datastore, firestore, nats, nginx, pubsub, spanner, toxiproxy] uses: ./.github/workflows/ci-test-go.yml with: go-version: ${{ matrix.go-version }} diff --git a/docs/examples/mongodb.md b/docs/examples/mongodb.md deleted file mode 100644 index 8d6ef039c8..0000000000 --- a/docs/examples/mongodb.md +++ /dev/null @@ -1,9 +0,0 @@ -# MongoDB - - -[Creating a MongoDB container](../../examples/mongodb/mongodb.go) - - - -[Test for a MongoDB container](../../examples/mongodb/mongodb_test.go) - diff --git a/docs/modules/mongodb.md b/docs/modules/mongodb.md new file mode 100644 index 0000000000..4d82e5b199 --- /dev/null +++ b/docs/modules/mongodb.md @@ -0,0 +1,74 @@ +# MongoDB + +Not available until the next release of testcontainers-go :material-tag: main + +## Introduction + +The Testcontainers module for MongoDB. + +## Adding this module to your project dependencies + +Please run the following command to add the MongoDB module to your Go dependencies: + +``` +go get github.com/testcontainers/testcontainers-go/modules/mongodb +``` + +## Usage example + + +[Creating a MongoDB container](../../modules/mongodb/mongodb_test.go) inside_block:createMongoDBContainer + + +## Module reference + +The MongoDB module exposes one entrypoint function to create the MongoDB container, and this function receives two parameters: + +```golang +func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*MongoDBContainer, error) +``` + +- `context.Context`, the Go context. +- `testcontainers.ContainerCustomizer`, a variadic argument for passing options. + +### Container Options + +When starting the MongoDB container, you can pass options in a variadic way to configure it. + +#### Image + +If you need to set a different MongoDB Docker image, you can use `testcontainers.WithImage` with a valid Docker image +for MongoDB. E.g. `testcontainers.WithImage("mongo:6")`. + +#### Wait Strategies + +If you need to set a different wait strategy for MongoDB, you can use `testcontainers.WithWaitStrategy` with a valid wait strategy +for MongoDB. + +!!!info + The default deadline for the wait strategy is 60 seconds. + +At the same time, it's possible to set a wait strategy and a custom deadline with `testcontainers.WithWaitStrategyAndDeadline`. + +#### Docker type modifiers + +If you need an advanced configuration for MongoDB, you can leverage the following Docker type modifiers: + +- `testcontainers.WithConfigModifier` +- `testcontainers.WithHostConfigModifier` +- `testcontainers.WithEndpointSettingsModifier` + +Please read the [Create containers: Advanced Settings](../features/creating_container.md#advanced-settings) documentation for more information. + +### Container Methods + +The MongoDB container exposes the following methods: + +#### ConnectionString + +This method returns the connection string to connect to the MongoDB container. +It returns a string with the format `mongodb://:`. + + +[Get connection string](../../modules/mongodb/mongodb_test.go) inside_block:connectionString + \ No newline at end of file diff --git a/examples/mongodb/mongodb.go b/examples/mongodb/mongodb.go deleted file mode 100644 index 5d73f79113..0000000000 --- a/examples/mongodb/mongodb.go +++ /dev/null @@ -1,34 +0,0 @@ -package mongodb - -import ( - "context" - - "github.com/testcontainers/testcontainers-go" - "github.com/testcontainers/testcontainers-go/wait" -) - -// mongodbContainer represents the mongodb container type used in the module -type mongodbContainer struct { - testcontainers.Container -} - -// startContainer creates an instance of the mongodb container type -func startContainer(ctx context.Context) (*mongodbContainer, error) { - req := testcontainers.ContainerRequest{ - Image: "mongo:6", - ExposedPorts: []string{"27017/tcp"}, - WaitingFor: wait.ForAll( - wait.ForLog("Waiting for connections"), - wait.ForListeningPort("27017/tcp"), - ), - } - container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ - ContainerRequest: req, - Started: true, - }) - if err != nil { - return nil, err - } - - return &mongodbContainer{Container: container}, nil -} diff --git a/mkdocs.yml b/mkdocs.yml index a54ebdd7aa..f09b40479d 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -64,6 +64,7 @@ nav: - modules/couchbase.md - modules/k3s.md - modules/localstack.md + - modules/mongodb.md - modules/mysql.md - modules/neo4j.md - modules/postgres.md @@ -78,7 +79,6 @@ nav: - examples/consul.md - examples/datastore.md - examples/firestore.md - - examples/mongodb.md - examples/nats.md - examples/nginx.md - examples/pubsub.md diff --git a/examples/mongodb/Makefile b/modules/mongodb/Makefile similarity index 100% rename from examples/mongodb/Makefile rename to modules/mongodb/Makefile diff --git a/examples/mongodb/go.mod b/modules/mongodb/go.mod similarity index 95% rename from examples/mongodb/go.mod rename to modules/mongodb/go.mod index 23869f8db8..bdf7b8564c 100644 --- a/examples/mongodb/go.mod +++ b/modules/mongodb/go.mod @@ -1,10 +1,10 @@ -module github.com/testcontainers/testcontainers-go/examples/mongodb +module github.com/testcontainers/testcontainers-go/modules/mongodb go 1.19 require ( github.com/testcontainers/testcontainers-go v0.22.0 - go.mongodb.org/mongo-driver v1.12.0 + go.mongodb.org/mongo-driver v1.12.1 ) require ( diff --git a/examples/mongodb/go.sum b/modules/mongodb/go.sum similarity index 99% rename from examples/mongodb/go.sum rename to modules/mongodb/go.sum index dafae04811..e3024772f3 100644 --- a/examples/mongodb/go.sum +++ b/modules/mongodb/go.sum @@ -108,8 +108,8 @@ github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7Jul github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -go.mongodb.org/mongo-driver v1.12.0 h1:aPx33jmn/rQuJXPQLZQ8NtfPQG8CaqgLThFtqRb0PiE= -go.mongodb.org/mongo-driver v1.12.0/go.mod h1:AZkxhPnFJUoH7kZlFkVKucV20K387miPfm7oimrSmK0= +go.mongodb.org/mongo-driver v1.12.1 h1:nLkghSU8fQNaK7oUmDhQFsnrtcoNy7Z6LVFKsEecqgE= +go.mongodb.org/mongo-driver v1.12.1/go.mod h1:/rGBTebI3XYboVmgz+Wv3Bcbl3aD0QF9zl6kDDw18rQ= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= diff --git a/modules/mongodb/mongodb.go b/modules/mongodb/mongodb.go new file mode 100644 index 0000000000..8e3bb305d1 --- /dev/null +++ b/modules/mongodb/mongodb.go @@ -0,0 +1,49 @@ +package mongodb + +import ( + "context" + + "github.com/testcontainers/testcontainers-go" + "github.com/testcontainers/testcontainers-go/wait" +) + +// defaultImage is the default MongoDB container image +const defaultImage = "mongo:6" + +// MongoDBContainer represents the MongoDB container type used in the module +type MongoDBContainer struct { + testcontainers.Container +} + +// RunContainer creates an instance of the MongoDB container type +func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*MongoDBContainer, error) { + req := testcontainers.ContainerRequest{ + Image: defaultImage, + ExposedPorts: []string{"27017/tcp"}, + WaitingFor: wait.ForAll( + wait.ForLog("Waiting for connections"), + wait.ForListeningPort("27017/tcp"), + ), + } + + genericContainerReq := testcontainers.GenericContainerRequest{ + ContainerRequest: req, + Started: true, + } + + for _, opt := range opts { + opt.Customize(&genericContainerReq) + } + + container, err := testcontainers.GenericContainer(ctx, genericContainerReq) + if err != nil { + return nil, err + } + + return &MongoDBContainer{Container: container}, nil +} + +// ConnectionString returns the connection string for the MongoDB container +func (c *MongoDBContainer) ConnectionString(ctx context.Context) (string, error) { + return c.Endpoint(ctx, "mongodb") +} diff --git a/examples/mongodb/mongodb_test.go b/modules/mongodb/mongodb_test.go similarity index 66% rename from examples/mongodb/mongodb_test.go rename to modules/mongodb/mongodb_test.go index 9b3e3b9d7f..957fb516da 100644 --- a/examples/mongodb/mongodb_test.go +++ b/modules/mongodb/mongodb_test.go @@ -12,10 +12,12 @@ import ( func TestMongoDB(t *testing.T) { ctx := context.Background() - container, err := startContainer(ctx) + // createMongoDBContainer { + container, err := RunContainer(ctx) if err != nil { t.Fatal(err) } + // } // Clean up the container after the test is complete t.Cleanup(func() { @@ -26,21 +28,18 @@ func TestMongoDB(t *testing.T) { // perform assertions - endpoint, err := container.Endpoint(ctx, "mongodb") + // connectionString { + endpoint, err := container.ConnectionString(ctx) if err != nil { - t.Error(fmt.Errorf("failed to get endpoint: %w", err)) + t.Error(fmt.Errorf("failed to get connection string: %w", err)) } + // } - mongoClient, err := mongo.NewClient(options.Client().ApplyURI(endpoint)) + mongoClient, err := mongo.Connect(ctx, options.Client().ApplyURI(endpoint)) if err != nil { t.Fatal(fmt.Errorf("error creating mongo client: %w", err)) } - err = mongoClient.Connect(ctx) - if err != nil { - t.Fatal(fmt.Errorf("error connecting to mongo: %w", err)) - } - err = mongoClient.Ping(ctx, nil) if err != nil { t.Fatal(fmt.Errorf("error pinging mongo: %w", err))