completer.test.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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.metadataRequest = jest.fn(() =>
  18. Promise.resolve({ data: { data: [{ metric: { job: 'node', instance: 'localhost:9100' } }] } })
  19. );
  20. datasourceStub.getTimeRange = jest.fn(() => {
  21. return { start: 1514732400, end: 1514818800 };
  22. });
  23. datasourceStub.performSuggestQuery = jest.fn(() => Promise.resolve(['node_cpu']));
  24. const templateSrv = {
  25. variables: [
  26. {
  27. name: 'var_name',
  28. options: [{ text: 'foo', value: 'foo', selected: false }, { text: 'bar', value: 'bar', selected: true }],
  29. },
  30. ],
  31. };
  32. const completer = new PromCompleter(datasourceStub, templateSrv);
  33. describe('When inside brackets', () => {
  34. it('Should return range vectors', () => {
  35. const session = getSessionStub({
  36. currentToken: { type: 'paren.lparen', value: '[', index: 2, start: 9 },
  37. tokens: [{ type: 'identifier', value: 'node_cpu' }, { type: 'paren.lparen', value: '[' }],
  38. line: 'node_cpu[',
  39. });
  40. return completer.getCompletions(editor, session, { row: 0, column: 10 }, '[', (s, res) => {
  41. expect(res[0].caption).toEqual('$__interval');
  42. expect(res[0].value).toEqual('[$__interval');
  43. expect(res[0].meta).toEqual('range vector');
  44. });
  45. });
  46. });
  47. describe('When inside label matcher, and located at label name', () => {
  48. it('Should return label name list', () => {
  49. const session = getSessionStub({
  50. currentToken: {
  51. type: 'entity.name.tag.label-matcher',
  52. value: 'j',
  53. index: 2,
  54. start: 9,
  55. },
  56. tokens: [
  57. { type: 'identifier', value: 'node_cpu' },
  58. { type: 'paren.lparen.label-matcher', value: '{' },
  59. {
  60. type: 'entity.name.tag.label-matcher',
  61. value: 'j',
  62. index: 2,
  63. start: 9,
  64. },
  65. { type: 'paren.rparen.label-matcher', value: '}' },
  66. ],
  67. line: 'node_cpu{j}',
  68. });
  69. return completer.getCompletions(editor, session, { row: 0, column: 10 }, 'j', (s, res) => {
  70. expect(res[0].meta).toEqual('label name');
  71. });
  72. });
  73. });
  74. describe('When inside label matcher, and located at label name with __name__ match', () => {
  75. it('Should return label name list', () => {
  76. const session = getSessionStub({
  77. currentToken: {
  78. type: 'entity.name.tag.label-matcher',
  79. value: 'j',
  80. index: 5,
  81. start: 22,
  82. },
  83. tokens: [
  84. { type: 'paren.lparen.label-matcher', value: '{' },
  85. { type: 'entity.name.tag.label-matcher', value: '__name__' },
  86. { type: 'keyword.operator.label-matcher', value: '=~' },
  87. { type: 'string.quoted.label-matcher', value: '"node_cpu"' },
  88. { type: 'punctuation.operator.label-matcher', value: ',' },
  89. {
  90. type: 'entity.name.tag.label-matcher',
  91. value: 'j',
  92. index: 5,
  93. start: 22,
  94. },
  95. { type: 'paren.rparen.label-matcher', value: '}' },
  96. ],
  97. line: '{__name__=~"node_cpu",j}',
  98. });
  99. return completer.getCompletions(editor, session, { row: 0, column: 23 }, 'j', (s, res) => {
  100. expect(res[0].meta).toEqual('label name');
  101. });
  102. });
  103. });
  104. describe('When inside label matcher, and located at label value', () => {
  105. it('Should return label value list', () => {
  106. const session = getSessionStub({
  107. currentToken: {
  108. type: 'string.quoted.label-matcher',
  109. value: '"n"',
  110. index: 4,
  111. start: 13,
  112. },
  113. tokens: [
  114. { type: 'identifier', value: 'node_cpu' },
  115. { type: 'paren.lparen.label-matcher', value: '{' },
  116. { type: 'entity.name.tag.label-matcher', value: 'job' },
  117. { type: 'keyword.operator.label-matcher', value: '=' },
  118. {
  119. type: 'string.quoted.label-matcher',
  120. value: '"n"',
  121. index: 4,
  122. start: 13,
  123. },
  124. { type: 'paren.rparen.label-matcher', value: '}' },
  125. ],
  126. line: 'node_cpu{job="n"}',
  127. });
  128. return completer.getCompletions(editor, session, { row: 0, column: 15 }, 'n', (s, res) => {
  129. expect(res[0].meta).toEqual('label value');
  130. });
  131. });
  132. });
  133. describe('When inside by', () => {
  134. it('Should return label name list', () => {
  135. const session = getSessionStub({
  136. currentToken: {
  137. type: 'entity.name.tag.label-list-matcher',
  138. value: 'm',
  139. index: 9,
  140. start: 22,
  141. },
  142. tokens: [
  143. { type: 'paren.lparen', value: '(' },
  144. { type: 'keyword', value: 'count' },
  145. { type: 'paren.lparen', value: '(' },
  146. { type: 'identifier', value: 'node_cpu' },
  147. { type: 'paren.rparen', value: '))' },
  148. { type: 'text', value: ' ' },
  149. { type: 'keyword.control', value: 'by' },
  150. { type: 'text', value: ' ' },
  151. { type: 'paren.lparen.label-list-matcher', value: '(' },
  152. {
  153. type: 'entity.name.tag.label-list-matcher',
  154. value: 'm',
  155. index: 9,
  156. start: 22,
  157. },
  158. { type: 'paren.rparen.label-list-matcher', value: ')' },
  159. ],
  160. line: '(count(node_cpu)) by (m)',
  161. });
  162. return completer.getCompletions(editor, session, { row: 0, column: 23 }, 'm', (s, res) => {
  163. expect(res[0].meta).toEqual('label name');
  164. });
  165. });
  166. });
  167. });