From 2fddef0d4e2491b7664610fff6ec46774a7e0ad7 Mon Sep 17 00:00:00 2001 From: Jeremy Mikola Date: Mon, 17 Jul 2023 13:03:11 -0400 Subject: [PATCH] PHPLIB-1202: Assume sharded clusters use replica sets --- tests/DocumentationExamplesTest.php | 4 +- tests/FunctionalTestCase.php | 62 ++----------------- ...reateEncryptedCollectionFunctionalTest.php | 2 +- .../Operation/FindAndModifyFunctionalTest.php | 2 +- ...rose21_AutomaticDataEncryptionKeysTest.php | 2 +- .../Prose22_RangeExplicitEncryptionTest.php | 2 +- .../ClientSideEncryptionSpecTest.php | 2 +- tests/SpecTests/RetryableWritesSpecTest.php | 4 -- tests/UnifiedSpecTests/UnifiedTestRunner.php | 25 ++------ 9 files changed, 17 insertions(+), 88 deletions(-) diff --git a/tests/DocumentationExamplesTest.php b/tests/DocumentationExamplesTest.php index 9b9dac795..5f3d96aba 100644 --- a/tests/DocumentationExamplesTest.php +++ b/tests/DocumentationExamplesTest.php @@ -1550,7 +1550,7 @@ public function testSnapshotQueries(): void { $this->skipIfServerVersion('<', '5.0.0', 'Snapshot queries outside of transactions are not supported'); - if (! ($this->isReplicaSet() || $this->isShardedClusterUsingReplicasets())) { + if ($this->isStandalone()) { $this->markTestSkipped('Snapshot read concern is only supported with replicasets'); } @@ -1803,7 +1803,7 @@ public function testWithTransactionExample(): void */ public function testQueryableEncryption(): void { - if ($this->isStandalone() || ($this->isShardedCluster() && ! $this->isShardedClusterUsingReplicasets())) { + if ($this->isStandalone()) { $this->markTestSkipped('Queryable encryption requires replica sets'); } diff --git a/tests/FunctionalTestCase.php b/tests/FunctionalTestCase.php index 1820641d7..3f02148e7 100644 --- a/tests/FunctionalTestCase.php +++ b/tests/FunctionalTestCase.php @@ -9,7 +9,6 @@ use MongoDB\Driver\Command; use MongoDB\Driver\Exception\CommandException; use MongoDB\Driver\Manager; -use MongoDB\Driver\Query; use MongoDB\Driver\ReadPreference; use MongoDB\Driver\Server; use MongoDB\Driver\ServerApi; @@ -444,33 +443,6 @@ protected function isShardedCluster() return false; } - protected function isShardedClusterUsingReplicasets() - { - // Assume serverless is a sharded cluster using replica sets - if (static::isServerless()) { - return true; - } - - $cursor = $this->getPrimaryServer()->executeQuery( - 'config.shards', - new Query([], ['limit' => 1]), - ); - - $cursor->setTypeMap(['root' => 'array', 'document' => 'array']); - $document = current($cursor->toArray()); - - if (! $document) { - return false; - } - - /** - * Use regular expression to distinguish between standalone or replicaset: - * Without a replicaset: "host" : "localhost:4100" - * With a replicaset: "host" : "dec6d8a7-9bc1-4c0e-960c-615f860b956f/localhost:4400,localhost:4401" - */ - return preg_match('@^.*/.*:\d+@', $document['host']); - } - protected function skipIfServerVersion(string $operator, string $version, ?string $message = null): void { if (version_compare($this->getServerVersion(), $version, $operator)) { @@ -480,43 +452,25 @@ protected function skipIfServerVersion(string $operator, string $version, ?strin protected function skipIfChangeStreamIsNotSupported(): void { - switch ($this->getPrimaryServer()->getType()) { - case Server::TYPE_MONGOS: - case Server::TYPE_LOAD_BALANCER: - if (! $this->isShardedClusterUsingReplicasets()) { - $this->markTestSkipped('$changeStream is only supported with replicasets'); - } - - break; - - case Server::TYPE_RS_PRIMARY: - break; - - default: - $this->markTestSkipped('$changeStream is not supported'); + if ($this->isStandalone()) { + $this->markTestSkipped('$changeStream requires replica sets'); } } protected function skipIfCausalConsistencyIsNotSupported(): void { switch ($this->getPrimaryServer()->getType()) { - case Server::TYPE_MONGOS: - case Server::TYPE_LOAD_BALANCER: - if (! $this->isShardedClusterUsingReplicasets()) { - $this->markTestSkipped('Causal Consistency is only supported with replicasets'); - } - + case Server::TYPE_STANDALONE: + $this->markTestSkipped('Causal consistency requires replica sets'); break; case Server::TYPE_RS_PRIMARY: + // Note: mongos does not report storage engine information if ($this->getServerStorageEngine() !== 'wiredTiger') { - $this->markTestSkipped('Causal Consistency requires WiredTiger storage engine'); + $this->markTestSkipped('Causal consistency requires WiredTiger storage engine'); } break; - - default: - $this->markTestSkipped('Causal Consistency is not supported'); } } @@ -549,10 +503,6 @@ protected function skipIfTransactionsAreNotSupported(): void } if ($this->isShardedCluster()) { - if (! $this->isShardedClusterUsingReplicasets()) { - $this->markTestSkipped('Transactions are not supported on sharded clusters without replica sets'); - } - if (version_compare($this->getFeatureCompatibilityVersion(), '4.2', '<')) { $this->markTestSkipped('Transactions are only supported on FCV 4.2 or higher'); } diff --git a/tests/Operation/CreateEncryptedCollectionFunctionalTest.php b/tests/Operation/CreateEncryptedCollectionFunctionalTest.php index b1359e314..dde1f7d4e 100644 --- a/tests/Operation/CreateEncryptedCollectionFunctionalTest.php +++ b/tests/Operation/CreateEncryptedCollectionFunctionalTest.php @@ -30,7 +30,7 @@ public function setUp(): void $this->markTestSkipped('Neither crypt_shared nor mongocryptd are available'); } - if ($this->isStandalone() || ($this->isShardedCluster() && ! $this->isShardedClusterUsingReplicasets())) { + if ($this->isStandalone()) { $this->markTestSkipped('Queryable Encryption requires replica sets'); } diff --git a/tests/Operation/FindAndModifyFunctionalTest.php b/tests/Operation/FindAndModifyFunctionalTest.php index 1b82328a1..51723a93c 100644 --- a/tests/Operation/FindAndModifyFunctionalTest.php +++ b/tests/Operation/FindAndModifyFunctionalTest.php @@ -159,7 +159,7 @@ public function testHintOptionAndUnacknowledgedWriteConcernUnsupportedClientSide public function testFindAndModifyReportedWriteConcernError(): void { - if (($this->isShardedCluster() && ! $this->isShardedClusterUsingReplicasets()) || ! $this->isReplicaSet()) { + if ($this->isStandalone()) { $this->markTestSkipped('Test only applies to replica sets'); } diff --git a/tests/SpecTests/ClientSideEncryption/Prose21_AutomaticDataEncryptionKeysTest.php b/tests/SpecTests/ClientSideEncryption/Prose21_AutomaticDataEncryptionKeysTest.php index 05480d9d6..ddea08972 100644 --- a/tests/SpecTests/ClientSideEncryption/Prose21_AutomaticDataEncryptionKeysTest.php +++ b/tests/SpecTests/ClientSideEncryption/Prose21_AutomaticDataEncryptionKeysTest.php @@ -31,7 +31,7 @@ public function setUp(): void { parent::setUp(); - if ($this->isStandalone() || ($this->isShardedCluster() && ! $this->isShardedClusterUsingReplicasets())) { + if ($this->isStandalone()) { $this->markTestSkipped('Automatic data encryption key tests require replica sets'); } diff --git a/tests/SpecTests/ClientSideEncryption/Prose22_RangeExplicitEncryptionTest.php b/tests/SpecTests/ClientSideEncryption/Prose22_RangeExplicitEncryptionTest.php index a0274b3c7..2085300df 100644 --- a/tests/SpecTests/ClientSideEncryption/Prose22_RangeExplicitEncryptionTest.php +++ b/tests/SpecTests/ClientSideEncryption/Prose22_RangeExplicitEncryptionTest.php @@ -39,7 +39,7 @@ public function setUp(): void { parent::setUp(); - if ($this->isStandalone() || ($this->isShardedCluster() && ! $this->isShardedClusterUsingReplicasets())) { + if ($this->isStandalone()) { $this->markTestSkipped('Range explicit encryption tests require replica sets'); } diff --git a/tests/SpecTests/ClientSideEncryptionSpecTest.php b/tests/SpecTests/ClientSideEncryptionSpecTest.php index d01c5378c..dc7a6b6ac 100644 --- a/tests/SpecTests/ClientSideEncryptionSpecTest.php +++ b/tests/SpecTests/ClientSideEncryptionSpecTest.php @@ -1323,7 +1323,7 @@ static function (self $test, ClientEncryption $clientEncryptionNoClientCert, Cli */ public function testExplicitEncryption(Closure $test): void { - if ($this->isStandalone() || ($this->isShardedCluster() && ! $this->isShardedClusterUsingReplicasets())) { + if ($this->isStandalone()) { $this->markTestSkipped('Explicit encryption tests require replica sets'); } diff --git a/tests/SpecTests/RetryableWritesSpecTest.php b/tests/SpecTests/RetryableWritesSpecTest.php index 8f6ed2fd1..b85eb1c27 100644 --- a/tests/SpecTests/RetryableWritesSpecTest.php +++ b/tests/SpecTests/RetryableWritesSpecTest.php @@ -34,10 +34,6 @@ class RetryableWritesSpecTest extends FunctionalTestCase */ public function testRetryableWrites(stdClass $test, ?array $runOn, array $data): void { - if ($this->isShardedCluster() && ! $this->isShardedClusterUsingReplicasets()) { - $this->markTestSkipped('Transaction numbers are only allowed on a replica set member or mongos (PHPC-1415)'); - } - $useMultipleMongoses = isset($test->useMultipleMongoses) && $test->useMultipleMongoses && $this->isMongos(); if (isset($runOn)) { diff --git a/tests/UnifiedSpecTests/UnifiedTestRunner.php b/tests/UnifiedSpecTests/UnifiedTestRunner.php index 4c947b752..27f953a89 100644 --- a/tests/UnifiedSpecTests/UnifiedTestRunner.php +++ b/tests/UnifiedSpecTests/UnifiedTestRunner.php @@ -33,7 +33,6 @@ use function PHPUnit\Framework\assertIsString; use function PHPUnit\Framework\assertNotEmpty; use function PHPUnit\Framework\assertNotFalse; -use function preg_match; use function preg_replace; use function sprintf; use function strlen; @@ -299,9 +298,10 @@ private function getTopology(): string return RunOnRequirement::TOPOLOGY_REPLICASET; case Server::TYPE_MONGOS: - return $this->isShardedClusterUsingReplicasets() - ? RunOnRequirement::TOPOLOGY_SHARDED_REPLICASET - : RunOnRequirement::TOPOLOGY_SHARDED; + /* Since MongoDB 3.6, all sharded clusters use replica sets. The + * unified test format deprecated use of "sharded-replicaset" in + * tests but we should still identify as such. */ + return RunOnRequirement::TOPOLOGY_SHARDED_REPLICASET; case Server::TYPE_LOAD_BALANCER: return RunOnRequirement::TOPOLOGY_LOAD_BALANCED; @@ -368,23 +368,6 @@ private function isSchemaVersionSupported(string $schemaVersion): bool return version_compare($schemaVersion, self::MIN_SCHEMA_VERSION, '>=') && version_compare($schemaVersion, self::MAX_SCHEMA_VERSION, '<='); } - private function isShardedClusterUsingReplicasets(): bool - { - $collection = $this->internalClient->selectCollection('config', 'shards'); - $config = $collection->findOne(); - - if ($config === null) { - return false; - } - - /** - * Use regular expression to distinguish between standalone or replicaset: - * Without a replicaset: "host" : "localhost:4100" - * With a replicaset: "host" : "dec6d8a7-9bc1-4c0e-960c-615f860b956f/localhost:4400,localhost:4401" - */ - return preg_match('@^.*/.*:\d+@', $config['host']); - } - /** * Kill all sessions on the cluster. *