DBaaS instances now function correctly.

This commit is contained in:
Technosophos
2012-05-22 19:01:54 -05:00
parent 4d474bea7b
commit 56841e8f6c
3 changed files with 92 additions and 77 deletions

View File

@@ -76,17 +76,14 @@ class Instance {
* The name of the database. * The name of the database.
* @param string $flavor * @param string $flavor
* The string flavor name. Known values are: * The string flavor name. Known values are:
*- small
*- medium *- medium
* @param int $port
* If this is not specified, the default is used.
* @param array $typeSpec * @param array $typeSpec
* A typespec array. * A typespec array. Currently, only 'mysql', '5.5' is supported.
* @retval object HPCloud::Services::DBaaS::InstanceDetails * @retval object HPCloud::Services::DBaaS::InstanceDetails
* The details of creation, including login and password info. * The details of creation, including login and password info.
* @see http://api-docs.hpcloud.com/hpcloud-dbaas/1.0/content/instance-create.html * @see http://api-docs.hpcloud.com/hpcloud-dbaas/1.0/content/instance-create.html
*/ */
public function create($name, $flavor = 'medium', $port = NULL, $typeSpec = NULL) { public function create($name, $flavor = 'medium', $typeSpec = NULL) {
// Set type spec. As of the initial release of DBaaS, the only support // Set type spec. As of the initial release of DBaaS, the only support
// type is mysql 5.5. // type is mysql 5.5.
if (empty($typeSpec)) { if (empty($typeSpec)) {
@@ -102,13 +99,9 @@ class Instance {
'dbtype' => $typeSpec, 'dbtype' => $typeSpec,
), ),
); );
if (isset($port)) {
$json['instance']['port'] = $port;
}
$url = $this->url . '/instances'; $url = $this->url . '/instances';
$postData = json_encode($json); $postData = json_encode($json);
fwrite(STDOUT, "POST DATA: $postData\n"); //fwrite(STDOUT, "POST DATA: $postData\n");
$length = strlen($postData); $length = strlen($postData);
$headers = $this->headers(array( $headers = $this->headers(array(
'Accept' => 'application/json', 'Accept' => 'application/json',

View File

@@ -35,14 +35,13 @@ class InstanceDetails {
protected $created; protected $created;
protected $status; protected $status;
protected $hostname; protected $hostname;
protected $port;
protected $username; protected $username;
protected $password; protected $password;
public function newFromJSON($json) { public function newFromJSON($json) {
fwrite(STDOUT, json_encode($json)); // fwrite(STDOUT, json_encode($json));
$o = new InstanceDetails($json['name'], $json['id']); $o = new InstanceDetails($json['name'], $json['id']);
$o->links = $json['links']; $o->links = $json['links'];
@@ -50,7 +49,6 @@ class InstanceDetails {
$o->status = $json['status']; $o->status = $json['status'];
if (!empty($json['hostname'])) { if (!empty($json['hostname'])) {
$o->hostname = $json['hostname']; $o->hostname = $json['hostname'];
$o->port= !empty($json['port']) ? $json['port'] : '3306';
} }
if (!empty($json['credential']['username'])) { if (!empty($json['credential']['username'])) {
@@ -108,6 +106,7 @@ class InstanceDetails {
* Known status messages: * Known status messages:
*- running: Instance is fully operational. *- running: Instance is fully operational.
*- building: Instance is being created. *- building: Instance is being created.
*- restarting: Instance has been restarted, and is still coming online.
* *
* @retval string * @retval string
* A short status message. * A short status message.
@@ -116,9 +115,26 @@ class InstanceDetails {
return $this->status; return $this->status;
} }
/**
* Check whether the present instance is running.
*
* This is a convenience function for determining whether a remote
* instance reports itself to be running. It is equivalent to
* checking that status() returns 'running'.
*
* @retval boolean
* TRUE if this is running, FALSE otherwise.
*/
public function isRunning() {
return strcasecmp($this->status(), 'running') == 0;
}
/** /**
* Get the hostname. * Get the hostname.
* *
* Note that the port is always 3306, the MySQL default. Only the hostname
* is returned.
*
* @attention * @attention
* In version 1.0 of the DBaaS protocol, this is ONLY available immediately * In version 1.0 of the DBaaS protocol, this is ONLY available immediately
* after creation. * after creation.
@@ -132,17 +148,6 @@ class InstanceDetails {
return $this->hostname; return $this->hostname;
} }
/**
* The port number of the MySQL server.
*
* @retval integer
* The port number. If this is empty, the
* default (3306) should be assumed.
*/
public function port() {
return $this->port;
}
/** /**
* The username field, if available. * The username field, if available.
* *
@@ -200,11 +205,6 @@ class InstanceDetails {
* *
* A convenience function for PDO. * A convenience function for PDO.
* *
* @attention
* This may only be available immediately after the creation of
* the database. The current version of DBaaS does not return
* hostname and port in subsequent queries.
*
* @see http://us3.php.net/manual/en/ref.pdo-mysql.connection.php * @see http://us3.php.net/manual/en/ref.pdo-mysql.connection.php
* *
* @param string $dbName * @param string $dbName
@@ -221,7 +221,7 @@ class InstanceDetails {
* need to change? * need to change?
*/ */
public function dsn($dbName = NULL, $charset = NULL) { public function dsn($dbName = NULL, $charset = NULL) {
$dsn = sprintf('mysql:host=%s;port=%s', $this->hostname(), $this->port()); $dsn = sprintf('mysql:host=%s;port=3306', $this->hostname());
if (!empty($dbName)) { if (!empty($dbName)) {
$dsn .= ';dbname=' . $dbName; $dsn .= ';dbname=' . $dbName;
} }

View File

@@ -47,12 +47,12 @@ class DBaaSInstanceTest extends \HPCloud\Tests\TestCase {
$inst = $this->inst(); $inst = $this->inst();
$list = $inst->listInstances(); $list = $inst->listInstances();
fwrite(STDOUT, print_r($list, TRUE)); //fwrite(STDOUT, print_r($list, TRUE));
if (!empty($list)) { if (!empty($list)) {
$dbName = self::conf('hpcloud.dbaas.database'); $dbName = self::conf('hpcloud.dbaas.database');
foreach ($list as $item) { foreach ($list as $item) {
if ($item->name() == $dbName) { if ($item->name() == $dbName) {
fprintf(STDOUT, "Deleting %s (%s)\n", $item->name(), $item->id()); // fprintf(STDOUT, "Deleting %s (%s)\n", $item->name(), $item->id());
$inst->delete($item->id()); $inst->delete($item->id());
} }
} }
@@ -60,6 +60,28 @@ class DBaaSInstanceTest extends \HPCloud\Tests\TestCase {
} }
public function waitUntilRunning($inst, &$details, $verbose = FALSE, $max = 15, $sleep = 5) {
if ($details->isRunning()) {
return TRUE;
}
for ($i = 0; $i < $max; ++$i) {
if ($verbose) fwrite(STDOUT, '⌛');
//fprintf(STDOUT, "Status: %s\n", $details->status());
sleep($sleep);
$details = $inst->describe($details->id());
if ($details->isRunning()) {
return TRUE;
}
}
throw \Exception(sprintf("Instance did not start after %d attempts (%d seconds)", $max, $max * $sleep));
}
public function testConstruct() { public function testConstruct() {
$ident = $this->identity(); $ident = $this->identity();
$dbaas = DBaaS::newFromIdentity($ident); $dbaas = DBaaS::newFromIdentity($ident);
@@ -84,7 +106,7 @@ class DBaaSInstanceTest extends \HPCloud\Tests\TestCase {
$dbName = self::conf('hpcloud.dbaas.database'); $dbName = self::conf('hpcloud.dbaas.database');
$details = $this->inst()->create($dbName, 'small', '3307'); $details = $this->inst()->create($dbName, 'small');
$this->assertInstanceOf('\HPCloud\Services\DBaaS\InstanceDetails', $details); $this->assertInstanceOf('\HPCloud\Services\DBaaS\InstanceDetails', $details);
@@ -92,11 +114,10 @@ class DBaaSInstanceTest extends \HPCloud\Tests\TestCase {
$this->assertNotEmpty($details->password()); $this->assertNotEmpty($details->password());
$this->assertNotEmpty($details->id()); $this->assertNotEmpty($details->id());
$this->assertNotEmpty($details->hostname()); $this->assertNotEmpty($details->hostname());
$this->assertNotEmpty($details->port());
$this->assertNotEmpty($details->createdOn()); $this->assertNotEmpty($details->createdOn());
$this->assertEquals($dbName, $details->name()); $this->assertEquals($dbName, $details->name());
$dsn = sprintf('mysql:host=%s;port=3307;dbname=foo;charset=utf-8', $details->hostname()); $dsn = sprintf('mysql:host=%s;port=3306;dbname=foo;charset=utf-8', $details->hostname());
$this->assertEquals($dsn, $details->dsn('foo', 'utf-8')); $this->assertEquals($dsn, $details->dsn('foo', 'utf-8'));
@@ -104,20 +125,22 @@ class DBaaSInstanceTest extends \HPCloud\Tests\TestCase {
'name' => $details->username(), 'name' => $details->username(),
'pass' => $details->password(), 'pass' => $details->password(),
); );
$this->dbId = $details->id(); //$db->id() = $details->id();
$this->created = $details->createdOn(); //$this->created = $details->createdOn();
return $details;
} }
/** /**
* @depends testCreate * @depends testCreate
*/ */
public function testDescribe() { public function testDescribe($db) {
$dbName = self::conf('hpcloud.dbaas.database'); $dbName = self::conf('hpcloud.dbaas.database');
// Canary. // Canary.
$this->assertNotEmpty($this->dbId); $this->assertNotEmpty($db->id());
$details = $this->inst()->describe($this->dbId); $details = $this->inst()->describe($db->id());
$this->assertInstanceOf('\HPCloud\Services\DBaaS\InstanceDetails', $details); $this->assertInstanceOf('\HPCloud\Services\DBaaS\InstanceDetails', $details);
$this->assertEmpty($details->username()); $this->assertEmpty($details->username());
@@ -126,7 +149,7 @@ class DBaaSInstanceTest extends \HPCloud\Tests\TestCase {
$this->assertNotEmpty($details->hostname()); $this->assertNotEmpty($details->hostname());
$this->assertNotEmpty($details->createdOn()); $this->assertNotEmpty($details->createdOn());
$this->assertEquals($this->dbId, $details->id()); $this->assertEquals($db->id(), $details->id());
$this->assertEquals($dbName, $details->name()); $this->assertEquals($dbName, $details->name());
} }
@@ -134,39 +157,30 @@ class DBaaSInstanceTest extends \HPCloud\Tests\TestCase {
/** /**
* @depends testCreate * @depends testCreate
*/ */
public function testRestart() { public function testRestart($db) {
// Canary. // Canary.
$this->assertNotEmpty($this->dbId); $this->assertNotEmpty($db->id());
$this->inst()->restart($this->dbId); $inst = $this->inst();
sleep(5);
$details = $this->inst()->details($this->dbId);
$this->assertEquals($this->created, $details->createdOn()); $this->waitUntilRunning($inst, $db, TRUE);
$inst->restart($db->id());
$this->waitUntilRunning($inst, $db, TRUE);
$details = $this->inst()->describe($db->id());
$this->assertEquals($db->createdOn(), $details->createdOn());
} }
/** /**
* @depends testCreate * @depends testCreate
*/ */
public function testResetPassword() { public function testListInstances($db) {
$pass = $this->credentials['pass'];
$this->assertNotEmpty($pass);
$newPass = $this->inst()->resetPassword($this->dbId);
$this->assertNotEmpty($newPass);
$this->assertNotEquals($pass, $newPass);
}
/**
* @depends testCreate
*/
public function testListInstances() {
$instances = $this->inst()->listInstances(); $instances = $this->inst()->listInstances();
$this->assertNotEmpty($instances); $this->assertNotEmpty($instances);
$this->assertGreaterThan(0, count($instances['instances']));
$match = 0; $match = 0;
$dbName = self::conf('hpcloud.dbaas.database'); $dbName = self::conf('hpcloud.dbaas.database');
@@ -179,28 +193,22 @@ class DBaaSInstanceTest extends \HPCloud\Tests\TestCase {
} }
} }
$this->assertEquals(1, $match); $this->assertEquals(1, $match);
return $db;
} }
/** /**
* @depends testListInstances * @depends testListInstances
*/ */
public function testIsItAllWorthIt() { public function testIsItAllWorthIt($db) {
$inst = $this->inst(); $inst = $this->inst();
$maxAttempts = 5; $this->waitUntilRunning($inst, $db, TRUE);
for($i = 0; $i < $maxAttempts; ++$i) { $dsn = $db->dsn();
$details = $inst->describe($this->dbId);
if ($details->status == 'ready') {
$dsn = $details->dsn();
break;
}
}
$this->assertNotEmpty($dsn); $this->assertNotEmpty($dsn);
$conn = new PDO($dsn, $this->credentials['user'], $this->credentials['pass']); $conn = new \PDO($dsn, $db->username(), $db->password());
$affected = $conn->execute('SELECT 1'); $affected = $conn->execute('SELECT 1');
@@ -208,19 +216,33 @@ class DBaaSInstanceTest extends \HPCloud\Tests\TestCase {
unset($conn); unset($conn);
} }
/**
* @depends testCreate
*/
public function testResetPassword($db) {
$pass = $db->password();
$this->assertNotEmpty($pass);
$newPass = $this->inst()->resetPassword($db->id());
$this->assertNotEmpty($newPass);
$this->assertNotEquals($pass, $newPass);
}
/** /**
* @depends testCreate * @depends testCreate
*/ */
public function testDelete() { public function testDelete($db) {
$match = 0; $match = 0;
$dbName = self::conf('hpcloud.dbaas.database'); $dbName = self::conf('hpcloud.dbaas.database');
$inst = $this->inst(); $inst = $this->inst();
$inst->delete($this->dbId); $inst->delete($db->id());
foreach ($instances['instances'] as $server) { $list = $inst->listInstances();
foreach ($list as $server) {
if ($server->name() == $dbName) { if ($server->name() == $dbName) {
++$match; ++$match;
} }