ElasticipTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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_Service_Amazon
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Test helper
  24. */
  25. require_once dirname(__FILE__) . '/../../../../TestHelper.php';
  26. require_once 'PHPUnit/Framework/TestCase.php';
  27. require_once 'Zend/Http/Client.php';
  28. require_once 'Zend/Http/Client/Adapter/Test.php';
  29. require_once 'Zend/Service/Amazon/Ec2/Elasticip.php';
  30. /**
  31. * Zend_Service_Amazon_Ec2_Elasticip test case.
  32. *
  33. * @category Zend
  34. * @package Zend_Service_Amazon
  35. * @subpackage UnitTests
  36. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. * @group Zend_Service
  39. * @group Zend_Service_Amazon
  40. * @group Zend_Service_Amazon_Ec2
  41. */
  42. class Zend_Service_Amazon_Ec2_ElasticipTest extends PHPUnit_Framework_TestCase
  43. {
  44. /**
  45. * @var Zend_Service_Amazon_Ec2_Elasticip
  46. */
  47. private $Zend_Service_Amazon_Ec2_Elasticip;
  48. /**
  49. * Prepares the environment before running a test.
  50. */
  51. protected function setUp()
  52. {
  53. parent::setUp();
  54. $this->Zend_Service_Amazon_Ec2_Elasticip = new Zend_Service_Amazon_Ec2_Elasticip('access_key', 'secret_access_key');
  55. $adapter = new Zend_Http_Client_Adapter_Test();
  56. $client = new Zend_Http_Client(null, array(
  57. 'adapter' => $adapter
  58. ));
  59. $this->adapter = $adapter;
  60. Zend_Service_Amazon_Ec2_Elasticip::setHttpClient($client);
  61. }
  62. /**
  63. * Cleans up the environment after running a test.
  64. */
  65. protected function tearDown()
  66. {
  67. unset($this->adapter);
  68. $this->Zend_Service_Amazon_Ec2_Elasticip = null;
  69. parent::tearDown();
  70. }
  71. public function testAllocateNewElasticIp()
  72. {
  73. $rawHttpResponse = "HTTP/1.1 200 OK\r\n"
  74. . "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  75. . "Server: hi\r\n"
  76. . "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  77. . "Status: 200 OK\r\n"
  78. . "Content-type: application/xml; charset=utf-8\r\n"
  79. . "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
  80. . "Connection: close\r\n"
  81. . "\r\n"
  82. . "<AllocateAddressResponse xmlns=\"http://ec2.amazonaws.com/doc/2009-04-04/\">\r\n"
  83. . " <publicIp>67.202.55.255</publicIp>\r\n"
  84. . "</AllocateAddressResponse>";
  85. $this->adapter->setResponse($rawHttpResponse);
  86. $ipAddress = $this->Zend_Service_Amazon_Ec2_Elasticip->allocate();
  87. $this->assertEquals('67.202.55.255', $ipAddress);
  88. }
  89. public function testAssociateElasticIpWithInstanceReturnsTrue()
  90. {
  91. $rawHttpResponse = "HTTP/1.1 200 OK\r\n"
  92. . "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  93. . "Server: hi\r\n"
  94. . "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  95. . "Status: 200 OK\r\n"
  96. . "Content-type: application/xml; charset=utf-8\r\n"
  97. . "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
  98. . "Connection: close\r\n"
  99. . "\r\n"
  100. . "<AssociateAddressResponse xmlns=\"http://ec2.amazonaws.com/doc/2009-04-04/\">\r\n"
  101. . " <return>true</return>\r\n"
  102. . "</AssociateAddressResponse>";
  103. $this->adapter->setResponse($rawHttpResponse);
  104. $return = $this->Zend_Service_Amazon_Ec2_Elasticip->associate('67.202.55.255', 'i-ag8ga0a');
  105. $this->assertTrue($return);
  106. }
  107. /**
  108. * Tests Zend_Service_Amazon_Ec2_Elasticip->describe()
  109. */
  110. public function testDescribeSingleElasticIp()
  111. {
  112. $rawHttpResponse = "HTTP/1.1 200 OK\r\n"
  113. . "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  114. . "Server: hi\r\n"
  115. . "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  116. . "Status: 200 OK\r\n"
  117. . "Content-type: application/xml; charset=utf-8\r\n"
  118. . "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
  119. . "Connection: close\r\n"
  120. . "\r\n"
  121. . "<DescribeAddressesResponse xmlns=\"http://ec2.amazonaws.com/doc/2009-04-04/\">\r\n"
  122. . " <addressSet>\r\n"
  123. . " <item>\r\n"
  124. . " <publicIp>67.202.55.255</publicIp>\r\n"
  125. . " <instanceId>i-ag8ga0a</instanceId>\r\n"
  126. . " </item>\r\n"
  127. . " </addressSet>\r\n"
  128. . "</DescribeAddressesResponse>";
  129. $this->adapter->setResponse($rawHttpResponse);
  130. $response = $this->Zend_Service_Amazon_Ec2_Elasticip->describe('67.202.55.255');
  131. $arrIp = array(
  132. 'publicIp' => '67.202.55.255',
  133. 'instanceId' => 'i-ag8ga0a'
  134. );
  135. $this->assertSame($arrIp, $response[0]);
  136. }
  137. public function testDescribeMultipleElasticIp()
  138. {
  139. $rawHttpResponse = "HTTP/1.1 200 OK\r\n"
  140. . "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  141. . "Server: hi\r\n"
  142. . "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  143. . "Status: 200 OK\r\n"
  144. . "Content-type: application/xml; charset=utf-8\r\n"
  145. . "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
  146. . "Connection: close\r\n"
  147. . "\r\n"
  148. . "<DescribeAddressesResponse xmlns=\"http://ec2.amazonaws.com/doc/2009-04-04/\">\r\n"
  149. . " <addressSet>\r\n"
  150. . " <item>\r\n"
  151. . " <publicIp>67.202.55.255</publicIp>\r\n"
  152. . " <instanceId>i-ag8ga0a</instanceId>\r\n"
  153. . " </item>\r\n"
  154. . " <item>\r\n"
  155. . " <publicIp>67.202.55.200</publicIp>\r\n"
  156. . " <instanceId>i-aauoi9g</instanceId>\r\n"
  157. . " </item>\r\n"
  158. . " </addressSet>\r\n"
  159. . "</DescribeAddressesResponse>";
  160. $this->adapter->setResponse($rawHttpResponse);
  161. $response = $this->Zend_Service_Amazon_Ec2_Elasticip->describe(array('67.202.55.255', '67.202.55.200'));
  162. $arrIps = array(
  163. array(
  164. 'publicIp' => '67.202.55.255',
  165. 'instanceId' => 'i-ag8ga0a'
  166. ),
  167. array(
  168. 'publicIp' => '67.202.55.200',
  169. 'instanceId' => 'i-aauoi9g'
  170. )
  171. );
  172. foreach($response as $k => $r) {
  173. $this->assertSame($arrIps[$k], $r);
  174. }
  175. }
  176. /**
  177. * Tests Zend_Service_Amazon_Ec2_Elasticip->disassocate()
  178. */
  179. public function testDisassocateElasticIpFromInstance()
  180. {
  181. $rawHttpResponse = "HTTP/1.1 200 OK\r\n"
  182. . "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  183. . "Server: hi\r\n"
  184. . "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  185. . "Status: 200 OK\r\n"
  186. . "Content-type: application/xml; charset=utf-8\r\n"
  187. . "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
  188. . "Connection: close\r\n"
  189. . "\r\n"
  190. . "<DisassociateAddressResponse xmlns=\"http://ec2.amazonaws.com/doc/2009-04-04/\">\r\n"
  191. . " <return>true</return>\r\n"
  192. . "</DisassociateAddressResponse>";
  193. $this->adapter->setResponse($rawHttpResponse);
  194. $return = $this->Zend_Service_Amazon_Ec2_Elasticip->disassocate('67.202.55.255');
  195. $this->assertTrue($return);
  196. }
  197. /**
  198. * Tests Zend_Service_Amazon_Ec2_Elasticip->release()
  199. */
  200. public function testReleaseElasticIp()
  201. {
  202. $rawHttpResponse = "HTTP/1.1 200 OK\r\n"
  203. . "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  204. . "Server: hi\r\n"
  205. . "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  206. . "Status: 200 OK\r\n"
  207. . "Content-type: application/xml; charset=utf-8\r\n"
  208. . "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
  209. . "Connection: close\r\n"
  210. . "\r\n"
  211. . "<ReleaseAddressResponse xmlns=\"http://ec2.amazonaws.com/doc/2009-04-04/\">\r\n"
  212. . " <return>true</return>\r\n"
  213. . "</ReleaseAddressResponse>";
  214. $this->adapter->setResponse($rawHttpResponse);
  215. $return = $this->Zend_Service_Amazon_Ec2_Elasticip->release('67.202.55.255');
  216. $this->assertTrue($return);
  217. }
  218. }