datasource.test.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import LokiDatasource from './datasource';
  2. import { LokiQuery } from './types';
  3. import { getQueryOptions } from 'test/helpers/getQueryOptions';
  4. import { DataSourceApi } from '@grafana/ui';
  5. import { DataFrame } from '@grafana/data';
  6. import { BackendSrv } from 'app/core/services/backend_srv';
  7. import { TemplateSrv } from 'app/features/templating/template_srv';
  8. describe('LokiDatasource', () => {
  9. const instanceSettings: any = {
  10. url: 'myloggingurl',
  11. };
  12. const testResp = {
  13. data: {
  14. streams: [
  15. {
  16. entries: [{ ts: '2019-02-01T10:27:37.498180581Z', line: 'hello' }],
  17. labels: '{}',
  18. },
  19. ],
  20. },
  21. };
  22. describe('when querying', () => {
  23. const backendSrvMock = { datasourceRequest: jest.fn() };
  24. const backendSrv = (backendSrvMock as unknown) as BackendSrv;
  25. const templateSrvMock = ({
  26. getAdhocFilters: (): any[] => [],
  27. replace: (a: string) => a,
  28. } as unknown) as TemplateSrv;
  29. test('should use default max lines when no limit given', () => {
  30. const ds = new LokiDatasource(instanceSettings, backendSrv, templateSrvMock);
  31. backendSrvMock.datasourceRequest = jest.fn(() => Promise.resolve(testResp));
  32. const options = getQueryOptions<LokiQuery>({ targets: [{ expr: 'foo', refId: 'B' }] });
  33. ds.query(options);
  34. expect(backendSrvMock.datasourceRequest.mock.calls.length).toBe(1);
  35. expect(backendSrvMock.datasourceRequest.mock.calls[0][0].url).toContain('limit=1000');
  36. });
  37. test('should use custom max lines if limit is set', () => {
  38. const customData = { ...(instanceSettings.jsonData || {}), maxLines: 20 };
  39. const customSettings = { ...instanceSettings, jsonData: customData };
  40. const ds = new LokiDatasource(customSettings, backendSrv, templateSrvMock);
  41. backendSrvMock.datasourceRequest = jest.fn(() => Promise.resolve(testResp));
  42. const options = getQueryOptions<LokiQuery>({ targets: [{ expr: 'foo', refId: 'B' }] });
  43. ds.query(options);
  44. expect(backendSrvMock.datasourceRequest.mock.calls.length).toBe(1);
  45. expect(backendSrvMock.datasourceRequest.mock.calls[0][0].url).toContain('limit=20');
  46. });
  47. test('should return series data', async done => {
  48. const customData = { ...(instanceSettings.jsonData || {}), maxLines: 20 };
  49. const customSettings = { ...instanceSettings, jsonData: customData };
  50. const ds = new LokiDatasource(customSettings, backendSrv, templateSrvMock);
  51. backendSrvMock.datasourceRequest = jest.fn(() => Promise.resolve(testResp));
  52. const options = getQueryOptions<LokiQuery>({
  53. targets: [{ expr: '{} foo', refId: 'B' }],
  54. });
  55. const res = await ds.query(options);
  56. const dataFrame = res.data[0] as DataFrame;
  57. expect(dataFrame.fields[1].values.get(0)).toBe('hello');
  58. expect(dataFrame.meta.limit).toBe(20);
  59. expect(dataFrame.meta.searchWords).toEqual(['(?i)foo']);
  60. done();
  61. });
  62. });
  63. describe('when performing testDataSource', () => {
  64. let ds: DataSourceApi<any, any>;
  65. let result: any;
  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. } as unknown) as BackendSrv;
  78. ds = new LokiDatasource(instanceSettings, backendSrv, {} as TemplateSrv);
  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. } as unknown) as BackendSrv;
  98. ds = new LokiDatasource(instanceSettings, backendSrv, {} as TemplateSrv);
  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. } as unknown) as BackendSrv;
  117. ds = new LokiDatasource(instanceSettings, backendSrv, {} as TemplateSrv);
  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. } as unknown) as BackendSrv;
  136. ds = new LokiDatasource(instanceSettings, backendSrv, {} as TemplateSrv);
  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. });