completer_specs.ts 6.2 KB

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