Skip to content

Commit d007971

Browse files
committed
PHON-7: Trivial Monog Orchestration abstraction
->start("preset.json") # See scripts/presets/ ->getURI("preset.json") # Get the MONGODB_URI ->stopAll() # Stops all running mongods ->stopId("SERVER-ID") # The 'id' key in preset.json
1 parent 23bc48c commit d007971

File tree

2 files changed

+46
-23
lines changed

2 files changed

+46
-23
lines changed

tests/utils/basic.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ if (!$orch->ping()) {
1515
}
1616

1717
$consts = array(
18-
"MONGODB_URI" => $orch->hasStandalone() ?: $orch->startStandalone(),
19-
"MONGODB_CLEANUP_URI" => $orch->hasStandalone() ?: $orch->startStandalone(),
18+
"MONGODB_URI" => $orch->getURI("standalone.json"),
19+
"MONGODB_CLEANUP_URI" => $orch->getURI("standalone.json"),
2020
"DATABASE_NAME" => "phongo",
2121
"COLLECTION_NAME" => makeCollectionNameFromFilename($_SERVER["SCRIPT_FILENAME"]),
2222
"DEBUG_DIR" => sys_get_temp_dir() . "/PHONGO-TESTS/",

tests/utils/orchestration.php

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,41 +22,38 @@ function ping() {
2222
return true;
2323
}
2424

25+
26+
2527
function stopAll() {
2628
$servers = $this->get("servers");
2729
foreach($servers["servers"] as $server) {
28-
$this->stopServer($server["id"]);
30+
$this->stopId($server["id"]);
2931
}
3032
}
3133

32-
function hasStandalone() {
33-
try {
34-
$data = $this->get("servers/STANDALONE");
35-
} catch(\Exception $e) {
36-
return false;
37-
}
38-
return $data["mongodb_uri"];
39-
}
4034

41-
function startStandalone() {
42-
$retval = $this->post("servers", ["name" => "mongod", "id" => "STANDALONE"]);
43-
if ($retval["procInfo"]["alive"]) {
44-
return $retval["mongodb_uri"];
35+
36+
function start($preset) {
37+
$relative = __DIR__ . "/../../";
38+
$file = "scripts/presets/$preset";
39+
if (!file_exists($relative.$file)) {
40+
throw new \Exception("Cannot file $file in $relative");
4541
}
4642

47-
return false;
43+
$retval = $this->post("servers", ["preset" => "/phongo/$file"]);
44+
return $this->_returnURIIfAlive($retval);
4845
}
4946

50-
function stopStandalone() {
51-
return $this->stopServer("STANDALONE");
47+
function getURI($preset) {
48+
$relative = __DIR__ . "/../../";
49+
$file = "scripts/presets/$preset";
50+
$content = file_get_contents($relative.$file);
51+
$id = json_decode($content, true)["id"];
52+
return $this->_returnURIIfOK($id);
5253
}
5354

54-
function getServerInfo($id) {
55-
$data = $this->get("servers/" . $id);
56-
return $data;
57-
}
5855

59-
function stopServer($id) {
56+
function stopId($id) {
6057
try {
6158
$retval = $this->delete("servers/$id");
6259
return true;
@@ -68,10 +65,13 @@ function stopServer($id) {
6865
return false;
6966
}
7067
}
68+
7169
function getTimeout() {
7270
return $this->conf["timeout"];
7371
}
7472

73+
74+
7575
function delete($target) {
7676
$opts = [
7777
"http" => [
@@ -119,6 +119,29 @@ function post($target, $array) {
119119
return $this->_sendAndReceive($target, $opts);
120120
}
121121

122+
123+
124+
protected function _returnURIIfOK($id) {
125+
try {
126+
$data = $this->get("servers/$id");
127+
} catch(\Exception $e) {
128+
return false;
129+
}
130+
return $data["mongodb_uri"];
131+
}
132+
133+
protected function _returnURIIfAlive($info) {
134+
if (!isset($info["procInfo"])) {
135+
return false;
136+
}
137+
138+
if ($info["procInfo"]["alive"]) {
139+
return $info["mongodb_uri"];
140+
}
141+
142+
return false;
143+
}
144+
122145
protected function _sendAndReceive($target, $opts) {
123146
$context = stream_context_create($opts);
124147
$url = $this->baseuri . "/" . $target;

0 commit comments

Comments
 (0)