Skip to content

Commit 8ece2a3

Browse files
committed
PHPC-1179: Reimplement tests that start servers with Mongo Orchestration
1 parent deff694 commit 8ece2a3

File tree

4 files changed

+96
-69
lines changed

4 files changed

+96
-69
lines changed

tests/cursor/cursor-getmore-005.phpt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
--TEST--
22
MongoDB\Driver\Cursor query result iteration with getmore failure
3-
--XFAIL--
4-
START() tests must be reimplemented (PHPC-1179)
53
--SKIPIF--
64
<?php require __DIR__ . "/" ."../utils/basic-skipif.inc"; ?>
7-
<?php START("THROWAWAY", ["version" => "30-release"]); CLEANUP(THROWAWAY); ?>
5+
<?php
6+
/* This test spins up its own mongod instance, so only run this in the most default "standalone, no
7+
* auth" configurations. This way, we can test on multiple server versions, but not waste resources
8+
* on f.e. Travis. */
9+
?>
10+
<?php skip_if_not_standalone(); ?>
11+
<?php skip_if_auth(); ?>
812
--FILE--
913
<?php
1014
require_once __DIR__ . "/../utils/basic.inc";
1115

12-
$manager = new MongoDB\Driver\Manager(THROWAWAY);
16+
$uri = createTemporaryMongoInstance();
17+
$manager = new MongoDB\Driver\Manager($uri);
1318

1419
$bulkWrite = new MongoDB\Driver\BulkWrite;
1520

@@ -30,14 +35,13 @@ throws(function() use ($cursor) {
3035
printf("%d => {_id: %d}\n", $i, $document->_id);
3136
}
3237
}, "MongoDB\Driver\Exception\ConnectionException");
33-
3438
?>
3539
===DONE===
36-
<?php DELETE("THROWAWAY"); ?>
40+
<?php destroyTemporaryMongoInstance(); ?>
3741
<?php exit(0); ?>
3842
--CLEAN--
3943
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
40-
<?php DELETE("THROWAWAY"); ?>
44+
<?php destroyTemporaryMongoInstance(); ?>
4145
--EXPECT--
4246
Inserted: 5
4347
0 => {_id: 0}

tests/cursor/cursor-getmore-006.phpt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
--TEST--
22
MongoDB\Driver\Cursor command result iteration with getmore failure
3-
--XFAIL--
4-
START() tests must be reimplemented (PHPC-1179)
53
--SKIPIF--
64
<?php require __DIR__ . "/" ."../utils/basic-skipif.inc"; ?>
7-
<?php START("THROWAWAY", ["version" => "30-release"]); CLEANUP(THROWAWAY); ?>
5+
<?php
6+
/* This test spins up its own mongod instance, so only run this in the most default "standalone, no
7+
* auth" configurations. This way, we can test on multiple server versions, but not waste resources
8+
* on f.e. Travis. */
9+
?>
10+
<?php skip_if_not_standalone(); ?>
11+
<?php skip_if_auth(); ?>
812
--FILE--
913
<?php
1014
require_once __DIR__ . "/../utils/basic.inc";
1115

12-
$manager = new MongoDB\Driver\Manager(THROWAWAY);
16+
$uri = createTemporaryMongoInstance();
17+
$manager = new MongoDB\Driver\Manager($uri);
1318

1419
$bulkWrite = new MongoDB\Driver\BulkWrite;
1520

