datasource.test.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { of } from 'rxjs';
  2. import { backendSrv } from 'app/core/services/backend_srv'; // will use the version in __mocks__
  3. import { createFetchResponse } from '../../../../../test/helpers/createFetchResponse';
  4. import OpenTsDatasource from '../datasource';
  5. import { OpenTsdbQuery } from '../types';
  6. jest.mock('@grafana/runtime', () => ({
  7. ...(jest.requireActual('@grafana/runtime') as unknown as object),
  8. getBackendSrv: () => backendSrv,
  9. }));
  10. const metricFindQueryData = [
  11. {
  12. target: 'prod1.count',
  13. datapoints: [
  14. [10, 1],
  15. [12, 1],
  16. ],
  17. },
  18. ];
  19. describe('opentsdb', () => {
  20. function getTestcontext({ data = metricFindQueryData }: { data?: any } = {}) {
  21. jest.clearAllMocks();
  22. const fetchMock = jest.spyOn(backendSrv, 'fetch');
  23. fetchMock.mockImplementation(() => of(createFetchResponse(data)));
  24. const instanceSettings = { url: '', jsonData: { tsdbVersion: 1 } };
  25. const replace = jest.fn((value) => value);
  26. const templateSrv: any = {
  27. replace,
  28. };
  29. const ds = new OpenTsDatasource(instanceSettings, templateSrv);
  30. return { ds, templateSrv, fetchMock };
  31. }
  32. describe('When performing metricFindQuery', () => {
  33. it('metrics() should generate api suggest query', async () => {
  34. const { ds, fetchMock } = getTestcontext();
  35. const results = await ds.metricFindQuery('metrics(pew)');
  36. expect(fetchMock).toHaveBeenCalledTimes(1);
  37. expect(fetchMock.mock.calls[0][0].url).toBe('/api/suggest');
  38. expect(fetchMock.mock.calls[0][0].params?.type).toBe('metrics');
  39. expect(fetchMock.mock.calls[0][0].params?.q).toBe('pew');
  40. expect(results).not.toBe(null);
  41. });
  42. it('tag_names(cpu) should generate lookup query', async () => {
  43. const { ds, fetchMock } = getTestcontext();
  44. const results = await ds.metricFindQuery('tag_names(cpu)');
  45. expect(fetchMock).toHaveBeenCalledTimes(1);
  46. expect(fetchMock.mock.calls[0][0].url).toBe('/api/search/lookup');
  47. expect(fetchMock.mock.calls[0][0].params?.m).toBe('cpu');
  48. expect(results).not.toBe(null);
  49. });
  50. it('tag_values(cpu, test) should generate lookup query', async () => {
  51. const { ds, fetchMock } = getTestcontext();
  52. const results = await ds.metricFindQuery('tag_values(cpu, hostname)');
  53. expect(fetchMock).toHaveBeenCalledTimes(1);
  54. expect(fetchMock.mock.calls[0][0].url).toBe('/api/search/lookup');
  55. expect(fetchMock.mock.calls[0][0].params?.m).toBe('cpu{hostname=*}');
  56. expect(results).not.toBe(null);
  57. });
  58. it('tag_values(cpu, test) should generate lookup query', async () => {
  59. const { ds, fetchMock } = getTestcontext();
  60. const results = await ds.metricFindQuery('tag_values(cpu, hostname, env=$env)');
  61. expect(fetchMock).toHaveBeenCalledTimes(1);
  62. expect(fetchMock.mock.calls[0][0].url).toBe('/api/search/lookup');
  63. expect(fetchMock.mock.calls[0][0].params?.m).toBe('cpu{hostname=*,env=$env}');
  64. expect(results).not.toBe(null);
  65. });
  66. it('tag_values(cpu, test) should generate lookup query', async () => {
  67. const { ds, fetchMock } = getTestcontext();
  68. const results = await ds.metricFindQuery('tag_values(cpu, hostname, env=$env, region=$region)');
  69. expect(fetchMock).toHaveBeenCalledTimes(1);
  70. expect(fetchMock.mock.calls[0][0].url).toBe('/api/search/lookup');
  71. expect(fetchMock.mock.calls[0][0].params?.m).toBe('cpu{hostname=*,env=$env,region=$region}');
  72. expect(results).not.toBe(null);
  73. });
  74. it('suggest_tagk() should generate api suggest query', async () => {
  75. const { ds, fetchMock } = getTestcontext();
  76. const results = await ds.metricFindQuery('suggest_tagk(foo)');
  77. expect(fetchMock).toHaveBeenCalledTimes(1);
  78. expect(fetchMock.mock.calls[0][0].url).toBe('/api/suggest');
  79. expect(fetchMock.mock.calls[0][0].params?.type).toBe('tagk');
  80. expect(fetchMock.mock.calls[0][0].params?.q).toBe('foo');
  81. expect(results).not.toBe(null);
  82. });
  83. it('suggest_tagv() should generate api suggest query', async () => {
  84. const { ds, fetchMock } = getTestcontext();
  85. const results = await ds.metricFindQuery('suggest_tagv(bar)');
  86. expect(fetchMock).toHaveBeenCalledTimes(1);
  87. expect(fetchMock.mock.calls[0][0].url).toBe('/api/suggest');
  88. expect(fetchMock.mock.calls[0][0].params?.type).toBe('tagv');
  89. expect(fetchMock.mock.calls[0][0].params?.q).toBe('bar');
  90. expect(results).not.toBe(null);
  91. });
  92. });
  93. describe('When interpolating variables', () => {
  94. it('should return an empty array if no queries are provided', () => {
  95. const { ds } = getTestcontext();
  96. expect(ds.interpolateVariablesInQueries([], {})).toHaveLength(0);
  97. });
  98. it('should replace correct variables', () => {
  99. const { ds, templateSrv } = getTestcontext();
  100. const variableName = 'someVar';
  101. const logQuery: OpenTsdbQuery = {
  102. refId: 'someRefId',
  103. metric: `$${variableName}`,
  104. };
  105. ds.interpolateVariablesInQueries([logQuery], {});
  106. expect(templateSrv.replace).toHaveBeenCalledWith('$someVar', {});
  107. expect(templateSrv.replace).toHaveBeenCalledTimes(1);
  108. });
  109. });
  110. });