variable_srv_specs.ts 18 KB

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