RackspaceTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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->assertType('Zend_Service_Rackspace_Servers',$this->infrastructure->getAdapter());
  127. }
  128. /**
  129. * Test create an instance
  130. */
  131. public function testCreateInstance()
  132. {
  133. $options = array (
  134. 'imageId' => constant('TESTS_ZEND_SERVICE_RACKSPACE_SERVER_IMAGEID'),
  135. 'flavorId' => constant('TESTS_ZEND_SERVICE_RACKSPACE_SERVER_FLAVORID'),
  136. 'metadata' => array (
  137. 'foo' => 'bar'
  138. )
  139. );
  140. $instance = $this->infrastructure->createInstance(constant('TESTS_ZEND_SERVICE_RACKSPACE_SERVER_IMAGE_NAME'), $options);
  141. self::$instanceId= $instance->getId();
  142. $this->assertEquals(constant('TESTS_ZEND_SERVICE_RACKSPACE_SERVER_IMAGEID'), $instance->getImageId());
  143. }
  144. /**
  145. * Test list of an instance
  146. */
  147. public function testListInstance()
  148. {
  149. $instances = $this->infrastructure->listInstances(self::$instanceId);
  150. $this->assertTrue(!empty($instances));
  151. }
  152. /**
  153. * Test images instance
  154. */
  155. public function testImagesInstance()
  156. {
  157. $images = $this->infrastructure->imagesInstance();
  158. $this->assertTrue(!empty($images));
  159. }
  160. /**
  161. * Test zones instance
  162. */
  163. public function testZonesInstance()
  164. {
  165. $zones = $this->infrastructure->zonesInstance();
  166. $this->assertTrue(!empty($zones));
  167. }
  168. /**
  169. * Test monitor instance
  170. */
  171. public function testMonitorInstance()
  172. {
  173. $this->markTestSkipped('Test monitor instance skipped');
  174. }
  175. /**
  176. * Test deploy instance
  177. */
  178. public function testDeployInstance()
  179. {
  180. $this->markTestSkipped('Test deploy instance skipped');
  181. }
  182. /**
  183. * Test stop an instance
  184. */
  185. public function testStopInstance()
  186. {
  187. $this->markTestSkipped('Test stop instance skipped');
  188. }
  189. /**
  190. * Test start an instance
  191. */
  192. public function testStartInstance()
  193. {
  194. $this->markTestSkipped('Test start instance skipped');
  195. }
  196. /**
  197. * Test reboot and instance
  198. */
  199. public function testRebootInstance()
  200. {
  201. $this->assertTrue($this->infrastructure->rebootInstance(self::$instanceId));
  202. }
  203. /**
  204. * Test destroy instance
  205. */
  206. public function testDestroyInstance()
  207. {
  208. $this->assertTrue($this->infrastructure->destroyInstance(self::$instanceId));
  209. }
  210. }
  211. if (PHPUnit_MAIN_METHOD == 'Zend_Cloud_Infrastructure_Adapter_RackspaceTest::main') {
  212. Zend_Cloud_Infrastructure_Adapter_RackspaceTest::main();
  213. }