Update getUri helper to extract a single mongos node

For most tests, we only want to expose a single mongos node to avoid issues with fail points. This change parses and reassembles the connection URI to only return a single mongos node in the URI.
parent 6cfc674d
......@@ -38,6 +38,68 @@ abstract class FunctionalTestCase extends TestCase
parent::tearDown();
}
public static function getUri($allowMultipleMongoses = false)
{
$uri = parent::getUri();
if ($allowMultipleMongoses) {
return $uri;
}
$urlParts = parse_url($uri);
if ($urlParts === false) {
return $uri;
}
// Only modify URIs using the mongodb scheme
if ($urlParts['scheme'] !== 'mongodb') {
return $uri;
}
$hosts = explode(',', $urlParts['host']);
$numHosts = count($hosts);
if ($numHosts === 1) {
return $uri;
}
$manager = new Manager($uri);
if ($manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY))->getType() !== Server::TYPE_MONGOS) {
return $uri;
}
// Re-append port to last host
if (isset($urlParts['port'])) {
$hosts[$numHosts-1] .= ':' . $urlParts['port'];
}
$parts = [
'mongodb://'
];
if (isset($urlParts['user'], $urlParts['pass'])) {
$parts += [
$urlParts['user'],
':',
$urlParts['pass'],
'@',
];
}
$parts[] = $hosts[0];
if (isset($urlParts['path'])) {
$parts[] = $urlParts['path'];
}
if (isset($urlParts['query'])) {
$parts += [
'?',
$urlParts['path']
];
}
return implode('', $parts);
}
protected function assertCollectionCount($namespace, $count)
{
list($databaseName, $collectionName) = explode('.', $namespace, 2);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment