2
0

CloudWatchTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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-2010 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-2010 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. public function testZF8149()
  198. {
  199. $rawHttpResponse = "HTTP/1.1 200 OK\r\n"
  200. . "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  201. . "Server: hi\r\n"
  202. . "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
  203. . "Status: 200 OK\r\n"
  204. . "Content-type: application/xml; charset=utf-8\r\n"
  205. . "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
  206. . "Connection: close\r\n"
  207. . "\r\n"
  208. ."<GetMetricStatisticsResponse xmlns=\"http://monitoring.amazonaws.com/doc/2009-05-15/\">\r\n"
  209. ." <GetMetricStatisticsResult>\r\n"
  210. ." <Datapoints>\r\n"
  211. ." <member>\r\n"
  212. ." <Timestamp>2009-11-19T21:52:00Z</Timestamp>\r\n"
  213. ." <Unit>Percent</Unit>\r\n"
  214. ." <Samples>1.0</Samples>\r\n"
  215. ." <Average>0.09</Average>\r\n"
  216. ." </member>\r\n"
  217. ." <member>\r\n"
  218. ." <Timestamp>2009-11-19T21:55:00Z</Timestamp>\r\n"
  219. ." <Unit>Percent</Unit>\r\n"
  220. ." <Samples>1.0</Samples>\r\n"
  221. ." <Average>0.18</Average>\r\n"
  222. ." </member>\r\n"
  223. ." <member>\r\n"
  224. ." <Timestamp>2009-11-19T21:54:00Z</Timestamp>\r\n"
  225. ." <Unit>Percent</Unit>\r\n"
  226. ." <Samples>1.0</Samples>\r\n"
  227. ." <Average>0.09</Average>\r\n"
  228. ." </member>\r\n"
  229. ." <member>\r\n"
  230. ." <Timestamp>2009-11-19T21:51:00Z</Timestamp>\r\n"
  231. ." <Unit>Percent</Unit>\r\n"
  232. ." <Samples>1.0</Samples>\r\n"
  233. ." <Average>0.18</Average>\r\n"
  234. ." </member>\r\n"
  235. ." <member>\r\n"
  236. ." <Timestamp>2009-11-19T21:53:00Z</Timestamp>\r\n"
  237. ." <Unit>Percent</Unit>\r\n"
  238. ." <Samples>1.0</Samples>\r\n"
  239. ." <Average>0.09</Average>\r\n"
  240. ." </member>\r\n"
  241. ." </Datapoints>\r\n"
  242. ." <Label>CPUUtilization</Label>\r\n"
  243. ." </GetMetricStatisticsResult>\r\n"
  244. ." <ResponseMetadata>\r\n"
  245. ." <RequestId>6fb864fd-d557-11de-ac37-475775222f21</RequestId>\r\n"
  246. ." </ResponseMetadata>\r\n"
  247. ."</GetMetricStatisticsResponse>";
  248. $this->adapter->setResponse($rawHttpResponse);
  249. $return = $this->Zend_Service_Amazon_Ec2_CloudWatch->getMetricStatistics(
  250. array(
  251. 'MeasureName' => 'CPUUtilization',
  252. 'Statistics' => array('Average'),
  253. 'Dimensions'=> array('InstanceId'=>'i-93ba31fa'),
  254. 'StartTime'=> '2009-11-19T21:51:57+00:00',
  255. 'EndTime'=> '2009-11-19T21:56:57+00:00'
  256. )
  257. );
  258. $arrReturn = array (
  259. 'label' => 'CPUUtilization',
  260. 'datapoints' =>
  261. array (
  262. 0 =>
  263. array (
  264. 'Timestamp' => '2009-11-19T21:52:00Z',
  265. 'Unit' => 'Percent',
  266. 'Samples' => '1.0',
  267. 'Average' => '0.09',
  268. ),
  269. 1 =>
  270. array (
  271. 'Timestamp' => '2009-11-19T21:55:00Z',
  272. 'Unit' => 'Percent',
  273. 'Samples' => '1.0',
  274. 'Average' => '0.18',
  275. ),
  276. 2 =>
  277. array (
  278. 'Timestamp' => '2009-11-19T21:54:00Z',
  279. 'Unit' => 'Percent',
  280. 'Samples' => '1.0',
  281. 'Average' => '0.09',
  282. ),
  283. 3 =>
  284. array (
  285. 'Timestamp' => '2009-11-19T21:51:00Z',
  286. 'Unit' => 'Percent',
  287. 'Samples' => '1.0',
  288. 'Average' => '0.18',
  289. ),
  290. 4 =>
  291. array (
  292. 'Timestamp' => '2009-11-19T21:53:00Z',
  293. 'Unit' => 'Percent',
  294. 'Samples' => '1.0',
  295. 'Average' => '0.09',
  296. ),
  297. ),
  298. );
  299. $this->assertSame($arrReturn, $return);
  300. }
  301. }