datasource.jest.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import _ from 'lodash';
  2. import moment from 'moment';
  3. import q from 'q';
  4. import { PrometheusDatasource, prometheusSpecialRegexEscape, prometheusRegularEscape } from '../datasource';
  5. describe('PrometheusDatasource', () => {
  6. let ctx: any = {};
  7. let instanceSettings = {
  8. url: 'proxied',
  9. directUrl: 'direct',
  10. user: 'test',
  11. password: 'mupp',
  12. jsonData: {},
  13. };
  14. ctx.backendSrvMock = {};
  15. ctx.templateSrvMock = {
  16. replace: a => a,
  17. };
  18. ctx.timeSrvMock = {};
  19. beforeEach(() => {
  20. ctx.ds = new PrometheusDatasource(instanceSettings, q, ctx.backendSrvMock, ctx.templateSrvMock, ctx.timeSrvMock);
  21. });
  22. describe('Datasource metadata requests', () => {
  23. it('should perform a GET request with the default config', () => {
  24. ctx.backendSrvMock.datasourceRequest = jest.fn();
  25. ctx.ds.metadataRequest('/foo');
  26. expect(ctx.backendSrvMock.datasourceRequest.mock.calls.length).toBe(1);
  27. expect(ctx.backendSrvMock.datasourceRequest.mock.calls[0][0].method).toBe('GET');
  28. });
  29. it('should still perform a GET request with the DS HTTP method set to POST', () => {
  30. ctx.backendSrvMock.datasourceRequest = jest.fn();
  31. const postSettings = _.cloneDeep(instanceSettings);
  32. postSettings.jsonData.httpMethod = 'POST';
  33. const ds = new PrometheusDatasource(postSettings, q, ctx.backendSrvMock, ctx.templateSrvMock, ctx.timeSrvMock);
  34. ds.metadataRequest('/foo');
  35. expect(ctx.backendSrvMock.datasourceRequest.mock.calls.length).toBe(1);
  36. expect(ctx.backendSrvMock.datasourceRequest.mock.calls[0][0].method).toBe('GET');
  37. });
  38. });
  39. describe('When converting prometheus histogram to heatmap format', () => {
  40. beforeEach(() => {
  41. ctx.query = {
  42. range: { from: moment(1443454528000), to: moment(1443454528000) },
  43. targets: [{ expr: 'test{job="testjob"}', format: 'heatmap', legendFormat: '{{le}}' }],
  44. interval: '60s',
  45. };
  46. });
  47. it('should convert cumullative histogram to ordinary', () => {
  48. const resultMock = [
  49. {
  50. metric: { __name__: 'metric', job: 'testjob', le: '10' },
  51. values: [[1443454528.0, '10'], [1443454528.0, '10']],
  52. },
  53. {
  54. metric: { __name__: 'metric', job: 'testjob', le: '20' },
  55. values: [[1443454528.0, '20'], [1443454528.0, '10']],
  56. },
  57. {
  58. metric: { __name__: 'metric', job: 'testjob', le: '30' },
  59. values: [[1443454528.0, '25'], [1443454528.0, '10']],
  60. },
  61. ];
  62. const responseMock = { data: { data: { result: resultMock } } };
  63. const expected = [
  64. {
  65. target: '10',
  66. datapoints: [[10, 1443454528000], [10, 1443454528000]],
  67. },
  68. {
  69. target: '20',
  70. datapoints: [[10, 1443454528000], [0, 1443454528000]],
  71. },
  72. {
  73. target: '30',
  74. datapoints: [[5, 1443454528000], [0, 1443454528000]],
  75. },
  76. ];
  77. ctx.ds.performTimeSeriesQuery = jest.fn().mockReturnValue(responseMock);
  78. return ctx.ds.query(ctx.query).then(result => {
  79. let results = result.data;
  80. return expect(results).toEqual(expected);
  81. });
  82. });
  83. it('should sort series by label value', () => {
  84. const resultMock = [
  85. {
  86. metric: { __name__: 'metric', job: 'testjob', le: '2' },
  87. values: [[1443454528.0, '10'], [1443454528.0, '10']],
  88. },
  89. {
  90. metric: { __name__: 'metric', job: 'testjob', le: '4' },
  91. values: [[1443454528.0, '20'], [1443454528.0, '10']],
  92. },
  93. {
  94. metric: { __name__: 'metric', job: 'testjob', le: '+Inf' },
  95. values: [[1443454528.0, '25'], [1443454528.0, '10']],
  96. },
  97. {
  98. metric: { __name__: 'metric', job: 'testjob', le: '1' },
  99. values: [[1443454528.0, '25'], [1443454528.0, '10']],
  100. },
  101. ];
  102. const responseMock = { data: { data: { result: resultMock } } };
  103. const expected = ['1', '2', '4', '+Inf'];
  104. ctx.ds.performTimeSeriesQuery = jest.fn().mockReturnValue(responseMock);
  105. return ctx.ds.query(ctx.query).then(result => {
  106. let seriesLabels = _.map(result.data, 'target');
  107. return expect(seriesLabels).toEqual(expected);
  108. });
  109. });
  110. });
  111. describe('Prometheus regular escaping', function() {
  112. it('should not escape simple string', function() {
  113. expect(prometheusRegularEscape('cryptodepression')).toEqual('cryptodepression');
  114. });
  115. it("should escape '", function() {
  116. expect(prometheusRegularEscape("looking'glass")).toEqual("looking\\\\'glass");
  117. });
  118. it('should escape multiple characters', function() {
  119. expect(prometheusRegularEscape("'looking'glass'")).toEqual("\\\\'looking\\\\'glass\\\\'");
  120. });
  121. });
  122. describe('Prometheus regexes escaping', function() {
  123. it('should not escape simple string', function() {
  124. expect(prometheusSpecialRegexEscape('cryptodepression')).toEqual('cryptodepression');
  125. });
  126. it('should escape $^*+?.()\\', function() {
  127. expect(prometheusSpecialRegexEscape("looking'glass")).toEqual("looking\\\\'glass");
  128. expect(prometheusSpecialRegexEscape('looking{glass')).toEqual('looking\\\\{glass');
  129. expect(prometheusSpecialRegexEscape('looking}glass')).toEqual('looking\\\\}glass');
  130. expect(prometheusSpecialRegexEscape('looking[glass')).toEqual('looking\\\\[glass');
  131. expect(prometheusSpecialRegexEscape('looking]glass')).toEqual('looking\\\\]glass');
  132. expect(prometheusSpecialRegexEscape('looking$glass')).toEqual('looking\\\\$glass');
  133. expect(prometheusSpecialRegexEscape('looking^glass')).toEqual('looking\\\\^glass');
  134. expect(prometheusSpecialRegexEscape('looking*glass')).toEqual('looking\\\\*glass');
  135. expect(prometheusSpecialRegexEscape('looking+glass')).toEqual('looking\\\\+glass');
  136. expect(prometheusSpecialRegexEscape('looking?glass')).toEqual('looking\\\\?glass');
  137. expect(prometheusSpecialRegexEscape('looking.glass')).toEqual('looking\\\\.glass');
  138. expect(prometheusSpecialRegexEscape('looking(glass')).toEqual('looking\\\\(glass');
  139. expect(prometheusSpecialRegexEscape('looking)glass')).toEqual('looking\\\\)glass');
  140. expect(prometheusSpecialRegexEscape('looking\\glass')).toEqual('looking\\\\\\\\glass');
  141. });
  142. it('should escape multiple special characters', function() {
  143. expect(prometheusSpecialRegexEscape('+looking$glass?')).toEqual('\\\\+looking\\\\$glass\\\\?');
  144. });
  145. });
  146. });