variable_srv_specs.ts 18 KB

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