variable_srv_specs.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. import {
  2. describe,
  3. beforeEach,
  4. it,
  5. sinon,
  6. expect,
  7. angularMocks
  8. } from "test/lib/common";
  9. import "../all";
  10. import moment from "moment";
  11. import helpers from "test/specs/helpers";
  12. import { Emitter } from "app/core/core";
  13. describe("VariableSrv", function() {
  14. var ctx = new helpers.ControllerTestContext();
  15. beforeEach(angularMocks.module("grafana.core"));
  16. beforeEach(angularMocks.module("grafana.controllers"));
  17. beforeEach(angularMocks.module("grafana.services"));
  18. beforeEach(
  19. ctx.providePhase(["datasourceSrv", "timeSrv", "templateSrv", "$location"])
  20. );
  21. beforeEach(
  22. angularMocks.inject(($rootScope, $q, $location, $injector) => {
  23. ctx.$q = $q;
  24. ctx.$rootScope = $rootScope;
  25. ctx.$location = $location;
  26. ctx.variableSrv = $injector.get("variableSrv");
  27. ctx.variableSrv.init({
  28. templating: { list: [] },
  29. events: new Emitter(),
  30. updateSubmenuVisibility: sinon.stub()
  31. });
  32. ctx.$rootScope.$digest();
  33. })
  34. );
  35. function describeUpdateVariable(desc, fn) {
  36. describe(desc, function() {
  37. var scenario: any = {};
  38. scenario.setup = function(setupFn) {
  39. scenario.setupFn = setupFn;
  40. };
  41. beforeEach(function() {
  42. scenario.setupFn();
  43. var ds: any = {};
  44. ds.metricFindQuery = sinon
  45. .stub()
  46. .returns(ctx.$q.when(scenario.queryResult));
  47. ctx.datasourceSrv.get = sinon.stub().returns(ctx.$q.when(ds));
  48. ctx.datasourceSrv.getMetricSources = sinon
  49. .stub()
  50. .returns(scenario.metricSources);
  51. scenario.variable = ctx.variableSrv.createVariableFromModel(
  52. scenario.variableModel
  53. );
  54. ctx.variableSrv.addVariable(scenario.variable);
  55. ctx.variableSrv.updateOptions(scenario.variable);
  56. ctx.$rootScope.$digest();
  57. });
  58. fn(scenario);
  59. });
  60. }
  61. describeUpdateVariable("interval variable without auto", scenario => {
  62. scenario.setup(() => {
  63. scenario.variableModel = {
  64. type: "interval",
  65. query: "1s,2h,5h,1d",
  66. name: "test"
  67. };
  68. });
  69. it("should update options array", () => {
  70. expect(scenario.variable.options.length).to.be(4);
  71. expect(scenario.variable.options[0].text).to.be("1s");
  72. expect(scenario.variable.options[0].value).to.be("1s");
  73. });
  74. });
  75. //
  76. // Interval variable update
  77. //
  78. describeUpdateVariable("interval variable with auto", scenario => {
  79. scenario.setup(() => {
  80. scenario.variableModel = {
  81. type: "interval",
  82. query: "1s,2h,5h,1d",
  83. name: "test",
  84. auto: true,
  85. auto_count: 10
  86. };
  87. var range = {
  88. from: moment(new Date())
  89. .subtract(7, "days")
  90. .toDate(),
  91. to: new Date()
  92. };
  93. ctx.timeSrv.timeRange = sinon.stub().returns(range);
  94. ctx.templateSrv.setGrafanaVariable = sinon.spy();
  95. });
  96. it("should update options array", function() {
  97. expect(scenario.variable.options.length).to.be(5);
  98. expect(scenario.variable.options[0].text).to.be("auto");
  99. expect(scenario.variable.options[0].value).to.be("$__auto_interval_test");
  100. });
  101. it("should set $__auto_interval_test", function() {
  102. var call = ctx.templateSrv.setGrafanaVariable.firstCall;
  103. expect(call.args[0]).to.be("$__auto_interval_test");
  104. expect(call.args[1]).to.be("12h");
  105. });
  106. // updateAutoValue() gets called twice: once directly once via VariableSrv.validateVariableSelectionState()
  107. // So use lastCall instead of a specific call number
  108. it("should set $__auto_interval", function() {
  109. var call = ctx.templateSrv.setGrafanaVariable.lastCall;
  110. expect(call.args[0]).to.be("$__auto_interval");
  111. expect(call.args[1]).to.be("12h");
  112. });
  113. });
  114. //
  115. // Query variable update
  116. //
  117. describeUpdateVariable(
  118. "query variable with empty current object and refresh",
  119. function(scenario) {
  120. scenario.setup(function() {
  121. scenario.variableModel = {
  122. type: "query",
  123. query: "",
  124. name: "test",
  125. current: {}
  126. };
  127. scenario.queryResult = [{ text: "backend1" }, { text: "backend2" }];
  128. });
  129. it("should set current value to first option", function() {
  130. expect(scenario.variable.options.length).to.be(2);
  131. expect(scenario.variable.current.value).to.be("backend1");
  132. });
  133. }
  134. );
  135. describeUpdateVariable(
  136. "query variable with multi select and new options does not contain some selected values",
  137. function(scenario) {
  138. scenario.setup(function() {
  139. scenario.variableModel = {
  140. type: "query",
  141. query: "",
  142. name: "test",
  143. current: {
  144. value: ["val1", "val2", "val3"],
  145. text: "val1 + val2 + val3"
  146. }
  147. };
  148. scenario.queryResult = [{ text: "val2" }, { text: "val3" }];
  149. });
  150. it("should update current value", function() {
  151. expect(scenario.variable.current.value).to.eql(["val2", "val3"]);
  152. expect(scenario.variable.current.text).to.eql("val2 + val3");
  153. });
  154. }
  155. );
  156. describeUpdateVariable(
  157. "query variable with multi select and new options does not contain any selected values",
  158. function(scenario) {
  159. scenario.setup(function() {
  160. scenario.variableModel = {
  161. type: "query",
  162. query: "",
  163. name: "test",
  164. current: {
  165. value: ["val1", "val2", "val3"],
  166. text: "val1 + val2 + val3"
  167. }
  168. };
  169. scenario.queryResult = [{ text: "val5" }, { text: "val6" }];
  170. });
  171. it("should update current value with first one", function() {
  172. expect(scenario.variable.current.value).to.eql("val5");
  173. expect(scenario.variable.current.text).to.eql("val5");
  174. });
  175. }
  176. );
  177. describeUpdateVariable(
  178. "query variable with multi select and $__all selected",
  179. function(scenario) {
  180. scenario.setup(function() {
  181. scenario.variableModel = {
  182. type: "query",
  183. query: "",
  184. name: "test",
  185. includeAll: true,
  186. current: {
  187. value: ["$__all"],
  188. text: "All"
  189. }
  190. };
  191. scenario.queryResult = [{ text: "val5" }, { text: "val6" }];
  192. });
  193. it("should keep current All value", function() {
  194. expect(scenario.variable.current.value).to.eql(["$__all"]);
  195. expect(scenario.variable.current.text).to.eql("All");
  196. });
  197. }
  198. );
  199. describeUpdateVariable("query variable with numeric results", function(
  200. scenario
  201. ) {
  202. scenario.setup(function() {
  203. scenario.variableModel = {
  204. type: "query",
  205. query: "",
  206. name: "test",
  207. current: {}
  208. };
  209. scenario.queryResult = [{ text: 12, value: 12 }];
  210. });
  211. it("should set current value to first option", function() {
  212. expect(scenario.variable.current.value).to.be("12");
  213. expect(scenario.variable.options[0].value).to.be("12");
  214. expect(scenario.variable.options[0].text).to.be("12");
  215. });
  216. });
  217. describeUpdateVariable("basic query variable", function(scenario) {
  218. scenario.setup(function() {
  219. scenario.variableModel = { type: "query", query: "apps.*", name: "test" };
  220. scenario.queryResult = [{ text: "backend1" }, { text: "backend2" }];
  221. });
  222. it("should update options array", function() {
  223. expect(scenario.variable.options.length).to.be(2);
  224. expect(scenario.variable.options[0].text).to.be("backend1");
  225. expect(scenario.variable.options[0].value).to.be("backend1");
  226. expect(scenario.variable.options[1].value).to.be("backend2");
  227. });
  228. it("should select first option as value", function() {
  229. expect(scenario.variable.current.value).to.be("backend1");
  230. });
  231. });
  232. describeUpdateVariable("and existing value still exists in options", function(
  233. scenario
  234. ) {
  235. scenario.setup(function() {
  236. scenario.variableModel = { type: "query", query: "apps.*", name: "test" };
  237. scenario.variableModel.current = { value: "backend2", text: "backend2" };
  238. scenario.queryResult = [{ text: "backend1" }, { text: "backend2" }];
  239. });
  240. it("should keep variable value", function() {
  241. expect(scenario.variable.current.text).to.be("backend2");
  242. });
  243. });
  244. describeUpdateVariable("and regex pattern exists", function(scenario) {
  245. scenario.setup(function() {
  246. scenario.variableModel = { type: "query", query: "apps.*", name: "test" };
  247. scenario.variableModel.regex = "/apps.*(backend_[0-9]+)/";
  248. scenario.queryResult = [
  249. { text: "apps.backend.backend_01.counters.req" },
  250. { text: "apps.backend.backend_02.counters.req" }
  251. ];
  252. });
  253. it("should extract and use match group", function() {
  254. expect(scenario.variable.options[0].value).to.be("backend_01");
  255. });
  256. });
  257. describeUpdateVariable("and regex pattern exists and no match", function(
  258. scenario
  259. ) {
  260. scenario.setup(function() {
  261. scenario.variableModel = { type: "query", query: "apps.*", name: "test" };
  262. scenario.variableModel.regex = "/apps.*(backendasd[0-9]+)/";
  263. scenario.queryResult = [
  264. { text: "apps.backend.backend_01.counters.req" },
  265. { text: "apps.backend.backend_02.counters.req" }
  266. ];
  267. });
  268. it("should not add non matching items, None option should be added instead", function() {
  269. expect(scenario.variable.options.length).to.be(1);
  270. expect(scenario.variable.options[0].isNone).to.be(true);
  271. });
  272. });
  273. describeUpdateVariable("regex pattern without slashes", function(scenario) {
  274. scenario.setup(function() {
  275. scenario.variableModel = { type: "query", query: "apps.*", name: "test" };
  276. scenario.variableModel.regex = "backend_01";
  277. scenario.queryResult = [
  278. { text: "apps.backend.backend_01.counters.req" },
  279. { text: "apps.backend.backend_02.counters.req" }
  280. ];
  281. });
  282. it("should return matches options", function() {
  283. expect(scenario.variable.options.length).to.be(1);
  284. });
  285. });
  286. describeUpdateVariable("regex pattern remove duplicates", function(scenario) {
  287. scenario.setup(function() {
  288. scenario.variableModel = { type: "query", query: "apps.*", name: "test" };
  289. scenario.variableModel.regex = "/backend_01/";
  290. scenario.queryResult = [
  291. { text: "apps.backend.backend_01.counters.req" },
  292. { text: "apps.backend.backend_01.counters.req" }
  293. ];
  294. });
  295. it("should return matches options", function() {
  296. expect(scenario.variable.options.length).to.be(1);
  297. });
  298. });
  299. describeUpdateVariable("with include All", function(scenario) {
  300. scenario.setup(function() {
  301. scenario.variableModel = {
  302. type: "query",
  303. query: "apps.*",
  304. name: "test",
  305. includeAll: true
  306. };
  307. scenario.queryResult = [
  308. { text: "backend1" },
  309. { text: "backend2" },
  310. { text: "backend3" }
  311. ];
  312. });
  313. it("should add All option", function() {
  314. expect(scenario.variable.options[0].text).to.be("All");
  315. expect(scenario.variable.options[0].value).to.be("$__all");
  316. });
  317. });
  318. describeUpdateVariable("with include all and custom value", function(
  319. scenario
  320. ) {
  321. scenario.setup(function() {
  322. scenario.variableModel = {
  323. type: "query",
  324. query: "apps.*",
  325. name: "test",
  326. includeAll: true,
  327. allValue: "*"
  328. };
  329. scenario.queryResult = [
  330. { text: "backend1" },
  331. { text: "backend2" },
  332. { text: "backend3" }
  333. ];
  334. });
  335. it("should add All option with custom value", function() {
  336. expect(scenario.variable.options[0].value).to.be("$__all");
  337. });
  338. });
  339. describeUpdateVariable("without sort", function(scenario) {
  340. scenario.setup(function() {
  341. scenario.variableModel = {
  342. type: "query",
  343. query: "apps.*",
  344. name: "test",
  345. sort: 0
  346. };
  347. scenario.queryResult = [
  348. { text: "bbb2" },
  349. { text: "aaa10" },
  350. { text: "ccc3" }
  351. ];
  352. });
  353. it("should return options without sort", function() {
  354. expect(scenario.variable.options[0].text).to.be("bbb2");
  355. expect(scenario.variable.options[1].text).to.be("aaa10");
  356. expect(scenario.variable.options[2].text).to.be("ccc3");
  357. });
  358. });
  359. describeUpdateVariable("with alphabetical sort (asc)", function(scenario) {
  360. scenario.setup(function() {
  361. scenario.variableModel = {
  362. type: "query",
  363. query: "apps.*",
  364. name: "test",
  365. sort: 1
  366. };
  367. scenario.queryResult = [
  368. { text: "bbb2" },
  369. { text: "aaa10" },
  370. { text: "ccc3" }
  371. ];
  372. });
  373. it("should return options with alphabetical sort", function() {
  374. expect(scenario.variable.options[0].text).to.be("aaa10");
  375. expect(scenario.variable.options[1].text).to.be("bbb2");
  376. expect(scenario.variable.options[2].text).to.be("ccc3");
  377. });
  378. });
  379. describeUpdateVariable("with alphabetical sort (desc)", function(scenario) {
  380. scenario.setup(function() {
  381. scenario.variableModel = {
  382. type: "query",
  383. query: "apps.*",
  384. name: "test",
  385. sort: 2
  386. };
  387. scenario.queryResult = [
  388. { text: "bbb2" },
  389. { text: "aaa10" },
  390. { text: "ccc3" }
  391. ];
  392. });
  393. it("should return options with alphabetical sort", function() {
  394. expect(scenario.variable.options[0].text).to.be("ccc3");
  395. expect(scenario.variable.options[1].text).to.be("bbb2");
  396. expect(scenario.variable.options[2].text).to.be("aaa10");
  397. });
  398. });
  399. describeUpdateVariable("with numerical sort (asc)", function(scenario) {
  400. scenario.setup(function() {
  401. scenario.variableModel = {
  402. type: "query",
  403. query: "apps.*",
  404. name: "test",
  405. sort: 3
  406. };
  407. scenario.queryResult = [
  408. { text: "bbb2" },
  409. { text: "aaa10" },
  410. { text: "ccc3" }
  411. ];
  412. });
  413. it("should return options with numerical sort", function() {
  414. expect(scenario.variable.options[0].text).to.be("bbb2");
  415. expect(scenario.variable.options[1].text).to.be("ccc3");
  416. expect(scenario.variable.options[2].text).to.be("aaa10");
  417. });
  418. });
  419. describeUpdateVariable("with numerical sort (desc)", function(scenario) {
  420. scenario.setup(function() {
  421. scenario.variableModel = {
  422. type: "query",
  423. query: "apps.*",
  424. name: "test",
  425. sort: 4
  426. };
  427. scenario.queryResult = [
  428. { text: "bbb2" },
  429. { text: "aaa10" },
  430. { text: "ccc3" }
  431. ];
  432. });
  433. it("should return options with numerical sort", function() {
  434. expect(scenario.variable.options[0].text).to.be("aaa10");
  435. expect(scenario.variable.options[1].text).to.be("ccc3");
  436. expect(scenario.variable.options[2].text).to.be("bbb2");
  437. });
  438. });
  439. //
  440. // datasource variable update
  441. //
  442. describeUpdateVariable("datasource variable with regex filter", function(
  443. scenario
  444. ) {
  445. scenario.setup(function() {
  446. scenario.variableModel = {
  447. type: "datasource",
  448. query: "graphite",
  449. name: "test",
  450. current: { value: "backend4_pee", text: "backend4_pee" },
  451. regex: "/pee$/"
  452. };
  453. scenario.metricSources = [
  454. { name: "backend1", meta: { id: "influx" } },
  455. { name: "backend2_pee", meta: { id: "graphite" } },
  456. { name: "backend3", meta: { id: "graphite" } },
  457. { name: "backend4_pee", meta: { id: "graphite" } }
  458. ];
  459. });
  460. it("should set only contain graphite ds and filtered using regex", function() {
  461. expect(scenario.variable.options.length).to.be(2);
  462. expect(scenario.variable.options[0].value).to.be("backend2_pee");
  463. expect(scenario.variable.options[1].value).to.be("backend4_pee");
  464. });
  465. it("should keep current value if available", function() {
  466. expect(scenario.variable.current.value).to.be("backend4_pee");
  467. });
  468. });
  469. //
  470. // Custom variable update
  471. //
  472. describeUpdateVariable("update custom variable", function(scenario) {
  473. scenario.setup(function() {
  474. scenario.variableModel = {
  475. type: "custom",
  476. query: "hej, hop, asd",
  477. name: "test"
  478. };
  479. });
  480. it("should update options array", function() {
  481. expect(scenario.variable.options.length).to.be(3);
  482. expect(scenario.variable.options[0].text).to.be("hej");
  483. expect(scenario.variable.options[1].value).to.be("hop");
  484. });
  485. });
  486. describe("multiple interval variables with auto", function() {
  487. var variable1, variable2;
  488. beforeEach(function() {
  489. var range = {
  490. from: moment(new Date())
  491. .subtract(7, "days")
  492. .toDate(),
  493. to: new Date()
  494. };
  495. ctx.timeSrv.timeRange = sinon.stub().returns(range);
  496. ctx.templateSrv.setGrafanaVariable = sinon.spy();
  497. var variableModel1 = {
  498. type: "interval",
  499. query: "1s,2h,5h,1d",
  500. name: "variable1",
  501. auto: true,
  502. auto_count: 10
  503. };
  504. variable1 = ctx.variableSrv.createVariableFromModel(variableModel1);
  505. ctx.variableSrv.addVariable(variable1);
  506. var variableModel2 = {
  507. type: "interval",
  508. query: "1s,2h,5h",
  509. name: "variable2",
  510. auto: true,
  511. auto_count: 1000
  512. };
  513. variable2 = ctx.variableSrv.createVariableFromModel(variableModel2);
  514. ctx.variableSrv.addVariable(variable2);
  515. ctx.variableSrv.updateOptions(variable1);
  516. ctx.variableSrv.updateOptions(variable2);
  517. ctx.$rootScope.$digest();
  518. });
  519. it("should update options array", function() {
  520. expect(variable1.options.length).to.be(5);
  521. expect(variable1.options[0].text).to.be("auto");
  522. expect(variable1.options[0].value).to.be("$__auto_interval_variable1");
  523. expect(variable2.options.length).to.be(4);
  524. expect(variable2.options[0].text).to.be("auto");
  525. expect(variable2.options[0].value).to.be("$__auto_interval_variable2");
  526. });
  527. it("should correctly set $__auto_interval_variableX", function() {
  528. var variable1Set,
  529. variable2Set,
  530. legacySet,
  531. unknownSet = false;
  532. // updateAutoValue() gets called repeatedly: once directly once via VariableSrv.validateVariableSelectionState()
  533. // So check that all calls are valid rather than expect a specific number and/or ordering of calls
  534. for (var i = 0; i < ctx.templateSrv.setGrafanaVariable.callCount; i++) {
  535. var call = ctx.templateSrv.setGrafanaVariable.getCall(i);
  536. switch (call.args[0]) {
  537. case "$__auto_interval_variable1":
  538. expect(call.args[1]).to.be("12h");
  539. variable1Set = true;
  540. break;
  541. case "$__auto_interval_variable2":
  542. expect(call.args[1]).to.be("10m");
  543. variable2Set = true;
  544. break;
  545. case "$__auto_interval":
  546. expect(call.args[1]).to.match(/^(12h|10m)$/);
  547. legacySet = true;
  548. break;
  549. default:
  550. unknownSet = true;
  551. break;
  552. }
  553. }
  554. expect(variable1Set).to.be.equal(true);
  555. expect(variable2Set).to.be.equal(true);
  556. expect(legacySet).to.be.equal(true);
  557. expect(unknownSet).to.be.equal(false);
  558. });
  559. });
  560. });