completer_specs.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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.label-matcher',
  58. value: 'j',
  59. index: 2,
  60. start: 9,
  61. },
  62. tokens: [
  63. { type: 'identifier', value: 'node_cpu' },
  64. { type: 'paren.lparen.label-matcher', value: '{' },
  65. {
  66. type: 'entity.name.tag.label-matcher',
  67. value: 'j',
  68. index: 2,
  69. start: 9,
  70. },
  71. { type: 'paren.rparen.label-matcher', value: '}' },
  72. ],
  73. line: 'node_cpu{j}',
  74. });
  75. return completer.getCompletions(editor, session, { row: 0, column: 10 }, 'j', (s, res) => {
  76. expect(res[0].meta).to.eql('label name');
  77. });
  78. });
  79. });
  80. describe('When inside label matcher, and located at label name with __name__ match', () => {
  81. it('Should return label name list', () => {
  82. const session = getSessionStub({
  83. currentToken: {
  84. type: 'entity.name.tag.label-matcher',
  85. value: 'j',
  86. index: 5,
  87. start: 22,
  88. },
  89. tokens: [
  90. { type: 'paren.lparen.label-matcher', value: '{' },
  91. { type: 'entity.name.tag.label-matcher', value: '__name__' },
  92. { type: 'keyword.operator.label-matcher', value: '=~' },
  93. { type: 'string.quoted.label-matcher', value: '"node_cpu"' },
  94. { type: 'punctuation.operator.label-matcher', value: ',' },
  95. {
  96. type: 'entity.name.tag.label-matcher',
  97. value: 'j',
  98. index: 5,
  99. start: 22,
  100. },
  101. { type: 'paren.rparen.label-matcher', value: '}' },
  102. ],
  103. line: '{__name__=~"node_cpu",j}',
  104. });
  105. return completer.getCompletions(editor, session, { row: 0, column: 23 }, 'j', (s, res) => {
  106. expect(res[0].meta).to.eql('label name');
  107. });
  108. });
  109. });
  110. describe('When inside label matcher, and located at label value', () => {
  111. it('Should return label value list', () => {
  112. const session = getSessionStub({
  113. currentToken: {
  114. type: 'string.quoted.label-matcher',
  115. value: '"n"',
  116. index: 4,
  117. start: 13,
  118. },
  119. tokens: [
  120. { type: 'identifier', value: 'node_cpu' },
  121. { type: 'paren.lparen.label-matcher', value: '{' },
  122. { type: 'entity.name.tag.label-matcher', value: 'job' },
  123. { type: 'keyword.operator.label-matcher', value: '=' },
  124. {
  125. type: 'string.quoted.label-matcher',
  126. value: '"n"',
  127. index: 4,
  128. start: 13,
  129. },
  130. { type: 'paren.rparen.label-matcher', value: '}' },
  131. ],
  132. line: 'node_cpu{job="n"}',
  133. });
  134. return completer.getCompletions(editor, session, { row: 0, column: 15 }, 'n', (s, res) => {
  135. expect(res[0].meta).to.eql('label value');
  136. });
  137. });
  138. });
  139. describe('When inside by', () => {
  140. it('Should return label name list', () => {
  141. const session = getSessionStub({
  142. currentToken: {
  143. type: 'entity.name.tag.label-list-matcher',
  144. value: 'm',
  145. index: 9,
  146. start: 22,
  147. },
  148. tokens: [
  149. { type: 'paren.lparen', value: '(' },
  150. { type: 'keyword', value: 'count' },
  151. { type: 'paren.lparen', value: '(' },
  152. { type: 'identifier', value: 'node_cpu' },
  153. { type: 'paren.rparen', value: '))' },
  154. { type: 'text', value: ' ' },
  155. { type: 'keyword.control', value: 'by' },
  156. { type: 'text', value: ' ' },
  157. { type: 'paren.lparen.label-list-matcher', value: '(' },
  158. {
  159. type: 'entity.name.tag.label-list-matcher',
  160. value: 'm',
  161. index: 9,
  162. start: 22,
  163. },
  164. { type: 'paren.rparen.label-list-matcher', value: ')' },
  165. ],
  166. line: '(count(node_cpu)) by (m)',
  167. });
  168. return completer.getCompletions(editor, session, { row: 0, column: 23 }, 'm', (s, res) => {
  169. expect(res[0].meta).to.eql('label name');
  170. });
  171. });
  172. });
  173. });