Commit b0946211 by Hugo Häggmark Committed by GitHub

OpenTSDB: Support request cancellation properly (#29992)

parent 6dfa9b48
import OpenTsDatasource from '../datasource'; import OpenTsDatasource from '../datasource';
import { backendSrv } from 'app/core/services/backend_srv'; // will use the version in __mocks__ import { backendSrv } from 'app/core/services/backend_srv'; // will use the version in __mocks__
import { OpenTsdbQuery } from '../types'; import { OpenTsdbQuery } from '../types';
import { createFetchResponse } from '../../../../../test/helpers/createFetchResponse';
import { of } from 'rxjs';
jest.mock('@grafana/runtime', () => ({ jest.mock('@grafana/runtime', () => ({
...((jest.requireActual('@grafana/runtime') as unknown) as object), ...((jest.requireActual('@grafana/runtime') as unknown) as object),
getBackendSrv: () => backendSrv, getBackendSrv: () => backendSrv,
})); }));
describe('opentsdb', () => { const metricFindQueryData = [
const datasourceRequestMock = jest.spyOn(backendSrv, 'datasourceRequest');
beforeEach(() => {
jest.clearAllMocks();
});
const ctx = {
ds: {},
templateSrv: {
replace: (str: string) => str,
},
} as any;
const instanceSettings = { url: '', jsonData: { tsdbVersion: 1 } };
beforeEach(() => {
ctx.ctrl = new OpenTsDatasource(instanceSettings, ctx.templateSrv);
});
describe('When performing metricFindQuery', () => {
let results: any;
let requestOptions: any;
beforeEach(async () => {
datasourceRequestMock.mockImplementation(
await ((options: any) => {
requestOptions = options;
return Promise.resolve({
data: [
{ {
target: 'prod1.count', target: 'prod1.count',
datapoints: [ datapoints: [
...@@ -43,99 +17,125 @@ describe('opentsdb', () => { ...@@ -43,99 +17,125 @@ describe('opentsdb', () => {
[12, 1], [12, 1],
], ],
}, },
], ];
});
})
);
});
it('metrics() should generate api suggest query', () => { describe('opentsdb', () => {
ctx.ctrl.metricFindQuery('metrics(pew)').then((data: any) => { function getTestcontext({ data = metricFindQueryData }: { data?: any } = {}) {
results = data; jest.clearAllMocks();
}); const fetchMock = jest.spyOn(backendSrv, 'fetch');
expect(requestOptions.url).toBe('/api/suggest'); fetchMock.mockImplementation(() => of(createFetchResponse(data)));
expect(requestOptions.params.type).toBe('metrics');
expect(requestOptions.params.q).toBe('pew'); const instanceSettings = { url: '', jsonData: { tsdbVersion: 1 } };
const replace = jest.fn(value => value);
const templateSrv: any = {
replace,
};
const ds = new OpenTsDatasource(instanceSettings, templateSrv);
return { ds, templateSrv, fetchMock };
}
describe('When performing metricFindQuery', () => {
it('metrics() should generate api suggest query', async () => {
const { ds, fetchMock } = getTestcontext();
const results = await ds.metricFindQuery('metrics(pew)');
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock.mock.calls[0][0].url).toBe('/api/suggest');
expect(fetchMock.mock.calls[0][0].params?.type).toBe('metrics');
expect(fetchMock.mock.calls[0][0].params?.q).toBe('pew');
expect(results).not.toBe(null); expect(results).not.toBe(null);
}); });
it('tag_names(cpu) should generate lookup query', () => { it('tag_names(cpu) should generate lookup query', async () => {
ctx.ctrl.metricFindQuery('tag_names(cpu)').then((data: any) => { const { ds, fetchMock } = getTestcontext();
results = data;
});
expect(requestOptions.url).toBe('/api/search/lookup');
expect(requestOptions.params.m).toBe('cpu');
});
it('tag_values(cpu, test) should generate lookup query', () => { const results = await ds.metricFindQuery('tag_names(cpu)');
ctx.ctrl.metricFindQuery('tag_values(cpu, hostname)').then((data: any) => {
results = data;
});
expect(requestOptions.url).toBe('/api/search/lookup');
expect(requestOptions.params.m).toBe('cpu{hostname=*}');
});
it('tag_values(cpu, test) should generate lookup query', () => { expect(fetchMock).toHaveBeenCalledTimes(1);
ctx.ctrl.metricFindQuery('tag_values(cpu, hostname, env=$env)').then((data: any) => { expect(fetchMock.mock.calls[0][0].url).toBe('/api/search/lookup');
results = data; expect(fetchMock.mock.calls[0][0].params?.m).toBe('cpu');
}); expect(results).not.toBe(null);
expect(requestOptions.url).toBe('/api/search/lookup');
expect(requestOptions.params.m).toBe('cpu{hostname=*,env=$env}');
}); });
it('tag_values(cpu, test) should generate lookup query', () => { it('tag_values(cpu, test) should generate lookup query', async () => {
ctx.ctrl.metricFindQuery('tag_values(cpu, hostname, env=$env, region=$region)').then((data: any) => { const { ds, fetchMock } = getTestcontext();
results = data;
});
expect(requestOptions.url).toBe('/api/search/lookup');
expect(requestOptions.params.m).toBe('cpu{hostname=*,env=$env,region=$region}');
});
it('suggest_tagk() should generate api suggest query', () => { const results = await ds.metricFindQuery('tag_values(cpu, hostname)');
ctx.ctrl.metricFindQuery('suggest_tagk(foo)').then((data: any) => {
results = data; expect(fetchMock).toHaveBeenCalledTimes(1);
}); expect(fetchMock.mock.calls[0][0].url).toBe('/api/search/lookup');
expect(requestOptions.url).toBe('/api/suggest'); expect(fetchMock.mock.calls[0][0].params?.m).toBe('cpu{hostname=*}');
expect(requestOptions.params.type).toBe('tagk'); expect(results).not.toBe(null);
expect(requestOptions.params.q).toBe('foo');
}); });
it('suggest_tagv() should generate api suggest query', () => { it('tag_values(cpu, test) should generate lookup query', async () => {
ctx.ctrl.metricFindQuery('suggest_tagv(bar)').then((data: any) => { const { ds, fetchMock } = getTestcontext();
results = data;
const results = await ds.metricFindQuery('tag_values(cpu, hostname, env=$env)');
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock.mock.calls[0][0].url).toBe('/api/search/lookup');
expect(fetchMock.mock.calls[0][0].params?.m).toBe('cpu{hostname=*,env=$env}');
expect(results).not.toBe(null);
}); });
expect(requestOptions.url).toBe('/api/suggest');
expect(requestOptions.params.type).toBe('tagv'); it('tag_values(cpu, test) should generate lookup query', async () => {
expect(requestOptions.params.q).toBe('bar'); const { ds, fetchMock } = getTestcontext();
const results = await ds.metricFindQuery('tag_values(cpu, hostname, env=$env, region=$region)');
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock.mock.calls[0][0].url).toBe('/api/search/lookup');
expect(fetchMock.mock.calls[0][0].params?.m).toBe('cpu{hostname=*,env=$env,region=$region}');
expect(results).not.toBe(null);
}); });
it('suggest_tagk() should generate api suggest query', async () => {
const { ds, fetchMock } = getTestcontext();
const results = await ds.metricFindQuery('suggest_tagk(foo)');
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock.mock.calls[0][0].url).toBe('/api/suggest');
expect(fetchMock.mock.calls[0][0].params?.type).toBe('tagk');
expect(fetchMock.mock.calls[0][0].params?.q).toBe('foo');
expect(results).not.toBe(null);
}); });
describe('When interpolating variables', () => { it('suggest_tagv() should generate api suggest query', async () => {
beforeEach(() => { const { ds, fetchMock } = getTestcontext();
jest.clearAllMocks();
ctx.mockedTemplateSrv = { const results = await ds.metricFindQuery('suggest_tagv(bar)');
replace: jest.fn(),
};
ctx.ds = new OpenTsDatasource(instanceSettings, ctx.mockedTemplateSrv); expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock.mock.calls[0][0].url).toBe('/api/suggest');
expect(fetchMock.mock.calls[0][0].params?.type).toBe('tagv');
expect(fetchMock.mock.calls[0][0].params?.q).toBe('bar');
expect(results).not.toBe(null);
});
}); });
describe('When interpolating variables', () => {
it('should return an empty array if no queries are provided', () => { it('should return an empty array if no queries are provided', () => {
expect(ctx.ds.interpolateVariablesInQueries([], {})).toHaveLength(0); const { ds } = getTestcontext();
expect(ds.interpolateVariablesInQueries([], {})).toHaveLength(0);
}); });
it('should replace correct variables', () => { it('should replace correct variables', () => {
const { ds, templateSrv } = getTestcontext();
const variableName = 'someVar'; const variableName = 'someVar';
const logQuery: OpenTsdbQuery = { const logQuery: OpenTsdbQuery = {
refId: 'someRefId', refId: 'someRefId',
metric: `$${variableName}`, metric: `$${variableName}`,
}; };
ctx.ds.interpolateVariablesInQueries([logQuery], {}); ds.interpolateVariablesInQueries([logQuery], {});
expect(ctx.mockedTemplateSrv.replace).toHaveBeenCalledWith(`$${variableName}`, {}); expect(templateSrv.replace).toHaveBeenCalledWith('$someVar', {});
expect(ctx.mockedTemplateSrv.replace).toHaveBeenCalledTimes(1); expect(templateSrv.replace).toHaveBeenCalledTimes(1);
}); });
}); });
}); });
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment