datasource.test.ts 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { lastValueFrom, of } from 'rxjs';
  2. import { createFetchResponse } from 'test/helpers/createFetchResponse';
  3. import { DataSourceInstanceSettings, FieldType } from '@grafana/data';
  4. import { TemplateSrv } from '@grafana/runtime';
  5. import { backendSrv } from 'app/core/services/backend_srv';
  6. import { ZipkinDatasource } from './datasource';
  7. import mockJson from './mockJsonResponse.json';
  8. import { traceFrameFields, zipkinResponse } from './utils/testData';
  9. jest.mock('@grafana/runtime', () => ({
  10. ...(jest.requireActual('@grafana/runtime') as unknown as object),
  11. getBackendSrv: () => backendSrv,
  12. }));
  13. describe('ZipkinDatasource', () => {
  14. describe('query', () => {
  15. const templateSrv: TemplateSrv = {
  16. replace: jest.fn(),
  17. getVariables: jest.fn(),
  18. containsTemplate: jest.fn(),
  19. updateTimeRange: jest.fn(),
  20. };
  21. it('runs query', async () => {
  22. setupBackendSrv(zipkinResponse);
  23. const ds = new ZipkinDatasource(defaultSettings, templateSrv);
  24. await expect(ds.query({ targets: [{ query: '12345' }] } as any)).toEmitValuesWith((val) => {
  25. expect(val[0].data[0].fields).toMatchObject(traceFrameFields);
  26. });
  27. });
  28. it('runs query with traceId that includes special characters', async () => {
  29. setupBackendSrv(zipkinResponse);
  30. const ds = new ZipkinDatasource(defaultSettings, templateSrv);
  31. await expect(ds.query({ targets: [{ query: 'a/b' }] } as any)).toEmitValuesWith((val) => {
  32. expect(val[0].data[0].fields).toMatchObject(traceFrameFields);
  33. });
  34. });
  35. it('should handle json file upload', async () => {
  36. const ds = new ZipkinDatasource(defaultSettings);
  37. ds.uploadedJson = JSON.stringify(mockJson);
  38. const response = await lastValueFrom(
  39. ds.query({
  40. targets: [{ queryType: 'upload', refId: 'A' }],
  41. } as any)
  42. );
  43. const field = response.data[0].fields[0];
  44. expect(field.name).toBe('traceID');
  45. expect(field.type).toBe(FieldType.string);
  46. expect(field.values.length).toBe(3);
  47. });
  48. it('should fail on invalid json file upload', async () => {
  49. const ds = new ZipkinDatasource(defaultSettings);
  50. ds.uploadedJson = JSON.stringify({ key: 'value', arr: [] });
  51. const response = await lastValueFrom(
  52. ds.query({
  53. targets: [{ queryType: 'upload', refId: 'A' }],
  54. } as any)
  55. );
  56. expect(response.error?.message).toBeDefined();
  57. expect(response.data.length).toBe(0);
  58. });
  59. });
  60. describe('metadataRequest', () => {
  61. it('runs query', async () => {
  62. setupBackendSrv(['service 1', 'service 2']);
  63. const ds = new ZipkinDatasource(defaultSettings);
  64. const response = await ds.metadataRequest('/api/v2/services');
  65. expect(response).toEqual(['service 1', 'service 2']);
  66. });
  67. });
  68. });
  69. function setupBackendSrv(response: any) {
  70. const defaultMock = () => of(createFetchResponse(response));
  71. const fetchMock = jest.spyOn(backendSrv, 'fetch');
  72. fetchMock.mockImplementation(defaultMock);
  73. }
  74. const defaultSettings: DataSourceInstanceSettings = {
  75. id: 1,
  76. uid: '1',
  77. type: 'tracing',
  78. name: 'zipkin',
  79. meta: {} as any,
  80. jsonData: {},
  81. access: 'proxy',
  82. };