ElasticipTest.php 9.2 KB

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