datasource_specs.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 return series list', function(done) {
  76. ctx.ds.query(query).then(function(result) {
  77. expect(result.data[0].target).to.be('CPUUtilization_Average');
  78. expect(result.data[0].datapoints[0][0]).to.be(response.Datapoints[0]['Average']);
  79. done();
  80. });
  81. ctx.$rootScope.$apply();
  82. });
  83. it('should return null for missing data point', function(done) {
  84. ctx.ds.query(query).then(function(result) {
  85. expect(result.data[0].datapoints[2][0]).to.be(null);
  86. done();
  87. });
  88. ctx.$rootScope.$apply();
  89. });
  90. it('should generate the correct targets by expanding template variables', function() {
  91. var templateSrv = {
  92. variables: [
  93. {
  94. name: 'instance_id',
  95. options: [
  96. { value: 'i-23456789', selected: false },
  97. { value: 'i-34567890', selected: true }
  98. ]
  99. }
  100. ],
  101. variableExists: function (e) { return true; },
  102. containsVariable: function (str, variableName) { return str.indexOf('$' + variableName) !== -1; }
  103. };
  104. var targets = [
  105. {
  106. region: 'us-east-1',
  107. namespace: 'AWS/EC2',
  108. metricName: 'CPUUtilization',
  109. dimensions: {
  110. InstanceId: '$instance_id'
  111. },
  112. statistics: ['Average'],
  113. period: 300
  114. }
  115. ];
  116. var result = ctx.ds.expandTemplateVariable(targets, templateSrv);
  117. expect(result[0].dimensions.InstanceId).to.be('i-34567890');
  118. });
  119. });
  120. function describeMetricFindQuery(query, func) {
  121. describe('metricFindQuery ' + query, () => {
  122. let scenario: any = {};
  123. scenario.setup = setupCallback => {
  124. beforeEach(() => {
  125. setupCallback();
  126. ctx.backendSrv.datasourceRequest = args => {
  127. scenario.request = args;
  128. return ctx.$q.when({data: scenario.requestResponse });
  129. };
  130. ctx.ds.metricFindQuery(query).then(args => {
  131. scenario.result = args;
  132. });
  133. ctx.$rootScope.$apply();
  134. });
  135. };
  136. func(scenario);
  137. });
  138. }
  139. describeMetricFindQuery('regions()', scenario => {
  140. scenario.setup(() => {
  141. scenario.requestResponse = [{text: 'us-east-1'}];
  142. });
  143. it('should call __GetRegions and return result', () => {
  144. expect(scenario.result[0].text).to.contain('us-east-1');
  145. expect(scenario.request.data.action).to.be('__GetRegions');
  146. });
  147. });
  148. describeMetricFindQuery('namespaces()', scenario => {
  149. scenario.setup(() => {
  150. scenario.requestResponse = [{text: 'AWS/EC2'}];
  151. });
  152. it('should call __GetNamespaces and return result', () => {
  153. expect(scenario.result[0].text).to.contain('AWS/EC2');
  154. expect(scenario.request.data.action).to.be('__GetNamespaces');
  155. });
  156. });
  157. describeMetricFindQuery('metrics(AWS/EC2)', scenario => {
  158. scenario.setup(() => {
  159. scenario.requestResponse = [{text: 'CPUUtilization'}];
  160. });
  161. it('should call __GetMetrics and return result', () => {
  162. expect(scenario.result[0].text).to.be('CPUUtilization');
  163. expect(scenario.request.data.action).to.be('__GetMetrics');
  164. });
  165. });
  166. describeMetricFindQuery('dimension_keys(AWS/EC2)', scenario => {
  167. scenario.setup(() => {
  168. scenario.requestResponse = [{text: 'InstanceId'}];
  169. });
  170. it('should call __GetDimensions and return result', () => {
  171. expect(scenario.result[0].text).to.be('InstanceId');
  172. expect(scenario.request.data.action).to.be('__GetDimensions');
  173. });
  174. });
  175. describeMetricFindQuery('dimension_values(us-east-1,AWS/EC2,CPUUtilization,InstanceId)', scenario => {
  176. scenario.setup(() => {
  177. scenario.requestResponse = {
  178. Metrics: [
  179. {
  180. Namespace: 'AWS/EC2',
  181. MetricName: 'CPUUtilization',
  182. Dimensions: [
  183. {
  184. Name: 'InstanceId',
  185. Value: 'i-12345678'
  186. }
  187. ]
  188. }
  189. ]
  190. };
  191. });
  192. it('should call __ListMetrics and return result', () => {
  193. expect(scenario.result[0].text).to.be('i-12345678');
  194. expect(scenario.request.data.action).to.be('ListMetrics');
  195. });
  196. });
  197. });