completer_specs.ts 4.2 KB

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