| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- import { PromCompleter } from '../completer';
- import { PrometheusDatasource } from '../datasource';
- 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';
- jest.mock('../datasource');
- jest.mock('@grafana/ui');
- describe('Prometheus editor completer', () => {
- function getSessionStub(data: any) {
- return {
- getTokenAt: jest.fn(() => data.currentToken),
- getTokens: jest.fn(() => data.tokens),
- getLine: jest.fn(() => data.line),
- };
- }
- const editor = {};
- const backendSrv = {} as BackendSrv;
- const datasourceStub = new PrometheusDatasource(
- {} as DataSourceInstanceSettings<PromOptions>,
- {} as IQService,
- backendSrv,
- {} as TemplateSrv,
- {} as TimeSrv
- );
- datasourceStub.metadataRequest = jest.fn(() =>
- Promise.resolve({ data: { data: [{ metric: { job: 'node', instance: 'localhost:9100' } }] } })
- );
- datasourceStub.getTimeRange = jest.fn(() => {
- return { start: 1514732400, end: 1514818800 };
- });
- 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', () => {
- 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: any, res: any) => {
- 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: any, res: any) => {
- 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: any, res: any) => {
- 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: any, res: any) => {
- 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: any, res: any) => {
- expect(res[0].meta).toEqual('label name');
- });
- });
- });
- });
|