datasource_specs.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. ///<amd-dependency path="app/plugins/datasource/cloudwatch/datasource" />
  2. ///<amd-dependency path="test/specs/helpers" name="helpers" />
  3. import {describe, beforeEach, it, sinon, expect, angularMocks} from 'test/lib/common';
  4. declare var helpers: any;
  5. describe('CloudWatchDatasource', function() {
  6. var ctx = new helpers.ServiceTestContext();
  7. beforeEach(angularMocks.module('grafana.core'));
  8. beforeEach(angularMocks.module('grafana.services'));
  9. beforeEach(angularMocks.module('grafana.controllers'));
  10. beforeEach(ctx.providePhase(['templateSrv', 'backendSrv']));
  11. beforeEach(ctx.createService('CloudWatchDatasource'));
  12. beforeEach(function() {
  13. ctx.ds = new ctx.service({
  14. jsonData: {
  15. defaultRegion: 'us-east-1',
  16. access: 'proxy'
  17. }
  18. });
  19. });
  20. describe('When performing CloudWatch query', function() {
  21. var requestParams;
  22. var query = {
  23. range: { from: 'now-1h', to: 'now' },
  24. targets: [
  25. {
  26. region: 'us-east-1',
  27. namespace: 'AWS/EC2',
  28. metricName: 'CPUUtilization',
  29. dimensions: {
  30. InstanceId: 'i-12345678'
  31. },
  32. statistics: ['Average'],
  33. period: 300
  34. }
  35. ]
  36. };
  37. var response = {
  38. Datapoints: [
  39. {
  40. Average: 1,
  41. Timestamp: 'Wed Dec 31 1969 16:00:00 GMT-0800 (PST)'
  42. },
  43. {
  44. Average: 2,
  45. Timestamp: 'Wed Dec 31 1969 16:05:00 GMT-0800 (PST)'
  46. },
  47. {
  48. Average: 5,
  49. Timestamp: 'Wed Dec 31 1969 16:15:00 GMT-0800 (PST)'
  50. }
  51. ],
  52. Label: 'CPUUtilization'
  53. };
  54. beforeEach(function() {
  55. ctx.backendSrv.datasourceRequest = function(params) {
  56. requestParams = params;
  57. return ctx.$q.when({data: response});
  58. };
  59. });
  60. it('should generate the correct query', function(done) {
  61. ctx.ds.query(query).then(function() {
  62. var params = requestParams.data.parameters;
  63. expect(params.namespace).to.be(query.targets[0].namespace);
  64. expect(params.metricName).to.be(query.targets[0].metricName);
  65. expect(params.dimensions[0].Name).to.be(Object.keys(query.targets[0].dimensions)[0]);
  66. expect(params.dimensions[0].Value).to.be(query.targets[0].dimensions[Object.keys(query.targets[0].dimensions)[0]]);
  67. expect(params.statistics).to.eql(query.targets[0].statistics);
  68. expect(params.period).to.be(query.targets[0].period);
  69. done();
  70. });
  71. ctx.$rootScope.$apply();
  72. });
  73. it('should return series list', function(done) {
  74. ctx.ds.query(query).then(function(result) {
  75. expect(result.data[0].target).to.be('CPUUtilization_Average');
  76. expect(result.data[0].datapoints[0][0]).to.be(response.Datapoints[0]['Average']);
  77. done();
  78. });
  79. ctx.$rootScope.$apply();
  80. });
  81. it('should return null for missing data point', function(done) {
  82. ctx.ds.query(query).then(function(result) {
  83. expect(result.data[0].datapoints[2][0]).to.be(null);
  84. done();
  85. });
  86. ctx.$rootScope.$apply();
  87. });
  88. });
  89. function describeMetricFindQuery(query, func) {
  90. describe('metricFindQuery ' + query, () => {
  91. let scenario: any = {};
  92. scenario.setup = setupCallback => {
  93. beforeEach(() => {
  94. setupCallback();
  95. ctx.backendSrv.datasourceRequest = args => {
  96. scenario.request = args;
  97. return ctx.$q.when({data: scenario.requestResponse });
  98. };
  99. ctx.ds.metricFindQuery(query).then(args => {
  100. scenario.result = args;
  101. });
  102. ctx.$rootScope.$apply();
  103. });
  104. };
  105. func(scenario);
  106. });
  107. }
  108. describeMetricFindQuery('regions()', scenario => {
  109. scenario.setup(() => {
  110. scenario.requestResponse = [{text: 'us-east-1'}];
  111. });
  112. it('should call __GetRegions and return result', () => {
  113. expect(scenario.result[0].text).to.contain('us-east-1');
  114. expect(scenario.request.data.action).to.be('__GetRegions');
  115. });
  116. });
  117. describeMetricFindQuery('namespaces()', scenario => {
  118. scenario.setup(() => {
  119. scenario.requestResponse = [{text: 'AWS/EC2'}];
  120. });
  121. it('should call __GetNamespaces and return result', () => {
  122. expect(scenario.result[0].text).to.contain('AWS/EC2');
  123. expect(scenario.request.data.action).to.be('__GetNamespaces');
  124. });
  125. });
  126. describeMetricFindQuery('metrics(AWS/EC2)', scenario => {
  127. scenario.setup(() => {
  128. scenario.requestResponse = [{text: 'CPUUtilization'}];
  129. });
  130. it('should call __GetMetrics and return result', () => {
  131. expect(scenario.result[0].text).to.be('CPUUtilization');
  132. expect(scenario.request.data.action).to.be('__GetMetrics');
  133. });
  134. });
  135. describeMetricFindQuery('dimension_keys(AWS/EC2)', scenario => {
  136. scenario.setup(() => {
  137. scenario.requestResponse = [{text: 'InstanceId'}];
  138. });
  139. it('should call __GetDimensions and return result', () => {
  140. expect(scenario.result[0].text).to.be('InstanceId');
  141. expect(scenario.request.data.action).to.be('__GetDimensions');
  142. });
  143. });
  144. describeMetricFindQuery('dimension_values(us-east-1,AWS/EC2,CPUUtilization,InstanceId)', scenario => {
  145. scenario.setup(() => {
  146. scenario.requestResponse = {
  147. Metrics: [
  148. {
  149. Namespace: 'AWS/EC2',
  150. MetricName: 'CPUUtilization',
  151. Dimensions: [
  152. {
  153. Name: 'InstanceId',
  154. Value: 'i-12345678'
  155. }
  156. ]
  157. }
  158. ]
  159. };
  160. });
  161. it('should call __ListMetrics and return result', () => {
  162. expect(scenario.result[0].text).to.be('i-12345678');
  163. expect(scenario.request.data.action).to.be('ListMetrics');
  164. });
  165. });
  166. });