datasource_specs.ts 7.5 KB

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