Skip to content

CSHARP-5930: Enable running tests locally using Docker#1920

Open
ajcvickers wants to merge 4 commits intomongodb:mainfrom
ajcvickers:Tests_3
Open

CSHARP-5930: Enable running tests locally using Docker#1920
ajcvickers wants to merge 4 commits intomongodb:mainfrom
ajcvickers:Tests_3

Conversation

@ajcvickers
Copy link
Copy Markdown
Contributor

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.

@ajcvickers ajcvickers requested a review from a team as a code owner March 19, 2026 12:10
@ajcvickers ajcvickers requested review from Copilot and papafe March 19, 2026 12:10
@ajcvickers ajcvickers added the chore Non–user-facing code changes (tests, build scripts, etc.). label Mar 19, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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.MongoUri so examples and smoke tests pick up MONGODB_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.

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);
Copy link
Copy Markdown
Member

@sanych-sun sanych-sun Mar 19, 2026

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@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();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed, and that's what I am going to investigate.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed the cluster checks here, since I think Supports(Feature.Transactions) is sufficient.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed the cluster checks here, since I think Supports(Feature.Csfle2) is sufficient.

@ajcvickers ajcvickers force-pushed the Tests_3 branch 2 times, most recently from 952f45f to 7a449dd Compare March 23, 2026 10:22
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.
@ajcvickers ajcvickers changed the title CSHARP-5930: Enable SmoteTests and Driver.Examples running locally CSHARP-5930: Enable running tests locally using Docker Mar 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

chore Non–user-facing code changes (tests, build scripts, etc.).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants