datasource.test.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import LokiDatasource from './datasource';
  2. describe('LokiDatasource', () => {
  3. const instanceSettings = {
  4. url: 'myloggingurl',
  5. };
  6. describe('when performing testDataSource', () => {
  7. let ds;
  8. let result;
  9. describe('and call succeeds', () => {
  10. beforeEach(async () => {
  11. const backendSrv = {
  12. async datasourceRequest() {
  13. return Promise.resolve({
  14. status: 200,
  15. data: {
  16. values: ['avalue'],
  17. },
  18. });
  19. },
  20. };
  21. ds = new LokiDatasource(instanceSettings, backendSrv, {});
  22. result = await ds.testDatasource();
  23. });
  24. it('should return successfully', () => {
  25. expect(result.status).toBe('success');
  26. });
  27. });
  28. describe('and call fails with 401 error', () => {
  29. beforeEach(async () => {
  30. const backendSrv = {
  31. async datasourceRequest() {
  32. return Promise.reject({
  33. statusText: 'Unauthorized',
  34. status: 401,
  35. data: {
  36. message: 'Unauthorized',
  37. },
  38. });
  39. },
  40. };
  41. ds = new LokiDatasource(instanceSettings, backendSrv, {});
  42. result = await ds.testDatasource();
  43. });
  44. it('should return error status and a detailed error message', () => {
  45. expect(result.status).toEqual('error');
  46. expect(result.message).toBe('Loki: Unauthorized. 401. Unauthorized');
  47. });
  48. });
  49. describe('and call fails with 404 error', () => {
  50. beforeEach(async () => {
  51. const backendSrv = {
  52. async datasourceRequest() {
  53. return Promise.reject({
  54. statusText: 'Not found',
  55. status: 404,
  56. data: '404 page not found',
  57. });
  58. },
  59. };
  60. ds = new LokiDatasource(instanceSettings, backendSrv, {});
  61. result = await ds.testDatasource();
  62. });
  63. it('should return error status and a detailed error message', () => {
  64. expect(result.status).toEqual('error');
  65. expect(result.message).toBe('Loki: Not found. 404. 404 page not found');
  66. });
  67. });
  68. describe('and call fails with 502 error', () => {
  69. beforeEach(async () => {
  70. const backendSrv = {
  71. async datasourceRequest() {
  72. return Promise.reject({
  73. statusText: 'Bad Gateway',
  74. status: 502,
  75. data: '',
  76. });
  77. },
  78. };
  79. ds = new LokiDatasource(instanceSettings, backendSrv, {});
  80. result = await ds.testDatasource();
  81. });
  82. it('should return error status and a detailed error message', () => {
  83. expect(result.status).toEqual('error');
  84. expect(result.message).toBe('Loki: Bad Gateway. 502');
  85. });
  86. });
  87. });
  88. });