Files
grafana/public/app/plugins/datasource/prometheus/specs/completer.test.ts

194 lines
6.4 KiB
TypeScript
Raw Normal View History

import { PromCompleter } from '../completer';
import { PrometheusDatasource } from '../datasource';
2018-06-29 13:37:21 +02:00
import { BackendSrv } from 'app/core/services/backend_srv';
import { DataSourceInstanceSettings } from '@grafana/ui';
import { PromOptions } from '../types';
import { TemplateSrv } from 'app/features/templating/template_srv';
import { TimeSrv } from 'app/features/dashboard/services/TimeSrv';
import { IQService } from 'angular';
2018-06-29 13:37:21 +02:00
jest.mock('../datasource');
jest.mock('@grafana/ui');
describe('Prometheus editor completer', () => {
function getSessionStub(data: any) {
2017-10-07 22:05:31 +02:00
return {
2018-06-29 13:44:11 +02:00
getTokenAt: jest.fn(() => data.currentToken),
getTokens: jest.fn(() => data.tokens),
getLine: jest.fn(() => data.line),
2017-10-07 22:05:31 +02:00
};
}
const editor = {};
2018-06-29 13:37:21 +02:00
const backendSrv = {} as BackendSrv;
const datasourceStub = new PrometheusDatasource(
{} as DataSourceInstanceSettings<PromOptions>,
{} as IQService,
backendSrv,
{} as TemplateSrv,
{} as TimeSrv
);
2018-06-29 13:44:11 +02:00
datasourceStub.metadataRequest = jest.fn(() =>
2018-07-27 11:39:00 +09:00
Promise.resolve({ data: { data: [{ metric: { job: 'node', instance: 'localhost:9100' } }] } })
2018-06-29 13:44:11 +02:00
);
2018-07-27 11:39:00 +09:00
datasourceStub.getTimeRange = jest.fn(() => {
return { start: 1514732400, end: 1514818800 };
});
2018-06-29 13:37:21 +02:00
datasourceStub.performSuggestQuery = jest.fn(() => Promise.resolve(['node_cpu']));
const templateSrv: TemplateSrv = ({
variables: [
{
name: 'var_name',
options: [{ text: 'foo', value: 'foo', selected: false }, { text: 'bar', value: 'bar', selected: true }],
},
],
} as any) as TemplateSrv;
const completer = new PromCompleter(datasourceStub, templateSrv);
describe('When inside brackets', () => {
it('Should return range vectors', () => {
2017-10-07 22:05:31 +02:00
const session = getSessionStub({
currentToken: { type: 'paren.lparen', value: '[', index: 2, start: 9 },
tokens: [{ type: 'identifier', value: 'node_cpu' }, { type: 'paren.lparen', value: '[' }],
line: 'node_cpu[',
2017-10-07 22:05:31 +02:00
});
return completer.getCompletions(editor, session, { row: 0, column: 10 }, '[', (s: any, res: any) => {
2018-06-29 13:37:21 +02:00
expect(res[0].caption).toEqual('$__interval');
expect(res[0].value).toEqual('[$__interval');
expect(res[0].meta).toEqual('range vector');
});
});
});
describe('When inside label matcher, and located at label name', () => {
it('Should return label name list', () => {
2017-10-07 22:05:31 +02:00
const session = getSessionStub({
currentToken: {
type: 'entity.name.tag.label-matcher',
value: 'j',
index: 2,
start: 9,
},
2017-10-07 22:05:31 +02:00
tokens: [
{ type: 'identifier', value: 'node_cpu' },
{ type: 'paren.lparen.label-matcher', value: '{' },
{
type: 'entity.name.tag.label-matcher',
value: 'j',
index: 2,
start: 9,
},
{ type: 'paren.rparen.label-matcher', value: '}' },
2017-10-07 22:05:31 +02:00
],
line: 'node_cpu{j}',
2017-10-07 22:05:31 +02:00
});
2017-09-12 20:05:07 +09:00
return completer.getCompletions(editor, session, { row: 0, column: 10 }, 'j', (s: any, res: any) => {
2018-06-29 13:37:21 +02:00
expect(res[0].meta).toEqual('label name');
});
2017-09-12 20:05:07 +09:00
});
});
describe('When inside label matcher, and located at label name with __name__ match', () => {
it('Should return label name list', () => {
2017-10-07 22:05:31 +02:00
const session = getSessionStub({
currentToken: {
type: 'entity.name.tag.label-matcher',
value: 'j',
index: 5,
start: 22,
},
2017-10-07 22:05:31 +02:00
tokens: [
{ type: 'paren.lparen.label-matcher', value: '{' },
{ type: 'entity.name.tag.label-matcher', value: '__name__' },
{ type: 'keyword.operator.label-matcher', value: '=~' },
{ type: 'string.quoted.label-matcher', value: '"node_cpu"' },
{ type: 'punctuation.operator.label-matcher', value: ',' },
{
type: 'entity.name.tag.label-matcher',
value: 'j',
index: 5,
start: 22,
},
{ type: 'paren.rparen.label-matcher', value: '}' },
2017-10-07 22:05:31 +02:00
],
line: '{__name__=~"node_cpu",j}',
2017-10-07 22:05:31 +02:00
});
2017-09-12 20:05:07 +09:00
return completer.getCompletions(editor, session, { row: 0, column: 23 }, 'j', (s: any, res: any) => {
2018-06-29 13:37:21 +02:00
expect(res[0].meta).toEqual('label name');
});
2017-09-12 20:05:07 +09:00
});
});
describe('When inside label matcher, and located at label value', () => {
it('Should return label value list', () => {
2017-10-07 22:05:31 +02:00
const session = getSessionStub({
currentToken: {
type: 'string.quoted.label-matcher',
value: '"n"',
index: 4,
start: 13,
},
2017-10-07 22:05:31 +02:00
tokens: [
{ type: 'identifier', value: 'node_cpu' },
{ type: 'paren.lparen.label-matcher', value: '{' },
{ type: 'entity.name.tag.label-matcher', value: 'job' },
{ type: 'keyword.operator.label-matcher', value: '=' },
{
type: 'string.quoted.label-matcher',
value: '"n"',
index: 4,
start: 13,
},
{ type: 'paren.rparen.label-matcher', value: '}' },
2017-10-07 22:05:31 +02:00
],
line: 'node_cpu{job="n"}',
2017-10-07 22:05:31 +02:00
});
2017-09-12 20:05:07 +09:00
return completer.getCompletions(editor, session, { row: 0, column: 15 }, 'n', (s: any, res: any) => {
2018-06-29 13:37:21 +02:00
expect(res[0].meta).toEqual('label value');
});
2017-09-12 20:05:07 +09:00
});
});
describe('When inside by', () => {
it('Should return label name list', () => {
const session = getSessionStub({
currentToken: {
type: 'entity.name.tag.label-list-matcher',
value: 'm',
index: 9,
start: 22,
},
tokens: [
{ type: 'paren.lparen', value: '(' },
{ type: 'keyword', value: 'count' },
{ type: 'paren.lparen', value: '(' },
{ type: 'identifier', value: 'node_cpu' },
{ type: 'paren.rparen', value: '))' },
{ type: 'text', value: ' ' },
{ type: 'keyword.control', value: 'by' },
{ type: 'text', value: ' ' },
{ type: 'paren.lparen.label-list-matcher', value: '(' },
{
type: 'entity.name.tag.label-list-matcher',
value: 'm',
index: 9,
start: 22,
},
{ type: 'paren.rparen.label-list-matcher', value: ')' },
],
line: '(count(node_cpu)) by (m)',
});
return completer.getCompletions(editor, session, { row: 0, column: 23 }, 'm', (s: any, res: any) => {
2018-06-29 13:37:21 +02:00
expect(res[0].meta).toEqual('label name');
});
});
});
});