datasource_specs.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. ///<amd-dependency path="app/plugins/datasource/cloudwatch/datasource" />
  2. ///<amd-dependency path="test/specs/helpers" name="helpers" />
  3. import {describe, beforeEach, it, sinon, expect, angularMocks} from 'test/lib/common';
  4. import moment = require('moment');
  5. declare var helpers: any;
  6. describe('CloudWatchDatasource', function() {
  7. var ctx = new helpers.ServiceTestContext();
  8. beforeEach(angularMocks.module('grafana.core'));
  9. beforeEach(angularMocks.module('grafana.services'));
  10. beforeEach(angularMocks.module('grafana.controllers'));
  11. beforeEach(ctx.providePhase(['templateSrv', 'backendSrv']));
  12. beforeEach(ctx.createService('CloudWatchDatasource'));
  13. beforeEach(function() {
  14. ctx.ds = new ctx.service({
  15. jsonData: {
  16. defaultRegion: 'us-east-1',
  17. access: 'proxy'
  18. }
  19. });
  20. });
  21. describe('When performing CloudWatch query', function() {
  22. var requestParams;
  23. var query = {
  24. range: { from: 'now-1h', to: 'now' },
  25. targets: [
  26. {
  27. region: 'us-east-1',
  28. namespace: 'AWS/EC2',
  29. metricName: 'CPUUtilization',
  30. dimensions: {
  31. InstanceId: 'i-12345678'
  32. },
  33. statistics: ['Average'],
  34. period: 300
  35. }
  36. ]
  37. };
  38. var response = {
  39. Datapoints: [
  40. {
  41. Average: 1,
  42. Timestamp: 'Wed Dec 31 1969 16:00:00 GMT-0800 (PST)'
  43. },
  44. {
  45. Average: 2,
  46. Timestamp: 'Wed Dec 31 1969 16:05:00 GMT-0800 (PST)'
  47. },
  48. {
  49. Average: 5,
  50. Timestamp: 'Wed Dec 31 1969 16:15:00 GMT-0800 (PST)'
  51. }
  52. ],
  53. Label: 'CPUUtilization'
  54. };
  55. beforeEach(function() {
  56. ctx.backendSrv.datasourceRequest = function(params) {
  57. requestParams = params;
  58. return ctx.$q.when({data: response});
  59. };
  60. });
  61. it('should generate the correct query', function(done) {
  62. ctx.ds.query(query).then(function() {
  63. var params = requestParams.data.parameters;
  64. expect(params.namespace).to.be(query.targets[0].namespace);
  65. expect(params.metricName).to.be(query.targets[0].metricName);
  66. expect(params.dimensions[0].Name).to.be(Object.keys(query.targets[0].dimensions)[0]);
  67. expect(params.dimensions[0].Value).to.be(query.targets[0].dimensions[Object.keys(query.targets[0].dimensions)[0]]);
  68. expect(params.statistics).to.eql(query.targets[0].statistics);
  69. expect(params.period).to.be(query.targets[0].period);
  70. done();
  71. });
  72. ctx.$rootScope.$apply();
  73. });
  74. it('should return series list', function(done) {
  75. ctx.ds.query(query).then(function(result) {
  76. expect(result.data[0].target).to.be('CPUUtilization_Average');
  77. expect(result.data[0].datapoints[0][0]).to.be(response.Datapoints[0]['Average']);
  78. done();
  79. });
  80. ctx.$rootScope.$apply();
  81. });
  82. it('should return null for missing data point', function(done) {
  83. ctx.ds.query(query).then(function(result) {
  84. expect(result.data[0].datapoints[2][0]).to.be(null);
  85. done();
  86. });
  87. ctx.$rootScope.$apply();
  88. });
  89. });
  90. function describeMetricFindQuery(query, func) {
  91. describe('metricFindQuery ' + query, () => {
  92. let scenario: any = {};
  93. scenario.setup = setupCallback => {
  94. beforeEach(() => {
  95. setupCallback();
  96. ctx.backendSrv.datasourceRequest = args => {
  97. scenario.request = args;
  98. return ctx.$q.when({data: scenario.requestResponse });
  99. };
  100. ctx.ds.metricFindQuery(query).then(args => {
  101. scenario.result = args;
  102. });
  103. ctx.$rootScope.$apply();
  104. });
  105. };
  106. func(scenario);
  107. });
  108. }
  109. describeMetricFindQuery('regions()', scenario => {
  110. scenario.setup(() => {
  111. scenario.requestResponse = [{text: 'us-east-1'}];
  112. });
  113. it('should call __GetRegions and return result', () => {
  114. expect(scenario.result[0].text).to.contain('us-east-1');
  115. expect(scenario.request.data.action).to.be('__GetRegions');
  116. });
  117. });
  118. describeMetricFindQuery('namespaces()', scenario => {
  119. scenario.setup(() => {
  120. scenario.requestResponse = [{text: 'AWS/EC2'}];
  121. });
  122. it('should call __GetNamespaces and return result', () => {
  123. expect(scenario.result[0].text).to.contain('AWS/EC2');
  124. expect(scenario.request.data.action).to.be('__GetNamespaces');
  125. });
  126. });
  127. describeMetricFindQuery('metrics(AWS/EC2)', scenario => {
  128. scenario.setup(() => {
  129. scenario.requestResponse = [{text: 'CPUUtilization'}];
  130. });
  131. it('should call __GetMetrics and return result', () => {
  132. expect(scenario.result[0].text).to.be('CPUUtilization');
  133. expect(scenario.request.data.action).to.be('__GetMetrics');
  134. });
  135. });
  136. describeMetricFindQuery('dimension_keys(AWS/EC2)', scenario => {
  137. scenario.setup(() => {
  138. scenario.requestResponse = [{text: 'InstanceId'}];
  139. });
  140. it('should call __GetDimensions and return result', () => {
  141. expect(scenario.result[0].text).to.be('InstanceId');
  142. expect(scenario.request.data.action).to.be('__GetDimensions');
  143. });
  144. });
  145. describeMetricFindQuery('dimension_values(us-east-1,AWS/EC2,CPUUtilization,InstanceId)', scenario => {
  146. scenario.setup(() => {
  147. scenario.requestResponse = {
  148. Metrics: [
  149. {
  150. Namespace: 'AWS/EC2',
  151. MetricName: 'CPUUtilization',
  152. Dimensions: [
  153. {
  154. Name: 'InstanceId',
  155. Value: 'i-12345678'
  156. }
  157. ]
  158. }
  159. ]
  160. };
  161. });
  162. it('should call __ListMetrics and return result', () => {
  163. expect(scenario.result[0].text).to.be('i-12345678');
  164. expect(scenario.request.data.action).to.be('ListMetrics');
  165. });
  166. });
  167. describe('When performing annotationQuery', function() {
  168. var annotation = {
  169. region: 'us-east-1',
  170. namespace: 'AWS/EC2',
  171. metricName: 'CPUUtilization',
  172. dimensions: 'InstanceId=i-12345678'
  173. };
  174. var alarmResponse = {
  175. MetricAlarms: [
  176. {
  177. AlarmName: 'test_alarm_name'
  178. }
  179. ]
  180. };
  181. var historyResponse = {
  182. AlarmHistoryItems: [
  183. {
  184. Timestamp: '2015-01-01T00:00:00.000Z',
  185. HistoryItemType: 'StateUpdate',
  186. AlarmName: 'test_alarm_name',
  187. HistoryData: '{}',
  188. HistorySummary: 'test_history_summary'
  189. }
  190. ]
  191. };
  192. beforeEach(function() {
  193. ctx.backendSrv.datasourceRequest = function(params) {
  194. switch (params.data.action) {
  195. case 'DescribeAlarmsForMetric':
  196. return ctx.$q.when({data: alarmResponse});
  197. break;
  198. case 'DescribeAlarmHistory':
  199. return ctx.$q.when({data: historyResponse});
  200. break;
  201. }
  202. };
  203. });
  204. it('should return annotation list', function(done) {
  205. ctx.ds.annotationQuery(annotation, {from: moment(1443438674760), to: moment(1443460274760)}).then(function(result) {
  206. expect(result[0].title).to.be('test_alarm_name');
  207. expect(result[0].text).to.be('test_history_summary');
  208. done();
  209. });
  210. ctx.$rootScope.$apply();
  211. });
  212. });
  213. });