@@ -40,11 +45,11 @@ throws(function() use ($cursor) {
4045

4146
?>
4247
===DONE===
43-
<?php DELETE("THROWAWAY"); ?>
48+
<?php destroyTemporaryMongoInstance(); ?>
4449
<?php exit(0); ?>
4550
--CLEAN--
4651
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
47-
<?php DELETE("THROWAWAY"); ?>
52+
<?php destroyTemporaryMongoInstance(); ?>
4853
--EXPECT--
4954
Inserted: 5
5055
0 => {_id: 0}

tests/utils/basic.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require_once __DIR__ . "/" . "tools.php";
44

55
define('URI', getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/');
6+
define('MONGO_ORCHESTRATION_URI', getenv('MONGO_ORCHESTRATION_URI') ?: 'http://localhost:8889/v1');
67
define('DATABASE_NAME', getenv('MONGODB_DATABASE') ?: 'phongo');
78
define('COLLECTION_NAME', makeCollectionNameFromFilename($_SERVER['SCRIPT_FILENAME']));
89
define('NS', DATABASE_NAME . '.' . COLLECTION_NAME);

tests/utils/tools.php

Lines changed: 73 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -148,21 +148,33 @@ function get_server_storage_engine($uri)
148148
}
149149

150150
/**
151-
* Returns the version of the primary server.
151+
* Helper to return the version of a specific server.
152152
*
153-
* @param string $uri Connection string
153+
* @param MongoDB\Driver\Server $server
154154
* @return string
155155
* @throws RuntimeException
156156
*/
157-
function get_server_version($uri)
157+
function get_server_version_from_server(MongoDB\Driver\Server $server)
158158
{
159-
$server = get_primary_server($uri);
160159
$command = new Command(['buildInfo' => 1]);
161160
$cursor = $server->executeCommand('admin', $command);
162161

163162
return current($cursor->toArray())->version;
164163
}
165164

165+
/**
166+
* Returns the version of the primary server.
167+
*
168+
* @param string $uri Connection string
169+
* @return string
170+
* @throws RuntimeException
171+
*/
172+
function get_server_version($uri)
173+
{
174+
$server = get_primary_server($uri);
175+
return get_server_version_from_server($server);
176+
}
177+
166178
/**
167179
* Returns the value of a URI option, or null if it's not found.
168180
*
@@ -441,11 +453,19 @@ function loadFixtures(\MongoDB\Driver\Manager $manager, $dbname = DATABASE_NAME,
441453
}
442454
}
443455

444-
function START($id, array $options = array()) {
445-
/* starting/stopping servers only works using the Vagrant setup */
446-
PREDICTABLE();
447-
448-
$options += array("name" => "mongod", "id" => $id);
456+
function createTemporaryMongoInstance(array $options = [])
457+
{
458+
$id = 'mo_' . COLLECTION_NAME;
459+
$options += [
460+
"name" => "mongod",
461+
"id" => $id,
462+
'procParams' => [
463+
'dbpath' => "/tmp/phongo/{$id}",
464+
'logpath' => "/tmp/phongo/{$id}.log",
465+
'ipv6' => true,
466+
'setParameter' => [ 'enableTestCommands' => 1 ],
467+
],
468+
];
449469
$opts = array(
450470
"http" => array(
451471
"timeout" => 60,
@@ -457,24 +477,24 @@ function START($id, array $options = array()) {
457477
),
458478
);
459479
$ctx = stream_context_create($opts);
460-
$json = file_get_contents(getMOUri() . "/servers/$id", false, $ctx);
480+
$json = file_get_contents(MONGO_ORCHESTRATION_URI . "/servers/$id", false, $ctx);
461481
$result = json_decode($json, true);
462482

463483
/* Failed -- or was already started */
464484
if (!isset($result["mongodb_uri"])) {
465-
DELETE($id);
466-
define($id, false);
485+
destroyTemporaryMongoInstance($id);
486+
throw new Exception("Could not start temporary server instance\n");
467487
} else {
468-
define($id, $result["mongodb_uri"]);
469-
$FILENAME = sys_get_temp_dir() . "/PHONGO-SERVERS.json";
470-
471-
$json = file_get_contents($FILENAME);
472-
$config = json_decode($json, true);
473-
$config[$id] = constant($id);
474-
file_put_contents($FILENAME, json_encode($config, JSON_PRETTY_PRINT));
488+
return $result['mongodb_uri'];
475489
}
476490
}
477-
function DELETE($id) {
491+
492+
function destroyTemporaryMongoInstance($id = NULL)
493+
{
494+
if ($id == NULL) {
495+
$id = 'mo_' . COLLECTION_NAME;
496+
}
497+
478498
$opts = array(
479499
"http" => array(
480500
"timeout" => 60,
@@ -484,14 +504,9 @@ function DELETE($id) {
484504
),
485505
);
486506
$ctx = stream_context_create($opts);
487-
$json = file_get_contents(getMOUri() . "/servers/$id", false, $ctx);
488-
$FILENAME = sys_get_temp_dir() . "/PHONGO-SERVERS.json";
489-
490-
$json = file_get_contents($FILENAME);
491-
$config = json_decode($json, true);
492-
unset($config[$id]);
493-
file_put_contents($FILENAME, json_encode($config, JSON_PRETTY_PRINT));
507+
$json = file_get_contents(MONGO_ORCHESTRATION_URI . "/servers/$id", false, $ctx);
494508
}
509+
495510
function severityToString($type) {
496511
switch($type) {
497512
case E_WARNING:
@@ -629,41 +644,23 @@ function def($arr) {
629644
}
630645
}
631646

632-
function configureFailPoint(MongoDB\Driver\Manager $manager, $failPoint, $mode, $data = array()) {
633-
647+
function configureFailPoint(MongoDB\Driver\Manager $manager, $failPoint, $mode, array $data = [])
648+
{
634649
$doc = array(
635-
"configureFailPoint" => $failPoint,
636-
"mode" => $mode,
650+
'configureFailPoint' => $failPoint,
651+
'mode' => $mode,
637652
);
638653
if ($data) {
639-
$doc["data"] = $data;
654+
$doc['data'] = $data;
640655
}
641656

642657
$cmd = new MongoDB\Driver\Command($doc);
643-
$result = $manager->executeCommand("admin", $cmd);
644-
$arr = current($result->toArray());
645-
if (empty($arr->ok)) {
646-
var_dump($result);
647-
throw new RuntimeException("Failpoint failed");
648-
}
649-
return true;
658+
$manager->executeCommand('admin', $cmd);
650659
}
651660

652-
function failMaxTimeMS(MongoDB\Driver\Manager $manager) {
653-
return configureFailPoint($manager, "maxTimeAlwaysTimeOut", array("times" => 1));
654-
}
655-
656-
function getMOUri() {
657-
if (!($HOST = getenv("MONGODB_ORCHESTRATION_HOST"))) {
658-
$HOST = "192.168.112.10";
659-
}
660-
661-
if (!($PORT = getenv("MONGODB_ORCHESTRATION_PORT"))) {
662-
$PORT = "8889";
663-
}
664-
$MO = "http://$HOST:$PORT/v1";
665-
666-
return $MO;
661+
function failMaxTimeMS(MongoDB\Driver\Manager $manager)
662+
{
663+
configureFailPoint($manager, 'maxTimeAlwaysTimeOut', [ 'times' => 1 ]);
667664
}
668665

669666
function getMOPresetBase() {
@@ -695,6 +692,26 @@ function fromJSON($var) {
695692

696693
/* Note: this fail point may terminate the mongod process, so you may want to
697694
* use this in conjunction with a throwaway server. */
698-
function failGetMore(MongoDB\Driver\Manager $manager) {
699-
return configureFailPoint($manager, "failReceivedGetmore", "alwaysOn");
695+
function failGetMore(MongoDB\Driver\Manager $manager)
696+
{
697+
/* We need to do version detection here */
698+
$primary = $manager->selectServer(new ReadPreference('primary'));
699+
$version = get_server_version_from_server($primary);
700+
701+
if (version_compare($version, "3.2", "<")) {
702+
configureFailPoint($manager, 'failReceivedGetmore', 'alwaysOn');
703+
return;
704+
}
705+
706+
if (version_compare($version, "4.0", ">=")) {
707+
/* We use 237 here, as that's the same original code that MongoD would
708+
* throw if a cursor had already gone by the time we call getMore. This
709+
* allows us to make things consistent with the getMore OP behaviour
710+
* from previous mongod versions. An errorCode is required here for the
711+
* failPoint to work. */
712+
configureFailPoint($manager, 'failCommand', 'alwaysOn', [ 'errorCode' => 237, 'failCommands' => ['getMore'] ]);
713+
return;
714+
}
715+
716+
throw new Exception("Trying to configure a getMore fail point for a server version ($version) that doesn't support it");
700717
}

0 commit comments

Comments
 (0)