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

193 lines
6.0 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';
jest.mock('../datasource');
jest.mock('app/core/services/backend_srv');
describe('Prometheus editor completer', function() {
2017-10-07 22:05:31 +02:00
function getSessionStub(data) {
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
};
}
2017-10-07 22:05:31 +02:00
let editor = {};
2018-06-29 13:37:21 +02:00
2018-06-29 13:44:11 +02:00
let backendSrv = <BackendSrv>{};
let datasourceStub = new PrometheusDatasource({}, {}, backendSrv, {}, {});
datasourceStub.performInstantQuery = jest.fn(() =>
Promise.resolve({
data: {
data: {
result: [
{
metric: {
job: 'node',
instance: 'localhost:9100',
2018-06-29 13:37:21 +02:00
},
},
2018-06-29 13:44:11 +02:00
],
},
},
})
);
2018-06-29 13:37:21 +02:00
datasourceStub.performSuggestQuery = jest.fn(() => Promise.resolve(['node_cpu']));
let templateSrv = {
variables: [
{
name: 'var_name',
options: [{ text: 'foo', value: 'foo', selected: false }, { text: 'bar', value: 'bar', selected: true }],
},
],
};
let 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, res) => {
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, res) => {
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, res) => {
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, res) => {
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, res) => {
2018-06-29 13:37:21 +02:00
expect(res[0].meta).toEqual('label name');
});
});
});
});