Skip to content

Commit 73e25b0

Browse files
committed
Updates to run against non-Atlas docker.
1 parent 699b7db commit 73e25b0

19 files changed

Lines changed: 1081 additions & 133 deletions

File tree

CONTRIBUTING.md

Lines changed: 557 additions & 9 deletions
Large diffs are not rendered by default.

docker-compose.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
services:
2+
mongodb:
3+
image: mongo:8
4+
container_name: mongodb-test
5+
command: >
6+
mongod
7+
--replSet rs0
8+
--bind_ip_all
9+
--setParameter enableTestCommands=1
10+
ports:
11+
- "56665:27017"
12+
volumes:
13+
- mongodb_data:/data/db
14+
healthcheck:
15+
test: |
16+
mongosh --quiet --eval "
17+
try {
18+
rs.status();
19+
print('Replica set initialized');
20+
} catch(e) {
21+
print('Initializing replica set...');
22+
rs.initiate({
23+
_id: 'rs0',
24+
members: [{ _id: 0, host: 'localhost:27017' }]
25+
});
26+
}
27+
"
28+
interval: 5s
29+
timeout: 10s
30+
retries: 10
31+
start_period: 10s
32+
33+
volumes:
34+
mongodb_data:
35+
driver: local

start-mongodb.sh

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/bin/bash
2+
3+
# MongoDB Test Environment Startup Script
4+
# Starts MongoDB with single-node replica set and test commands enabled
5+
6+
set -e
7+
8+
# Colors for output
9+
RED='\033[0;31m'
10+
GREEN='\033[0;32m'
11+
YELLOW='\033[1;33m'
12+
BLUE='\033[0;34m'
13+
NC='\033[0m' # No Color
14+
15+
# Function to print colored messages
16+
print_info() {
17+
echo -e "${BLUE}[INFO]${NC} $1"
18+
}
19+
20+
print_success() {
21+
echo -e "${GREEN}[SUCCESS]${NC} $1"
22+
}
23+
24+
print_warning() {
25+
echo -e "${YELLOW}[WARNING]${NC} $1"
26+
}
27+
28+
print_error() {
29+
echo -e "${RED}[ERROR]${NC} $1"
30+
}
31+
32+
# Check if Docker is installed
33+
if ! command -v docker &> /dev/null; then
34+
print_error "Docker is not installed. Please install Docker first."
35+
exit 1
36+
fi
37+
38+
# Check if Docker Compose is available
39+
if ! docker compose version &> /dev/null; then
40+
print_error "Docker Compose is not available. Please install Docker Compose."
41+
exit 1
42+
fi
43+
44+
# Check if docker-compose.yml exists
45+
if [ ! -f "docker-compose.yml" ]; then
46+
print_error "docker-compose.yml not found in current directory."
47+
exit 1
48+
fi
49+
50+
print_info "Starting MongoDB with test commands enabled..."
51+
52+
# Stop any existing containers
53+
docker compose down 2>/dev/null || true
54+
55+
# Start MongoDB
56+
docker compose up -d
57+
58+
print_info "Waiting for MongoDB to be ready..."
59+
60+
# Wait for container to be healthy
61+
max_attempts=30
62+
attempt=0
63+
while [ $attempt -lt $max_attempts ]; do
64+
if docker compose ps | grep -q "healthy"; then
65+
print_success "MongoDB is ready!"
66+
break
67+
fi
68+
69+
attempt=$((attempt + 1))
70+
if [ $attempt -eq $max_attempts ]; then
71+
print_error "MongoDB failed to start within expected time."
72+
print_info "Showing container logs:"
73+
docker compose logs
74+
exit 1
75+
fi
76+
77+
echo -n "."
78+
sleep 2
79+
done
80+
81+
echo ""
82+
83+
# Display connection information
84+
print_success "MongoDB is running with the following configuration:"
85+
echo ""
86+
echo " • Single-node replica set: rs0"
87+
echo " • Test commands: ENABLED"
88+
echo " • Port: 56665"
89+
echo ""
90+
echo "Connection String:"
91+
echo " mongodb://localhost:56665/?replicaSet=rs0&directConnection=true"
92+
echo ""
93+
echo "C# Driver Connection String:"
94+
echo ' var connectionString = "mongodb://localhost:56665/?replicaSet=rs0&directConnection=true";'
95+
echo ' var client = new MongoClient(connectionString);'
96+
echo ""
97+
print_info "To stop MongoDB, run:"
98+
echo " docker compose down"
99+
echo ""
100+
print_info "To view logs, run:"
101+
echo " docker compose logs -f"
102+
echo ""

