completer_specs.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import {describe, it, sinon, expect} from 'test/lib/common';
  2. import {PromCompleter} from '../completer';
  3. import {PrometheusDatasource} from '../datasource';
  4. describe('Prometheus editor completer', function() {
  5. let sessionData = {
  6. currentToken: {},
  7. tokens: [],
  8. line: ''
  9. };
  10. let session = {
  11. getTokenAt: sinon.stub().returns(sessionData.currentToken),
  12. getTokens: sinon.stub().returns(sessionData.tokens),
  13. getLine: sinon.stub().returns(sessionData.line),
  14. };
  15. let editor = { session: session };
  16. let datasourceStub = <PrometheusDatasource>{
  17. performInstantQuery: sinon.stub().withArgs({ expr: '{__name__="node_cpu"' }).returns(Promise.resolve(
  18. [
  19. {
  20. metric: {
  21. job: 'node',
  22. instance: 'localhost:9100'
  23. }
  24. }
  25. ]
  26. )),
  27. performSuggestQuery: sinon.stub().withArgs('node', true).returns(Promise.resolve(
  28. [
  29. 'node_cpu'
  30. ]
  31. ))
  32. };
  33. let completer = new PromCompleter(datasourceStub);
  34. describe("When inside brackets", () => {
  35. it("Should return range vectors", () => {
  36. completer.getCompletions(editor, session, { row: 0, column: 10 }, '[', (s, res) => {
  37. expect(res[0]).to.eql({caption: '1s', value: '[1s', meta: 'range vector'});
  38. });
  39. });
  40. });
  41. describe("When inside label matcher, and located at label name", () => {
  42. sessionData = {
  43. currentToken: { type: 'entity.name.tag', value: 'j', index: 2, start: 9 },
  44. tokens: [
  45. { type: 'identifier', value: 'node_cpu' },
  46. { type: 'paren.lparen', value: '{' },
  47. { type: 'entity.name.tag', value: 'j', index: 2, start: 9 },
  48. { type: 'paren.rparen', value: '}' }
  49. ],
  50. line: 'node_cpu{j}'
  51. };
  52. it("Should return label name list", () => {
  53. completer.getCompletions(editor, session, { row: 0, column: 10 }, 'j', (s, res) => {
  54. expect(res[0]).to.eql({caption: 'job', value: 'job', meta: 'label name'});
  55. });
  56. });
  57. });
  58. describe("When inside label matcher, and located at label name with __name__ match", () => {
  59. sessionData = {
  60. currentToken: { type: 'entity.name.tag', value: 'j', index: 5, start: 22 },
  61. tokens: [
  62. { type: 'paren.lparen', value: '{' },
  63. { type: 'entity.name.tag', value: '__name__' },
  64. { type: 'keyword.operator', value: '=~' },
  65. { type: 'string.quoted', value: '"node_cpu"' },
  66. { type: 'punctuation.operator', value: ',' },
  67. { type: 'entity.name.tag', value: 'j', 'index': 5, 'start': 22 },
  68. { type: 'paren.rparen', value: '}' }
  69. ],
  70. line: '{__name__=~"node_cpu",j}'
  71. };
  72. it("Should return label name list", () => {
  73. completer.getCompletions(editor, session, { row: 0, column: 23 }, 'j', (s, res) => {
  74. expect(res[0]).to.eql({caption: 'job', value: 'job', meta: 'label name'});
  75. });
  76. });
  77. });
  78. describe("When inside label matcher, and located at label value", () => {
  79. sessionData = {
  80. currentToken: { type: 'string.quoted', value: '"n"', index: 4, start: 13 },
  81. tokens: [
  82. { type: 'identifier', value: 'node_cpu' },
  83. { type: 'paren.lparen', value: '{' },
  84. { type: 'entity.name.tag', value: 'job' },
  85. { type: 'keyword.operator', value: '=' },
  86. { type: 'string.quoted', value: '"n"', index: 4, start: 13 },
  87. { type: 'paren.rparen', value: '}' }
  88. ],
  89. line: 'node_cpu{job="n"}'
  90. };
  91. it("Should return label value list", () => {
  92. completer.getCompletions(editor, session, { row: 0, column: 15 }, 'n', (s, res) => {
  93. expect(res[0]).to.eql({caption: 'node', value: 'node', meta: 'label value'});
  94. });
  95. });
  96. });
  97. });