Skip to content

Commit 2d4d0ce

Browse files
committed
PHPLIB-688 Check serverless requirement in legacy spec test runner
1 parent f0edf8f commit 2d4d0ce

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

tests/FunctionalTestCase.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use function count;
2626
use function current;
2727
use function explode;
28+
use function filter_var;
2829
use function getenv;
2930
use function implode;
3031
use function is_array;
@@ -42,6 +43,7 @@
4243
use function sprintf;
4344
use function version_compare;
4445

46+
use const FILTER_VALIDATE_BOOLEAN;
4547
use const INFO_MODULES;
4648

4749
abstract class FunctionalTestCase extends TestCase
@@ -393,6 +395,16 @@ protected function isReplicaSet()
393395
return $this->getPrimaryServer()->getType() == Server::TYPE_RS_PRIMARY;
394396
}
395397

398+
/**
399+
* Return whether serverless (i.e. proxy as mongos) is being utilized.
400+
*/
401+
protected static function isServerless(): bool
402+
{
403+
$isServerless = getenv('MONGODB_IS_SERVERLESS');
404+
405+
return $isServerless !== false ? filter_var($isServerless, FILTER_VALIDATE_BOOLEAN) : false;
406+
}
407+
396408
protected function isShardedCluster()
397409
{
398410
return $this->getPrimaryServer()->getType() == Server::TYPE_MONGOS;

tests/SpecTests/FunctionalTestCase.php

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ class FunctionalTestCase extends BaseFunctionalTestCase
3030
public const TOPOLOGY_REPLICASET = 'replicaset';
3131
public const TOPOLOGY_SHARDED = 'sharded';
3232

33+
public const SERVERLESS_ALLOW = 'allow';
34+
public const SERVERLESS_FORBID = 'forbid';
35+
public const SERVERLESS_REQUIRE = 'require';
36+
3337
/** @var Context|null */
3438
private $context;
3539

@@ -137,8 +141,9 @@ protected function checkServerRequirements(array $runOn): void
137141
$minServerVersion = $req->minServerVersion ?? null;
138142
$maxServerVersion = $req->maxServerVersion ?? null;
139143
$topologies = $req->topology ?? null;
144+
$serverlessMode = $req->serverless ?? null;
140145

141-
if ($this->isServerRequirementSatisifed($minServerVersion, $maxServerVersion, $topologies)) {
146+
if ($this->isServerRequirementSatisifed($minServerVersion, $maxServerVersion, $topologies, $serverlessMode)) {
142147
return;
143148
}
144149
}
@@ -268,7 +273,27 @@ private function getTopology(): string
268273
return $topologyTypeMap[$primaryType];
269274
}
270275

271-
throw new UnexpectedValueException('Toplogy is neither single nor RS nor sharded');
276+
throw new UnexpectedValueException(sprintf('Cannot find topology for primary of type "%s".', $primaryType));
277+
}
278+
279+
private function isServerlessRequirementSatisfied(?string $serverlessMode): bool
280+
{
281+
if ($serverlessMode === null) {
282+
return true;
283+
}
284+
285+
switch ($serverlessMode) {
286+
case self::SERVERLESS_ALLOW:
287+
return true;
288+
289+
case self::SERVERLESS_FORBID:
290+
return ! static::isServerless();
291+
292+
case self::SERVERLESS_REQUIRE:
293+
return static::isServerless();
294+
}
295+
296+
throw new UnexpectedValueException(sprintf('Invalid serverless requirement "%s" found.', $serverlessMode));
272297
}
273298

274299
/**
@@ -279,7 +304,7 @@ private function getTopology(): string
279304
* @param array|null $topologies
280305
* @return boolean
281306
*/
282-
private function isServerRequirementSatisifed(?string $minServerVersion, ?string $maxServerVersion, ?array $topologies = null): bool
307+
private function isServerRequirementSatisifed(?string $minServerVersion, ?string $maxServerVersion, ?array $topologies = null, ?string $serverlessMode = null): bool
283308
{
284309
$serverVersion = $this->getServerVersion();
285310

@@ -297,6 +322,10 @@ private function isServerRequirementSatisifed(?string $minServerVersion, ?string
297322
return false;
298323
}
299324

325+
if (! $this->isServerlessRequirementSatisfied($serverlessMode)) {
326+
return false;
327+
}
328+
300329
return true;
301330
}
302331
}

0 commit comments

Comments
 (0)