completer.test.ts 6.3 KB

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