datasource.test.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. describe('when querying', () => {
  9. const backendSrvMock = { datasourceRequest: jest.fn() };
  10. const templateSrvMock = {
  11. getAdhocFilters: () => [],
  12. replace: a => a,
  13. };
  14. test('should use default max lines when no limit given', () => {
  15. const ds = new LokiDatasource(instanceSettings, backendSrvMock, templateSrvMock);
  16. backendSrvMock.datasourceRequest = jest.fn();
  17. const options = getQueryOptions<LokiQuery>({ targets: [{ expr: 'foo', refId: 'B' }] });
  18. ds.query(options);
  19. expect(backendSrvMock.datasourceRequest.mock.calls.length).toBe(1);
  20. expect(backendSrvMock.datasourceRequest.mock.calls[0][0].url).toContain('limit=1000');
  21. });
  22. test('should use custom max lines if limit is set', () => {
  23. const customData = { ...(instanceSettings.jsonData || {}), maxLines: 20 };
  24. const customSettings = { ...instanceSettings, jsonData: customData };
  25. const ds = new LokiDatasource(customSettings, backendSrvMock, templateSrvMock);
  26. backendSrvMock.datasourceRequest = jest.fn();
  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=20');
  31. });
  32. });
  33. describe('when performing testDataSource', () => {
  34. let ds;
  35. let result;
  36. describe('and call succeeds', () => {
  37. beforeEach(async () => {
  38. const backendSrv = {
  39. async datasourceRequest() {
  40. return Promise.resolve({
  41. status: 200,
  42. data: {
  43. values: ['avalue'],
  44. },
  45. });
  46. },
  47. };
  48. ds = new LokiDatasource(instanceSettings, backendSrv, {});
  49. result = await ds.testDatasource();
  50. });
  51. it('should return successfully', () => {
  52. expect(result.status).toBe('success');
  53. });
  54. });
  55. describe('and call fails with 401 error', () => {
  56. beforeEach(async () => {
  57. const backendSrv = {
  58. async datasourceRequest() {
  59. return Promise.reject({
  60. statusText: 'Unauthorized',
  61. status: 401,
  62. data: {
  63. message: 'Unauthorized',
  64. },
  65. });
  66. },
  67. };
  68. ds = new LokiDatasource(instanceSettings, backendSrv, {});
  69. result = await ds.testDatasource();
  70. });
  71. it('should return error status and a detailed error message', () => {
  72. expect(result.status).toEqual('error');
  73. expect(result.message).toBe('Loki: Unauthorized. 401. Unauthorized');
  74. });
  75. });
  76. describe('and call fails with 404 error', () => {
  77. beforeEach(async () => {
  78. const backendSrv = {
  79. async datasourceRequest() {
  80. return Promise.reject({
  81. statusText: 'Not found',
  82. status: 404,
  83. data: '404 page not found',
  84. });
  85. },
  86. };
  87. ds = new LokiDatasource(instanceSettings, backendSrv, {});
  88. result = await ds.testDatasource();
  89. });
  90. it('should return error status and a detailed error message', () => {
  91. expect(result.status).toEqual('error');
  92. expect(result.message).toBe('Loki: Not found. 404. 404 page not found');
  93. });
  94. });
  95. describe('and call fails with 502 error', () => {
  96. beforeEach(async () => {
  97. const backendSrv = {
  98. async datasourceRequest() {
  99. return Promise.reject({
  100. statusText: 'Bad Gateway',
  101. status: 502,
  102. data: '',
  103. });
  104. },
  105. };
  106. ds = new LokiDatasource(instanceSettings, backendSrv, {});
  107. result = await ds.testDatasource();
  108. });
  109. it('should return error status and a detailed error message', () => {
  110. expect(result.status).toEqual('error');
  111. expect(result.message).toBe('Loki: Bad Gateway. 502');
  112. });
  113. });
  114. });
  115. });