completer.test.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import { PromCompleter } from '../completer';
  2. import { PrometheusDatasource } from '../datasource';
  3. import { BackendSrv } from 'app/core/services/backend_srv';
  4. jest.mock('../datasource');
  5. jest.mock('app/core/services/backend_srv');
  6. describe('Prometheus editor completer', () => {
  7. function getSessionStub(data) {
  8. return {
  9. getTokenAt: jest.fn(() => data.currentToken),
  10. getTokens: jest.fn(() => data.tokens),
  11. getLine: jest.fn(() => data.line),
  12. };
  13. }
  14. const editor = {};
  15. const backendSrv = {} as BackendSrv;
  16. const datasourceStub = new PrometheusDatasource({}, {}, backendSrv, {}, {});
  17. datasourceStub.performInstantQuery = jest.fn(() =>
  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. datasourceStub.performSuggestQuery = jest.fn(() => Promise.resolve(['node_cpu']));
  34. const templateSrv = {
  35. variables: [
  36. {
  37. name: 'var_name',
  38. options: [{ text: 'foo', value: 'foo', selected: false }, { text: 'bar', value: 'bar', selected: true }],
  39. },
  40. ],
  41. };
  42. const completer = new PromCompleter(datasourceStub, templateSrv);
  43. describe('When inside brackets', () => {
  44. it('Should return range vectors', () => {
  45. const session = getSessionStub({
  46. currentToken: { type: 'paren.lparen', value: '[', index: 2, start: 9 },
  47. tokens: [{ type: 'identifier', value: 'node_cpu' }, { type: 'paren.lparen', value: '[' }],
  48. line: 'node_cpu[',
  49. });
  50. return completer.getCompletions(editor, session, { row: 0, column: 10 }, '[', (s, res) => {
  51. expect(res[0].caption).toEqual('$__interval');
  52. expect(res[0].value).toEqual('[$__interval');
  53. expect(res[0].meta).toEqual('range vector');
  54. });
  55. });
  56. });
  57. describe('When inside label matcher, and located at label name', () => {
  58. it('Should return label name list', () => {
  59. const session = getSessionStub({
  60. currentToken: {
  61. type: 'entity.name.tag.label-matcher',
  62. value: 'j',
  63. index: 2,
  64. start: 9,
  65. },
  66. tokens: [
  67. { type: 'identifier', value: 'node_cpu' },
  68. { type: 'paren.lparen.label-matcher', value: '{' },
  69. {
  70. type: 'entity.name.tag.label-matcher',
  71. value: 'j',
  72. index: 2,
  73. start: 9,
  74. },
  75. { type: 'paren.rparen.label-matcher', value: '}' },
  76. ],
  77. line: 'node_cpu{j}',
  78. });
  79. return completer.getCompletions(editor, session, { row: 0, column: 10 }, 'j', (s, res) => {
  80. expect(res[0].meta).toEqual('label name');
  81. });
  82. });
  83. });
  84. describe('When inside label matcher, and located at label name with __name__ match', () => {
  85. it('Should return label name list', () => {
  86. const session = getSessionStub({
  87. currentToken: {
  88. type: 'entity.name.tag.label-matcher',
  89. value: 'j',
  90. index: 5,
  91. start: 22,
  92. },
  93. tokens: [
  94. { type: 'paren.lparen.label-matcher', value: '{' },
  95. { type: 'entity.name.tag.label-matcher', value: '__name__' },
  96. { type: 'keyword.operator.label-matcher', value: '=~' },
  97. { type: 'string.quoted.label-matcher', value: '"node_cpu"' },
  98. { type: 'punctuation.operator.label-matcher', value: ',' },
  99. {
  100. type: 'entity.name.tag.label-matcher',
  101. value: 'j',
  102. index: 5,
  103. start: 22,
  104. },
  105. { type: 'paren.rparen.label-matcher', value: '}' },
  106. ],
  107. line: '{__name__=~"node_cpu",j}',
  108. });
  109. return completer.getCompletions(editor, session, { row: 0, column: 23 }, 'j', (s, res) => {
  110. expect(res[0].meta).toEqual('label name');
  111. });
  112. });
  113. });
  114. describe('When inside label matcher, and located at label value', () => {
  115. it('Should return label value list', () => {
  116. const session = getSessionStub({
  117. currentToken: {
  118. type: 'string.quoted.label-matcher',
  119. value: '"n"',
  120. index: 4,
  121. start: 13,
  122. },
  123. tokens: [
  124. { type: 'identifier', value: 'node_cpu' },
  125. { type: 'paren.lparen.label-matcher', value: '{' },
  126. { type: 'entity.name.tag.label-matcher', value: 'job' },
  127. { type: 'keyword.operator.label-matcher', value: '=' },
  128. {
  129. type: 'string.quoted.label-matcher',
  130. value: '"n"',
  131. index: 4,
  132. start: 13,
  133. },
  134. { type: 'paren.rparen.label-matcher', value: '}' },
  135. ],
  136. line: 'node_cpu{job="n"}',
  137. });
  138. return completer.getCompletions(editor, session, { row: 0, column: 15 }, 'n', (s, res) => {
  139. expect(res[0].meta).toEqual('label value');
  140. });
  141. });
  142. });
  143. describe('When inside by', () => {
  144. it('Should return label name list', () => {
  145. const session = getSessionStub({
  146. currentToken: {
  147. type: 'entity.name.tag.label-list-matcher',
  148. value: 'm',
  149. index: 9,
  150. start: 22,
  151. },
  152. tokens: [
  153. { type: 'paren.lparen', value: '(' },
  154. { type: 'keyword', value: 'count' },
  155. { type: 'paren.lparen', value: '(' },
  156. { type: 'identifier', value: 'node_cpu' },
  157. { type: 'paren.rparen', value: '))' },
  158. { type: 'text', value: ' ' },
  159. { type: 'keyword.control', value: 'by' },
  160. { type: 'text', value: ' ' },
  161. { type: 'paren.lparen.label-list-matcher', value: '(' },
  162. {
  163. type: 'entity.name.tag.label-list-matcher',
  164. value: 'm',
  165. index: 9,
  166. start: 22,
  167. },
  168. { type: 'paren.rparen.label-list-matcher', value: ')' },
  169. ],
  170. line: '(count(node_cpu)) by (m)',
  171. });
  172. return completer.getCompletions(editor, session, { row: 0, column: 23 }, 'm', (s, res) => {
  173. expect(res[0].meta).toEqual('label name');
  174. });
  175. });
  176. });
  177. });