-
Notifications
You must be signed in to change notification settings - Fork 266
PHPLIB-1194: Test log message for non-genuine hosts #1173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
<?php | ||
|
||
namespace MongoDB\Tests; | ||
|
||
use MongoDB\Client; | ||
use MongoDB\Driver\Exception\InvalidArgumentException; | ||
use Psr\Log\AbstractLogger; | ||
use Psr\Log\LoggerInterface; | ||
|
||
use function func_get_args; | ||
use function MongoDB\addLogger; | ||
use function MongoDB\removeLogger; | ||
|
||
/** @see https://jira.mongodb.org/browse/DRIVERS-2583 */ | ||
class LogNonGenuineHostTest extends TestCase | ||
{ | ||
private LoggerInterface $logger; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->logger = $this->createTestPsrLogger(); | ||
|
||
addLogger($this->logger); | ||
} | ||
|
||
public function tearDown(): void | ||
{ | ||
removeLogger($this->logger); | ||
} | ||
|
||
/** @dataProvider provideCosmosUris */ | ||
public function testCosmosUriLogsInfoMessage(string $uri): void | ||
{ | ||
$this->createClientAndIgnoreSrvLookupError($uri); | ||
|
||
$expectedLog = [ | ||
'info', | ||
'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', | ||
['domain' => 'mongoc'], | ||
]; | ||
|
||
$this->assertContains($expectedLog, $this->logger->logs); | ||
} | ||
|
||
public static function provideCosmosUris(): array | ||
{ | ||
return [ | ||
['mongodb://a.mongo.cosmos.azure.com:19555/'], | ||
['mongodb://a.MONGO.COSMOS.AZURE.COM:19555/'], | ||
['mongodb+srv://a.mongo.cosmos.azure.com/'], | ||
['mongodb+srv://A.MONGO.COSMOS.AZURE.COM/'], | ||
// Mixing genuine and nongenuine hosts (unlikely in practice) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: not using named data providers was intentional, since it would have involved a lot of redundancy. I only added comments to notable test cases from my original libmongoc tests. |
||
['mongodb://a.example.com:27017,b.mongo.cosmos.azure.com:19555/'], | ||
]; | ||
} | ||
|
||
/** @dataProvider provideDocumentDbUris */ | ||
public function testDocumentDbUriLogsInfoMessage(string $uri): void | ||
{ | ||
$this->createClientAndIgnoreSrvLookupError($uri); | ||
|
||
$expectedLog = [ | ||
'info', | ||
'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', | ||
['domain' => 'mongoc'], | ||
]; | ||
|
||
$this->assertContains($expectedLog, $this->logger->logs); | ||
} | ||
|
||
public static function provideDocumentDbUris(): array | ||
{ | ||
return [ | ||
['mongodb://a.docdb.amazonaws.com:27017/'], | ||
['mongodb://a.docdb-elastic.amazonaws.com:27017/'], | ||
['mongodb://a.DOCDB.AMAZONAWS.COM:27017/'], | ||
['mongodb://a.DOCDB-ELASTIC.AMAZONAWS.COM:27017/'], | ||
['mongodb+srv://a.DOCDB.AMAZONAWS.COM/'], | ||
['mongodb+srv://a.DOCDB-ELASTIC.AMAZONAWS.COM/'], | ||
// Mixing genuine and nongenuine hosts (unlikely in practice) | ||
['mongodb://a.example.com:27017,b.docdb.amazonaws.com:27017/'], | ||
['mongodb://a.example.com:27017,b.docdb-elastic.amazonaws.com:27017/'], | ||
]; | ||
} | ||
|
||
/** @dataProvider provideGenuineUris */ | ||
public function testGenuineUriDoesNotLog(string $uri): void | ||
{ | ||
$this->createClientAndIgnoreSrvLookupError($uri); | ||
$this->assertEmpty($this->logger->logs); | ||
} | ||
|
||
public static function provideGenuineUris(): array | ||
{ | ||
return [ | ||
['mongodb://a.example.com:27017,b.example.com:27017/'], | ||
['mongodb://a.mongodb.net:27017'], | ||
['mongodb+srv://a.example.com/'], | ||
['mongodb+srv://a.mongodb.net/'], | ||
// Host names do not end with expected suffix | ||
['mongodb://a.mongo.cosmos.azure.com.tld:19555/'], | ||
['mongodb://a.docdb.amazonaws.com.tld:27017/'], | ||
['mongodb://a.docdb-elastic.amazonaws.com.tld:27017/'], | ||
// SRV host names do not end with expected suffix | ||
['mongodb+srv://a.mongo.cosmos.azure.com.tld/'], | ||
['mongodb+srv://a.docdb.amazonaws.com.tld/'], | ||
['mongodb+srv://a.docdb-elastic.amazonaws.com.tld/'], | ||
]; | ||
} | ||
|
||
private function createClientAndIgnoreSrvLookupError(string $uri): void | ||
{ | ||
try { | ||
$client = new Client($uri); | ||
} catch (InvalidArgumentException $e) { | ||
$this->assertStringContainsString('Failed to look up SRV record', $e->getMessage()); | ||
} | ||
} | ||
|
||
private function createTestPsrLogger(): LoggerInterface | ||
{ | ||
return new class extends AbstractLogger { | ||
public $logs = []; | ||
|
||
/** | ||
* Note: parameter type hints are omitted for compatibility with | ||
* psr/log 1.1.4 and PHP 7.4. | ||
*/ | ||
public function log($level, $message, array $context = []): void | ||
{ | ||
/* Ignore debug-level log messages from PHPC (e.g. connection | ||
* string, Manager creation, handshake data). */ | ||
if ($level === 'debug') { | ||
return; | ||
} | ||
|
||
$this->logs[] = func_get_args(); | ||
} | ||
}; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the only relevant class in the PR. Everything else is from #1158.