Ec2Test.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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.php';
  22. require_once 'Zend/Http/Client/Adapter/Test.php';
  23. require_once 'Zend/Cloud/Infrastructure/Adapter/Ec2.php';
  24. require_once 'Zend/Cloud/Infrastructure/Factory.php';
  25. class Zend_Cloud_Infrastructure_Adapter_Ec2Test extends PHPUnit_Framework_TestCase
  26. {
  27. /**
  28. * Timeout in seconds for status change
  29. */
  30. const STATUS_TIMEOUT= 120;
  31. /**
  32. * Reference to Infrastructure object
  33. *
  34. * @var Zend_Cloud_Infrastructure_Adapter
  35. */
  36. protected $infrastructure;
  37. /**
  38. * Socket based HTTP client adapter
  39. *
  40. * @var Zend_Http_Client_Adapter_Test
  41. */
  42. protected $httpClientAdapterTest;
  43. /**
  44. * Image ID of the instance
  45. *
  46. * @var string
  47. */
  48. protected static $instanceId;
  49. /**
  50. * Setup for each test
  51. */
  52. public function setUp()
  53. {
  54. $this->infrastructure = Zend_Cloud_Infrastructure_Factory::getAdapter(array(
  55. Zend_Cloud_Infrastructure_Factory::INFRASTRUCTURE_ADAPTER_KEY => 'Zend_Cloud_Infrastructure_Adapter_Ec2',
  56. Zend_Cloud_Infrastructure_Adapter_Ec2::AWS_ACCESS_KEY => 'foo',
  57. Zend_Cloud_Infrastructure_Adapter_Ec2::AWS_SECRET_KEY => 'bar',
  58. Zend_Cloud_Infrastructure_Adapter_Ec2::AWS_REGION => 'us-east-1'
  59. ));
  60. $this->httpClientAdapterTest = new Zend_Http_Client_Adapter_Test();
  61. // load the HTTP response (from a file)
  62. $shortClassName = substr(__CLASS__,strlen('Zend_Cloud_Infrastructure_Adapter_'));
  63. $filename= dirname(__FILE__) . '/_files/' . $shortClassName . '_'. $this->getName().'.response';
  64. if (file_exists($filename)) {
  65. $this->httpClientAdapterTest->setResponse($this->loadResponse($filename));
  66. }
  67. $adapter= $this->infrastructure->getAdapter();
  68. $client = new Zend_Http_Client(null, array(
  69. 'adapter' => $this->httpClientAdapterTest
  70. ));
  71. call_user_func(array($adapter,'setHttpClient'),$client);
  72. }
  73. /**
  74. * Utility method for returning a string HTTP response, which is loaded from a file
  75. *
  76. * @param string $name
  77. * @return string
  78. */
  79. protected function loadResponse($name)
  80. {
  81. return file_get_contents($name);
  82. }
  83. /**
  84. * Get Config Array
  85. *
  86. * @return array
  87. */
  88. static function getConfigArray()
  89. {
  90. return array(
  91. Zend_Cloud_Infrastructure_Factory::INFRASTRUCTURE_ADAPTER_KEY => 'Zend_Cloud_Infrastructure_Adapter_Ec2',
  92. Zend_Cloud_Infrastructure_Adapter_Ec2::AWS_ACCESS_KEY => 'foo',
  93. Zend_Cloud_Infrastructure_Adapter_Ec2::AWS_SECRET_KEY => 'bar',
  94. Zend_Cloud_Infrastructure_Adapter_Ec2::AWS_REGION => 'us-east-1',
  95. Zend_Cloud_Infrastructure_Adapter_Ec2::AWS_SECURITY_GROUP => 'default'
  96. );
  97. }
  98. /**
  99. * Test all the constants of the class
  100. */
  101. public function testConstants()
  102. {
  103. $this->assertEquals('aws_accesskey', Zend_Cloud_Infrastructure_Adapter_Ec2::AWS_ACCESS_KEY);
  104. $this->assertEquals('aws_secretkey', Zend_Cloud_Infrastructure_Adapter_Ec2::AWS_SECRET_KEY);
  105. $this->assertEquals('aws_region', Zend_Cloud_Infrastructure_Adapter_Ec2::AWS_REGION);
  106. }
  107. /**
  108. * Test construct with missing params
  109. */
  110. public function testConstructExceptionMissingParams()
  111. {
  112. $this->setExpectedException(
  113. 'Zend_Cloud_Infrastructure_Exception',
  114. 'Invalid options provided'
  115. );
  116. $image = new Zend_Cloud_Infrastructure_Adapter_Ec2('foo');
  117. }
  118. /**
  119. * Test getAdapter
  120. */
  121. public function testGetAdapter()
  122. {
  123. $this->assertTrue(
  124. $this->infrastructure->getAdapter() instanceof Zend_Service_Amazon_Ec2_Instance
  125. );
  126. }
  127. /**
  128. * Test create an instance
  129. */
  130. public function testCreateInstance()
  131. {
  132. $options = array (
  133. Zend_Cloud_Infrastructure_Instance::INSTANCE_IMAGEID => 'ami-7f418316',
  134. Zend_Cloud_Infrastructure_Adapter_Ec2::AWS_SECURITY_GROUP => array('default')
  135. );
  136. $instance = $this->infrastructure->createInstance('test', $options);
  137. self::$instanceId= $instance->getId();
  138. $this->assertEquals('ami-7f418316', $instance->getImageId());
  139. }
  140. /**
  141. * Test list of an instance
  142. */
  143. public function testListInstance()
  144. {
  145. $instances = $this->infrastructure->listInstances(self::$instanceId);
  146. $found = false;
  147. foreach ($instances as $instance) {
  148. if ($instance->getId()==self::$instanceId) {
  149. $found = true;
  150. break;
  151. }
  152. }
  153. $this->assertTrue($found);
  154. }
  155. /**
  156. * Test images instance
  157. */
  158. public function testImagesInstance()
  159. {
  160. $images = $this->infrastructure->imagesInstance();
  161. $this->assertTrue(!empty($images));
  162. }
  163. /**
  164. * Test zones instance
  165. */
  166. public function testZonesInstance()
  167. {
  168. $zones = $this->infrastructure->zonesInstance();
  169. $this->assertTrue(!empty($zones));
  170. }
  171. /**
  172. * Test monitor instance
  173. */
  174. public function testMonitorInstance()
  175. {
  176. $monitor = $this->infrastructure->monitorInstance(self::$instanceId,Zend_Cloud_Infrastructure_Instance::MONITOR_CPU);
  177. $adapterResult = $this->infrastructure->getAdapterResult();
  178. $this->assertTrue(!empty($adapterResult['label']));
  179. }
  180. /**
  181. * Test deploy instance
  182. */
  183. public function testDeployInstance()
  184. {
  185. $this->markTestSkipped('Test deploy instance skipped');
  186. }
  187. /**
  188. * Test stop an instance
  189. */
  190. public function testStopInstance()
  191. {
  192. $this->markTestSkipped('Test stop instance skipped');
  193. }
  194. /**
  195. * Test start an instance
  196. */
  197. public function testStartInstance()
  198. {
  199. $this->markTestSkipped('Test start instance skipped');
  200. }
  201. /**
  202. * Test reboot and instance
  203. */
  204. public function testRebootInstance()
  205. {
  206. $this->assertTrue($this->infrastructure->rebootInstance(self::$instanceId));
  207. }
  208. /**
  209. * Test destroy instance
  210. */
  211. public function testDestroyInstance()
  212. {
  213. $this->assertTrue($this->infrastructure->destroyInstance(self::$instanceId));
  214. }
  215. }
  216. if (PHPUnit_MAIN_METHOD == 'Zend_Cloud_Infrastructure_Adapter_Ec2Test::main') {
  217. Zend_Cloud_Infrastructure_Adapter_Ec2Test::main();
  218. }