graphiteDatasource-specs.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. define([
  2. 'helpers',
  3. 'features/graphite/datasource'
  4. ], function(helpers) {
  5. 'use strict';
  6. describe('graphiteDatasource', function() {
  7. var ctx = new helpers.ServiceTestContext();
  8. beforeEach(module('grafana.services'));
  9. beforeEach(ctx.providePhase());
  10. beforeEach(ctx.createService('GraphiteDatasource'));
  11. beforeEach(function() {
  12. ctx.ds = new ctx.service({ url: [''] });
  13. });
  14. describe('When querying influxdb with one target using query editor target spec', function() {
  15. var query = {
  16. range: { from: 'now-1h', to: 'now' },
  17. targets: [{ target: 'prod1.count' }, {target: 'prod2.count'}],
  18. maxDataPoints: 500,
  19. };
  20. var response = [{ target: 'prod1.count', datapoints: [[10, 1], [12,1]], }];
  21. var results;
  22. var request;
  23. beforeEach(function() {
  24. ctx.$httpBackend.expectPOST('/render', function(body) { request = body; return true; })
  25. .respond(response);
  26. ctx.ds.query(query).then(function(data) { results = data; });
  27. ctx.$httpBackend.flush();
  28. });
  29. it('should generate the correct query', function() {
  30. ctx.$httpBackend.verifyNoOutstandingExpectation();
  31. });
  32. it('should query correctly', function() {
  33. var params = request.split('&');
  34. expect(params).to.contain('target=prod1.count');
  35. expect(params).to.contain('target=prod2.count');
  36. expect(params).to.contain('from=-1h');
  37. expect(params).to.contain('until=now');
  38. });
  39. it('should exclude undefined params', function() {
  40. var params = request.split('&');
  41. expect(params).to.not.contain('cacheTimeout=undefined');
  42. });
  43. it('should return series list', function() {
  44. expect(results.data.length).to.be(1);
  45. expect(results.data[0].target).to.be('prod1.count');
  46. });
  47. });
  48. describe('building graphite params', function() {
  49. it('should uri escape targets', function() {
  50. var results = ctx.ds.buildGraphiteParams({
  51. targets: [{target: 'prod1.{test,test2}'}, {target: 'prod2.count'}]
  52. });
  53. expect(results).to.contain('target=prod1.%7Btest%2Ctest2%7D');
  54. });
  55. it('should replace target placeholder', function() {
  56. var results = ctx.ds.buildGraphiteParams({
  57. targets: [{target: 'series1'}, {target: 'series2'}, {target: 'asPercent(#A,#B)'}]
  58. });
  59. expect(results[2]).to.be('target=asPercent(series1%2Cseries2)');
  60. });
  61. it('should fix wrong minute interval parameters', function() {
  62. var results = ctx.ds.buildGraphiteParams({
  63. targets: [{target: "summarize(prod.25m.count, '25m', 'sum')" }]
  64. });
  65. expect(results[0]).to.be('target=' + encodeURIComponent("summarize(prod.25m.count, '25min', 'sum')"));
  66. });
  67. it('should fix wrong month interval parameters', function() {
  68. var results = ctx.ds.buildGraphiteParams({
  69. targets: [{target: "summarize(prod.5M.count, '5M', 'sum')" }]
  70. });
  71. expect(results[0]).to.be('target=' + encodeURIComponent("summarize(prod.5M.count, '5mon', 'sum')"));
  72. });
  73. it('should ignore empty targets', function() {
  74. var results = ctx.ds.buildGraphiteParams({
  75. targets: [{target: 'series1'}, {target: ''}]
  76. });
  77. expect(results.length).to.be(2);
  78. });
  79. });
  80. });
  81. });