completer_specs.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import { describe, it, sinon, expect } from "test/lib/common";
  2. import { PromCompleter } from "../completer";
  3. import { PrometheusDatasource } from "../datasource";
  4. describe("Prometheus editor completer", function() {
  5. function getSessionStub(data) {
  6. return {
  7. getTokenAt: sinon.stub().returns(data.currentToken),
  8. getTokens: sinon.stub().returns(data.tokens),
  9. getLine: sinon.stub().returns(data.line)
  10. };
  11. }
  12. let editor = {};
  13. let datasourceStub = <PrometheusDatasource>{
  14. performInstantQuery: sinon
  15. .stub()
  16. .withArgs({ expr: '{__name__="node_cpu"' })
  17. .returns(
  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. performSuggestQuery: sinon
  34. .stub()
  35. .withArgs("node", true)
  36. .returns(Promise.resolve(["node_cpu"]))
  37. };
  38. let completer = new PromCompleter(datasourceStub);
  39. describe("When inside brackets", () => {
  40. it("Should return range vectors", () => {
  41. const session = getSessionStub({
  42. currentToken: { type: "paren.lparen", value: "[", index: 2, start: 9 },
  43. tokens: [
  44. { type: "identifier", value: "node_cpu" },
  45. { type: "paren.lparen", value: "[" }
  46. ],
  47. line: "node_cpu["
  48. });
  49. return completer.getCompletions(
  50. editor,
  51. session,
  52. { row: 0, column: 10 },
  53. "[",
  54. (s, res) => {
  55. expect(res[0].caption).to.eql("1s");
  56. expect(res[0].value).to.eql("[1s");
  57. expect(res[0].meta).to.eql("range vector");
  58. }
  59. );
  60. });
  61. });
  62. describe("When inside label matcher, and located at label name", () => {
  63. it("Should return label name list", () => {
  64. const session = getSessionStub({
  65. currentToken: {
  66. type: "entity.name.tag",
  67. value: "j",
  68. index: 2,
  69. start: 9
  70. },
  71. tokens: [
  72. { type: "identifier", value: "node_cpu" },
  73. { type: "paren.lparen", value: "{" },
  74. { type: "entity.name.tag", value: "j", index: 2, start: 9 },
  75. { type: "paren.rparen", value: "}" }
  76. ],
  77. line: "node_cpu{j}"
  78. });
  79. return completer.getCompletions(
  80. editor,
  81. session,
  82. { row: 0, column: 10 },
  83. "j",
  84. (s, res) => {
  85. expect(res[0].meta).to.eql("label name");
  86. }
  87. );
  88. });
  89. });
  90. describe("When inside label matcher, and located at label name with __name__ match", () => {
  91. it("Should return label name list", () => {
  92. const session = getSessionStub({
  93. currentToken: {
  94. type: "entity.name.tag",
  95. value: "j",
  96. index: 5,
  97. start: 22
  98. },
  99. tokens: [
  100. { type: "paren.lparen", value: "{" },
  101. { type: "entity.name.tag", value: "__name__" },
  102. { type: "keyword.operator", value: "=~" },
  103. { type: "string.quoted", value: '"node_cpu"' },
  104. { type: "punctuation.operator", value: "," },
  105. { type: "entity.name.tag", value: "j", index: 5, start: 22 },
  106. { type: "paren.rparen", value: "}" }
  107. ],
  108. line: '{__name__=~"node_cpu",j}'
  109. });
  110. return completer.getCompletions(
  111. editor,
  112. session,
  113. { row: 0, column: 23 },
  114. "j",
  115. (s, res) => {
  116. expect(res[0].meta).to.eql("label name");
  117. }
  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",
  126. value: '"n"',
  127. index: 4,
  128. start: 13
  129. },
  130. tokens: [
  131. { type: "identifier", value: "node_cpu" },
  132. { type: "paren.lparen", value: "{" },
  133. { type: "entity.name.tag", value: "job" },
  134. { type: "keyword.operator", value: "=" },
  135. { type: "string.quoted", value: '"n"', index: 4, start: 13 },
  136. { type: "paren.rparen", value: "}" }
  137. ],
  138. line: 'node_cpu{job="n"}'
  139. });
  140. return completer.getCompletions(
  141. editor,
  142. session,
  143. { row: 0, column: 15 },
  144. "n",
  145. (s, res) => {
  146. expect(res[0].meta).to.eql("label value");
  147. }
  148. );
  149. });
  150. });
  151. });