2
0

CloudWatchTest.php 8.1 KB

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