forked from mongodb/mongo-csharp-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoreTestConfiguration.cs
572 lines (493 loc) · 23.3 KB
/
CoreTestConfiguration.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Serializers;
using MongoDB.Driver.Core.Authentication;
using MongoDB.Driver.Core.Bindings;
using MongoDB.Driver.Core.Clusters;
using MongoDB.Driver.Core.Clusters.ServerSelectors;
using MongoDB.Driver.Core.Configuration;
using MongoDB.Driver.Core.Misc;
using MongoDB.Driver.Core.Operations;
using MongoDB.Driver.Core.Servers;
using MongoDB.Driver.Core.WireProtocol.Messages.Encoders;
namespace MongoDB.Driver
{
public static class CoreTestConfiguration
{
#region static
// static fields
private static Lazy<ICluster> __cluster = new Lazy<ICluster>(CreateCluster, isThreadSafe: true);
private static Lazy<ConnectionString> __connectionString = new Lazy<ConnectionString>(CreateConnectionString, isThreadSafe: true);
private static Lazy<ConnectionString> __connectionStringWithMultipleShardRouters = new Lazy<ConnectionString>(
GetConnectionStringWithMultipleShardRouters, isThreadSafe: true);
private static Lazy<DatabaseNamespace> __databaseNamespace = new Lazy<DatabaseNamespace>(GetDatabaseNamespace, isThreadSafe: true);
private static Lazy<TimeSpan> __defaultServerSelectionTimeout = new Lazy<TimeSpan>(GetDefaultServerSelectionTimeout, isThreadSafe: true);
private static Lazy<int> __maxWireVersion = new Lazy<int>(GetMaxWireVersion, isThreadSafe: true);
private static MessageEncoderSettings __messageEncoderSettings = new MessageEncoderSettings();
private static Lazy<int> __numberOfMongoses = new Lazy<int>(GetNumberOfMongoses, isThreadSafe: true);
private static Lazy<ServerApi> __serverApi = new Lazy<ServerApi>(GetServerApi, isThreadSafe: true);
private static Lazy<bool> __serverless = new Lazy<bool>(GetServerless, isThreadSafe: true);
private static Lazy<SemanticVersion> __serverVersion = new Lazy<SemanticVersion>(GetServerVersion, isThreadSafe: true);
private static Lazy<string> __storageEngine = new Lazy<string>(GetStorageEngine, isThreadSafe: true);
private static TraceSource __traceSource;
public static TimeSpan DefaultTestTimeout { get; } = TimeSpan.FromMinutes(3);
// static properties
public static ICluster Cluster
{
get { return __cluster.Value; }
}
public static ConnectionString ConnectionString
{
get { return __connectionString.Value; }
}
public static ConnectionString ConnectionStringWithMultipleShardRouters
{
get => __connectionStringWithMultipleShardRouters.Value;
}
public static DatabaseNamespace DatabaseNamespace
{
get { return __databaseNamespace.Value; }
}
public static ICluster ClusterWithConnectedPrimary
{
get
{
var timeout = __defaultServerSelectionTimeout.Value;
var cluster = __cluster.Value;
if (!SpinWait.SpinUntil(() => cluster.Description.Servers.Any(s => s.Type == ServerType.ReplicaSetPrimary), timeout))
{
throw new Exception($"The cluster didn't find a primary during {timeout}. The current cluster description: {cluster.Description}.");
}
return cluster;
}
}
public static MessageEncoderSettings MessageEncoderSettings
{
get { return __messageEncoderSettings; }
}
public static int NumberOfMongoses => __numberOfMongoses.Value;
public static bool RequireApiVersion
{
get { return __serverApi.Value != null; }
}
public static ServerApi ServerApi
{
get { return __serverApi.Value; }
}
public static bool Serverless
{
get { return __serverless.Value; }
}
public static SemanticVersion ServerVersion => __serverVersion.Value;
public static int MaxWireVersion => __maxWireVersion.Value;
public static string StorageEngine => __storageEngine.Value;
public static TraceSource TraceSource
{
get { return __traceSource; }
}
// static methods
public static ClusterBuilder ConfigureCluster()
{
return ConfigureCluster(new ClusterBuilder());
}
public static ClusterBuilder ConfigureCluster(ClusterBuilder builder)
{
builder = builder
.ConfigureWithConnectionString(__connectionString.Value, __serverApi.Value)
.ConfigureCluster(c => c.With(serverSelectionTimeout: __defaultServerSelectionTimeout.Value));
if (__connectionString.Value.Tls.HasValue &&
__connectionString.Value.Tls.Value &&
__connectionString.Value.AuthMechanism != null &&
__connectionString.Value.AuthMechanism == MongoDBX509Authenticator.MechanismName)
{
var certificateFilename = Environment.GetEnvironmentVariable("MONGO_X509_CLIENT_CERTIFICATE_PATH");
if (certificateFilename != null)
{
builder.ConfigureSsl(ssl =>
{
var password = Environment.GetEnvironmentVariable("MONGO_X509_CLIENT_CERTIFICATE_PASSWORD");
X509Certificate cert;
if (password == null)
{
cert = new X509Certificate2(certificateFilename);
}
else
{
cert = new X509Certificate2(certificateFilename, password);
}
return ssl.With(
clientCertificates: new[] { cert });
});
}
}
return ConfigureLogging(builder);
}
public static ClusterBuilder ConfigureLogging(ClusterBuilder builder)
{
var environmentVariable = Environment.GetEnvironmentVariable("MONGO_LOGGING");
if (environmentVariable == null)
{
return builder;
}
SourceLevels defaultLevel;
if (!Enum.TryParse<SourceLevels>(environmentVariable, ignoreCase: true, result: out defaultLevel))
{
return builder;
}
__traceSource = new TraceSource("mongodb-tests", defaultLevel);
__traceSource.Listeners.Clear(); // remove the default listener
var listener = new TextWriterTraceListener(Console.Out);
listener.TraceOutputOptions = TraceOptions.DateTime;
__traceSource.Listeners.Add(listener);
return builder.TraceWith(__traceSource);
}
public static ICluster CreateCluster()
{
return CreateCluster(b => b);
}
public static ICluster CreateCluster(Func<ClusterBuilder, ClusterBuilder> postConfigurator, bool allowDataBearingServers = false)
{
var builder = new ClusterBuilder();
builder = ConfigureCluster(builder);
builder = postConfigurator(builder);
return CreateCluster(builder, allowDataBearingServers);
}
public static ICluster CreateCluster(ClusterBuilder builder, bool allowDataBearingServers = false)
{
var expectedServerKey = allowDataBearingServers ? "Databearing" : "Writable";
bool hasExpectedServer = false;
var cluster = builder.BuildCluster();
cluster.DescriptionChanged += (o, e) =>
{
var anyExpectedServer = e.NewClusterDescription.Servers.Any(
description => allowDataBearingServers ? description.IsDataBearing : description.Type.IsWritable());
if (__traceSource != null)
{
__traceSource.TraceEvent(TraceEventType.Information, 0, $"CreateCluster: DescriptionChanged event handler called.");
__traceSource.TraceEvent(TraceEventType.Information, 0, $"CreateCluster: any{expectedServerKey}Server = {anyExpectedServer}.");
__traceSource.TraceEvent(TraceEventType.Information, 0, $"CreateCluster: new description: {e.NewClusterDescription.ToString()}.");
}
Volatile.Write(ref hasExpectedServer, anyExpectedServer);
};
if (__traceSource != null)
{
__traceSource.TraceEvent(TraceEventType.Information, 0, "CreateCluster: initializing cluster.");
}
cluster.Initialize();
// wait until the cluster has connected to the expected server
if (!SpinWait.SpinUntil(() => Volatile.Read(ref hasExpectedServer), TimeSpan.FromSeconds(30)))
{
var message = $"Test cluster has no {expectedServerKey.ToLower()} server. Client view of the cluster is {cluster.Description}.";
throw new Exception(message);
}
if (__traceSource != null)
{
__traceSource.TraceEvent(TraceEventType.Information, 0, $"CreateCluster: {expectedServerKey.ToLower()} server found.");
}
return cluster;
}
public static CollectionNamespace GetCollectionNamespaceForTestClass(Type testClassType)
{
var collectionName = TruncateCollectionNameIfTooLong(__databaseNamespace.Value, testClassType.Name);
return new CollectionNamespace(__databaseNamespace.Value, collectionName);
}
public static CollectionNamespace GetCollectionNamespaceForTestMethod(string className, string methodName)
{
var collectionName = TruncateCollectionNameIfTooLong(__databaseNamespace.Value, $"{className}-{methodName}");
return new CollectionNamespace(__databaseNamespace.Value, collectionName);
}
public static string GetCryptSharedLibPath() => Environment.GetEnvironmentVariable("CRYPT_SHARED_LIB_PATH");
public static ConnectionString CreateConnectionString()
{
var uri = Environment.GetEnvironmentVariable("MONGODB_URI") ?? Environment.GetEnvironmentVariable("MONGO_URI");
if (uri == null)
{
uri = "mongodb://localhost";
if (IsReplicaSet(uri))
{
uri += "/?connect=replicaSet";
}
}
return new ConnectionString(uri);
}
private static ConnectionString GetConnectionStringWithMultipleShardRouters()
{
var uri = Environment.GetEnvironmentVariable("MONGODB_URI_WITH_MULTIPLE_MONGOSES") ?? "mongodb://localhost,localhost:27018";
return new ConnectionString(uri);
}
private static DatabaseNamespace GetDatabaseNamespace()
{
if (!string.IsNullOrEmpty(__connectionString.Value.DatabaseName))
{
return new DatabaseNamespace(__connectionString.Value.DatabaseName);
}
var timestamp = DateTime.Now.ToString("MMddHHmm");
return new DatabaseNamespace("Tests" + timestamp);
}
private static ServerApi GetServerApi()
{
var serverApiVersion = Environment.GetEnvironmentVariable("MONGODB_API_VERSION");
if (serverApiVersion == null)
{
return null;
}
if (serverApiVersion != "1")
{
throw new ArgumentException($"Server API version \"{serverApiVersion}\" is not supported");
}
return new ServerApi(ServerApiVersion.V1);
}
private static bool GetServerless()
{
var serverless = Environment.GetEnvironmentVariable("SERVERLESS");
return serverless?.ToLower() == "true";
}
public static DatabaseNamespace GetDatabaseNamespaceForTestClass(Type testClassType)
{
var databaseName = TruncateDatabaseNameIfTooLong(__databaseNamespace.Value.DatabaseName + "-" + testClassType.Name);
if (databaseName.Length >= 64)
{
databaseName = databaseName.Substring(0, 63);
}
return new DatabaseNamespace(databaseName);
}
private static int GetMaxWireVersion()
{
using (var session = StartSession())
using (var binding = CreateReadBinding(session))
{
var command = new BsonDocument("hello", 1);
var operation = new ReadCommandOperation<BsonDocument>(DatabaseNamespace.Admin, command, BsonDocumentSerializer.Instance, __messageEncoderSettings);
var response = operation.Execute(binding, CancellationToken.None);
return response["maxWireVersion"].AsInt32;
}
}
private static SemanticVersion GetServerVersion()
{
using (var session = StartSession())
using (var binding = CreateReadBinding(session))
{
var command = new BsonDocument("buildinfo", 1);
var operation = new ReadCommandOperation<BsonDocument>(DatabaseNamespace.Admin, command, BsonDocumentSerializer.Instance, __messageEncoderSettings);
var response = operation.Execute(binding, CancellationToken.None);
return SemanticVersion.Parse(response["version"].AsString);
}
}
public static BsonDocument GetServerParameters()
{
using (var session = StartSession())
using (var binding = CreateReadBinding(session))
{
var command = new BsonDocument("getParameter", new BsonString("*"));
var operation = new ReadCommandOperation<BsonDocument>(DatabaseNamespace.Admin, command, BsonDocumentSerializer.Instance, __messageEncoderSettings);
var serverParameters = operation.Execute(binding, CancellationToken.None);
return serverParameters;
}
}
public static ICoreSessionHandle StartSession()
{
return StartSession(__cluster.Value);
}
public static ICoreSessionHandle StartSession(ICluster cluster, CoreSessionOptions options = null)
{
if (AreSessionsSupported(cluster))
{
return cluster.StartSession(options);
}
else
{
return NoCoreSession.NewHandle();
}
}
// private methods
private static bool AreSessionsSupported(ICluster cluster)
{
SpinWait.SpinUntil(() => cluster.Description.Servers.Any(s => s.State == ServerState.Connected), TimeSpan.FromSeconds(30));
return AreSessionsSupported(cluster.Description);
}
private static bool AreSessionsSupported(ClusterDescription clusterDescription)
{
return
clusterDescription.Servers.Any(s => s.State == ServerState.Connected) &&
(clusterDescription.LogicalSessionTimeout.HasValue || clusterDescription.Type == ClusterType.LoadBalanced);
}
private static IReadBindingHandle CreateReadBinding(ICoreSessionHandle session)
{
return CreateReadBinding(ReadPreference.Primary, session);
}
private static IReadBindingHandle CreateReadBinding(ReadPreference readPreference, ICoreSessionHandle session)
{
return CreateReadBinding(__cluster.Value, readPreference, session);
}
private static IReadBindingHandle CreateReadBinding(ICluster cluster, ReadPreference readPreference, ICoreSessionHandle session)
{
var binding = new ReadPreferenceBinding(cluster, readPreference, session.Fork());
return new ReadBindingHandle(binding);
}
private static IReadWriteBindingHandle CreateReadWriteBinding(ICoreSessionHandle session)
{
var binding = new WritableServerBinding(__cluster.Value, session.Fork());
return new ReadWriteBindingHandle(binding);
}
private static void DropDatabase()
{
var operation = new DropDatabaseOperation(__databaseNamespace.Value, __messageEncoderSettings);
using (var session = StartSession())
using (var binding = CreateReadWriteBinding(session))
{
operation.Execute(binding, CancellationToken.None);
}
}
private static IEnumerable<BsonDocument> FindDocuments(ICluster cluster, CollectionNamespace collectionNamespace)
{
using (var session = StartSession(cluster))
using (var binding = CreateReadBinding(cluster, ReadPreference.Primary, session))
{
var operation = new FindOperation<BsonDocument>(collectionNamespace, BsonDocumentSerializer.Instance, __messageEncoderSettings);
return operation.Execute(binding, CancellationToken.None).ToList();
}
}
private static int GetNumberOfMongoses()
{
var clusterType = __cluster.Value.Description.Type;
if (clusterType == ClusterType.Sharded || clusterType == ClusterType.LoadBalanced)
{
var mongosCollection = new CollectionNamespace("config", "mongos");
var mongosDocuments = FindDocuments(__cluster.Value, mongosCollection).ToList();
return mongosDocuments.Count;
}
else
{
throw new InvalidOperationException("Cluster is not sharded or load balanced.");
}
}
private static TimeSpan GetDefaultServerSelectionTimeout()
{
var serverSelectionTimeoutString = Environment.GetEnvironmentVariable("MONGO_SERVER_SELECTION_TIMEOUT_MS");
if (serverSelectionTimeoutString == null)
{
serverSelectionTimeoutString = "30000";
}
return TimeSpan.FromMilliseconds(int.Parse(serverSelectionTimeoutString));
}
private static string GetStorageEngine()
{
string result;
var clusterType = __cluster.Value.Description.Type;
switch (clusterType)
{
case ClusterType.LoadBalanced:
case var _ when Serverless:
// Load balancing and serverless are only supported for servers higher than 50
result = "wiredTiger";
break;
case ClusterType.Sharded:
{
// mongos cannot provide this data directly, so we need connection to a particular mongos shard
var shardsCollection = new CollectionNamespace("config", "shards");
var shards = FindDocuments(__cluster.Value, shardsCollection).FirstOrDefault();
if (shards != null)
{
var fullHosts = shards["host"].AsString; // for example: "shard01/localhost:27018,localhost:27019,localhost:27020"
var firstHost = fullHosts.Substring(fullHosts.IndexOf('/') + 1).Split(',')[0];
using (var cluster = CreateCluster(
configurator => configurator.ConfigureCluster(cs => cs.With(endPoints: new[] { EndPointHelper.Parse(firstHost) })),
allowDataBearingServers: true))
{
result = GetStorageEngineForCluster(cluster);
}
}
else
{
throw new InvalidOperationException("mongos has not been found.");
}
break;
}
default:
result = GetStorageEngineForCluster(__cluster.Value);
break;
}
return result ?? "mmapv1";
string GetStorageEngineForCluster(ICluster cluster)
{
var command = new BsonDocument("serverStatus", 1);
using (var session = StartSession(cluster))
using (var binding = CreateReadBinding(cluster, ReadPreference.PrimaryPreferred, session))
{
var operation = new ReadCommandOperation<BsonDocument>(DatabaseNamespace.Admin, command, BsonDocumentSerializer.Instance, __messageEncoderSettings);
var response = operation.Execute(binding, CancellationToken.None);
if (response.TryGetValue("storageEngine", out var storageEngine) && storageEngine.AsBsonDocument.TryGetValue("name", out var name))
{
return name.AsString;
}
return null;
}
}
}
private static bool IsReplicaSet(string uri)
{
var clusterBuilder = new ClusterBuilder().ConfigureWithConnectionString(uri, __serverApi.Value);
using (var cluster = clusterBuilder.BuildCluster())
{
cluster.Initialize();
var serverSelector = new ReadPreferenceServerSelector(ReadPreference.PrimaryPreferred);
var server = cluster.SelectServer(serverSelector, CancellationToken.None);
return server.Description.Type.IsReplicaSetMember();
}
}
public static void TearDown()
{
if (__cluster.IsValueCreated)
{
// TODO: DropDatabase
//DropDatabase();
__cluster.Value.Dispose();
__cluster = new Lazy<ICluster>(CreateCluster, isThreadSafe: true);
}
}
private static string TruncateCollectionNameIfTooLong(DatabaseNamespace databaseNamespace, string collectionName)
{
var fullNameLength = databaseNamespace.DatabaseName.Length + 1 + collectionName.Length;
if (fullNameLength <= 120)
{
return collectionName;
}
else
{
var maxCollectionNameLength = 120 - (databaseNamespace.DatabaseName.Length + 1);
return collectionName.Substring(0, maxCollectionNameLength - 1);
}
}
private static string TruncateDatabaseNameIfTooLong(string databaseName)
{
if (databaseName.Length < 64)
{
return databaseName;
}
else
{
return databaseName.Substring(0, 63);
}
}
#endregion
}
}