RackspaceTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend\Cloud\Infrastructure\Adapter
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. require_once 'Zend/Http/Client/Adapter/Test.php';
  22. require_once 'Zend/Cloud/Infrastructure/Adapter/Rackspace.php';
  23. require_once 'Zend/Cloud/Infrastructure/Factory.php';
  24. class Zend_Cloud_Infrastructure_Adapter_RackspaceTest extends PHPUnit_Framework_TestCase
  25. {
  26. /**
  27. * Timeout in seconds for status change
  28. */
  29. const STATUS_TIMEOUT= 120;
  30. /**
  31. * Reference to Infrastructure object
  32. *
  33. * @var Zend_Cloud_Infrastructure_Adapter
  34. */
  35. protected $infrastructure;
  36. /**
  37. * Socket based HTTP client adapter
  38. *
  39. * @var Zend_Http_Client_Adapter_Test
  40. */
  41. protected $httpClientAdapterTest;
  42. /**
  43. * Image ID of the instance
  44. *
  45. * @var string
  46. */
  47. protected static $instanceId;
  48. /**
  49. * Setup for each test
  50. */
  51. public function setUp()
  52. {
  53. $this->infrastructure = Zend_Cloud_Infrastructure_Factory::getAdapter(array(
  54. Zend_Cloud_Infrastructure_Factory::INFRASTRUCTURE_ADAPTER_KEY => 'Zend_Cloud_Infrastructure_Adapter_Rackspace',
  55. Zend_Cloud_Infrastructure_Adapter_Rackspace::RACKSPACE_USER => 'foo',
  56. Zend_Cloud_Infrastructure_Adapter_Rackspace::RACKSPACE_KEY => 'bar',
  57. Zend_Cloud_Infrastructure_Adapter_Rackspace::RACKSPACE_REGION => 'USA'
  58. ));
  59. $this->httpClientAdapterTest = new Zend_Http_Client_Adapter_Test();
  60. $this->infrastructure->getAdapter()
  61. ->getHttpClient()
  62. ->setAdapter($this->httpClientAdapterTest);
  63. // load the HTTP response (from a file)
  64. $shortClassName = 'RackspaceTest';
  65. $filename= dirname(__FILE__) . '/_files/' . $shortClassName . '_'. $this->getName().'.response';
  66. if (file_exists($filename)) {
  67. // authentication (from file)
  68. $content = file_get_contents(dirname(__FILE__) . '/_files/'.$shortClassName . '_testAuthenticate.response');
  69. $this->httpClientAdapterTest->setResponse($content);
  70. $this->assertTrue($this->infrastructure->getAdapter()->authenticate(),'Authentication failed');
  71. $this->httpClientAdapterTest->setResponse($this->loadResponse($filename));
  72. }
  73. }
  74. /**
  75. * Utility method for returning a string HTTP response, which is loaded from a file
  76. *
  77. * @param string $name
  78. * @return string
  79. */
  80. protected function loadResponse($name)
  81. {
  82. return file_get_contents($name);
  83. }
  84. /**
  85. * Get Config Array
  86. *
  87. * @return array
  88. */
  89. static function getConfigArray()
  90. {
  91. return array(
  92. Zend_Cloud_Infrastructure_Factory::INFRASTRUCTURE_ADAPTER_KEY => 'Zend_Cloud_Infrastructure_Adapter_Rackspace',
  93. Zend_Cloud_Infrastructure_Adapter_Rackspace::RACKSPACE_USER => constant('TESTS_ZEND_SERVICE_RACKSPACE_ONLINE_USER'),
  94. Zend_Cloud_Infrastructure_Adapter_Rackspace::RACKSPACE_KEY => constant('TESTS_ZEND_SERVICE_RACKSPACE_ONLINE_KEY'),
  95. Zend_Cloud_Infrastructure_Adapter_Rackspace::RACKSPACE_REGION => constant('TESTS_ZEND_SERVICE_RACKSPACE_ONLINE_REGION')
  96. );
  97. }
  98. /**
  99. * Test all the constants of the class
  100. */
  101. public function testConstants()
  102. {
  103. $this->assertEquals('rackspace_user', Zend_Cloud_Infrastructure_Adapter_Rackspace::RACKSPACE_USER);
  104. $this->assertEquals('rackspace_key', Zend_Cloud_Infrastructure_Adapter_Rackspace::RACKSPACE_KEY);
  105. $this->assertEquals('rackspace_region', Zend_Cloud_Infrastructure_Adapter_Rackspace::RACKSPACE_REGION);
  106. $this->assertEquals('USA', Zend_Cloud_Infrastructure_Adapter_Rackspace::RACKSPACE_ZONE_USA);
  107. $this->assertEquals('UK', Zend_Cloud_Infrastructure_Adapter_Rackspace::RACKSPACE_ZONE_UK);
  108. $this->assertTrue(Zend_Cloud_Infrastructure_Adapter_Rackspace::MONITOR_CPU_SAMPLES>0);
  109. }
  110. /**
  111. * Test construct with missing params
  112. */
  113. public function testConstructExceptionMissingParams()
  114. {
  115. $this->setExpectedException(
  116. 'Zend_Cloud_Infrastructure_Exception',
  117. 'Invalid options provided'
  118. );
  119. $instance = new Zend_Cloud_Infrastructure_Adapter_Rackspace('foo');
  120. }
  121. /**
  122. * Test getAdapter
  123. */
  124. public function testGetAdapter()
  125. {
  126. $this->assertTrue(
  127. $this->infrastructure->getAdapter() instanceof Zend_Service_Rackspace_Servers
  128. );
  129. }
  130. /**
  131. * Test create an instance
  132. */
  133. public function testCreateInstance()
  134. {
  135. $options = array (
  136. 'imageId' => constant('TESTS_ZEND_SERVICE_RACKSPACE_SERVER_IMAGEID'),
  137. 'flavorId' => constant('TESTS_ZEND_SERVICE_RACKSPACE_SERVER_FLAVORID'),
  138. 'metadata' => array (
  139. 'foo' => 'bar'
  140. )
  141. );
  142. $instance = $this->infrastructure->createInstance(constant('TESTS_ZEND_SERVICE_RACKSPACE_SERVER_IMAGE_NAME'), $options);
  143. self::$instanceId= $instance->getId();
  144. $this->assertEquals(constant('TESTS_ZEND_SERVICE_RACKSPACE_SERVER_IMAGEID'), $instance->getImageId());
  145. }
  146. /**
  147. * Test list of an instance
  148. */
  149. public function testListInstance()
  150. {
  151. $instances = $this->infrastructure->listInstances(self::$instanceId);
  152. $this->assertTrue(!empty($instances));
  153. }
  154. /**
  155. * Test images instance
  156. */
  157. public function testImagesInstance()
  158. {
  159. $images = $this->infrastructure->imagesInstance();
  160. $this->assertTrue(!empty($images));
  161. }
  162. /**
  163. * Test zones instance
  164. */
  165. public function testZonesInstance()
  166. {
  167. $zones = $this->infrastructure->zonesInstance();
  168. $this->assertTrue(!empty($zones));
  169. }
  170. /**
  171. * Test monitor instance
  172. */
  173. public function testMonitorInstance()
  174. {
  175. $this->markTestSkipped('Test monitor instance skipped');
  176. }
  177. /**
  178. * Test deploy instance
  179. */
  180. public function testDeployInstance()
  181. {
  182. $this->markTestSkipped('Test deploy instance skipped');
  183. }
  184. /**
  185. * Test stop an instance
  186. */
  187. public function testStopInstance()
  188. {
  189. $this->markTestSkipped('Test stop instance skipped');
  190. }
  191. /**
  192. * Test start an instance
  193. */
  194. public function testStartInstance()
  195. {
  196. $this->markTestSkipped('Test start instance skipped');
  197. }
  198. /**
  199. * Test reboot and instance
  200. */
  201. public function testRebootInstance()
  202. {
  203. $this->assertTrue($this->infrastructure->rebootInstance(self::$instanceId));
  204. }
  205. /**
  206. * Test destroy instance
  207. */
  208. public function testDestroyInstance()
  209. {
  210. $this->assertTrue($this->infrastructure->destroyInstance(self::$instanceId));
  211. }
  212. }
  213. if (PHPUnit_MAIN_METHOD == 'Zend_Cloud_Infrastructure_Adapter_RackspaceTest::main') {
  214. Zend_Cloud_Infrastructure_Adapter_RackspaceTest::main();
  215. }