RackspaceTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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-2015 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 = dirname(__FILE__) . '/_files/'.$shortClassName . '_testAuthenticate.response';
  69. $this->httpClientAdapterTest->setResponse($this->loadResponse($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. $response = file_get_contents($name);
  83. // Line endings are sometimes an issue inside the canned responses; the
  84. // following is a negative lookbehind assertion, and replaces any \n
  85. // not preceded by \r with the sequence \r\n, ensuring that the message
  86. // is well-formed.
  87. return preg_replace("#(?<!\r)\n#", "\r\n", $response);
  88. }
  89. /**
  90. * Get Config Array
  91. *
  92. * @return array
  93. */
  94. static function getConfigArray()
  95. {
  96. return array(
  97. Zend_Cloud_Infrastructure_Factory::INFRASTRUCTURE_ADAPTER_KEY => 'Zend_Cloud_Infrastructure_Adapter_Rackspace',
  98. Zend_Cloud_Infrastructure_Adapter_Rackspace::RACKSPACE_USER => constant('TESTS_ZEND_SERVICE_RACKSPACE_ONLINE_USER'),
  99. Zend_Cloud_Infrastructure_Adapter_Rackspace::RACKSPACE_KEY => constant('TESTS_ZEND_SERVICE_RACKSPACE_ONLINE_KEY'),
  100. Zend_Cloud_Infrastructure_Adapter_Rackspace::RACKSPACE_REGION => constant('TESTS_ZEND_SERVICE_RACKSPACE_ONLINE_REGION')
  101. );
  102. }
  103. /**
  104. * Test all the constants of the class
  105. */
  106. public function testConstants()
  107. {
  108. $this->assertEquals('rackspace_user', Zend_Cloud_Infrastructure_Adapter_Rackspace::RACKSPACE_USER);
  109. $this->assertEquals('rackspace_key', Zend_Cloud_Infrastructure_Adapter_Rackspace::RACKSPACE_KEY);
  110. $this->assertEquals('rackspace_region', Zend_Cloud_Infrastructure_Adapter_Rackspace::RACKSPACE_REGION);
  111. $this->assertEquals('USA', Zend_Cloud_Infrastructure_Adapter_Rackspace::RACKSPACE_ZONE_USA);
  112. $this->assertEquals('UK', Zend_Cloud_Infrastructure_Adapter_Rackspace::RACKSPACE_ZONE_UK);
  113. $this->assertTrue(Zend_Cloud_Infrastructure_Adapter_Rackspace::MONITOR_CPU_SAMPLES>0);
  114. }
  115. /**
  116. * Test construct with missing params
  117. */
  118. public function testConstructExceptionMissingParams()
  119. {
  120. $this->setExpectedException(
  121. 'Zend_Cloud_Infrastructure_Exception',
  122. 'Invalid options provided'
  123. );
  124. $instance = new Zend_Cloud_Infrastructure_Adapter_Rackspace('foo');
  125. }
  126. /**
  127. * Test getAdapter
  128. */
  129. public function testGetAdapter()
  130. {
  131. $this->assertTrue(
  132. $this->infrastructure->getAdapter() instanceof Zend_Service_Rackspace_Servers
  133. );
  134. }
  135. /**
  136. * Test create an instance
  137. */
  138. public function testCreateInstance()
  139. {
  140. $options = array (
  141. 'imageId' => constant('TESTS_ZEND_SERVICE_RACKSPACE_SERVER_IMAGEID'),
  142. 'flavorId' => constant('TESTS_ZEND_SERVICE_RACKSPACE_SERVER_FLAVORID'),
  143. 'metadata' => array (
  144. 'foo' => 'bar'
  145. )
  146. );
  147. $instance = $this->infrastructure->createInstance(constant('TESTS_ZEND_SERVICE_RACKSPACE_SERVER_IMAGE_NAME'), $options);
  148. self::$instanceId= $instance->getId();
  149. $this->assertEquals(constant('TESTS_ZEND_SERVICE_RACKSPACE_SERVER_IMAGEID'), $instance->getImageId());
  150. }
  151. /**
  152. * Test list of an instance
  153. */
  154. public function testListInstance()
  155. {
  156. $instances = $this->infrastructure->listInstances(self::$instanceId);
  157. $this->assertTrue(!empty($instances));
  158. }
  159. /**
  160. * Test images instance
  161. */
  162. public function testImagesInstance()
  163. {
  164. $images = $this->infrastructure->imagesInstance();
  165. $this->assertTrue(!empty($images));
  166. }
  167. /**
  168. * Test zones instance
  169. */
  170. public function testZonesInstance()
  171. {
  172. $zones = $this->infrastructure->zonesInstance();
  173. $this->assertTrue(!empty($zones));
  174. }
  175. /**
  176. * Test monitor instance
  177. */
  178. public function testMonitorInstance()
  179. {
  180. $this->markTestSkipped('Test monitor instance skipped');
  181. }
  182. /**
  183. * Test deploy instance
  184. */
  185. public function testDeployInstance()
  186. {
  187. $this->markTestSkipped('Test deploy instance skipped');
  188. }
  189. /**
  190. * Test stop an instance
  191. */
  192. public function testStopInstance()
  193. {
  194. $this->markTestSkipped('Test stop instance skipped');
  195. }
  196. /**
  197. * Test start an instance
  198. */
  199. public function testStartInstance()
  200. {
  201. $this->markTestSkipped('Test start instance skipped');
  202. }
  203. /**
  204. * Test reboot and instance
  205. */
  206. public function testRebootInstance()
  207. {
  208. $this->assertTrue($this->infrastructure->rebootInstance(self::$instanceId));
  209. }
  210. /**
  211. * Test destroy instance
  212. */
  213. public function testDestroyInstance()
  214. {
  215. $this->assertTrue($this->infrastructure->destroyInstance(self::$instanceId));
  216. }
  217. }
  218. if (PHPUnit_MAIN_METHOD == 'Zend_Cloud_Infrastructure_Adapter_RackspaceTest::main') {
  219. Zend_Cloud_Infrastructure_Adapter_RackspaceTest::main();
  220. }