completer_specs.ts 3.8 KB

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