Skip to content

Commit 2d88ca2

Browse files
committed
PHON-5: Use Mongo Orchestration to manage MongoDB servers
1 parent bbbda03 commit 2d88ca2

File tree

3 files changed

+170
-3
lines changed

3 files changed

+170
-3
lines changed

Makefile.frag

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ composer:
4444
exit 1; \
4545
fi
4646

47+
test-bootstrap:
48+
php scripts/start-servers.php
49+
4750
testunit: composer
4851
@command -v phpunit >/dev/null 2>&1; \
4952
if test $$? -eq 0; then \

tests/utils/basic.inc

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
<?php
2+
require __DIR__ . "/" . "orchestration.php";
23
require __DIR__ . "/" . "tools.php";
34

45

6+
$host = "http://localhost:8889";
7+
if ($_ENV && isset($_ENV["ORCHESTRATION"])) {
8+
$host = $_ENV["ORCHESTRATION"];
59
}
610

711

12+
$orch = new Mongo\Orchestration($host);
13+
if (!$orch->ping()) {
14+
exit("skip Mongo Orchestration not running on {$host} - run 'make test-bootstrap'");
15+
}
16+
817
$consts = array(
9-
"MONGODB_URI" => "mongodb://localhost:27017",
10-
"MONGODB_CLEANUP_URI" => "mongodb://localhost:27017",
11-
"DATABASE_NAME" => "phongo_test",
18+
"MONGODB_URI" => $orch->hasStandalone() ?: $orch->startStandalone(),
19+
"MONGODB_CLEANUP_URI" => $orch->hasStandalone() ?: $orch->startStandalone(),
20+
"DATABASE_NAME" => "phongo",
1221
"COLLECTION_NAME" => makeCollectionNameFromFilename($_SERVER["SCRIPT_FILENAME"]),
1322
"DEBUG_DIR" => sys_get_temp_dir() . "/PHONGO-TESTS/",
1423
);

tests/utils/orchestration.php

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
<?php
2+
3+
4+
namespace Mongo;
5+
class Orchestration {
6+
protected $baseuri;
7+
protected $conf;
8+
9+
function __construct($baseuri) {
10+
$this->baseuri = $baseuri;
11+
$this->conf = array(
12+
"timeout" => 35,
13+
);
14+
}
15+
16+
function ping() {
17+
try {
18+
$data = $this->get("");
19+
} catch(\Exception $e) {
20+
return false;
21+
}
22+
}
23+
24+
function stopAll() {
25+
$servers = $this->get("servers");
26+
foreach($servers["servers"] as $server) {
27+
$this->stopServer($server["id"]);
28+
}
29+
}
30+
31+
function hasStandalone() {
32+
try {
33+
$data = $this->get("servers/STANDALONE");
34+
} catch(\Exception $e) {
35+
return false;
36+
}
37+
return $data["mongodb_uri"];
38+
}
39+
40+
function startStandalone() {
41+
$retval = $this->post("servers", ["name" => "mongod", "id" => "STANDALONE"]);
42+
if ($retval["procInfo"]["alive"]) {
43+
return $retval["mongodb_uri"];
44+
}
45+
46+
return false;
47+
}
48+
49+
function stopStandalone() {
50+
return $this->stopServer("STANDALONE");
51+
}
52+
53+
function getServerInfo($id) {
54+
$data = $this->get("servers/" . $id);
55+
return $data;
56+
}
57+
58+
function stopServer($id) {
59+
try {
60+
$retval = $this->delete("servers/$id");
61+
return true;
62+
} catch(\Exception $e) {
63+
if ($e->getCode() == 204) {
64+
return true;
65+
}
66+
67+
return false;
68+
}
69+
}
70+
function getTimeout() {
71+
return $this->conf["timeout"];
72+
}
73+
74+
function delete($target) {
75+
$opts = [
76+
"http" => [
77+
"timeout" => $this->getTimeout(),
78+
"method" => "DELETE",
79+
"header" => "Accept: application/json\r\n" .
80+
"Content-type: application/x-www-form-urlencoded",
81+
"ignore_errors" => true,
82+
],
83+
];
84+
85+
$data = $this->_sendAndReceive($target, $opts);
86+
return $data;
87+
}
88+
89+
function get($target) {
90+
$opts = [
91+
"http" => [
92+
"timeout" => $this->getTimeout(),
93+
"method" => "GET",
94+
"header" => "Accept: application/json\r\n" .
95+
"Content-type: application/x-www-form-urlencoded",
96+
"ignore_errors" => true,
97+
],
98+
];
99+
100+
$data = $this->_sendAndReceive($target, $opts);
101+
return $data;
102+
}
103+
104+
function post($target, $array) {
105+
$postdata = json_encode($array);
106+
107+
$opts = [
108+
"http" => [
109+
"timeout" => $this->getTimeout(),
110+
"method" => "POST",
111+
"header" => "Accept: application/json\r\n" .
112+
"Content-type: application/x-www-form-urlencoded",
113+
"content" => $postdata,
114+
"ignore_errors" => true,
115+
],
116+
];
117+
118+
return $this->_sendAndReceive($target, $opts);
119+
}
120+
121+
protected function _sendAndReceive($target, $opts) {
122+
$context = stream_context_create($opts);
123+
$url = $this->baseuri . "/" . $target;
124+
125+
$http = $opts["http"];
126+
127+
$hdr = "";
128+
foreach(explode("\r\n", $http["header"]) as $header) {
129+
$hdr .= "--header='{$header}' ";
130+
}
131+
132+
$debug = sprintf("wget --body-data='%s' --method='%s' %s %s\n",
133+
isset($http["content"]) ? $http["content"] : "",
134+
$http["method"],
135+
$hdr,
136+
$url
137+
);
138+
if (defined("DUMP_WGET") && DUMP_WGET) {
139+
echo $debug;
140+
}
141+
142+
$data = @file_get_contents($url, false, $context);
143+
if ($data) {
144+
return json_decode($data, true);
145+
}
146+
if (!empty($http_response_header)) {
147+
sscanf($http_response_header[0], "HTTP/%f %i %s", $proto, $code, $desc);
148+
throw new \RuntimeException(json_encode($http_response_header), $code);
149+
}
150+
151+
throw new \RuntimeException(error_get_last()["message"]);
152+
153+
}
154+
}
155+

0 commit comments

Comments
 (0)