The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
grafana/public/app/plugins/datasource/prometheus/specs/completer.jest.ts

195 lines
6.0 KiB

import { PromCompleter } from '../completer';
import { PrometheusDatasource } from '../datasource';
import { BackendSrv } from 'app/core/services/backend_srv';
jest.mock('../datasource');
jest.mock('app/core/services/backend_srv');
describe('Prometheus editor completer', function() {
//beforeEach(ctx.providePhase(['templateSrv']));
function getSessionStub(data) {
return {
7 years ago
getTokenAt: jest.fn(() => data.currentToken),
getTokens: jest.fn(() => data.tokens),
getLine: jest.fn(() => data.line),
};
}
let editor = {};
7 years ago
let backendSrv = <BackendSrv>{};
let datasourceStub = new PrometheusDatasource({}, {}, backendSrv, {}, {});
datasourceStub.performInstantQuery = jest.fn(() =>
Promise.resolve({
data: {
data: {
result: [
{
metric: {
job: 'node',
instance: 'localhost:9100',
},
},
7 years ago
],
},
},
})
);
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', () => {
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[',
});
return completer.getCompletions(editor, session, { row: 0, column: 10 }, '[', (s, res) => {
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', () => {
const session = getSessionStub({
currentToken: {
type: 'entity.name.tag.label-matcher',
value: 'j',
index: 2,
start: 9,
},
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: '}' },
],
line: 'node_cpu{j}',
});
return completer.getCompletions(editor, session, { row: 0, column: 10 }, 'j', (s, res) => {
expect(res[0].meta).toEqual('label name');
});
});
});
describe('When inside label matcher, and located at label name with __name__ match', () => {
it('Should return label name list', () => {
const session = getSessionStub({
currentToken: {
type: 'entity.name.tag.label-matcher',
value: 'j',
index: 5,
start: 22,
},
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: '}' },
],
line: '{__name__=~"node_cpu",j}',
});
return completer.getCompletions(editor, session, { row: 0, column: 23 }, 'j', (s, res) => {
expect(res[0].meta).toEqual('label name');
});
});
});
describe('When inside label matcher, and located at label value', () => {
it('Should return label value list', () => {
const session = getSessionStub({
currentToken: {
type: 'string.quoted.label-matcher',
value: '"n"',
index: 4,
start: 13,
},
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: '}' },
],
line: 'node_cpu{job="n"}',
});
return completer.getCompletions(editor, session, { row: 0, column: 15 }, 'n', (s, res) => {
expect(res[0].meta).toEqual('label value');
});
});
});
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) => {
expect(res[0].meta).toEqual('label name');
});
});
});
});