datasource_specs.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. }));
  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. });