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
Member

@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
@heiytor heiytor force-pushed the feat/mongodb-replica-set branch from 4e6248c to fa37148 Compare May 8, 2024 12:52
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.
@heiytor heiytor force-pushed the feat/mongodb-replica-set branch from fa37148 to e6bc11e Compare May 8, 2024 12:56
Copy link
Member

@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
Member

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
Member

@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
mdelapenya added a commit to bearrito/testcontainers-go that referenced this pull request Jun 11, 2024
* main: (48 commits)
  Fix race condition when looking up reaper (ryuk) container (testcontainers#2508)
  chore: bring golangci-lint back (testcontainers#2571)
  docs(compose): Fix typo docker compose docs (testcontainers#2565)
  Handle error properly during port forwarding initialization. (testcontainers#2550)
  chore: pin vearch version (testcontainers#2568)
  feat: add vearch module (testcontainers#2560)
  chore: run tests against latest Docker engine, nightly (testcontainers#2566)
  chore(deps): bump mkdocs-include-markdown-plugin from 6.0.4 to 6.0.7 (testcontainers#2562)
  Fix network accessor for port-forwarding feature (testcontainers#2551)
  --- (testcontainers#2549)
  fix: update search bar eval in mkdocs (testcontainers#2547)
  docs: improve contributing docs for code snippets (testcontainers#2546)
  chore: use a virtualenv for working with the docs site (testcontainers#2545)
  docs: document test session semantics (testcontainers#2544)
  feat(ryuk): allow to configure ryuk timeouts using env variables (testcontainers#2541)
  docs: fix CircleCI docs (testcontainers#2539)
  fix: add import to module generation (testcontainers#2537)
  chore: prepare for next minor development cycle (0.32.0)
  chore: use new version (v0.31.0) in modules and examples
  feat(mongodb): add replica set support via opts (testcontainers#2469)
  ...
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