ElasticipTest.php 9.2 KB

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