completer_specs.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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: {type: 'paren.lparen', value: '[', index: 2, start: 9},
  43. tokens: [
  44. {type: 'identifier', value: 'node_cpu'},
  45. {type: 'paren.lparen', value: '['}
  46. ],
  47. line: 'node_cpu[',
  48. });
  49. return completer.getCompletions(editor, session, {row: 0, column: 10}, '[', (s, res) => {
  50. expect(res[0].caption).to.eql('1s');
  51. expect(res[0].value).to.eql('[1s');
  52. expect(res[0].meta).to.eql('range vector');
  53. });
  54. });
  55. });
  56. describe('When inside label matcher, and located at label name', () => {
  57. it('Should return label name list', () => {
  58. const session = getSessionStub({
  59. currentToken: {type: 'entity.name.tag', value: 'j', index: 2, start: 9},
  60. tokens: [
  61. {type: 'identifier', value: 'node_cpu'},
  62. {type: 'paren.lparen', value: '{'},
  63. {type: 'entity.name.tag', value: 'j', index: 2, start: 9},
  64. {type: 'paren.rparen', value: '}'},
  65. ],
  66. line: 'node_cpu{j}',
  67. });
  68. return completer.getCompletions(editor, session, {row: 0, column: 10}, 'j', (s, res) => {
  69. expect(res[0].meta).to.eql('label name');
  70. });
  71. });
  72. });
  73. describe('When inside label matcher, and located at label name with __name__ match', () => {
  74. it('Should return label name list', () => {
  75. const session = getSessionStub({
  76. currentToken: {type: 'entity.name.tag', value: 'j', index: 5, start: 22},
  77. tokens: [
  78. {type: 'paren.lparen', value: '{'},
  79. {type: 'entity.name.tag', value: '__name__'},
  80. {type: 'keyword.operator', value: '=~'},
  81. {type: 'string.quoted', value: '"node_cpu"'},
  82. {type: 'punctuation.operator', value: ','},
  83. {type: 'entity.name.tag', value: 'j', index: 5, start: 22},
  84. {type: 'paren.rparen', value: '}'},
  85. ],
  86. line: '{__name__=~"node_cpu",j}',
  87. });
  88. return completer.getCompletions(editor, session, {row: 0, column: 23}, 'j', (s, res) => {
  89. expect(res[0].meta).to.eql('label name');
  90. });
  91. });
  92. });
  93. describe('When inside label matcher, and located at label value', () => {
  94. it('Should return label value list', () => {
  95. const session = getSessionStub({
  96. currentToken: {type: 'string.quoted', value: '"n"', index: 4, start: 13},
  97. tokens: [
  98. {type: 'identifier', value: 'node_cpu'},
  99. {type: 'paren.lparen', value: '{'},
  100. {type: 'entity.name.tag', value: 'job'},
  101. {type: 'keyword.operator', value: '='},
  102. {type: 'string.quoted', value: '"n"', index: 4, start: 13},
  103. {type: 'paren.rparen', value: '}'},
  104. ],
  105. line: 'node_cpu{job="n"}',
  106. });
  107. return completer.getCompletions(editor, session, {row: 0, column: 15}, 'n', (s, res) => {
  108. expect(res[0].meta).to.eql('label value');
  109. });
  110. });
  111. });
  112. });