Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(modules.mongodb): add replica set support via opts #2469

Merged
merged 3 commits into from May 8, 2024

Conversation

heiytor
Copy link
Contributor

@heiytor heiytor commented Apr 5, 2024

What does this PR do?

Implementing a WithReplicaSet function to configure the container to run with a default single-node replica set named "rs". This function appends "--replSet rs" to req.Cmd to initialize MongoDB in preparation to receive a replica set. Additionally, after execution, it initializes the replica set using "rs.initiate()", configuring the current host as the PRIMARY.

Why is it important?

I've been utilizing testcontainers to test my service environment, which necessitates a replica set to execute transactions. As I've developed this functionality for internal use, I believe it would be beneficial to others as well.

Related issues

This PR closes #2138. Additionally, I noticed that there is another PR, #2139, which also aims to implement this feature. However, it has not been updated for a while, and this branch has certain differences, such as utilizing both mongosh and mongo to ensure version compatibility.

How to test this PR

I've tested this in a production environment, so it should work fine. Additionally, I've added new test cases to TestMongoDB.

Follow-ups

Maybe adding a sharding option would be great.

@heiytor heiytor requested a review from a team as a code owner April 5, 2024 18:18
Copy link

netlify bot commented Apr 5, 2024

Deploy Preview for testcontainers-go ready!

Name Link
🔨 Latest commit d5b7dc8
🔍 Latest deploy log https://app.netlify.com/sites/testcontainers-go/deploys/663b8707c3144400089210aa
😎 Deploy Preview https://deploy-preview-2469--testcontainers-go.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@heiytor
Copy link
Contributor Author

heiytor commented Apr 5, 2024

Some differences between this PR and #2139 include:

  1. the utilization of PostReadies instead of PostStart. I observed that occasionally, in PostStart, the availability of mongosh or mongo was unreliable.
  2. does not implement a wait function because the call to rs.initiate automatically designates the current container as the PRIMARY node (resulting in db.runCommand({ isMaster: 1 }).ismaster being automatically set to true).
  3. Does not modify any existing functions (such as ConnectionString) and does not require saving the replica set name.

Copy link
Collaborator

@mdelapenya mdelapenya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @heiytor I think we are going to build on top of this PR instead of #2139. Could you address the comments in there? I can do it myself adding commits on top of yours, in the case you have no bandwidth. Wdyt?

modules/mongodb/mongodb.go Outdated Show resolved Hide resolved
@heiytor heiytor force-pushed the feat/mongodb-replica-set branch 2 times, most recently from 504cd35 to 4e6248c Compare May 8, 2024 12:13
The `WithReplicaSet` option configures the MongoDB container to run a
single-node replica set named "rs". The container will wait until the
replica set is ready.
Copy link
Collaborator

@mdelapenya mdelapenya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@@ -58,6 +58,12 @@ It is used in conjunction with `WithUsername` to set a username and its password

E.g. `testcontainers.WithPassword("mymongopwd")`.

#### WithReplicaSet
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@heiytor I went ahead and added the docs for the new option: https://deploy-preview-2469--testcontainers-go.netlify.app/modules/mongodb/#withreplicaset

Once the CI passes, I'll merge this one, thanks!

@mdelapenya mdelapenya self-assigned this May 8, 2024
@mdelapenya mdelapenya added the feature New functionality or new behaviors on the existing one label May 8, 2024
@mdelapenya mdelapenya changed the title adding replica set support via opts feat(modules.mongodb): add replica set support via opts May 8, 2024
Copy link
Collaborator

@mdelapenya mdelapenya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thanks!

@mdelapenya mdelapenya merged commit 788097d into testcontainers:main May 8, 2024
104 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New functionality or new behaviors on the existing one
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature]: MongoDB ReplicaSet
3 participants