datasource.test.ts 4.0 KB

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