tests/AtlasConnectivity.Tests/ConnectivityTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using MongoDB.Bson;
1919
using MongoDB.Driver;
2020
using MongoDB.Driver.Core.TestHelpers.Logging;
21+
using MongoDB.TestHelpers.XunitExtensions;
2122
using Xunit;
2223
using Xunit.Abstractions;
2324

@@ -46,6 +47,8 @@ public ConnectivityTests(ITestOutputHelper testOutputHelper)
4647
[InlineData("ATLAS_SRV_TLS12")]
4748
public void Connection_to_Atlas_should_work(string environmentVariableName)
4849
{
50+
RequireEnvironment.Check().EnvironmentVariable(environmentVariableName);
51+
4952
var connectionString = Environment.GetEnvironmentVariable(environmentVariableName);
5053
connectionString.Should().NotBeNull();
5154

tests/AtlasConnectivity.Tests/Properties/AssemblyInfo.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,9 @@
1414
*/
1515

1616
using System.Runtime.InteropServices;
17+
using MongoDB.TestHelpers.XunitExtensions;
18+
using Xunit;
1719

1820
[assembly: ComVisible(false)]
21+
22+
[assembly: TestFramework(XunitExtensionsConstants.TimeoutEnforcingXunitFramework, XunitExtensionsConstants.TimeoutEnforcingFrameworkAssembly)]

tests/MongoDB.Driver.TestHelpers/Core/XunitExtensions/RequireServer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,8 @@ private bool IsRequirementSatisfied(BsonElement requirement)
338338
case "topology":
339339
var actualClusterType = CoreTestConfiguration.Cluster.Description.Type;
340340
var runOnClusterTypes = requirement.Value.AsBsonArray.Select(topology => MapTopologyToClusterType(topology.AsString)).ToList();
341-
return runOnClusterTypes.Contains(actualClusterType);
341+
return runOnClusterTypes.Contains(actualClusterType)
342+
&& !CoreTestConfiguration.ConnectionString.DirectConnection;
342343
case "csfle":
343344
return IsCsfleRequirementSatisfied(requirement);
344345
default:

tests/MongoDB.Driver.Tests/Encryption/AutoEncryptionTests.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ public void CryptClient_should_be_initialized([Values(false, true)] bool withAut
5353
{
5454
RequireServer.Check().Supports(Feature.ClientSideEncryption);
5555

56+
if (withAutoEncryption)
57+
{
58+
RequireEnvironment.Check().EnvironmentVariable("FLE_AWS_KEY");
59+
}
60+
5661
using (var client = GetClient(withAutoEncryption))
5762
{
5863
var libMongoCryptController = client.LibMongoCryptController;
@@ -74,9 +79,14 @@ public async Task Mongocryptd_should_be_initialized_when_auto_encryption([Values
7479
{
7580
RequireServer.Check().Supports(Feature.ClientSideEncryption);
7681

82+
if (withAutoEncryption)
83+
{
84+
RequireEnvironment.Check().EnvironmentVariable("FLE_AWS_KEY");
85+
}
86+
7787
using (var client = GetClient(
78-
withAutoEncryption,
79-
new Dictionary<string, object> { { "cryptSharedLibPath", "non_existing_path_to_use_mongocryptd" } }))
88+
withAutoEncryption,
89+
new Dictionary<string, object> { { "cryptSharedLibPath", "non_existing_path_to_use_mongocryptd" } }))
8090
{
8191
if (withAutoEncryption)
8292
{
@@ -124,6 +134,7 @@ public async Task Mongocryptd_should_be_initialized_when_auto_encryption([Values
124134
[Fact]
125135
public void Shared_library_should_be_loaded_when_CRYPT_SHARED_LIB_PATH_is_set()
126136
{
137+
RequireEnvironment.Check().EnvironmentVariable("FLE_AWS_KEY");
127138
RequireServer.Check().Supports(Feature.ClientSideEncryption);
128139
RequireEnvironment.Check().EnvironmentVariable("CRYPT_SHARED_LIB_PATH", isDefined: true, allowEmpty: false);
129140
Ensure.That(File.Exists(Environment.GetEnvironmentVariable("CRYPT_SHARED_LIB_PATH")), "CRYPT_SHARED_LIB_PATH should exist.");
@@ -161,7 +172,7 @@ private MongoClient GetClient(bool withAutoEncryption = false, Dictionary<string
161172
}
162173

163174
// private methods
164-
private IReadOnlyDictionary<string, IReadOnlyDictionary<string, object>> GetKmsProviders() => EncryptionTestHelper.GetKmsProviders(filter: "local");
175+
private IReadOnlyDictionary<string, IReadOnlyDictionary<string, object>> GetKmsProviders() => EncryptionTestHelper.GetKmsProviders("local");
165176
}
166177

167178
internal static class LibMongoCryptControllerBaseReflector

tests/MongoDB.Driver.Tests/Encryption/ClientEncryptionTests.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public class ClientEncryptionTests
4949
[Fact]
5050
public async Task AddAlternateKeyName_should_correctly_handle_input_arguments()
5151
{
52+
RequireEnvironment.Check().EnvironmentVariable("FLE_AWS_KEY");
5253
RequireServer.Check().Supports(Feature.ClientSideEncryption);
5354

5455
var guid = new Guid();
@@ -63,6 +64,7 @@ public async Task AddAlternateKeyName_should_correctly_handle_input_arguments()
6364
[Fact]
6465
public async Task CreateDataKey_should_correctly_handle_input_arguments()
6566
{
67+
RequireEnvironment.Check().EnvironmentVariable("FLE_AWS_KEY");
6668
RequireServer.Check().Supports(Feature.ClientSideEncryption);
6769

6870
using (var subject = CreateSubject())
@@ -78,6 +80,8 @@ public async Task CreateDataKey_should_correctly_handle_input_arguments()
7880
[Fact]
7981
public async Task CreateEncryptedCollection_should_handle_input_arguments()
8082
{
83+
RequireEnvironment.Check().EnvironmentVariable("FLE_AWS_KEY");
84+
8185
const string kmsProvider = "local";
8286
const string collectionName = "collName";
8387
var createCollectionOptions = new CreateCollectionOptions();
@@ -119,6 +123,8 @@ public async Task CreateEncryptedCollection_should_handle_input_arguments()
119123
[Fact]
120124
public async Task CreateEncryptedCollection_should_handle_generated_key_when_second_key_failed()
121125
{
126+
RequireEnvironment.Check().EnvironmentVariable("FLE_AWS_KEY");
127+
122128
const string kmsProvider = "local";
123129
const string collectionName = "collName";
124130
const string encryptedFieldsStr = "{ fields : [{ keyId : null }, { keyId : null }] }";
@@ -210,6 +216,8 @@ datakeys have already been created by the helper.
210216
[InlineData("{ fields : [{ keyId : 3 }, { keyId : null }] }", "{ fields: [{ keyId : 3 }, { keyId : '#binary_generated#' }] }")]
211217
public async Task CreateEncryptedCollection_should_handle_various_encryptedFields(string encryptedFieldsStr, string expectedResult)
212218
{
219+
RequireEnvironment.Check().EnvironmentVariable("FLE_AWS_KEY");
220+
213221
const string kmsProvider = "local";
214222
const string collectionName = "collName";
215223
var serverId = new ServerId(__clusterId, __endPoint);
@@ -287,6 +295,7 @@ public bool Equals(BsonDocument x, BsonDocument y) =>
287295
[Fact]
288296
public void CryptClient_should_be_initialized()
289297
{
298+
RequireEnvironment.Check().EnvironmentVariable("FLE_AWS_KEY");
290299
RequireServer.Check().Supports(Feature.ClientSideEncryption);
291300

292301
using (var subject = CreateSubject())
@@ -299,6 +308,7 @@ public void CryptClient_should_be_initialized()
299308
[Fact]
300309
public async Task Decrypt_should_correctly_handle_input_arguments()
301310
{
311+
RequireEnvironment.Check().EnvironmentVariable("FLE_AWS_KEY");
302312
RequireServer.Check().Supports(Feature.ClientSideEncryption);
303313

304314
using (var subject = CreateSubject())
@@ -311,6 +321,7 @@ public async Task Decrypt_should_correctly_handle_input_arguments()
311321
[Fact]
312322
public async Task Encrypt_should_correctly_handle_input_arguments()
313323
{
324+
RequireEnvironment.Check().EnvironmentVariable("FLE_AWS_KEY");
314325
RequireServer.Check().Supports(Feature.ClientSideEncryption);
315326

316327
using (var subject = CreateSubject())
@@ -327,6 +338,7 @@ public async Task Encrypt_should_correctly_handle_input_arguments()
327338
[ParameterAttributeData]
328339
public async Task Encryption_should_use_correct_binarySubType([Values(false, true)] bool async)
329340
{
341+
RequireEnvironment.Check().EnvironmentVariable("FLE_AWS_KEY");
330342
RequireServer.Check().Supports(Feature.ClientSideEncryption);
331343

332344
using (var subject = CreateSubject())
@@ -347,6 +359,7 @@ public async Task Encryption_should_use_correct_binarySubType([Values(false, tru
347359
[Fact]
348360
public async Task GetKeyByAlternateKeyName_should_correctly_handle_input_arguments()
349361
{
362+
RequireEnvironment.Check().EnvironmentVariable("FLE_AWS_KEY");
350363
RequireServer.Check().Supports(Feature.ClientSideEncryption);
351364

352365
using (var subject = CreateSubject())
@@ -359,6 +372,7 @@ public async Task GetKeyByAlternateKeyName_should_correctly_handle_input_argumen
359372
[Fact]
360373
public async Task RemoveAlternateKeyName_should_correctly_handle_input_arguments()
361374
{
375+
RequireEnvironment.Check().EnvironmentVariable("FLE_AWS_KEY");
362376
RequireServer.Check().Supports(Feature.ClientSideEncryption);
363377

364378
var guid = new Guid();
@@ -373,6 +387,7 @@ public async Task RemoveAlternateKeyName_should_correctly_handle_input_arguments
373387
[Fact]
374388
public async Task RewrapManyDataKey_should_correctly_handle_input_arguments()
375389
{
390+
RequireEnvironment.Check().EnvironmentVariable("FLE_AWS_KEY");
376391
RequireServer.Check().Supports(Feature.ClientSideEncryption);
377392

378393
using (var subject = CreateSubject())
@@ -391,7 +406,7 @@ private ClientEncryption CreateSubject(IMongoClient client = null)
391406
var clientEncryptionOptions = new ClientEncryptionOptions(
392407
client ?? DriverTestConfiguration.Client,
393408
__keyVaultCollectionNamespace,
394-
kmsProviders: EncryptionTestHelper.GetKmsProviders(filter: "local"));
409+
kmsProviders: EncryptionTestHelper.GetKmsProviders("local"));
395410

396411
return new ClientEncryption(clientEncryptionOptions);
397412
}

tests/MongoDB.Driver.Tests/MongoClientSettingsTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System;
1717
using System.Collections.Generic;
1818
using System.Linq;
19+
using System.Net;
1920
using System.Net.Sockets;
2021
using System.Security.Authentication;
2122
using System.Text;
@@ -726,6 +727,8 @@ public void TestFromUrlWithMongoDBX509_without_username()
726727
[ParameterAttributeData]
727728
public void TestFromUrlWithMongoDBAWS_should_parse_credentials_correctly([Values(false, true)] bool escapeToken)
728729
{
730+
RequireEnvironment.Check().HostReachable(new DnsEndPoint("awssessiontokentest.example.net", 53));
731+
729732
const string authMechanism = "MONGODB-AWS";
730733
const string username = "AKIAIOSFODNN7EXAMPLE";
731734
const string password = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYzEXAMPLEKEY";

tests/MongoDB.Driver.Tests/MongoUrlTests.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System;
1717
using System.Collections.Generic;
1818
using System.ComponentModel;
19+
using System.Net;
1920
using System.Threading.Tasks;
2021
using FluentAssertions;
2122
using MongoDB.Driver.Core.Compression;
@@ -66,6 +67,8 @@ public void constructor_with_string_should_set_isResolved_to_expected_value(stri
6667
[InlineData("mongodb+srv://test2.test.build.10gen.cc")]
6768
public void constructor_with_resolved_connection_string_should_initialize_resolved_instance(string url)
6869
{
70+
RequireEnvironment.Check().HostReachable(new DnsEndPoint("test2.test.build.10gen.cc", 53));
71+
6972
var connectionString = new ConnectionString(url);
7073
var resolvedConnectionString = connectionString.Resolve();
7174
var builder = new MongoUrlBuilder(resolvedConnectionString);
@@ -126,6 +129,8 @@ public void Resolve_with_resolveHosts_should_return_expected_result(string url,
126129
[InlineData("mongodb+srv://test1.test.build.10gen.cc/?srvMaxHosts=2", true, 2, true)]
127130
public void Resolve_with_srvMaxHosts_should_return_expected_result(string url, bool resolveHosts, int expectedSrvMaxHosts, bool async)
128131
{
132+
RequireEnvironment.Check().HostReachable(new DnsEndPoint("test1.test.build.10gen.cc", 53));
133+
129134
var subject = new MongoUrl(url);
130135

131136
MongoUrl result;

0 commit comments

Comments
 (0)