2
0

ElasticipTest.php 8.2 KB

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