Skip to content

Commit 98d318a

Browse files
committed
Support runOnRequirement.csfle
1 parent 928c83f commit 98d318a

File tree

3 files changed

+78
-19
lines changed

3 files changed

+78
-19
lines changed

tests/FunctionalTestCase.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,21 @@ public function configureFailPoint($command, ?Server $server = null): void
284284
$this->configuredFailPoints[] = [$command->configureFailPoint, $failPointServer];
285285
}
286286

287+
public static function getModuleInfo(string $row): ?string
288+
{
289+
ob_start();
290+
phpinfo(INFO_MODULES);
291+
$info = ob_get_clean();
292+
293+
$pattern = sprintf('/^%s([\w ]+)$/m', preg_quote($row . ' => '));
294+
295+
if (preg_match($pattern, $info, $matches) !== 1) {
296+
return null;
297+
}
298+
299+
return $matches[1];
300+
}
301+
287302
/**
288303
* Creates the test collection with the specified options.
289304
*
@@ -512,7 +527,7 @@ protected function skipIfClientSideEncryptionIsNotSupported(): void
512527
$this->markTestSkipped('Client Side Encryption only supported on FCV 4.2 or higher');
513528
}
514529

515-
if ($this->getModuleInfo('libmongocrypt') === 'disabled') {
530+
if (static::getModuleInfo('libmongocrypt') === 'disabled') {
516531
$this->markTestSkipped('Client Side Encryption is not enabled in the MongoDB extension');
517532
}
518533
}
@@ -598,21 +613,6 @@ private function disableFailPoints(): void
598613
}
599614
}
600615

601-
private function getModuleInfo(string $row): ?string
602-
{
603-
ob_start();
604-
phpinfo(INFO_MODULES);
605-
$info = ob_get_clean();
606-
607-
$pattern = sprintf('/^%s([\w ]+)$/m', preg_quote($row . ' => '));
608-
609-
if (preg_match($pattern, $info, $matches) !== 1) {
610-
return null;
611-
}
612-
613-
return $matches[1];
614-
}
615-
616616
/**
617617
* Checks if the failCommand command is supported on this server version
618618
*

tests/UnifiedSpecTests/RunOnRequirement.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ class RunOnRequirement
4949
/** @var string */
5050
private $serverless;
5151

52+
/** @var bool */
53+
private $csfle;
54+
5255
/** @var array */
5356
private static $supportedTopologies = [
5457
self::TOPOLOGY_SINGLE,
@@ -67,7 +70,7 @@ class RunOnRequirement
6770

6871
public function __construct(stdClass $o)
6972
{
70-
Util::assertHasOnlyKeys($o, ['minServerVersion', 'maxServerVersion', 'topologies', 'serverParameters', 'auth', 'serverless']);
73+
Util::assertHasOnlyKeys($o, ['minServerVersion', 'maxServerVersion', 'topologies', 'serverParameters', 'auth', 'serverless', 'csfle']);
7174

7275
if (isset($o->minServerVersion)) {
7376
assertIsString($o->minServerVersion);
@@ -103,9 +106,14 @@ public function __construct(stdClass $o)
103106
assertContains($o->serverless, self::$supportedServerless);
104107
$this->serverless = $o->serverless;
105108
}
109+
110+
if (isset($o->csfle)) {
111+
assertIsBool($o->csfle);
112+
$this->csfle = $o->csfle;
113+
}
106114
}
107115

108-
public function isSatisfied(string $serverVersion, string $topology, stdClass $serverParameters, bool $isAuthenticated, bool $isServerless): bool
116+
public function isSatisfied(string $serverVersion, string $topology, stdClass $serverParameters, bool $isAuthenticated, bool $isServerless, bool $isClientSideEncryptionSupported): bool
109117
{
110118
if (isset($this->minServerVersion) && version_compare($serverVersion, $this->minServerVersion, '<')) {
111119
return false;
@@ -140,6 +148,10 @@ public function isSatisfied(string $serverVersion, string $topology, stdClass $s
140148
}
141149
}
142150

151+
if (isset($this->csfle) && $isClientSideEncryptionSupported !== $this->csfle) {
152+
return false;
153+
}
154+
143155
return true;
144156
}
145157

tests/UnifiedSpecTests/UnifiedTestRunner.php

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
use function getenv;
2626
use function implode;
2727
use function in_array;
28+
use function is_executable;
29+
use function is_readable;
2830
use function is_string;
2931
use function parse_url;
3032
use function PHPUnit\Framework\assertContainsOnly;
@@ -43,7 +45,9 @@
4345
use function substr_replace;
4446
use function version_compare;
4547

48+
use const DIRECTORY_SEPARATOR;
4649
use const FILTER_VALIDATE_BOOLEAN;
50+
use const PATH_SEPARATOR;
4751
use const PHP_URL_HOST;
4852

4953
/**
@@ -60,7 +64,7 @@ final class UnifiedTestRunner
6064

6165
public const MIN_SCHEMA_VERSION = '1.0';
6266

63-
public const MAX_SCHEMA_VERSION = '1.7';
67+
public const MAX_SCHEMA_VERSION = '1.8';
6468

6569
/** @var MongoDB\Client */
6670
private $internalClient;
@@ -247,6 +251,7 @@ private function checkRunOnRequirements(array $runOnRequirements): void
247251
$this->getServerParameters(),
248252
$this->isAuthenticated(),
249253
$this->isServerless(),
254+
$this->isClientSideEncryptionSupported(),
250255
];
251256
}
252257

@@ -349,6 +354,48 @@ private function isAuthenticated(): bool
349354
throw new UnexpectedValueException('Could not determine authentication status');
350355
}
351356

357+
/**
358+
* Return whether client-side encryption is supported.
359+
*/
360+
private function isClientSideEncryptionSupported(): bool
361+
{
362+
/* CSFLE technically requires FCV 4.2+ but this is sufficient since we
363+
* do not test on mixed-version clusters. */
364+
if (version_compare($this->getServerVersion(), '4.2', '<')) {
365+
return false;
366+
}
367+
368+
if (FunctionalTestCase::getModuleInfo('libmongocrypt') === 'disabled') {
369+
return false;
370+
}
371+
372+
return static::isCryptSharedLibAvailable() || static::isMongocryptdAvailable();
373+
}
374+
375+
private static function isCryptSharedLibAvailable(): bool
376+
{
377+
$cryptSharedLibPath = getenv('CRYPT_SHARED_LIB_PATH');
378+
379+
if ($cryptSharedLibPath === false) {
380+
return false;
381+
}
382+
383+
return is_readable($cryptSharedLibPath);
384+
}
385+
386+
private static function isMongocryptdAvailable(): bool
387+
{
388+
$paths = explode(PATH_SEPARATOR, getenv("PATH"));
389+
390+
foreach ($paths as $path) {
391+
if (is_executable($path . DIRECTORY_SEPARATOR . 'mongocryptd')) {
392+
return true;
393+
}
394+
}
395+
396+
return false;
397+
}
398+
352399
/**
353400
* Return whether serverless (i.e. proxy as mongos) is being utilized.
354401
*/

0 commit comments

Comments
 (0)