2017-12-21 08:39:31 +01:00
|
|
|
import { describe, it, sinon, expect } from 'test/lib/common';
|
2017-09-12 11:05:32 +02:00
|
|
|
|
2017-12-21 08:39:31 +01:00
|
|
|
import { PromCompleter } from '../completer';
|
|
|
|
|
import { PrometheusDatasource } from '../datasource';
|
2017-09-12 11:05:32 +02:00
|
|
|
|
2017-12-21 08:39:31 +01:00
|
|
|
describe('Prometheus editor completer', function() {
|
2017-10-07 22:05:31 +02:00
|
|
|
function getSessionStub(data) {
|
|
|
|
|
return {
|
|
|
|
|
getTokenAt: sinon.stub().returns(data.currentToken),
|
|
|
|
|
getTokens: sinon.stub().returns(data.tokens),
|
2017-12-21 08:39:31 +01:00
|
|
|
getLine: sinon.stub().returns(data.line),
|
2017-10-07 22:05:31 +02:00
|
|
|
};
|
|
|
|
|
}
|
2017-09-12 11:05:32 +02:00
|
|
|
|
2017-10-07 22:05:31 +02:00
|
|
|
let editor = {};
|
2017-09-12 20:05:07 +09:00
|
|
|
let datasourceStub = <PrometheusDatasource>{
|
2017-10-07 22:05:31 +02:00
|
|
|
performInstantQuery: sinon
|
|
|
|
|
.stub()
|
2017-12-19 16:06:54 +01:00
|
|
|
.withArgs({ expr: '{__name__="node_cpu"' })
|
2017-10-07 22:05:31 +02:00
|
|
|
.returns(
|
|
|
|
|
Promise.resolve({
|
|
|
|
|
data: {
|
|
|
|
|
data: {
|
|
|
|
|
result: [
|
|
|
|
|
{
|
|
|
|
|
metric: {
|
2017-12-21 08:39:31 +01:00
|
|
|
job: 'node',
|
|
|
|
|
instance: 'localhost:9100',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
},
|
2017-12-19 16:06:54 +01:00
|
|
|
})
|
2017-10-07 22:05:31 +02:00
|
|
|
),
|
|
|
|
|
performSuggestQuery: sinon
|
|
|
|
|
.stub()
|
2017-12-21 08:39:31 +01:00
|
|
|
.withArgs('node', true)
|
|
|
|
|
.returns(Promise.resolve(['node_cpu'])),
|
2017-09-12 20:05:07 +09:00
|
|
|
};
|
2017-09-12 11:05:32 +02:00
|
|
|
|
2017-10-07 22:05:31 +02:00
|
|
|
let completer = new PromCompleter(datasourceStub);
|
2017-09-12 11:05:32 +02:00
|
|
|
|
2017-12-21 08:39:31 +01:00
|
|
|
describe('When inside brackets', () => {
|
|
|
|
|
it('Should return range vectors', () => {
|
2017-10-07 22:05:31 +02:00
|
|
|
const session = getSessionStub({
|
2017-12-21 08:39:31 +01:00
|
|
|
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
|
|
|
});
|
2017-10-18 13:57:25 +02:00
|
|
|
|
2017-12-21 08:39:31 +01:00
|
|
|
return completer.getCompletions(editor, session, { row: 0, column: 10 }, '[', (s, res) => {
|
|
|
|
|
expect(res[0].caption).to.eql('1s');
|
|
|
|
|
expect(res[0].value).to.eql('[1s');
|
|
|
|
|
expect(res[0].meta).to.eql('range vector');
|
|
|
|
|
});
|
2017-09-12 11:05:32 +02:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2017-12-21 08:39:31 +01:00
|
|
|
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({
|
2017-12-19 16:06:54 +01:00
|
|
|
currentToken: {
|
2017-12-21 08:39:31 +01:00
|
|
|
type: 'entity.name.tag',
|
|
|
|
|
value: 'j',
|
2017-12-19 16:06:54 +01:00
|
|
|
index: 2,
|
2017-12-21 08:39:31 +01:00
|
|
|
start: 9,
|
2017-12-19 16:06:54 +01:00
|
|
|
},
|
2017-10-07 22:05:31 +02:00
|
|
|
tokens: [
|
2017-12-21 08:39:31 +01:00
|
|
|
{ type: 'identifier', value: 'node_cpu' },
|
|
|
|
|
{ type: 'paren.lparen', value: '{' },
|
|
|
|
|
{ type: 'entity.name.tag', value: 'j', index: 2, start: 9 },
|
|
|
|
|
{ type: 'paren.rparen', value: '}' },
|
2017-10-07 22:05:31 +02:00
|
|
|
],
|
2017-12-21 08:39:31 +01:00
|
|
|
line: 'node_cpu{j}',
|
2017-10-07 22:05:31 +02:00
|
|
|
});
|
2017-09-12 20:05:07 +09:00
|
|
|
|
2017-12-21 08:39:31 +01:00
|
|
|
return completer.getCompletions(editor, session, { row: 0, column: 10 }, 'j', (s, res) => {
|
|
|
|
|
expect(res[0].meta).to.eql('label name');
|
|
|
|
|
});
|
2017-09-12 20:05:07 +09:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2017-12-21 08:39:31 +01: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({
|
2017-12-19 16:06:54 +01:00
|
|
|
currentToken: {
|
2017-12-21 08:39:31 +01:00
|
|
|
type: 'entity.name.tag',
|
|
|
|
|
value: 'j',
|
2017-12-19 16:06:54 +01:00
|
|
|
index: 5,
|
2017-12-21 08:39:31 +01:00
|
|
|
start: 22,
|
2017-12-19 16:06:54 +01:00
|
|
|
},
|
2017-10-07 22:05:31 +02:00
|
|
|
tokens: [
|
2017-12-21 08:39:31 +01:00
|
|
|
{ type: 'paren.lparen', value: '{' },
|
|
|
|
|
{ type: 'entity.name.tag', value: '__name__' },
|
|
|
|
|
{ type: 'keyword.operator', value: '=~' },
|
|
|
|
|
{ type: 'string.quoted', value: '"node_cpu"' },
|
|
|
|
|
{ type: 'punctuation.operator', value: ',' },
|
|
|
|
|
{ type: 'entity.name.tag', value: 'j', index: 5, start: 22 },
|
|
|
|
|
{ type: 'paren.rparen', value: '}' },
|
2017-10-07 22:05:31 +02:00
|
|
|
],
|
2017-12-21 08:39:31 +01:00
|
|
|
line: '{__name__=~"node_cpu",j}',
|
2017-10-07 22:05:31 +02:00
|
|
|
});
|
2017-09-12 20:05:07 +09:00
|
|
|
|
2017-12-21 08:39:31 +01:00
|
|
|
return completer.getCompletions(editor, session, { row: 0, column: 23 }, 'j', (s, res) => {
|
|
|
|
|
expect(res[0].meta).to.eql('label name');
|
|
|
|
|
});
|
2017-09-12 20:05:07 +09:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2017-12-21 08:39:31 +01: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({
|
2017-12-19 16:06:54 +01:00
|
|
|
currentToken: {
|
2017-12-21 08:39:31 +01:00
|
|
|
type: 'string.quoted',
|
2017-12-19 16:06:54 +01:00
|
|
|
value: '"n"',
|
|
|
|
|
index: 4,
|
2017-12-21 08:39:31 +01:00
|
|
|
start: 13,
|
2017-12-19 16:06:54 +01:00
|
|
|
},
|
2017-10-07 22:05:31 +02:00
|
|
|
tokens: [
|
2017-12-21 08:39:31 +01:00
|
|
|
{ type: 'identifier', value: 'node_cpu' },
|
|
|
|
|
{ type: 'paren.lparen', value: '{' },
|
|
|
|
|
{ type: 'entity.name.tag', value: 'job' },
|
|
|
|
|
{ type: 'keyword.operator', value: '=' },
|
|
|
|
|
{ type: 'string.quoted', value: '"n"', index: 4, start: 13 },
|
|
|
|
|
{ type: 'paren.rparen', value: '}' },
|
2017-10-07 22:05:31 +02:00
|
|
|
],
|
2017-12-21 08:39:31 +01:00
|
|
|
line: 'node_cpu{job="n"}',
|
2017-10-07 22:05:31 +02:00
|
|
|
});
|
2017-09-12 20:05:07 +09:00
|
|
|
|
2017-12-21 08:39:31 +01:00
|
|
|
return completer.getCompletions(editor, session, { row: 0, column: 15 }, 'n', (s, res) => {
|
|
|
|
|
expect(res[0].meta).to.eql('label value');
|
|
|
|
|
});
|
2017-09-12 20:05:07 +09:00
|
|
|
});
|
|
|
|
|
});
|
2017-09-12 11:05:32 +02:00
|
|
|
});
|