|
| 1 | +/* Copyright 2010-present MongoDB Inc. |
| 2 | +* |
| 3 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +* you may not use this file except in compliance with the License. |
| 5 | +* You may obtain a copy of the License at |
| 6 | +* |
| 7 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +* |
| 9 | +* Unless required by applicable law or agreed to in writing, software |
| 10 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +* See the License for the specific language governing permissions and |
| 13 | +* limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +using System.Linq; |
| 17 | +using FluentAssertions; |
| 18 | +using Microsoft.Extensions.Logging; |
| 19 | +using MongoDB.Driver.Core.Configuration; |
| 20 | +using MongoDB.Driver.Core.Events; |
| 21 | +using MongoDB.Driver.Core.Servers; |
| 22 | +using MongoDB.Driver.Core.TestHelpers.Logging; |
| 23 | +using Moq; |
| 24 | +using Xunit; |
| 25 | +using Xunit.Abstractions; |
| 26 | + |
| 27 | +namespace MongoDB.Driver.Core.Clusters |
| 28 | +{ |
| 29 | + public class ClusterFactoryTests : LoggableTestClass |
| 30 | + { |
| 31 | + private const string ExpectedCosmosDBMessage = "You appear to be connected to a CosmosDB cluster. For more information regarding feature compatibility and support please visit https://www.mongodb.com/supportability/cosmosdb"; |
| 32 | + private const string ExpectedDocumentDBMessage = "You appear to be connected to a DocumentDB cluster. For more information regarding feature compatibility and support please visit https://www.mongodb.com/supportability/documentdb"; |
| 33 | + |
| 34 | + public ClusterFactoryTests(ITestOutputHelper output) : base(output) |
| 35 | + { |
| 36 | + } |
| 37 | + |
| 38 | + [Theory] |
| 39 | + [InlineData("mongodb://a.MONGO.COSMOS.AZURE.COM:19555", ExpectedCosmosDBMessage)] |
| 40 | + [InlineData("mongodb://a.MONGO.COSMOS.AZURE.COM:19555", ExpectedCosmosDBMessage)] |
| 41 | + [InlineData("mongodb://a.mongo.cosmos.azure.com:19555", ExpectedCosmosDBMessage)] |
| 42 | + [InlineData("mongodb://a.DOCDB-ELASTIC.AMAZONAWS.COM:27017/", ExpectedDocumentDBMessage)] |
| 43 | + [InlineData("mongodb://a.docdb-elastic.amazonaws.com:27017/", ExpectedDocumentDBMessage)] |
| 44 | + [InlineData("mongodb://a.DOCDB.AMAZONAWS.COM", ExpectedDocumentDBMessage)] |
| 45 | + [InlineData("mongodb://a.docdb.amazonaws.com", ExpectedDocumentDBMessage)] |
| 46 | + // SRV matching |
| 47 | + [InlineData("mongodb+srv://A.MONGO.COSMOS.AZURE.COM", ExpectedCosmosDBMessage)] |
| 48 | + [InlineData("mongodb+srv://a.mongo.cosmos.azure.com", ExpectedCosmosDBMessage)] |
| 49 | + [InlineData("mongodb+srv://a.DOCDB.AMAZONAWS.COM/", ExpectedDocumentDBMessage)] |
| 50 | + [InlineData("mongodb+srv://a.docdb.amazonaws.com/", ExpectedDocumentDBMessage)] |
| 51 | + [InlineData("mongodb+srv://a.DOCDB-ELASTIC.AMAZONAWS.COM/", ExpectedDocumentDBMessage)] |
| 52 | + [InlineData("mongodb+srv://a.docdb-elastic.amazonaws.com/", ExpectedDocumentDBMessage)] |
| 53 | + // Mixing internal and external hosts (unlikely in practice) |
| 54 | + [InlineData("mongodb://a.example.com:27017,b.mongo.cosmos.azure.com:19555/", ExpectedCosmosDBMessage)] |
| 55 | + [InlineData("mongodb://a.example.com:27017,b.docdb.amazonaws.com:27017/", ExpectedDocumentDBMessage)] |
| 56 | + [InlineData("mongodb://a.example.com:27017,b.docdb-elastic.amazonaws.com:27017/", ExpectedDocumentDBMessage)] |
| 57 | + // Multiple external hosts |
| 58 | + [InlineData("mongodb://a.docdb-elastic.amazonaws.com:27017,b.mongo.cosmos.azure.com:19555/", ExpectedDocumentDBMessage)] |
| 59 | + [InlineData("mongodb://a.mongo.cosmos.azure.com:19554,b.docdb-elastic.amazonaws.com:27017,c.mongo.cosmos.azure.com:19555/", ExpectedCosmosDBMessage)] |
| 60 | + public void ClusterFactory_should_log_if_external_environment_is_detected(string connectionString, string expectedMessage) |
| 61 | + { |
| 62 | + var subject = CreateSubject(connectionString); |
| 63 | + _ = subject.CreateCluster(); |
| 64 | + |
| 65 | + var logs = GetLogs(); |
| 66 | + logs.Length.Should().Be(1); |
| 67 | + logs[0].FormattedMessage.Should().Be(expectedMessage); |
| 68 | + } |
| 69 | + |
| 70 | + [Theory] |
| 71 | + [InlineData("mongodb://a.mongo.cosmos.azure.com.tld:19555")] |
| 72 | + [InlineData("mongodb://a.docdb-elastic.amazonaws.com.t")] |
| 73 | + [InlineData("mongodb://a.docdb-elastic.amazonaws.com.t,b.docdb-elastic.amazonaws.com.t")] |
| 74 | + [InlineData("mongodb+srv://a.example.com")] |
| 75 | + [InlineData("mongodb+srv://a.mongodb.net/")] |
| 76 | + [InlineData("mongodb+srv://a.mongo.cosmos.azure.com.tld/")] |
| 77 | + [InlineData("mongodb+srv://a.docdb-elastic.amazonaws.com.tld/")] |
| 78 | + public void ClusterFactory_should_not_log_if_no_external_environment_is_detected(string connectionString) |
| 79 | + { |
| 80 | + var subject = CreateSubject(connectionString); |
| 81 | + _ = subject.CreateCluster(); |
| 82 | + |
| 83 | + var logs = GetLogs(); |
| 84 | + logs.Length.Should().Be(0); |
| 85 | + } |
| 86 | + |
| 87 | + private ClusterFactory CreateSubject(string connectionString) |
| 88 | + { |
| 89 | + var parsedConnectionString = new ConnectionString(connectionString); |
| 90 | + |
| 91 | + var eventSubscriberMock = Mock.Of<IEventSubscriber>(); |
| 92 | + var serverFactoryMock = Mock.Of<IClusterableServerFactory>(); |
| 93 | + |
| 94 | + var clusterSettings = new ClusterSettings(endPoints: Optional.Enumerable(parsedConnectionString.Hosts)); |
| 95 | + var clusterFactory = new ClusterFactory(clusterSettings, serverFactoryMock, eventSubscriberMock, LoggerFactory); |
| 96 | + |
| 97 | + return clusterFactory; |
| 98 | + } |
| 99 | + |
| 100 | + private LogEntry[] GetLogs() => Logs |
| 101 | + .Where(l => l.LogLevel == LogLevel.Information && l.Category == "MongoDB.Client") |
| 102 | + .ToArray(); |
| 103 | + } |
| 104 | +} |
0 commit comments