cloudwatch-datasource-specs.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. define([
  2. 'helpers',
  3. 'plugins/datasource/cloudwatch/datasource',
  4. 'aws-sdk',
  5. ], function(helpers) {
  6. 'use strict';
  7. describe('CloudWatchDatasource', function() {
  8. var ctx = new helpers.ServiceTestContext();
  9. beforeEach(module('grafana.services'));
  10. beforeEach(ctx.providePhase(['templateSrv']));
  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: {
  33. Average: true
  34. },
  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. Label: 'CPUUtilization'
  47. };
  48. beforeEach(function() {
  49. ctx.ds.getCloudWatchClient = function() {
  50. return {
  51. getMetricStatistics: function(params, callback) {
  52. setTimeout(function() {
  53. requestParams = params;
  54. callback(null, response);
  55. }, 0);
  56. }
  57. };
  58. };
  59. });
  60. it('should generate the correct query', function() {
  61. ctx.ds.query(query).then(function() {
  62. expect(requestParams.Namespace).to.be(query.targets[0].namespace);
  63. expect(requestParams.MetricName).to.be(query.targets[0].metricName);
  64. expect(requestParams.Dimensions[0].Name).to.be(Object.keys(query.targets[0].dimensions)[0]);
  65. expect(requestParams.Dimensions[0].Value).to.be(query.targets[0].dimensions[Object.keys(query.targets[0].dimensions)[0]]);
  66. expect(requestParams.Statistics).to.eql(Object.keys(query.targets[0].statistics));
  67. expect(requestParams.Period).to.be(query.targets[0].period);
  68. });
  69. });
  70. it('should return series list', function() {
  71. ctx.ds.query(query).then(function(result) {
  72. var s = Object.keys(query.targets[0].statistics)[0];
  73. expect(result.data[0].target).to.be(response.Label + s);
  74. expect(result.data[0].datapoints[0][0]).to.be(response.Datapoints[0][s]);
  75. });
  76. });
  77. });
  78. describe('When performing CloudWatch metricFindQuery', function() {
  79. var requestParams;
  80. var response = {
  81. Metrics: [
  82. {
  83. Namespace: 'AWS/EC2',
  84. MetricName: 'CPUUtilization',
  85. Dimensions: [
  86. {
  87. Name: 'InstanceId',
  88. Value: 'i-12345678'
  89. }
  90. ]
  91. }
  92. ]
  93. };
  94. beforeEach(function() {
  95. ctx.ds.getCloudWatchClient = function() {
  96. return {
  97. listMetrics: function(params, callback) {
  98. setTimeout(function() {
  99. requestParams = params;
  100. callback(null, response);
  101. }, 0);
  102. }
  103. };
  104. };
  105. });
  106. it('should return suggest list for region()', function() {
  107. var query = 'region()';
  108. ctx.ds.metricFindQuery(query).then(function(result) {
  109. expect(result).to.contain('us-east-1');
  110. });
  111. });
  112. it('should return suggest list for namespace()', function() {
  113. var query = 'namespace()';
  114. ctx.ds.metricFindQuery(query).then(function(result) {
  115. expect(result).to.contain('AWS/EC2');
  116. });
  117. });
  118. it('should return suggest list for metrics()', function() {
  119. var query = 'metrics(AWS/EC2)';
  120. ctx.ds.metricFindQuery(query).then(function(result) {
  121. expect(result).to.contain('CPUUtilization');
  122. });
  123. });
  124. it('should return suggest list for dimension_keys()', function() {
  125. var query = 'dimension_keys(AWS/EC2)';
  126. ctx.ds.metricFindQuery(query).then(function(result) {
  127. expect(result).to.contain('InstanceId');
  128. });
  129. });
  130. it('should return suggest list for dimension_values()', function() {
  131. var query = 'dimension_values(us-east-1,AWS/EC2,CPUUtilization)';
  132. ctx.ds.metricFindQuery(query).then(function(result) {
  133. expect(result).to.contain('InstanceId');
  134. });
  135. });
  136. });
  137. });
  138. });