datasource_specs.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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.services'));
  8. beforeEach(angularMocks.module('grafana.controllers'));
  9. beforeEach(ctx.providePhase(['templateSrv', 'backendSrv']));
  10. beforeEach(ctx.createService('CloudWatchDatasource'));
  11. beforeEach(function() {
  12. ctx.ds = new ctx.service({
  13. jsonData: {
  14. defaultRegion: 'us-east-1',
  15. access: 'proxy'
  16. }
  17. });
  18. });
  19. describe('When performing CloudWatch query', function() {
  20. var requestParams;
  21. var query = {
  22. range: { from: 'now-1h', to: 'now' },
  23. targets: [
  24. {
  25. region: 'us-east-1',
  26. namespace: 'AWS/EC2',
  27. metricName: 'CPUUtilization',
  28. dimensions: {
  29. InstanceId: 'i-12345678'
  30. },
  31. statistics: {
  32. Average: true
  33. },
  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. Label: 'CPUUtilization'
  46. };
  47. beforeEach(function() {
  48. ctx.backendSrv.datasourceRequest = function(params) {
  49. requestParams = params;
  50. return ctx.$q.when({data: response});
  51. };
  52. });
  53. it('should generate the correct query', function(done) {
  54. ctx.ds.query(query).then(function() {
  55. var params = requestParams.data.parameters;
  56. expect(params.namespace).to.be(query.targets[0].namespace);
  57. expect(params.metricName).to.be(query.targets[0].metricName);
  58. expect(params.dimensions[0].Name).to.be(Object.keys(query.targets[0].dimensions)[0]);
  59. expect(params.dimensions[0].Value).to.be(query.targets[0].dimensions[Object.keys(query.targets[0].dimensions)[0]]);
  60. expect(params.statistics).to.eql(Object.keys(query.targets[0].statistics));
  61. expect(params.period).to.be(query.targets[0].period);
  62. done();
  63. });
  64. ctx.$rootScope.$apply();
  65. });
  66. it('should return series list', function(done) {
  67. ctx.ds.query(query).then(function(result) {
  68. var s = Object.keys(query.targets[0].statistics)[0];
  69. expect(result.data[0].target).to.be(response.Label + '_' + s + JSON.stringify(query.targets[0].dimensions));
  70. expect(result.data[0].datapoints[0][0]).to.be(response.Datapoints[0][s]);
  71. done();
  72. });
  73. ctx.$rootScope.$apply();
  74. });
  75. });
  76. function describeMetricFindQuery(query, func) {
  77. describe('metricFindQuery ' + query, () => {
  78. let scenario: any = {};
  79. scenario.setup = setupCallback => {
  80. beforeEach(() => {
  81. setupCallback();
  82. ctx.backendSrv.datasourceRequest = args => {
  83. scenario.request = args;
  84. return ctx.$q.when({data: scenario.requestResponse });
  85. };
  86. ctx.ds.metricFindQuery(query).then(args => {
  87. scenario.result = args;
  88. });
  89. ctx.$rootScope.$apply();
  90. });
  91. };
  92. func(scenario);
  93. });
  94. }
  95. describeMetricFindQuery('regions()', scenario => {
  96. scenario.setup(() => {
  97. scenario.requestResponse = [{text: 'us-east-1'}];
  98. });
  99. it('should call __GetRegions and return result', () => {
  100. expect(scenario.result[0].text).to.contain('us-east-1');
  101. expect(scenario.request.data.action).to.be('__GetRegions');
  102. });
  103. });
  104. describeMetricFindQuery('namespaces()', scenario => {
  105. scenario.setup(() => {
  106. scenario.requestResponse = [{text: 'AWS/EC2'}];
  107. });
  108. it('should call __GetNamespaces and return result', () => {
  109. expect(scenario.result[0].text).to.contain('AWS/EC2');
  110. expect(scenario.request.data.action).to.be('__GetNamespaces');
  111. });
  112. });
  113. describeMetricFindQuery('metrics(AWS/EC2)', scenario => {
  114. scenario.setup(() => {
  115. scenario.requestResponse = [{text: 'CPUUtilization'}];
  116. });
  117. it('should call __GetMetrics and return result', () => {
  118. expect(scenario.result[0].text).to.be('CPUUtilization');
  119. expect(scenario.request.data.action).to.be('__GetMetrics');
  120. });
  121. });
  122. describeMetricFindQuery('dimension_keys(AWS/EC2)', scenario => {
  123. scenario.setup(() => {
  124. scenario.requestResponse = [{text: 'InstanceId'}];
  125. });
  126. it('should call __GetDimensions and return result', () => {
  127. expect(scenario.result[0].text).to.be('InstanceId');
  128. expect(scenario.request.data.action).to.be('__GetDimensions');
  129. });
  130. });
  131. describeMetricFindQuery('dimension_values(us-east-1,AWS/EC2,CPUUtilization)', scenario => {
  132. scenario.setup(() => {
  133. scenario.requestResponse = {
  134. Metrics: [
  135. {
  136. Namespace: 'AWS/EC2',
  137. MetricName: 'CPUUtilization',
  138. Dimensions: [
  139. {
  140. Name: 'InstanceId',
  141. Value: 'i-12345678'
  142. }
  143. ]
  144. }
  145. ]
  146. };
  147. });
  148. it('should call __GetMetrics and return result', () => {
  149. expect(scenario.result[0].text).to.be('i-12345678');
  150. expect(scenario.request.data.action).to.be('ListMetrics');
  151. });
  152. });
  153. });