CloudWatchTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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/Service/Amazon/Ec2/CloudWatch.php';
  24. require_once 'Zend/Http/Client.php';
  25. require_once 'Zend/Http/Client/Adapter/Test.php';
  26. /**
  27. * Zend_Service_Amazon_Ec2_CloudWatch 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_CloudWatchTest extends PHPUnit_Framework_TestCase
  39. {
  40. /**
  41. * @var Zend_Service_Amazon_Ec2_CloudWatch
  42. */
  43. private $Zend_Service_Amazon_Ec2_CloudWatch;
  44. /**
  45. * Prepares the environment before running a test.
  46. */
  47. protected function setUp()
  48. {
  49. parent::setUp();
  50. $this->Zend_Service_Amazon_Ec2_CloudWatch = new Zend_Service_Amazon_Ec2_CloudWatch('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_CloudWatch::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_CloudWatch = null;
  65. parent::tearDown();
  66. }
  67. /**
  68. * Tests Zend_Service_Amazon_Ec2_CloudWatch->getMetricStatistics()
  69. */
  70. public function testGetMetricStatistics()
  71. {
  72. $rawHttpResponse = "HTTP/1.1 200 OK\r\n"
  73. . "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  74. . "Server: hi\r\n"
  75. . "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  76. . "Status: 200 OK\r\n"
  77. . "Content-type: application/xml; charset=utf-8\r\n"
  78. . "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
  79. . "Connection: close\r\n"
  80. . "\r\n"
  81. ."<GetMetricStatisticsResponse xmlns=\"http://monitoring.amazonaws.com/doc/2009-05-15/\">\r\n"
  82. ." <GetMetricStatisticsResult>\r\n"
  83. ." <Datapoints>\r\n"
  84. ." <member>\r\n"
  85. ." <Timestamp>2009-06-16T23:57:00Z</Timestamp>\r\n"
  86. ." <Unit>Bytes</Unit>\r\n"
  87. ." <Samples>1.0</Samples>\r\n"
  88. ." <Average>14838.0</Average>\r\n"
  89. ." </member>\r\n"
  90. ." <member>\r\n"
  91. ." <Timestamp>2009-06-17T00:16:00Z</Timestamp>\r\n"
  92. ." <Unit>Bytes</Unit>\r\n"
  93. ." <Samples>1.0</Samples>\r\n"
  94. ." <Average>18251.0</Average>\r\n"
  95. ." </member>\r\n"
  96. ." </Datapoints>\r\n"
  97. ." <Label>NetworkIn</Label>"
  98. ." </GetMetricStatisticsResult>\r\n"
  99. ."</GetMetricStatisticsResponse>\r\n";
  100. $this->adapter->setResponse($rawHttpResponse);
  101. $return = $this->Zend_Service_Amazon_Ec2_CloudWatch->getMetricStatistics(array('MeasureName' => 'NetworkIn', 'Statistics' => array('Average')));
  102. $arrReturn = array(
  103. 'label' => 'NetworkIn',
  104. 'datapoints' => array(
  105. array(
  106. 'Timestamp' => '2009-06-16T23:57:00Z',
  107. 'Unit' => 'Bytes',
  108. 'Samples' => '1.0',
  109. 'Average' => '14838.0',
  110. ),
  111. array(
  112. 'Timestamp' => '2009-06-17T00:16:00Z',
  113. 'Unit' => 'Bytes',
  114. 'Samples' => '1.0',
  115. 'Average' => '18251.0',
  116. )
  117. )
  118. );
  119. $this->assertSame($arrReturn, $return);
  120. }
  121. /**
  122. * Tests Zend_Service_Amazon_Ec2_CloudWatch->listMetrics()
  123. */
  124. public function testListMetrics()
  125. {
  126. $rawHttpResponse = "HTTP/1.1 200 OK\r\n"
  127. . "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  128. . "Server: hi\r\n"
  129. . "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  130. . "Status: 200 OK\r\n"
  131. . "Content-type: application/xml; charset=utf-8\r\n"
  132. . "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
  133. . "Connection: close\r\n"
  134. . "\r\n"
  135. ."<ListMetricsResponse xmlns=\"http://monitoring.amazonaws.com/doc/2009-05-15/\">\r\n"
  136. ." <ListMetricsResult>\r\n"
  137. ." <Metrics>\r\n"
  138. ." <member>\r\n"
  139. ." <Dimensions>\r\n"
  140. ." <member>\r\n"
  141. ." <Name>InstanceId</Name>\r\n"
  142. ." <Value>i-bec576d7</Value>\r\n"
  143. ." </member>\r\n"
  144. ." </Dimensions>\r\n"
  145. ." <MeasureName>NetworkIn</MeasureName>\r\n"
  146. ." <Namespace>AWS/EC2</Namespace>\r\n"
  147. ." </member>\r\n"
  148. ." <member>\r\n"
  149. ." <Dimensions>\r\n"
  150. ." <member>\r\n"
  151. ." <Name>InstanceId</Name>\r\n"
  152. ." <Value>i-bec576d7</Value>\r\n"
  153. ." </member>\r\n"
  154. ." </Dimensions>\r\n"
  155. ." <MeasureName>CPUUtilization</MeasureName>\r\n"
  156. ." <Namespace>AWS/EC2</Namespace>\r\n"
  157. ." </member>\r\n"
  158. ." <member>\r\n"
  159. ." <Dimensions/>\r\n"
  160. ." <MeasureName>NetworkIn</MeasureName>\r\n"
  161. ." <Namespace>AWS/EC2</Namespace>\r\n"
  162. ." </member>\r\n"
  163. ." </Metrics>\r\n"
  164. ." </ListMetricsResult>\r\n"
  165. ."</ListMetricsResponse>\r\n";
  166. $this->adapter->setResponse($rawHttpResponse);
  167. $return = $this->Zend_Service_Amazon_Ec2_CloudWatch->listMetrics();
  168. $arrReturn = array(
  169. array(
  170. 'MeasureName' => 'NetworkIn',
  171. 'Namespace' => 'AWS/EC2',
  172. 'Deminsions' => array(
  173. 'name' => 'InstanceId',
  174. 'value' => 'i-bec576d7'
  175. )
  176. ),
  177. array(
  178. 'MeasureName' => 'CPUUtilization',
  179. 'Namespace' => 'AWS/EC2',
  180. 'Deminsions' => array(
  181. 'name' => 'InstanceId',
  182. 'value' => 'i-bec576d7'
  183. )
  184. ),
  185. array(
  186. 'MeasureName' => 'NetworkIn',
  187. 'Namespace' => 'AWS/EC2',
  188. 'Deminsions' => array()
  189. )
  190. );
  191. $this->assertSame($arrReturn, $return);
  192. }
  193. }