datasource.test.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import LokiDatasource from './datasource';
  2. import { LokiQuery } from './types';
  3. import { getQueryOptions } from 'test/helpers/getQueryOptions';
  4. import { SeriesData } from '@grafana/ui';
  5. describe('LokiDatasource', () => {
  6. const instanceSettings: any = {
  7. url: 'myloggingurl',
  8. };
  9. const testResp = {
  10. data: {
  11. streams: [
  12. {
  13. entries: [{ ts: '2019-02-01T10:27:37.498180581Z', line: 'hello' }],
  14. labels: '{}',
  15. },
  16. ],
  17. },
  18. };
  19. describe('when querying', () => {
  20. const backendSrvMock = { datasourceRequest: jest.fn() };
  21. const templateSrvMock = {
  22. getAdhocFilters: () => [],
  23. replace: a => a,
  24. };
  25. test('should use default max lines when no limit given', () => {
  26. const ds = new LokiDatasource(instanceSettings, backendSrvMock, templateSrvMock);
  27. backendSrvMock.datasourceRequest = jest.fn(() => Promise.resolve(testResp));
  28. const options = getQueryOptions<LokiQuery>({ targets: [{ expr: 'foo', refId: 'B' }] });
  29. ds.query(options);
  30. expect(backendSrvMock.datasourceRequest.mock.calls.length).toBe(1);
  31. expect(backendSrvMock.datasourceRequest.mock.calls[0][0].url).toContain('limit=1000');
  32. });
  33. test('should use custom max lines if limit is set', () => {
  34. const customData = { ...(instanceSettings.jsonData || {}), maxLines: 20 };
  35. const customSettings = { ...instanceSettings, jsonData: customData };
  36. const ds = new LokiDatasource(customSettings, backendSrvMock, templateSrvMock);
  37. backendSrvMock.datasourceRequest = jest.fn(() => Promise.resolve(testResp));
  38. const options = getQueryOptions<LokiQuery>({ targets: [{ expr: 'foo', refId: 'B' }] });
  39. ds.query(options);
  40. expect(backendSrvMock.datasourceRequest.mock.calls.length).toBe(1);
  41. expect(backendSrvMock.datasourceRequest.mock.calls[0][0].url).toContain('limit=20');
  42. });
  43. test('should return series data', async done => {
  44. const customData = { ...(instanceSettings.jsonData || {}), maxLines: 20 };
  45. const customSettings = { ...instanceSettings, jsonData: customData };
  46. const ds = new LokiDatasource(customSettings, backendSrvMock, templateSrvMock);
  47. backendSrvMock.datasourceRequest = jest.fn(() => Promise.resolve(testResp));
  48. const options = getQueryOptions<LokiQuery>({
  49. targets: [{ expr: 'foo', refId: 'B' }],
  50. });
  51. const res = await ds.query(options);
  52. const seriesData = res.data[0] as SeriesData;
  53. expect(seriesData.rows[0][1]).toBe('hello');
  54. expect(seriesData.meta.limit).toBe(20);
  55. expect(seriesData.meta.search).toBe('(?i)foo');
  56. done();
  57. });
  58. });
  59. describe('when performing testDataSource', () => {
  60. let ds;
  61. let result;
  62. describe('and call succeeds', () => {
  63. beforeEach(async () => {
  64. const backendSrv = {
  65. async datasourceRequest() {
  66. return Promise.resolve({
  67. status: 200,
  68. data: {
  69. values: ['avalue'],
  70. },
  71. });
  72. },
  73. };
  74. ds = new LokiDatasource(instanceSettings, backendSrv, {});
  75. result = await ds.testDatasource();
  76. });
  77. it('should return successfully', () => {
  78. expect(result.status).toBe('success');
  79. });
  80. });
  81. describe('and call fails with 401 error', () => {
  82. beforeEach(async () => {
  83. const backendSrv = {
  84. async datasourceRequest() {
  85. return Promise.reject({
  86. statusText: 'Unauthorized',
  87. status: 401,
  88. data: {
  89. message: 'Unauthorized',
  90. },
  91. });
  92. },
  93. };
  94. ds = new LokiDatasource(instanceSettings, backendSrv, {});
  95. result = await ds.testDatasource();
  96. });
  97. it('should return error status and a detailed error message', () => {
  98. expect(result.status).toEqual('error');
  99. expect(result.message).toBe('Loki: Unauthorized. 401. Unauthorized');
  100. });
  101. });
  102. describe('and call fails with 404 error', () => {
  103. beforeEach(async () => {
  104. const backendSrv = {
  105. async datasourceRequest() {
  106. return Promise.reject({
  107. statusText: 'Not found',
  108. status: 404,
  109. data: '404 page not found',
  110. });
  111. },
  112. };
  113. ds = new LokiDatasource(instanceSettings, backendSrv, {});
  114. result = await ds.testDatasource();
  115. });
  116. it('should return error status and a detailed error message', () => {
  117. expect(result.status).toEqual('error');
  118. expect(result.message).toBe('Loki: Not found. 404. 404 page not found');
  119. });
  120. });
  121. describe('and call fails with 502 error', () => {
  122. beforeEach(async () => {
  123. const backendSrv = {
  124. async datasourceRequest() {
  125. return Promise.reject({
  126. statusText: 'Bad Gateway',
  127. status: 502,
  128. data: '',
  129. });
  130. },
  131. };
  132. ds = new LokiDatasource(instanceSettings, backendSrv, {});
  133. result = await ds.testDatasource();
  134. });
  135. it('should return error status and a detailed error message', () => {
  136. expect(result.status).toEqual('error');
  137. expect(result.message).toBe('Loki: Bad Gateway. 502');
  138. });
  139. });
  140. });
  141. });