datasource.test.ts 5.4 KB

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