datasource.test.ts 5.7 KB

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