completer_specs.ts 6.2 KB

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