Ec2Test.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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.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. $response = file_get_contents($name);
  82. // Line endings are sometimes an issue inside the canned responses; the
  83. // following is a negative lookbehind assertion, and replaces any \n
  84. // not preceded by \r with the sequence \r\n, ensuring that the message
  85. // is well-formed.
  86. return preg_replace("#(?<!\r)\n#", "\r\n", $response);
  87. }
  88. /**
  89. * Get Config Array
  90. *
  91. * @return array
  92. */
  93. static function getConfigArray()
  94. {
  95. return array(
  96. Zend_Cloud_Infrastructure_Factory::INFRASTRUCTURE_ADAPTER_KEY => 'Zend_Cloud_Infrastructure_Adapter_Ec2',
  97. Zend_Cloud_Infrastructure_Adapter_Ec2::AWS_ACCESS_KEY => 'foo',
  98. Zend_Cloud_Infrastructure_Adapter_Ec2::AWS_SECRET_KEY => 'bar',
  99. Zend_Cloud_Infrastructure_Adapter_Ec2::AWS_REGION => 'us-east-1',
  100. Zend_Cloud_Infrastructure_Adapter_Ec2::AWS_SECURITY_GROUP => 'default'
  101. );
  102. }
  103. /**
  104. * Test all the constants of the class
  105. */
  106. public function testConstants()
  107. {
  108. $this->assertEquals('aws_accesskey', Zend_Cloud_Infrastructure_Adapter_Ec2::AWS_ACCESS_KEY);
  109. $this->assertEquals('aws_secretkey', Zend_Cloud_Infrastructure_Adapter_Ec2::AWS_SECRET_KEY);
  110. $this->assertEquals('aws_region', Zend_Cloud_Infrastructure_Adapter_Ec2::AWS_REGION);
  111. }
  112. /**
  113. * Test construct with missing params
  114. */
  115. public function testConstructExceptionMissingParams()
  116. {
  117. $this->setExpectedException(
  118. 'Zend_Cloud_Infrastructure_Exception',
  119. 'Invalid options provided'
  120. );
  121. $image = new Zend_Cloud_Infrastructure_Adapter_Ec2('foo');
  122. }
  123. /**
  124. * Test getAdapter
  125. */
  126. public function testGetAdapter()
  127. {
  128. $this->assertTrue(
  129. $this->infrastructure->getAdapter() instanceof Zend_Service_Amazon_Ec2_Instance
  130. );
  131. }
  132. /**
  133. * Test create an instance
  134. */
  135. public function testCreateInstance()
  136. {
  137. $options = array (
  138. Zend_Cloud_Infrastructure_Instance::INSTANCE_IMAGEID => 'ami-7f418316',
  139. Zend_Cloud_Infrastructure_Adapter_Ec2::AWS_SECURITY_GROUP => array('default')
  140. );
  141. $instance = $this->infrastructure->createInstance('test', $options);
  142. self::$instanceId= $instance->getId();
  143. $this->assertEquals('ami-7f418316', $instance->getImageId());
  144. }
  145. /**
  146. * Test list of an instance
  147. */
  148. public function testListInstance()
  149. {
  150. $instances = $this->infrastructure->listInstances(self::$instanceId);
  151. $found = false;
  152. foreach ($instances as $instance) {
  153. if ($instance->getId()==self::$instanceId) {
  154. $found = true;
  155. break;
  156. }
  157. }
  158. $this->assertTrue($found);
  159. }
  160. /**
  161. * Test images instance
  162. */
  163. public function testImagesInstance()
  164. {
  165. $images = $this->infrastructure->imagesInstance();
  166. $this->assertTrue(!empty($images));
  167. }
  168. /**
  169. * Test zones instance
  170. */
  171. public function testZonesInstance()
  172. {
  173. $zones = $this->infrastructure->zonesInstance();
  174. $this->assertTrue(!empty($zones));
  175. }
  176. /**
  177. * Test monitor instance
  178. */
  179. public function testMonitorInstance()
  180. {
  181. $monitor = $this->infrastructure->monitorInstance(self::$instanceId,Zend_Cloud_Infrastructure_Instance::MONITOR_CPU);
  182. $adapterResult = $this->infrastructure->getAdapterResult();
  183. $this->assertTrue(!empty($adapterResult['label']));
  184. }
  185. /**
  186. * Test deploy instance
  187. */
  188. public function testDeployInstance()
  189. {
  190. $this->markTestSkipped('Test deploy instance skipped');
  191. }
  192. /**
  193. * Test stop an instance
  194. */
  195. public function testStopInstance()
  196. {
  197. $this->markTestSkipped('Test stop instance skipped');
  198. }
  199. /**
  200. * Test start an instance
  201. */
  202. public function testStartInstance()
  203. {
  204. $this->markTestSkipped('Test start instance skipped');
  205. }
  206. /**
  207. * Test reboot and instance
  208. */
  209. public function testRebootInstance()
  210. {
  211. $this->assertTrue($this->infrastructure->rebootInstance(self::$instanceId));
  212. }
  213. /**
  214. * Test destroy instance
  215. */
  216. public function testDestroyInstance()
  217. {
  218. $this->assertTrue($this->infrastructure->destroyInstance(self::$instanceId));
  219. }
  220. }
  221. if (PHPUnit_MAIN_METHOD == 'Zend_Cloud_Infrastructure_Adapter_Ec2Test::main') {
  222. Zend_Cloud_Infrastructure_Adapter_Ec2Test::main();
  223. }