datasource_specs.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import {describe, beforeEach, it, expect, angularMocks} from 'test/lib/common';
  2. import helpers from 'test/specs/helpers';
  3. import {GraphiteDatasource} from "../datasource";
  4. import moment from 'moment';
  5. import _ from 'lodash';
  6. describe('graphiteDatasource', function() {
  7. let ctx = new helpers.ServiceTestContext();
  8. let instanceSettings: any = {url: [''], name: 'graphiteProd', jsonData: {}};
  9. beforeEach(angularMocks.module('grafana.core'));
  10. beforeEach(angularMocks.module('grafana.services'));
  11. beforeEach(ctx.providePhase(['backendSrv', 'templateSrv']));
  12. beforeEach(angularMocks.inject(function($q, $rootScope, $httpBackend, $injector) {
  13. ctx.$q = $q;
  14. ctx.$httpBackend = $httpBackend;
  15. ctx.$rootScope = $rootScope;
  16. ctx.$injector = $injector;
  17. $httpBackend.when('GET', /\.html$/).respond('');
  18. }));
  19. beforeEach(function() {
  20. ctx.ds = ctx.$injector.instantiate(GraphiteDatasource, {instanceSettings: instanceSettings});
  21. });
  22. describe('When querying graphite with one target using query editor target spec', function() {
  23. let query = {
  24. panelId: 3,
  25. rangeRaw: { from: 'now-1h', to: 'now' },
  26. targets: [{ target: 'prod1.count' }, {target: 'prod2.count'}],
  27. maxDataPoints: 500,
  28. };
  29. let results;
  30. let requestOptions;
  31. beforeEach(function() {
  32. ctx.backendSrv.datasourceRequest = function(options) {
  33. requestOptions = options;
  34. return ctx.$q.when({data: [{ target: 'prod1.count', datapoints: [[10, 1], [12,1]] }]});
  35. };
  36. ctx.ds.query(query).then(function(data) { results = data; });
  37. ctx.$rootScope.$apply();
  38. });
  39. it('should generate the correct query', function() {
  40. expect(requestOptions.url).to.be('/render');
  41. });
  42. it('should set unique requestId', function() {
  43. expect(requestOptions.requestId).to.be('graphiteProd.panelId.3');
  44. });
  45. it('should query correctly', function() {
  46. let params = requestOptions.data.split('&');
  47. expect(params).to.contain('target=prod1.count');
  48. expect(params).to.contain('target=prod2.count');
  49. expect(params).to.contain('from=-1h');
  50. expect(params).to.contain('until=now');
  51. });
  52. it('should exclude undefined params', function() {
  53. let params = requestOptions.data.split('&');
  54. expect(params).to.not.contain('cacheTimeout=undefined');
  55. });
  56. it('should return series list', function() {
  57. expect(results.data.length).to.be(1);
  58. expect(results.data[0].target).to.be('prod1.count');
  59. });
  60. it('should convert to millisecond resolution', function() {
  61. expect(results.data[0].datapoints[0][0]).to.be(10);
  62. });
  63. });
  64. describe('when fetching Graphite Events as annotations', () => {
  65. let results;
  66. const options = {
  67. annotation: {
  68. tags: 'tag1'
  69. },
  70. range: {
  71. from: moment(1432288354),
  72. to: moment(1432288401)
  73. },
  74. rangeRaw: {from: "now-24h", to: "now"}
  75. };
  76. describe('and tags are returned as string', () => {
  77. const response = {
  78. data: [
  79. {
  80. when: 1507222850,
  81. tags: 'tag1 tag2',
  82. data: 'some text',
  83. id: 2,
  84. what: 'Event - deploy'
  85. }
  86. ]};
  87. beforeEach(() => {
  88. ctx.backendSrv.datasourceRequest = function(options) {
  89. return ctx.$q.when(response);
  90. };
  91. ctx.ds.annotationQuery(options).then(function(data) { results = data; });
  92. ctx.$rootScope.$apply();
  93. });
  94. it('should parse the tags string into an array', () => {
  95. expect(_.isArray(results[0].tags)).to.eql(true);
  96. expect(results[0].tags.length).to.eql(2);
  97. expect(results[0].tags[0]).to.eql('tag1');
  98. expect(results[0].tags[1]).to.eql('tag2');
  99. });
  100. });
  101. describe('and tags are returned as an array', () => {
  102. const response = {
  103. data: [
  104. {
  105. when: 1507222850,
  106. tags: ['tag1', 'tag2'],
  107. data: 'some text',
  108. id: 2,
  109. what: 'Event - deploy'
  110. }
  111. ]};
  112. beforeEach(() => {
  113. ctx.backendSrv.datasourceRequest = function(options) {
  114. return ctx.$q.when(response);
  115. };
  116. ctx.ds.annotationQuery(options).then(function(data) { results = data; });
  117. ctx.$rootScope.$apply();
  118. });
  119. it('should parse the tags string into an array', () => {
  120. expect(_.isArray(results[0].tags)).to.eql(true);
  121. expect(results[0].tags.length).to.eql(2);
  122. expect(results[0].tags[0]).to.eql('tag1');
  123. expect(results[0].tags[1]).to.eql('tag2');
  124. });
  125. });
  126. });
  127. describe('building graphite params', function() {
  128. it('should return empty array if no targets', function() {
  129. let results = ctx.ds.buildGraphiteParams({
  130. targets: [{}]
  131. });
  132. expect(results.length).to.be(0);
  133. });
  134. it('should uri escape targets', function() {
  135. let results = ctx.ds.buildGraphiteParams({
  136. targets: [{target: 'prod1.{test,test2}'}, {target: 'prod2.count'}]
  137. });
  138. expect(results).to.contain('target=prod1.%7Btest%2Ctest2%7D');
  139. });
  140. it('should replace target placeholder', function() {
  141. let results = ctx.ds.buildGraphiteParams({
  142. targets: [{target: 'series1'}, {target: 'series2'}, {target: 'asPercent(#A,#B)'}]
  143. });
  144. expect(results[2]).to.be('target=asPercent(series1%2Cseries2)');
  145. });
  146. it('should replace target placeholder for hidden series', function() {
  147. let results = ctx.ds.buildGraphiteParams({
  148. targets: [{target: 'series1', hide: true}, {target: 'sumSeries(#A)', hide: true}, {target: 'asPercent(#A,#B)'}]
  149. });
  150. expect(results[0]).to.be('target=' + encodeURIComponent('asPercent(series1,sumSeries(series1))'));
  151. });
  152. it('should replace target placeholder when nesting query references', function() {
  153. let results = ctx.ds.buildGraphiteParams({
  154. targets: [{target: 'series1'}, {target: 'sumSeries(#A)'}, {target: 'asPercent(#A,#B)'}]
  155. });
  156. expect(results[2]).to.be('target=' + encodeURIComponent("asPercent(series1,sumSeries(series1))"));
  157. });
  158. it('should fix wrong minute interval parameters', function() {
  159. let results = ctx.ds.buildGraphiteParams({
  160. targets: [{target: "summarize(prod.25m.count, '25m', 'sum')" }]
  161. });
  162. expect(results[0]).to.be('target=' + encodeURIComponent("summarize(prod.25m.count, '25min', 'sum')"));
  163. });
  164. it('should fix wrong month interval parameters', function() {
  165. let results = ctx.ds.buildGraphiteParams({
  166. targets: [{target: "summarize(prod.5M.count, '5M', 'sum')" }]
  167. });
  168. expect(results[0]).to.be('target=' + encodeURIComponent("summarize(prod.5M.count, '5mon', 'sum')"));
  169. });
  170. it('should ignore empty targets', function() {
  171. let results = ctx.ds.buildGraphiteParams({
  172. targets: [{target: 'series1'}, {target: ''}]
  173. });
  174. expect(results.length).to.be(2);
  175. });
  176. });
  177. });