-
-
Notifications
You must be signed in to change notification settings - Fork 484
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for MongoDB testing module (#1447)
* Add support for MongoDB testing module This commit introduces functionalities for MongoDB testing, achieving 3 main goals: 1. Added mongodb module, `github.com/testcontainers/testcontainers-go/modules/mongodb`, in `dependabot.yml` with a monthly update cycle. 2. Created new `.md` documentation file for this module, providing details on how to add MongoDB to the Go dependencies, how to use this module, the MongoDB module reference, and other related details. 3. Implemented test files to verify MongoDB connection and integration, and added a `Makefile` for test execution. Justification: This helps users in testing their mongodb related go functionalities, effectively and efficiently. * Remove MongoDB example and associated files * fix: add schedule.day * Removed MongoDB from the CI test-examples matrix * Add ConnectionString method details in MongoDB documentation * Added code snippet * "Corrected error message in MongoDB test The error message was updated to clarify its meaning in modules/mongodb/mongodb_test.go. It now specifically states "failed to get connection string" instead of "failed to get endpoint", which enhances the readability and precision in case of a related error occurrence." * "Update MongoDB documentation and test example This update corrects the MongoDB usage example in our documentation in file docs/modules/mongodb.md and improves the corresponding test case in modules/mongodb/mongodb_test.go. Previously, the reference to 'Creating a MongoDB container' in documentation was missing the actual code block. The test has been modified to highlight the container setup steps visually within the code, which enhances test readability. This helps developers understand how to use the MongoDB module more effectively." * fix doc * docs: remove code snippet --------- Co-authored-by: Manuel de la Peña <[email protected]> Co-authored-by: Manuel de la Peña <[email protected]>
- Loading branch information
1 parent
82c79f7
commit f4d9b1d
Showing
11 changed files
with
153 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# MongoDB | ||
|
||
Not available until the next release of testcontainers-go <a href="https://github.com/testcontainers/testcontainers-go"><span class="tc-version">:material-tag: main</span></a> | ||
|
||
## 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 | ||
|
||
<!--codeinclude--> | ||
[Creating a MongoDB container](../../modules/mongodb/mongodb_test.go) inside_block:createMongoDBContainer | ||
<!--/codeinclude--> | ||
|
||
## 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://<host>:<port>`. | ||
|
||
<!--codeinclude--> | ||
[Get connection string](../../modules/mongodb/mongodb_test.go) inside_block:connectionString | ||
<!--/codeinclude--> |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters