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'); {
target: 'prod1.count',
datapoints: [
[10, 1],
[12, 1],
],
},
];
beforeEach(() => { describe('opentsdb', () => {
function getTestcontext({ data = metricFindQueryData }: { data?: any } = {}) {
jest.clearAllMocks(); jest.clearAllMocks();
}); const fetchMock = jest.spyOn(backendSrv, 'fetch');
fetchMock.mockImplementation(() => of(createFetchResponse(data)));
const ctx = { const instanceSettings = { url: '', jsonData: { tsdbVersion: 1 } };
ds: {}, const replace = jest.fn(value => value);
templateSrv: { const templateSrv: any = {
replace: (str: string) => str, replace,
}, };
} as any;
const instanceSettings = { url: '', jsonData: { tsdbVersion: 1 } };
beforeEach(() => { const ds = new OpenTsDatasource(instanceSettings, templateSrv);
ctx.ctrl = new OpenTsDatasource(instanceSettings, ctx.templateSrv);
}); return { ds, templateSrv, fetchMock };
}
describe('When performing metricFindQuery', () => { describe('When performing metricFindQuery', () => {
let results: any; it('metrics() should generate api suggest query', async () => {
let requestOptions: any; const { ds, fetchMock } = getTestcontext();
beforeEach(async () => { const results = await ds.metricFindQuery('metrics(pew)');
datasourceRequestMock.mockImplementation(
await ((options: any) => {
requestOptions = options;
return Promise.resolve({
data: [
{
target: 'prod1.count',
datapoints: [
[10, 1],
[12, 1],
],
},
],
});
})
);
});
it('metrics() should generate api suggest query', () => { expect(fetchMock).toHaveBeenCalledTimes(1);
ctx.ctrl.metricFindQuery('metrics(pew)').then((data: any) => { expect(fetchMock.mock.calls[0][0].url).toBe('/api/suggest');
results = data; expect(fetchMock.mock.calls[0][0].params?.type).toBe('metrics');
}); expect(fetchMock.mock.calls[0][0].params?.q).toBe('pew');
expect(requestOptions.url).toBe('/api/suggest');
expect(requestOptions.params.type).toBe('metrics');
expect(requestOptions.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;
}); const results = await ds.metricFindQuery('tag_names(cpu)');
expect(requestOptions.url).toBe('/api/search/lookup');
expect(requestOptions.params.m).toBe('cpu');
});
it('tag_values(cpu, test) should generate lookup query', () => { expect(fetchMock).toHaveBeenCalledTimes(1);
ctx.ctrl.metricFindQuery('tag_values(cpu, hostname)').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=*}');
}); });
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)').then((data: any) => { const { ds, fetchMock } = getTestcontext();
results = data;
}); const results = await ds.metricFindQuery('tag_values(cpu, hostname)');
expect(requestOptions.url).toBe('/api/search/lookup');
expect(requestOptions.params.m).toBe('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=*}');
expect(results).not.toBe(null);
}); });
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;
}); const results = await ds.metricFindQuery('tag_values(cpu, hostname, env=$env)');
expect(requestOptions.url).toBe('/api/search/lookup');
expect(requestOptions.params.m).toBe('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}');
expect(results).not.toBe(null);
}); });
it('suggest_tagk() should generate api suggest query', () => { it('tag_values(cpu, test) should generate lookup query', async () => {
ctx.ctrl.metricFindQuery('suggest_tagk(foo)').then((data: any) => { const { ds, fetchMock } = getTestcontext();
results = data;
}); const results = await ds.metricFindQuery('tag_values(cpu, hostname, env=$env, region=$region)');
expect(requestOptions.url).toBe('/api/suggest');
expect(requestOptions.params.type).toBe('tagk'); expect(fetchMock).toHaveBeenCalledTimes(1);
expect(requestOptions.params.q).toBe('foo'); 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_tagv() should generate api suggest query', () => { it('suggest_tagk() should generate api suggest query', async () => {
ctx.ctrl.metricFindQuery('suggest_tagv(bar)').then((data: any) => { const { ds, fetchMock } = getTestcontext();
results = data;
}); const results = await ds.metricFindQuery('suggest_tagk(foo)');
expect(requestOptions.url).toBe('/api/suggest');
expect(requestOptions.params.type).toBe('tagv'); expect(fetchMock).toHaveBeenCalledTimes(1);
expect(requestOptions.params.q).toBe('bar'); 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