datasource_specs.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. });
  117. function describeMetricFindQuery(query, func) {
  118. describe('metricFindQuery ' + query, () => {
  119. let scenario: any = {};
  120. scenario.setup = setupCallback => {
  121. beforeEach(() => {
  122. setupCallback();
  123. ctx.backendSrv.datasourceRequest = args => {
  124. scenario.request = args;
  125. return ctx.$q.when({data: scenario.requestResponse });
  126. };
  127. ctx.ds.metricFindQuery(query).then(args => {
  128. scenario.result = args;
  129. });
  130. ctx.$rootScope.$apply();
  131. });
  132. };
  133. func(scenario);
  134. });
  135. }
  136. describeMetricFindQuery('regions()', scenario => {
  137. scenario.setup(() => {
  138. scenario.requestResponse = [{text: 'us-east-1'}];
  139. });
  140. it('should call __GetRegions and return result', () => {
  141. expect(scenario.result[0].text).to.contain('us-east-1');
  142. expect(scenario.request.data.action).to.be('__GetRegions');
  143. });
  144. });
  145. describeMetricFindQuery('namespaces()', scenario => {
  146. scenario.setup(() => {
  147. scenario.requestResponse = [{text: 'AWS/EC2'}];
  148. });
  149. it('should call __GetNamespaces and return result', () => {
  150. expect(scenario.result[0].text).to.contain('AWS/EC2');
  151. expect(scenario.request.data.action).to.be('__GetNamespaces');
  152. });
  153. });
  154. describeMetricFindQuery('metrics(AWS/EC2)', scenario => {
  155. scenario.setup(() => {
  156. scenario.requestResponse = [{text: 'CPUUtilization'}];
  157. });
  158. it('should call __GetMetrics and return result', () => {
  159. expect(scenario.result[0].text).to.be('CPUUtilization');
  160. expect(scenario.request.data.action).to.be('__GetMetrics');
  161. });
  162. });
  163. describeMetricFindQuery('dimension_keys(AWS/EC2)', scenario => {
  164. scenario.setup(() => {
  165. scenario.requestResponse = [{text: 'InstanceId'}];
  166. });
  167. it('should call __GetDimensions and return result', () => {
  168. expect(scenario.result[0].text).to.be('InstanceId');
  169. expect(scenario.request.data.action).to.be('__GetDimensions');
  170. });
  171. });
  172. describeMetricFindQuery('dimension_values(us-east-1,AWS/EC2,CPUUtilization,InstanceId)', scenario => {
  173. scenario.setup(() => {
  174. scenario.requestResponse = {
  175. Metrics: [
  176. {
  177. Namespace: 'AWS/EC2',
  178. MetricName: 'CPUUtilization',
  179. Dimensions: [
  180. {
  181. Name: 'InstanceId',
  182. Value: 'i-12345678'
  183. }
  184. ]
  185. }
  186. ]
  187. };
  188. });
  189. it('should call __ListMetrics and return result', () => {
  190. expect(scenario.result[0].text).to.be('i-12345678');
  191. expect(scenario.request.data.action).to.be('ListMetrics');
  192. });
  193. });
  194. });