@@ -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+
495510function 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
669666function 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