2
0

RackspaceTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 => constant('TESTS_ZEND_SERVICE_RACKSPACE_ONLINE_USER'),
  56. Zend_Cloud_Infrastructure_Adapter_Rackspace::RACKSPACE_KEY => constant('TESTS_ZEND_SERVICE_RACKSPACE_ONLINE_KEY'),
  57. Zend_Cloud_Infrastructure_Adapter_Rackspace::RACKSPACE_REGION => constant('TESTS_ZEND_SERVICE_RACKSPACE_ONLINE_REGION')
  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 = substr(__CLASS__,strlen('Zend_Cloud_Infrastructure_Adapter_'));
  65. $filename= dirname(__FILE__) . '/_files/' . $shortClassName . '_'. $this->getName().'.response';
  66. if (file_exists($filename)) {
  67. $this->httpClientAdapterTest->setResponse($this->loadResponse($filename));
  68. }
  69. }
  70. /**
  71. * Utility method for returning a string HTTP response, which is loaded from a file
  72. *
  73. * @param string $name
  74. * @return string
  75. */
  76. protected function loadResponse($name)
  77. {
  78. return file_get_contents($name);
  79. }
  80. /**
  81. * Get Config Array
  82. *
  83. * @return array
  84. */
  85. static function getConfigArray()
  86. {
  87. return array(
  88. Zend_Cloud_Infrastructure_Factory::INFRASTRUCTURE_ADAPTER_KEY => 'Zend_Cloud_Infrastructure_Adapter_Rackspace',
  89. Zend_Cloud_Infrastructure_Adapter_Rackspace::RACKSPACE_USER => constant('TESTS_ZEND_SERVICE_RACKSPACE_ONLINE_USER'),
  90. Zend_Cloud_Infrastructure_Adapter_Rackspace::RACKSPACE_KEY => constant('TESTS_ZEND_SERVICE_RACKSPACE_ONLINE_KEY'),
  91. Zend_Cloud_Infrastructure_Adapter_Rackspace::RACKSPACE_REGION => constant('TESTS_ZEND_SERVICE_RACKSPACE_ONLINE_REGION')
  92. );
  93. }
  94. /**
  95. * Test all the constants of the class
  96. */
  97. public function testConstants()
  98. {
  99. $this->assertEquals('rackspace_user', Zend_Cloud_Infrastructure_Adapter_Rackspace::RACKSPACE_USER);
  100. $this->assertEquals('rackspace_key', Zend_Cloud_Infrastructure_Adapter_Rackspace::RACKSPACE_KEY);
  101. $this->assertEquals('rackspace_region', Zend_Cloud_Infrastructure_Adapter_Rackspace::RACKSPACE_REGION);
  102. $this->assertEquals('USA', Zend_Cloud_Infrastructure_Adapter_Rackspace::RACKSPACE_ZONE_USA);
  103. $this->assertEquals('UK', Zend_Cloud_Infrastructure_Adapter_Rackspace::RACKSPACE_ZONE_UK);
  104. $this->assertTrue(Zend_Cloud_Infrastructure_Adapter_Rackspace::MONITOR_CPU_SAMPLES>0);
  105. }
  106. /**
  107. * Test construct with missing params
  108. */
  109. public function testConstructExceptionMissingParams()
  110. {
  111. $this->setExpectedException(
  112. 'Zend_Cloud_Infrastructure_Exception',
  113. 'Invalid options provided'
  114. );
  115. $instance = new Zend_Cloud_Infrastructure_Adapter_Rackspace('foo');
  116. }
  117. /**
  118. * Test getAdapter
  119. */
  120. public function testGetAdapter()
  121. {
  122. $this->assertType('Zend_Service_Rackspace_Servers',$this->infrastructure->getAdapter());
  123. }
  124. /**
  125. * Test create an instance
  126. */
  127. public function testCreateInstance()
  128. {
  129. $options = array (
  130. 'imageId' => constant('TESTS_ZEND_SERVICE_RACKSPACE_SERVER_IMAGEID'),
  131. 'flavorId' => constant('TESTS_ZEND_SERVICE_RACKSPACE_SERVER_FLAVORID'),
  132. 'metadata' => array (
  133. 'foo' => 'bar'
  134. )
  135. );
  136. $instance = $this->infrastructure->createInstance(constant('TESTS_ZEND_SERVICE_RACKSPACE_SERVER_IMAGE_NAME'), $options);
  137. self::$instanceId= $instance->getId();
  138. $this->assertEquals(constant('TESTS_ZEND_SERVICE_RACKSPACE_SERVER_IMAGEID'), $instance->getImageId());
  139. }
  140. /**
  141. * Test last HTTP request
  142. */
  143. public function testGetLastHttpRequest()
  144. {
  145. $lastHttpRequest = $this->infrastructure->getLastHttpRequest();
  146. $this->assertTrue(!empty($lastHttpRequest));
  147. }
  148. /**
  149. * Test last HTTP response
  150. */
  151. public function testGetLastHttpResponse()
  152. {
  153. $lastHttpResponse = $this->infrastructure->getLastHttpResponse();
  154. $this->assertTrue(!empty($lastHttpResponse));
  155. }
  156. /**
  157. * Test list of an instance
  158. */
  159. public function testListInstance()
  160. {
  161. $instances = $this->infrastructure->listInstances(self::$instanceId);
  162. $this->assertTrue(!empty($instances));
  163. }
  164. /**
  165. * Test images instance
  166. */
  167. public function testImagesInstance()
  168. {
  169. $images = $this->infrastructure->imagesInstance();
  170. $this->assertTrue(!empty($images));
  171. }
  172. /**
  173. * Test zones instance
  174. */
  175. public function testZonesInstance()
  176. {
  177. $zones = $this->infrastructure->zonesInstance();
  178. $this->assertTrue(!empty($zones));
  179. }
  180. /**
  181. * Test monitor instance
  182. */
  183. public function testMonitorInstance()
  184. {
  185. $this->markTestSkipped('Test monitor instance skipped');
  186. }
  187. /**
  188. * Test deploy instance
  189. */
  190. public function testDeployInstance()
  191. {
  192. $this->markTestSkipped('Test deploy instance skipped');
  193. }
  194. /**
  195. * Test stop an instance
  196. */
  197. public function testStopInstance()
  198. {
  199. $this->markTestSkipped('Test stop instance skipped');
  200. }
  201. /**
  202. * Test start an instance
  203. */
  204. public function testStartInstance()
  205. {
  206. $this->markTestSkipped('Test start instance skipped');
  207. }
  208. /**
  209. * Test reboot and instance
  210. */
  211. public function testRebootInstance()
  212. {
  213. $this->assertTrue($this->infrastructure->rebootInstance(self::$instanceId));
  214. }
  215. /**
  216. * Test destroy instance
  217. */
  218. public function testDestroyInstance()
  219. {
  220. $this->assertTrue($this->infrastructure->destroyInstance(self::$instanceId));
  221. }
  222. }
  223. if (PHPUnit_MAIN_METHOD == 'Zend_Cloud_Infrastructure_Adapter_RackspaceTest::main') {
  224. Zend_Cloud_Infrastructure_Adapter_RackspaceTest::main();
  225. }