Skip to content

Conversation

@digital88
Copy link
Contributor

Hopefully closes #988

This implementation supports credentials only when replica set is disabled. Because using credentials with replica set requires rather complicated setup I think it could be implemented later as separate PR. I'm not even sure why replica set was implemented in first place, as TC creates single container for running tests it does not make much sense IMO. Either way the old replica set code remains untouched and is used by default, so nothing should break for existing users.

@netlify
Copy link

netlify bot commented Jul 9, 2025

Deploy Preview for testcontainers-node ready!

Name Link
🔨 Latest commit 20086b1
🔍 Latest deploy log https://app.netlify.com/projects/testcontainers-node/deploys/6880b22cb443b20008c1d6ac
😎 Deploy Preview https://deploy-preview-1070--testcontainers-node.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 project configuration.

@cristianrgreco cristianrgreco added enhancement New feature or request minor Backward compatible functionality labels Jul 9, 2025
@digital88
Copy link
Contributor Author

Also very interesting that 4.0 image fails tests with:

× MongodbContainer > should connect to mongo:4.0.1 with credentials and disabled replica set 127121ms (retry x3)
     → Server at localhost:41731 reports maximum wire version 7, but this version of the Node.js Driver requires at least 8 (MongoDB 4.2)

I do not encounter this on local machine. Need to investigate more.

@digital88 digital88 force-pushed the mongo-credentials branch from 7cab1ae to 31854b5 Compare July 10, 2025 18:46
@digital88
Copy link
Contributor Author

digital88 commented Jul 10, 2025

Ok, I decided to start from scratch here. I wrote a new mongodb-container-2.ts file with mongo container implementation. This version works for replica set both with and without auth. Mongo 5.0 and later is supported. Earlier images unfortunately don't work because of old mongo shell.
The main problem that I encountered was that mongo is doing some stuff in background after container is started and this causes eval commands to fail with Authentication failed errors. If I wait a little on breakpoint, tests suddenly start passing without errors. So I made basic retries logic in executeMongoEvalCommand and it actually solved this random errors I got earlier.
Edit: I'll try to port old code to this 'new' implementation.

@digital88
Copy link
Contributor Author

digital88 commented Jul 17, 2025

Also very interesting that 4.0 image fails tests with:

Had to pin mongoose to "8.15.0". Newer versions do not support mongo 4.

I moved all logic in containerStarted to healthcheck and changed wait strategy to wait for healthcheck. Took inspiration from searching how to run mongo replica set with auth in compose. Not sure about default timeouts tho.

Credentials work with images from 4.0.1 up to 8.0

@digital88
Copy link
Contributor Author

Tests with podman fail healthcheck even in older cases without credentials. I think my timeouts may be the reason? I have only docker installed locally. Will try later to test in podman too.

@digital88
Copy link
Contributor Author

Changed CMD to CMD-SHELL as used in other modules. Tests pass in podman locally.

@cristianrgreco
Copy link
Collaborator

@digital88 do you mind resolving the conflicts? I would but I’m afk at the moment. Should just be a testcontainers version bump

@digital88
Copy link
Contributor Author

@digital88 do you mind resolving the conflicts? I would but I’m afk at the moment. Should just be a testcontainers version bump

Resolved

@digital88
Copy link
Contributor Author

@digital88 do you mind resolving the conflicts? I would but I’m afk at the moment. Should just be a testcontainers version bump

Resolved

And resolved again 😄

@cristianrgreco
Copy link
Collaborator

cristianrgreco commented Jul 21, 2025

Apologies @digital88, things are a bit busy at the moment, I know I need to review this PR, I will get to it soon.

@cristianrgreco
Copy link
Collaborator

cristianrgreco commented Jul 21, 2025

Had to pin mongoose to "8.15.0". Newer versions do not support mongo 4.

Let's not pin the mongoose version. It's pinned because of a Mongo version which was EOL Feb 2024, so when we need to update mongoose for some other reason we'll be stuck. As this is a new behaviour we don't need to test it with older Mongo versions as there are no existing users using it, so let's remove the credentials test for Mongo 4. The existing Mongo 4 test continues to pass so that's good enough from my perspective that this isn't a breaking change. If a user wants to use an older version of Mongoose so that it works with their Mongo 4 then great, but let's not tie ourselves to it.

@cristianrgreco cristianrgreco changed the title (feat) mongo: support connecting with credentials Add support for MongoDBContainer credentials Jul 21, 2025
@digital88
Copy link
Contributor Author

Had to pin mongoose to "8.15.0". Newer versions do not support mongo 4.

Let's not pin the mongoose version. It's pinned because of a Mongo version which was EOL Feb 2024, so when we need to update mongoose for some other reason we'll be stuck. As this is a new behaviour we don't need to test it with older Mongo versions as there are no existing users using it, so let's remove the credentials test for Mongo 4. The existing Mongo 4 test continues to pass so that's good enough from my perspective that this isn't a breaking change. If a user wants to use an older version of Mongoose so that it works with their Mongo 4 then great, but let's not tie ourselves to it.

I unpinned mongoose. Made tests with creds to run just on mongo 8.0

@digital88
Copy link
Contributor Author

digital88 commented Jul 22, 2025

Oh, sorry, TC patch version just went up again...Will fix it in a minute.

@digital88
Copy link
Contributor Author

Looks like I messed lockfile..I'll fix it.

@cristianrgreco
Copy link
Collaborator

@digital88 Are there any more changes you'd like to make?

@digital88
Copy link
Contributor Author

@digital88 Are there any more changes you'd like to make?

I think it's good 👍

@cristianrgreco cristianrgreco merged commit c0571da into testcontainers:main Jul 23, 2025
316 checks passed
@cristianrgreco
Copy link
Collaborator

Thanks for the contribution, always a pleasure!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request minor Backward compatible functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@testcontainers/mongodb - how can creds to require be passed?

2 participants