CloudWatchTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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/Service/Amazon/Ec2/CloudWatch.php';
  23. require_once 'Zend/Http/Client.php';
  24. require_once 'Zend/Http/Client/Adapter/Test.php';
  25. /**
  26. * Zend_Service_Amazon_Ec2_CloudWatch 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_CloudWatchTest extends PHPUnit_Framework_TestCase
  38. {
  39. /**
  40. * @var Zend_Service_Amazon_Ec2_CloudWatch
  41. */
  42. private $Zend_Service_Amazon_Ec2_CloudWatch;
  43. /**
  44. * Prepares the environment before running a test.
  45. */
  46. protected function setUp()
  47. {
  48. parent::setUp();
  49. $this->Zend_Service_Amazon_Ec2_CloudWatch = new Zend_Service_Amazon_Ec2_CloudWatch('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_CloudWatch::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_CloudWatch = null;
  64. parent::tearDown();
  65. }
  66. /**
  67. * Tests Zend_Service_Amazon_Ec2_CloudWatch->getMetricStatistics()
  68. */
  69. public function testGetMetricStatistics()
  70. {
  71. $rawHttpResponse = "HTTP/1.1 200 OK\r\n"
  72. . "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  73. . "Server: hi\r\n"
  74. . "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  75. . "Status: 200 OK\r\n"
  76. . "Content-type: application/xml; charset=utf-8\r\n"
  77. . "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
  78. . "Connection: close\r\n"
  79. . "\r\n"
  80. ."<GetMetricStatisticsResponse xmlns=\"http://monitoring.amazonaws.com/doc/2009-05-15/\">\r\n"
  81. ." <GetMetricStatisticsResult>\r\n"
  82. ." <Datapoints>\r\n"
  83. ." <member>\r\n"
  84. ." <Timestamp>2009-06-16T23:57:00Z</Timestamp>\r\n"
  85. ." <Unit>Bytes</Unit>\r\n"
  86. ." <Samples>1.0</Samples>\r\n"
  87. ." <Average>14838.0</Average>\r\n"
  88. ." </member>\r\n"
  89. ." <member>\r\n"
  90. ." <Timestamp>2009-06-17T00:16:00Z</Timestamp>\r\n"
  91. ." <Unit>Bytes</Unit>\r\n"
  92. ." <Samples>1.0</Samples>\r\n"
  93. ." <Average>18251.0</Average>\r\n"
  94. ." </member>\r\n"
  95. ." </Datapoints>\r\n"
  96. ." <Label>NetworkIn</Label>"
  97. ." </GetMetricStatisticsResult>\r\n"
  98. ."</GetMetricStatisticsResponse>\r\n";
  99. $this->adapter->setResponse($rawHttpResponse);
  100. $return = $this->Zend_Service_Amazon_Ec2_CloudWatch->getMetricStatistics(array('MeasureName' => 'NetworkIn', 'Statistics' => array('Average')));
  101. $arrReturn = array(
  102. 'label' => 'NetworkIn',
  103. 'datapoints' => array(
  104. array(
  105. 'Timestamp' => '2009-06-16T23:57:00Z',
  106. 'Unit' => 'Bytes',
  107. 'Samples' => '1.0',
  108. 'Average' => '14838.0',
  109. ),
  110. array(
  111. 'Timestamp' => '2009-06-17T00:16:00Z',
  112. 'Unit' => 'Bytes',
  113. 'Samples' => '1.0',
  114. 'Average' => '18251.0',
  115. )
  116. )
  117. );
  118. $this->assertSame($arrReturn, $return);
  119. }
  120. /**
  121. * Tests Zend_Service_Amazon_Ec2_CloudWatch->listMetrics()
  122. */
  123. public function testListMetrics()
  124. {
  125. $rawHttpResponse = "HTTP/1.1 200 OK\r\n"
  126. . "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  127. . "Server: hi\r\n"
  128. . "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  129. . "Status: 200 OK\r\n"
  130. . "Content-type: application/xml; charset=utf-8\r\n"
  131. . "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
  132. . "Connection: close\r\n"
  133. . "\r\n"
  134. ."<ListMetricsResponse xmlns=\"http://monitoring.amazonaws.com/doc/2009-05-15/\">\r\n"
  135. ." <ListMetricsResult>\r\n"
  136. ." <Metrics>\r\n"
  137. ." <member>\r\n"
  138. ." <Dimensions>\r\n"
  139. ." <member>\r\n"
  140. ." <Name>InstanceId</Name>\r\n"
  141. ." <Value>i-bec576d7</Value>\r\n"
  142. ." </member>\r\n"
  143. ." </Dimensions>\r\n"
  144. ." <MeasureName>NetworkIn</MeasureName>\r\n"
  145. ." <Namespace>AWS/EC2</Namespace>\r\n"
  146. ." </member>\r\n"
  147. ." <member>\r\n"
  148. ." <Dimensions>\r\n"
  149. ." <member>\r\n"
  150. ." <Name>InstanceId</Name>\r\n"
  151. ." <Value>i-bec576d7</Value>\r\n"
  152. ." </member>\r\n"
  153. ." </Dimensions>\r\n"
  154. ." <MeasureName>CPUUtilization</MeasureName>\r\n"
  155. ." <Namespace>AWS/EC2</Namespace>\r\n"
  156. ." </member>\r\n"
  157. ." <member>\r\n"
  158. ." <Dimensions/>\r\n"
  159. ." <MeasureName>NetworkIn</MeasureName>\r\n"
  160. ." <Namespace>AWS/EC2</Namespace>\r\n"
  161. ." </member>\r\n"
  162. ." </Metrics>\r\n"
  163. ." </ListMetricsResult>\r\n"
  164. ."</ListMetricsResponse>\r\n";
  165. $this->adapter->setResponse($rawHttpResponse);
  166. $return = $this->Zend_Service_Amazon_Ec2_CloudWatch->listMetrics();
  167. $arrReturn = array(
  168. array(
  169. 'MeasureName' => 'NetworkIn',
  170. 'Namespace' => 'AWS/EC2',
  171. 'Deminsions' => array(
  172. 'name' => 'InstanceId',
  173. 'value' => 'i-bec576d7'
  174. )
  175. ),
  176. array(
  177. 'MeasureName' => 'CPUUtilization',
  178. 'Namespace' => 'AWS/EC2',
  179. 'Deminsions' => array(
  180. 'name' => 'InstanceId',
  181. 'value' => 'i-bec576d7'
  182. )
  183. ),
  184. array(
  185. 'MeasureName' => 'NetworkIn',
  186. 'Namespace' => 'AWS/EC2',
  187. 'Deminsions' => array()
  188. )
  189. );
  190. $this->assertSame($arrReturn, $return);
  191. }
  192. public function testZF8149()
  193. {
  194. $rawHttpResponse = "HTTP/1.1 200 OK\r\n"
  195. . "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  196. . "Server: hi\r\n"
  197. . "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  198. . "Status: 200 OK\r\n"
  199. . "Content-type: application/xml; charset=utf-8\r\n"
  200. . "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
  201. . "Connection: close\r\n"
  202. . "\r\n"
  203. ."<GetMetricStatisticsResponse xmlns=\"http://monitoring.amazonaws.com/doc/2009-05-15/\">\r\n"
  204. ." <GetMetricStatisticsResult>\r\n"
  205. ." <Datapoints>\r\n"
  206. ." <member>\r\n"
  207. ." <Timestamp>2009-11-19T21:52:00Z</Timestamp>\r\n"
  208. ." <Unit>Percent</Unit>\r\n"
  209. ." <Samples>1.0</Samples>\r\n"
  210. ." <Average>0.09</Average>\r\n"
  211. ." </member>\r\n"
  212. ." <member>\r\n"
  213. ." <Timestamp>2009-11-19T21:55:00Z</Timestamp>\r\n"
  214. ." <Unit>Percent</Unit>\r\n"
  215. ." <Samples>1.0</Samples>\r\n"
  216. ." <Average>0.18</Average>\r\n"
  217. ." </member>\r\n"
  218. ." <member>\r\n"
  219. ." <Timestamp>2009-11-19T21:54:00Z</Timestamp>\r\n"
  220. ." <Unit>Percent</Unit>\r\n"
  221. ." <Samples>1.0</Samples>\r\n"
  222. ." <Average>0.09</Average>\r\n"
  223. ." </member>\r\n"
  224. ." <member>\r\n"
  225. ." <Timestamp>2009-11-19T21:51:00Z</Timestamp>\r\n"
  226. ." <Unit>Percent</Unit>\r\n"
  227. ." <Samples>1.0</Samples>\r\n"
  228. ." <Average>0.18</Average>\r\n"
  229. ." </member>\r\n"
  230. ." <member>\r\n"
  231. ." <Timestamp>2009-11-19T21:53:00Z</Timestamp>\r\n"
  232. ." <Unit>Percent</Unit>\r\n"
  233. ." <Samples>1.0</Samples>\r\n"
  234. ." <Average>0.09</Average>\r\n"
  235. ." </member>\r\n"
  236. ." </Datapoints>\r\n"
  237. ." <Label>CPUUtilization</Label>\r\n"
  238. ." </GetMetricStatisticsResult>\r\n"
  239. ." <ResponseMetadata>\r\n"
  240. ." <RequestId>6fb864fd-d557-11de-ac37-475775222f21</RequestId>\r\n"
  241. ." </ResponseMetadata>\r\n"
  242. ."</GetMetricStatisticsResponse>";
  243. $this->adapter->setResponse($rawHttpResponse);
  244. $return = $this->Zend_Service_Amazon_Ec2_CloudWatch->getMetricStatistics(
  245. array(
  246. 'MeasureName' => 'CPUUtilization',
  247. 'Statistics' => array('Average'),
  248. 'Dimensions'=> array('InstanceId'=>'i-93ba31fa'),
  249. 'StartTime'=> '2009-11-19T21:51:57+00:00',
  250. 'EndTime'=> '2009-11-19T21:56:57+00:00'
  251. )
  252. );
  253. $arrReturn = array (
  254. 'label' => 'CPUUtilization',
  255. 'datapoints' =>
  256. array (
  257. 0 =>
  258. array (
  259. 'Timestamp' => '2009-11-19T21:52:00Z',
  260. 'Unit' => 'Percent',
  261. 'Samples' => '1.0',
  262. 'Average' => '0.09',
  263. ),
  264. 1 =>
  265. array (
  266. 'Timestamp' => '2009-11-19T21:55:00Z',
  267. 'Unit' => 'Percent',
  268. 'Samples' => '1.0',
  269. 'Average' => '0.18',
  270. ),
  271. 2 =>
  272. array (
  273. 'Timestamp' => '2009-11-19T21:54:00Z',
  274. 'Unit' => 'Percent',
  275. 'Samples' => '1.0',
  276. 'Average' => '0.09',
  277. ),
  278. 3 =>
  279. array (
  280. 'Timestamp' => '2009-11-19T21:51:00Z',
  281. 'Unit' => 'Percent',
  282. 'Samples' => '1.0',
  283. 'Average' => '0.18',
  284. ),
  285. 4 =>
  286. array (
  287. 'Timestamp' => '2009-11-19T21:53:00Z',
  288. 'Unit' => 'Percent',
  289. 'Samples' => '1.0',
  290. 'Average' => '0.09',
  291. ),
  292. ),
  293. );
  294. $this->assertSame($arrReturn, $return);
  295. }
  296. }