|
| 1 | +/* |
| 2 | + * Copyright 2008-present MongoDB, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.mongodb.internal.connection; |
| 17 | + |
| 18 | +import ch.qos.logback.classic.Level; |
| 19 | +import ch.qos.logback.classic.Logger; |
| 20 | +import ch.qos.logback.classic.LoggerContext; |
| 21 | +import ch.qos.logback.classic.spi.ILoggingEvent; |
| 22 | +import ch.qos.logback.core.read.ListAppender; |
| 23 | +import com.mongodb.ConnectionString; |
| 24 | +import com.mongodb.connection.ClusterSettings; |
| 25 | +import org.junit.jupiter.api.AfterAll; |
| 26 | +import org.junit.jupiter.api.AfterEach; |
| 27 | +import org.junit.jupiter.api.Assertions; |
| 28 | +import org.junit.jupiter.api.BeforeAll; |
| 29 | +import org.junit.jupiter.params.ParameterizedTest; |
| 30 | +import org.junit.jupiter.params.provider.Arguments; |
| 31 | +import org.junit.jupiter.params.provider.MethodSource; |
| 32 | +import org.slf4j.LoggerFactory; |
| 33 | + |
| 34 | +import java.util.List; |
| 35 | +import java.util.stream.Collectors; |
| 36 | +import java.util.stream.Stream; |
| 37 | + |
| 38 | +class DefaultClusterFactoryTest { |
| 39 | + private static final String EXPECTED_COSMOS_DB_MESSAGE = |
| 40 | + "You appear to be connected to a CosmosDB cluster. For more information regarding " |
| 41 | + + "feature compatibility and support please visit https://www.mongodb.com/supportability/cosmosdb"; |
| 42 | + |
| 43 | + private static final String EXPECTED_DOCUMENT_DB_MESSAGE = |
| 44 | + "You appear to be connected to a DocumentDB cluster. For more information regarding " |
| 45 | + + "feature compatibility and support please visit https://www.mongodb.com/supportability/documentdb"; |
| 46 | + |
| 47 | + private static final Logger LOGGER = (Logger) LoggerFactory.getLogger("org.mongodb.driver.DefaultClusterFactory"); |
| 48 | + private static final MemoryAppender MEMORY_APPENDER = new MemoryAppender(); |
| 49 | + |
| 50 | + @BeforeAll |
| 51 | + public static void setUp() { |
| 52 | + MEMORY_APPENDER.setContext((LoggerContext) LoggerFactory.getILoggerFactory()); |
| 53 | + LOGGER.setLevel(Level.DEBUG); |
| 54 | + LOGGER.addAppender(MEMORY_APPENDER); |
| 55 | + MEMORY_APPENDER.start(); |
| 56 | + } |
| 57 | + |
| 58 | + @AfterAll |
| 59 | + public static void cleanUp() { |
| 60 | + LOGGER.detachAppender(MEMORY_APPENDER); |
| 61 | + } |
| 62 | + |
| 63 | + @AfterEach |
| 64 | + public void reset() { |
| 65 | + MEMORY_APPENDER.reset(); |
| 66 | + } |
| 67 | + |
| 68 | + static Stream<Arguments> shouldLogAllegedClusterEnvironmentWhenNonGenuineHostsSpecified() { |
| 69 | + return Stream.of( |
| 70 | + Arguments.of("mongodb://a.MONGO.COSMOS.AZURE.COM:19555", EXPECTED_COSMOS_DB_MESSAGE), |
| 71 | + Arguments.of("mongodb://a.mongo.cosmos.azure.com:19555", EXPECTED_COSMOS_DB_MESSAGE), |
| 72 | + Arguments.of("mongodb://a.DOCDB-ELASTIC.AMAZONAWS.COM:27017/", EXPECTED_DOCUMENT_DB_MESSAGE), |
| 73 | + Arguments.of("mongodb://a.docdb-elastic.amazonaws.com:27017/", EXPECTED_DOCUMENT_DB_MESSAGE), |
| 74 | + Arguments.of("mongodb://a.DOCDB.AMAZONAWS.COM", EXPECTED_DOCUMENT_DB_MESSAGE), |
| 75 | + Arguments.of("mongodb://a.docdb.amazonaws.com", EXPECTED_DOCUMENT_DB_MESSAGE), |
| 76 | + |
| 77 | + /* SRV matching */ |
| 78 | + Arguments.of("mongodb+srv://A.MONGO.COSMOS.AZURE.COM", EXPECTED_COSMOS_DB_MESSAGE), |
| 79 | + Arguments.of("mongodb+srv://a.mongo.cosmos.azure.com", EXPECTED_COSMOS_DB_MESSAGE), |
| 80 | + Arguments.of("mongodb+srv://a.DOCDB.AMAZONAWS.COM/", EXPECTED_DOCUMENT_DB_MESSAGE), |
| 81 | + Arguments.of("mongodb+srv://a.docdb.amazonaws.com/", EXPECTED_DOCUMENT_DB_MESSAGE), |
| 82 | + Arguments.of("mongodb+srv://a.DOCDB-ELASTIC.AMAZONAWS.COM/", EXPECTED_DOCUMENT_DB_MESSAGE), |
| 83 | + Arguments.of("mongodb+srv://a.docdb-elastic.amazonaws.com/", EXPECTED_DOCUMENT_DB_MESSAGE), |
| 84 | + |
| 85 | + /* Mixing genuine and nongenuine hosts (unlikely in practice) */ |
| 86 | + Arguments.of("mongodb://a.example.com:27017,b.mongo.cosmos.azure.com:19555/", EXPECTED_COSMOS_DB_MESSAGE), |
| 87 | + Arguments.of("mongodb://a.example.com:27017,b.docdb.amazonaws.com:27017/", EXPECTED_DOCUMENT_DB_MESSAGE), |
| 88 | + Arguments.of("mongodb://a.example.com:27017,b.docdb-elastic.amazonaws.com:27017/", EXPECTED_DOCUMENT_DB_MESSAGE) |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + @ParameterizedTest |
| 93 | + @MethodSource |
| 94 | + void shouldLogAllegedClusterEnvironmentWhenNonGenuineHostsSpecified(final String connectionString, final String expectedLogMessage) { |
| 95 | + //when |
| 96 | + ClusterSettings clusterSettings = toClusterSettings(new ConnectionString(connectionString)); |
| 97 | + new DefaultClusterFactory().detectAndLogClusterEnvironment(clusterSettings); |
| 98 | + |
| 99 | + //then |
| 100 | + List<ILoggingEvent> loggedEvents = MEMORY_APPENDER.search(expectedLogMessage); |
| 101 | + |
| 102 | + Assertions.assertEquals(1, loggedEvents.size()); |
| 103 | + Assertions.assertEquals(Level.INFO, loggedEvents.get(0).getLevel()); |
| 104 | + |
| 105 | + } |
| 106 | + |
| 107 | + static Stream<String> shouldNotLogClusterEnvironmentWhenGenuineHostsSpecified() { |
| 108 | + return Stream.of( |
| 109 | + "mongodb://a.mongo.cosmos.azure.com.tld:19555", |
| 110 | + "mongodb://a.docdb-elastic.amazonaws.com.t", |
| 111 | + "mongodb+srv://a.example.com", |
| 112 | + "mongodb+srv://a.mongodb.net/", |
| 113 | + "mongodb+srv://a.mongo.cosmos.azure.com.tld/", |
| 114 | + "mongodb+srv://a.docdb-elastic.amazonaws.com.tld/" |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + @ParameterizedTest |
| 119 | + @MethodSource |
| 120 | + void shouldNotLogClusterEnvironmentWhenGenuineHostsSpecified(final String connectionUrl) { |
| 121 | + //when |
| 122 | + ClusterSettings clusterSettings = toClusterSettings(new ConnectionString(connectionUrl)); |
| 123 | + new DefaultClusterFactory().detectAndLogClusterEnvironment(clusterSettings); |
| 124 | + |
| 125 | + //then |
| 126 | + Assertions.assertEquals(0, MEMORY_APPENDER.search(EXPECTED_COSMOS_DB_MESSAGE).size()); |
| 127 | + Assertions.assertEquals(0, MEMORY_APPENDER.search(EXPECTED_DOCUMENT_DB_MESSAGE).size()); |
| 128 | + } |
| 129 | + |
| 130 | + private static ClusterSettings toClusterSettings(final ConnectionString connectionUrl) { |
| 131 | + return ClusterSettings.builder().applyConnectionString(connectionUrl).build(); |
| 132 | + } |
| 133 | + |
| 134 | + public static class MemoryAppender extends ListAppender<ILoggingEvent> { |
| 135 | + public void reset() { |
| 136 | + this.list.clear(); |
| 137 | + } |
| 138 | + |
| 139 | + public List<ILoggingEvent> search(final String message) { |
| 140 | + return this.list.stream() |
| 141 | + .filter(event -> event.getFormattedMessage().contains(message)) |
| 142 | + .collect(Collectors.toList()); |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments