CSHARP-5930: Enable running tests locally using Docker#1920
CSHARP-5930: Enable running tests locally using Docker#1920ajcvickers wants to merge 4 commits intomongodb:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR aims to make the smoke tests and MongoDB.Driver.Examples integration tests runnable locally by honoring MONGODB_URI (or MONGO_URI) instead of assuming mongodb://localhost, and by loosening a few server/topology gates so Atlas Local 8.2 default setups aren’t unnecessarily excluded.
Changes:
- Introduce/expand usage of
InfrastructureUtilities.MongoUriso examples and smoke tests pick upMONGODB_URI/MONGO_URI. - Adjust some topology requirements (e.g., sessions/causal consistency) and improve test isolation in change stream examples by waiting for worker threads to finish.
- Add an idempotency guard to auto-encryption provider registration.
Reviewed changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/SmokeTests/MongoDB.Driver.SmokeTests.Sdk/LibmongocryptTests.cs | Use configured MongoDB URI for key vault client. |
| tests/MongoDB.TestHelpers/XunitExtensions/TimeoutEnforcing/TimeoutEnforcingXunitTestAssemblyRunner.cs | Change unobserved-exception tracking test selection logic. |
| tests/MongoDB.Driver.TestHelpers/Core/XunitExtensions/RequireServer.cs | Allow SupportsCausalConsistency to run on standalone when sessions are supported. |
| tests/MongoDB.Driver.Examples/TransactionExamplesForDocs/WithTransactionExample1.cs | Expand eligible cluster types for transaction example. |
| tests/MongoDB.Driver.Examples/StableApiExamples.cs | Use configured URI in the strict migration example. |
| tests/MongoDB.Driver.Examples/InfrastructureUtilities.cs | New helper to read MONGODB_URI/MONGO_URI with localhost fallback. |
| tests/MongoDB.Driver.Examples/ExplicitEncryptionExamples.cs | Use configured URI when creating clients for explicit encryption examples. |
| tests/MongoDB.Driver.Examples/DocumentationExamples.cs | Update expected rendering for a $slice projection. |
| tests/MongoDB.Driver.Examples/ClientSideEncryption2Examples.cs | Use configured URI for key vault/encrypted clients and relax topology gating. |
| tests/MongoDB.Driver.Examples/ClientEncryptionExamples.cs | Use configured URI for auto-encryption client settings. |
| tests/MongoDB.Driver.Examples/ChangeStreamExamples.cs | Ensure worker threads complete before tests finish (adds joins / refactors worker loop). |
| tests/MongoDB.Driver.Examples/CausalConsistencyExamples.cs | Build MongoClientSettings for read preference rather than hardcoded URI query string. |
| src/MongoDB.Driver/Encryption/AutoEncryptionProviderRegistry.cs | Attempt to make provider registration idempotent. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
...goDB.TestHelpers/XunitExtensions/TimeoutEnforcing/TimeoutEnforcingXunitTestAssemblyRunner.cs
Show resolved
Hide resolved
tests/MongoDB.Driver.Examples/TransactionExamplesForDocs/WithTransactionExample1.cs
Outdated
Show resolved
Hide resolved
| public void Example1() | ||
| { | ||
| RequireServer.Check().ClusterTypes(ClusterType.ReplicaSet, ClusterType.Sharded).Supports(Feature.Transactions); | ||
| RequireServer.Check().ClusterTypes(ClusterType.ReplicaSet, ClusterType.Sharded, ClusterType.Standalone).Supports(Feature.Transactions); |
There was a problem hiding this comment.
I have a provocative question: why do we even have Examples project? We do not run this as a part of our CI, and also as far as I know Docs Team has their own repo with runnable examples. Should we remove this or at least convert to non-tests like project?
There was a problem hiding this comment.
@sanych-sun Very good questions! I don't know if we need this code, and if we don't, then it should be removed. @BorisDog?
If we do need this code, then it is better that it can be run. Dead code (code that is never run) rots. EF Core has runnable examples in the docs repo. This code did not run as part of the CI, so each year before the release I would load and run each example, and fix the ones where the rot had set in. This was a pain, but better than having completely un-run code. We could do the same thing here, but I'm not sure it is better than just having the code in tests that we can run easily. Like the unit tests, it's not like these are slow.
| public RequireServer SupportsCausalConsistency() | ||
| { | ||
| return ClusterTypes(Clusters.ClusterType.Sharded, Clusters.ClusterType.ReplicaSet).SupportsSessions(); | ||
| return ClusterTypes(Clusters.ClusterType.Sharded, Clusters.ClusterType.ReplicaSet, Clusters.ClusterType.Standalone).SupportsSessions(); |
There was a problem hiding this comment.
We probably should not allow this for any standalone server, but probably do some explicit check for Atlas. Is there any way to figure out the server is Atlas one?
There was a problem hiding this comment.
Agreed, and that's what I am going to investigate.
There was a problem hiding this comment.
So I could not find a way to detect Atlas, and Claude spent lots of tokens then also said that it was not possible to do it reliably and was only able to suggest some quite dodgy heuristics.
Instead, I've put some code in to check if the connection string for running Atlas tests is set to the same as the regular connection string, and if it is, then we still run these tests, since we know the connection string is for Atlas. These means a local setup using Atlas local will always run these, but usually they won't run elsewhere unless the cluster is not Standalone.
| public void Example1() | ||
| { | ||
| RequireServer.Check().ClusterTypes(ClusterType.ReplicaSet, ClusterType.Sharded).Supports(Feature.Transactions); | ||
| RequireServer.Check().ClusterTypes(ClusterType.ReplicaSet, ClusterType.Sharded, ClusterType.Standalone).Supports(Feature.Transactions); |
There was a problem hiding this comment.
Removed the cluster checks here, since I think Supports(Feature.Transactions) is sufficient.
There was a problem hiding this comment.
Generally speaking we have to check for topology here. Because Supports(feature) checks wire version only, but some features are not available on every topology (in this case Transactions require ReplicaSet and Sharded cluster). Other examples could be: change streams and casual consistency.
| public void FLE2AutomaticEncryption() | ||
| { | ||
| RequireServer.Check().Supports(Feature.Csfle2).ClusterTypes(ClusterType.ReplicaSet, ClusterType.Sharded, ClusterType.LoadBalanced); | ||
| RequireServer.Check().Supports(Feature.Csfle2).ClusterTypes(ClusterType.ReplicaSet, ClusterType.Sharded, ClusterType.LoadBalanced, ClusterType.Standalone); |
There was a problem hiding this comment.
Removed the cluster checks here, since I think Supports(Feature.Csfle2) is sufficient.
952f45f to
7a449dd
Compare
I have one env var set: MONGODB_URI I'm using Atlas Local 8.2, which is a standalone cluster by default, but tests that pass were being excluded from running. If people are using non-Atlas and this breaks, then we'll have to figure something out.
I have one env var set: MONGODB_URI
I'm using Atlas Local 8.2, which is a standalone cluster by default, but tests that pass were being excluded from running. If people are using non-Atlas and this breaks, then we'll have to figure something out.