2
0

Ec2Test.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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->assertType('Zend_Service_Amazon_Ec2_Instance',$this->infrastructure->getAdapter());
  124. }
  125. /**
  126. * Test create an instance
  127. */
  128. public function testCreateInstance()
  129. {
  130. $options = array (
  131. Zend_Cloud_Infrastructure_Instance::INSTANCE_IMAGEID => 'ami-7f418316',
  132. Zend_Cloud_Infrastructure_Adapter_Ec2::AWS_SECURITY_GROUP => array('default')
  133. );
  134. $instance = $this->infrastructure->createInstance('test', $options);
  135. self::$instanceId= $instance->getId();
  136. $this->assertEquals('ami-7f418316', $instance->getImageId());
  137. }
  138. /**
  139. * Test list of an instance
  140. */
  141. public function testListInstance()
  142. {
  143. $instances = $this->infrastructure->listInstances(self::$instanceId);
  144. $found = false;
  145. foreach ($instances as $instance) {
  146. if ($instance->getId()==self::$instanceId) {
  147. $found = true;
  148. break;
  149. }
  150. }
  151. $this->assertTrue($found);
  152. }
  153. /**
  154. * Test images instance
  155. */
  156. public function testImagesInstance()
  157. {
  158. $images = $this->infrastructure->imagesInstance();
  159. $this->assertTrue(!empty($images));
  160. }
  161. /**
  162. * Test zones instance
  163. */
  164. public function testZonesInstance()
  165. {
  166. $zones = $this->infrastructure->zonesInstance();
  167. $this->assertTrue(!empty($zones));
  168. }
  169. /**
  170. * Test monitor instance
  171. */
  172. public function testMonitorInstance()
  173. {
  174. $monitor = $this->infrastructure->monitorInstance(self::$instanceId,Zend_Cloud_Infrastructure_Instance::MONITOR_CPU);
  175. $adapterResult = $this->infrastructure->getAdapterResult();
  176. $this->assertTrue(!empty($adapterResult['label']));
  177. }
  178. /**
  179. * Test deploy instance
  180. */
  181. public function testDeployInstance()
  182. {
  183. $this->markTestSkipped('Test deploy instance skipped');
  184. }
  185. /**
  186. * Test stop an instance
  187. */
  188. public function testStopInstance()
  189. {
  190. $this->markTestSkipped('Test stop instance skipped');
  191. }
  192. /**
  193. * Test start an instance
  194. */
  195. public function testStartInstance()
  196. {
  197. $this->markTestSkipped('Test start instance skipped');
  198. }
  199. /**
  200. * Test reboot and instance
  201. */
  202. public function testRebootInstance()
  203. {
  204. $this->assertTrue($this->infrastructure->rebootInstance(self::$instanceId));
  205. }
  206. /**
  207. * Test destroy instance
  208. */
  209. public function testDestroyInstance()
  210. {
  211. $this->assertTrue($this->infrastructure->destroyInstance(self::$instanceId));
  212. }
  213. }
  214. if (PHPUnit_MAIN_METHOD == 'Zend_Cloud_Infrastructure_Adapter_Ec2Test::main') {
  215. Zend_Cloud_Infrastructure_Adapter_Ec2Test::main();
  216. }