datasource_specs.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. });
  91. function describeMetricFindQuery(query, func) {
  92. describe('metricFindQuery ' + query, () => {
  93. let scenario: any = {};
  94. scenario.setup = setupCallback => {
  95. beforeEach(() => {
  96. setupCallback();
  97. ctx.backendSrv.datasourceRequest = args => {
  98. scenario.request = args;
  99. return ctx.$q.when({data: scenario.requestResponse });
  100. };
  101. ctx.ds.metricFindQuery(query).then(args => {
  102. scenario.result = args;
  103. });
  104. ctx.$rootScope.$apply();
  105. });
  106. };
  107. func(scenario);
  108. });
  109. }
  110. describeMetricFindQuery('regions()', scenario => {
  111. scenario.setup(() => {
  112. scenario.requestResponse = [{text: 'us-east-1'}];
  113. });
  114. it('should call __GetRegions and return result', () => {
  115. expect(scenario.result[0].text).to.contain('us-east-1');
  116. expect(scenario.request.data.action).to.be('__GetRegions');
  117. });
  118. });
  119. describeMetricFindQuery('namespaces()', scenario => {
  120. scenario.setup(() => {
  121. scenario.requestResponse = [{text: 'AWS/EC2'}];
  122. });
  123. it('should call __GetNamespaces and return result', () => {
  124. expect(scenario.result[0].text).to.contain('AWS/EC2');
  125. expect(scenario.request.data.action).to.be('__GetNamespaces');
  126. });
  127. });
  128. describeMetricFindQuery('metrics(AWS/EC2)', scenario => {
  129. scenario.setup(() => {
  130. scenario.requestResponse = [{text: 'CPUUtilization'}];
  131. });
  132. it('should call __GetMetrics and return result', () => {
  133. expect(scenario.result[0].text).to.be('CPUUtilization');
  134. expect(scenario.request.data.action).to.be('__GetMetrics');
  135. });
  136. });
  137. describeMetricFindQuery('dimension_keys(AWS/EC2)', scenario => {
  138. scenario.setup(() => {
  139. scenario.requestResponse = [{text: 'InstanceId'}];
  140. });
  141. it('should call __GetDimensions and return result', () => {
  142. expect(scenario.result[0].text).to.be('InstanceId');
  143. expect(scenario.request.data.action).to.be('__GetDimensions');
  144. });
  145. });
  146. describeMetricFindQuery('dimension_values(us-east-1,AWS/EC2,CPUUtilization,InstanceId)', scenario => {
  147. scenario.setup(() => {
  148. scenario.requestResponse = {
  149. Metrics: [
  150. {
  151. Namespace: 'AWS/EC2',
  152. MetricName: 'CPUUtilization',
  153. Dimensions: [
  154. {
  155. Name: 'InstanceId',
  156. Value: 'i-12345678'
  157. }
  158. ]
  159. }
  160. ]
  161. };
  162. });
  163. it('should call __ListMetrics and return result', () => {
  164. expect(scenario.result[0].text).to.be('i-12345678');
  165. expect(scenario.request.data.action).to.be('ListMetrics');
  166. });
  167. });
  168. });