variable_srv_specs.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. import {describe, beforeEach, it, sinon, expect, angularMocks} from 'test/lib/common';
  2. import moment from 'moment';
  3. import helpers from 'test/specs/helpers';
  4. import '../all';
  5. describe('VariableSrv', function() {
  6. var ctx = new helpers.ControllerTestContext();
  7. beforeEach(angularMocks.module('grafana.core'));
  8. beforeEach(angularMocks.module('grafana.controllers'));
  9. beforeEach(angularMocks.module('grafana.services'));
  10. beforeEach(ctx.providePhase(['datasourceSrv', 'timeSrv', 'templateSrv', '$location']));
  11. beforeEach(angularMocks.inject(($rootScope, $q, $location, $injector) => {
  12. ctx.$q = $q;
  13. ctx.$rootScope = $rootScope;
  14. ctx.$location = $location;
  15. ctx.variableSrv = $injector.get('variableSrv');
  16. ctx.variableSrv.init({templating: {list: []}});
  17. ctx.$rootScope.$digest();
  18. }));
  19. function describeInitSceneario(desc, fn) {
  20. describe(desc, function() {
  21. var scenario: any = {
  22. urlParams: {},
  23. setup: setupFn => {
  24. scenario.setupFn = setupFn;
  25. }
  26. };
  27. beforeEach(function() {
  28. scenario.setupFn();
  29. var ds: any = {};
  30. ds.metricFindQuery = sinon.stub().returns(ctx.$q.when(scenario.queryResult));
  31. ctx.datasourceSrv.get = sinon.stub().returns(ctx.$q.when(ds));
  32. ctx.datasourceSrv.getMetricSources = sinon.stub().returns(scenario.metricSources);
  33. ctx.$location.search = sinon.stub().returns(scenario.urlParams);
  34. ctx.dashboard = {templating: {list: scenario.variables}};
  35. ctx.variableSrv.init(ctx.dashboard);
  36. ctx.$rootScope.$digest();
  37. scenario.variables = ctx.variableSrv.variables;
  38. });
  39. fn(scenario);
  40. });
  41. }
  42. describeInitSceneario('when setting simple variable via url', scenario => {
  43. scenario.setup(() => {
  44. scenario.variables = [{
  45. name: 'apps',
  46. type: 'query',
  47. current: {text: "test", value: "test"},
  48. options: [{text: "test", value: "test"}]
  49. }];
  50. scenario.urlParams["var-apps"] = "new";
  51. });
  52. it('should update current value', () => {
  53. expect(scenario.variables[0].current.value).to.be("new");
  54. expect(scenario.variables[0].current.text).to.be("new");
  55. });
  56. });
  57. function describeUpdateVariable(desc, fn) {
  58. describe(desc, function() {
  59. var scenario: any = {};
  60. scenario.setup = function(setupFn) {
  61. scenario.setupFn = setupFn;
  62. };
  63. beforeEach(function() {
  64. scenario.setupFn();
  65. var ds: any = {};
  66. ds.metricFindQuery = sinon.stub().returns(ctx.$q.when(scenario.queryResult));
  67. ctx.datasourceSrv.get = sinon.stub().returns(ctx.$q.when(ds));
  68. ctx.datasourceSrv.getMetricSources = sinon.stub().returns(scenario.metricSources);
  69. scenario.variable = ctx.variableSrv.addVariable(scenario.variableModel);
  70. ctx.variableSrv.updateOptions(scenario.variable);
  71. ctx.$rootScope.$digest();
  72. });
  73. fn(scenario);
  74. });
  75. }
  76. describeUpdateVariable('interval variable without auto', scenario => {
  77. scenario.setup(() => {
  78. scenario.variableModel = {type: 'interval', query: '1s,2h,5h,1d', name: 'test'};
  79. });
  80. it('should update options array', () => {
  81. expect(scenario.variable.options.length).to.be(4);
  82. expect(scenario.variable.options[0].text).to.be('1s');
  83. expect(scenario.variable.options[0].value).to.be('1s');
  84. });
  85. });
  86. //
  87. // Interval variable update
  88. //
  89. describeUpdateVariable('interval variable with auto', scenario => {
  90. scenario.setup(() => {
  91. scenario.variableModel = {type: 'interval', query: '1s,2h,5h,1d', name: 'test', auto: true, auto_count: 10 };
  92. var range = {
  93. from: moment(new Date()).subtract(7, 'days').toDate(),
  94. to: new Date()
  95. };
  96. ctx.timeSrv.timeRange = sinon.stub().returns(range);
  97. ctx.templateSrv.setGrafanaVariable = sinon.spy();
  98. });
  99. it('should update options array', function() {
  100. expect(scenario.variable.options.length).to.be(5);
  101. expect(scenario.variable.options[0].text).to.be('auto');
  102. expect(scenario.variable.options[0].value).to.be('$__auto_interval');
  103. });
  104. it('should set $__auto_interval', function() {
  105. var call = ctx.templateSrv.setGrafanaVariable.getCall(0);
  106. expect(call.args[0]).to.be('$__auto_interval');
  107. expect(call.args[1]).to.be('12h');
  108. });
  109. });
  110. //
  111. // Query variable update
  112. //
  113. describeUpdateVariable('query variable with empty current object and refresh', function(scenario) {
  114. scenario.setup(function() {
  115. scenario.variableModel = {type: 'query', query: '', name: 'test', current: {}};
  116. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}];
  117. });
  118. it('should set current value to first option', function() {
  119. expect(scenario.variable.options.length).to.be(2);
  120. expect(scenario.variable.current.value).to.be('backend1');
  121. });
  122. });
  123. describeUpdateVariable('query variable with multi select and new options does not contain some selected values', function(scenario) {
  124. scenario.setup(function() {
  125. scenario.variableModel = {
  126. type: 'query',
  127. query: '',
  128. name: 'test',
  129. current: {
  130. value: ['val1', 'val2', 'val3'],
  131. text: 'val1 + val2 + val3'
  132. }
  133. };
  134. scenario.queryResult = [{text: 'val2'}, {text: 'val3'}];
  135. });
  136. it('should update current value', function() {
  137. expect(scenario.variable.current.value).to.eql(['val2', 'val3']);
  138. expect(scenario.variable.current.text).to.eql('val2 + val3');
  139. });
  140. });
  141. describeUpdateVariable('query variable with multi select and new options does not contain any selected values', function(scenario) {
  142. scenario.setup(function() {
  143. scenario.variableModel = {
  144. type: 'query',
  145. query: '',
  146. name: 'test',
  147. current: {
  148. value: ['val1', 'val2', 'val3'],
  149. text: 'val1 + val2 + val3'
  150. }
  151. };
  152. scenario.queryResult = [{text: 'val5'}, {text: 'val6'}];
  153. });
  154. it('should update current value with first one', function() {
  155. expect(scenario.variable.current.value).to.eql('val5');
  156. expect(scenario.variable.current.text).to.eql('val5');
  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 = { type: 'query', query: '', name: 'test', current: {} };
  181. scenario.queryResult = [{text: 12, value: 12}];
  182. });
  183. it('should set current value to first option', function() {
  184. expect(scenario.variable.current.value).to.be('12');
  185. expect(scenario.variable.options[0].value).to.be('12');
  186. expect(scenario.variable.options[0].text).to.be('12');
  187. });
  188. });
  189. describeUpdateVariable('basic query variable', function(scenario) {
  190. scenario.setup(function() {
  191. scenario.variableModel = { type: 'query', query: 'apps.*', name: 'test' };
  192. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}];
  193. });
  194. it('should update options array', function() {
  195. expect(scenario.variable.options.length).to.be(2);
  196. expect(scenario.variable.options[0].text).to.be('backend1');
  197. expect(scenario.variable.options[0].value).to.be('backend1');
  198. expect(scenario.variable.options[1].value).to.be('backend2');
  199. });
  200. it('should select first option as value', function() {
  201. expect(scenario.variable.current.value).to.be('backend1');
  202. });
  203. });
  204. describeUpdateVariable('and existing value still exists in options', function(scenario) {
  205. scenario.setup(function() {
  206. scenario.variableModel = {type: 'query', query: 'apps.*', name: 'test'};
  207. scenario.variableModel.current = { value: 'backend2', text: 'backend2'};
  208. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}];
  209. });
  210. it('should keep variable value', function() {
  211. expect(scenario.variable.current.text).to.be('backend2');
  212. });
  213. });
  214. describeUpdateVariable('and regex pattern exists', function(scenario) {
  215. scenario.setup(function() {
  216. scenario.variableModel = {type: 'query', query: 'apps.*', name: 'test'};
  217. scenario.variableModel.regex = '/apps.*(backend_[0-9]+)/';
  218. scenario.queryResult = [{text: 'apps.backend.backend_01.counters.req'}, {text: 'apps.backend.backend_02.counters.req'}];
  219. });
  220. it('should extract and use match group', function() {
  221. expect(scenario.variable.options[0].value).to.be('backend_01');
  222. });
  223. });
  224. describeUpdateVariable('and regex pattern exists and no match', function(scenario) {
  225. scenario.setup(function() {
  226. scenario.variableModel = {type: 'query', query: 'apps.*', name: 'test'};
  227. scenario.variableModel.regex = '/apps.*(backendasd[0-9]+)/';
  228. scenario.queryResult = [{text: 'apps.backend.backend_01.counters.req'}, {text: 'apps.backend.backend_02.counters.req'}];
  229. });
  230. it('should not add non matching items, None option should be added instead', function() {
  231. expect(scenario.variable.options.length).to.be(1);
  232. expect(scenario.variable.options[0].isNone).to.be(true);
  233. });
  234. });
  235. describeUpdateVariable('regex pattern without slashes', function(scenario) {
  236. scenario.setup(function() {
  237. scenario.variableModel = {type: 'query', query: 'apps.*', name: 'test'};
  238. scenario.variableModel.regex = 'backend_01';
  239. scenario.queryResult = [{text: 'apps.backend.backend_01.counters.req'}, {text: 'apps.backend.backend_02.counters.req'}];
  240. });
  241. it('should return matches options', function() {
  242. expect(scenario.variable.options.length).to.be(1);
  243. });
  244. });
  245. describeUpdateVariable('regex pattern remove duplicates', function(scenario) {
  246. scenario.setup(function() {
  247. scenario.variableModel = {type: 'query', query: 'apps.*', name: 'test'};
  248. scenario.variableModel.regex = 'backend_01';
  249. scenario.queryResult = [{text: 'apps.backend.backend_01.counters.req'}, {text: 'apps.backend.backend_01.counters.req'}];
  250. });
  251. it('should return matches options', function() {
  252. expect(scenario.variable.options.length).to.be(1);
  253. });
  254. });
  255. describeUpdateVariable('with include All', function(scenario) {
  256. scenario.setup(function() {
  257. scenario.variableModel = {type: 'query', query: 'apps.*', name: 'test', includeAll: true};
  258. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
  259. });
  260. it('should add All option', function() {
  261. expect(scenario.variable.options[0].text).to.be('All');
  262. expect(scenario.variable.options[0].value).to.be('$__all');
  263. });
  264. });
  265. describeUpdateVariable('with include all and custom value', function(scenario) {
  266. scenario.setup(function() {
  267. scenario.variableModel = {type: 'query', query: 'apps.*', name: 'test', includeAll: true, allValue: '*'};
  268. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
  269. });
  270. it('should add All option with custom value', function() {
  271. expect(scenario.variable.options[0].value).to.be('$__all');
  272. });
  273. });
  274. describeUpdateVariable('without sort', function(scenario) {
  275. scenario.setup(function() {
  276. scenario.variableModel = {type: 'query', query: 'apps.*', name: 'test', sort: 0};
  277. scenario.queryResult = [{text: 'bbb2'}, {text: 'aaa10'}, { text: 'ccc3'}];
  278. });
  279. it('should return options without sort', function() {
  280. expect(scenario.variable.options[0].text).to.be('bbb2');
  281. expect(scenario.variable.options[1].text).to.be('aaa10');
  282. expect(scenario.variable.options[2].text).to.be('ccc3');
  283. });
  284. });
  285. describeUpdateVariable('with alphabetical sort (asc)', function(scenario) {
  286. scenario.setup(function() {
  287. scenario.variableModel = {type: 'query', query: 'apps.*', name: 'test', sort: 1};
  288. scenario.queryResult = [{text: 'bbb2'}, {text: 'aaa10'}, { text: 'ccc3'}];
  289. });
  290. it('should return options with alphabetical sort', function() {
  291. expect(scenario.variable.options[0].text).to.be('aaa10');
  292. expect(scenario.variable.options[1].text).to.be('bbb2');
  293. expect(scenario.variable.options[2].text).to.be('ccc3');
  294. });
  295. });
  296. describeUpdateVariable('with alphabetical sort (desc)', function(scenario) {
  297. scenario.setup(function() {
  298. scenario.variableModel = {type: 'query', query: 'apps.*', name: 'test', sort: 2};
  299. scenario.queryResult = [{text: 'bbb2'}, {text: 'aaa10'}, { text: 'ccc3'}];
  300. });
  301. it('should return options with alphabetical sort', function() {
  302. expect(scenario.variable.options[0].text).to.be('ccc3');
  303. expect(scenario.variable.options[1].text).to.be('bbb2');
  304. expect(scenario.variable.options[2].text).to.be('aaa10');
  305. });
  306. });
  307. describeUpdateVariable('with numerical sort (asc)', function(scenario) {
  308. scenario.setup(function() {
  309. scenario.variableModel = {type: 'query', query: 'apps.*', name: 'test', sort: 3};
  310. scenario.queryResult = [{text: 'bbb2'}, {text: 'aaa10'}, { text: 'ccc3'}];
  311. });
  312. it('should return options with numerical sort', function() {
  313. expect(scenario.variable.options[0].text).to.be('bbb2');
  314. expect(scenario.variable.options[1].text).to.be('ccc3');
  315. expect(scenario.variable.options[2].text).to.be('aaa10');
  316. });
  317. });
  318. describeUpdateVariable('with numerical sort (desc)', function(scenario) {
  319. scenario.setup(function() {
  320. scenario.variableModel = {type: 'query', query: 'apps.*', name: 'test', sort: 4};
  321. scenario.queryResult = [{text: 'bbb2'}, {text: 'aaa10'}, { text: 'ccc3'}];
  322. });
  323. it('should return options with numerical sort', function() {
  324. expect(scenario.variable.options[0].text).to.be('aaa10');
  325. expect(scenario.variable.options[1].text).to.be('ccc3');
  326. expect(scenario.variable.options[2].text).to.be('bbb2');
  327. });
  328. });
  329. //
  330. // datasource variable update
  331. //
  332. describeUpdateVariable('datasource variable with regex filter', function(scenario) {
  333. scenario.setup(function() {
  334. scenario.variableModel = {
  335. type: 'datasource',
  336. query: 'graphite',
  337. name: 'test',
  338. current: {value: 'backend4_pee', text: 'backend4_pee'},
  339. regex: '/pee$/'
  340. };
  341. scenario.metricSources = [
  342. {name: 'backend1', meta: {id: 'influx'}},
  343. {name: 'backend2_pee', meta: {id: 'graphite'}},
  344. {name: 'backend3', meta: {id: 'graphite'}},
  345. {name: 'backend4_pee', meta: {id: 'graphite'}},
  346. ];
  347. });
  348. it('should set only contain graphite ds and filtered using regex', function() {
  349. expect(scenario.variable.options.length).to.be(2);
  350. expect(scenario.variable.options[0].value).to.be('backend2_pee');
  351. expect(scenario.variable.options[1].value).to.be('backend4_pee');
  352. });
  353. it('should keep current value if available', function() {
  354. expect(scenario.variable.current.value).to.be('backend4_pee');
  355. });
  356. });
  357. //
  358. // Custom variable update
  359. //
  360. describeUpdateVariable('update custom variable', function(scenario) {
  361. scenario.setup(function() {
  362. scenario.variableModel = {type: 'custom', query: 'hej, hop, asd', name: 'test'};
  363. });
  364. it('should update options array', function() {
  365. expect(scenario.variable.options.length).to.be(3);
  366. expect(scenario.variable.options[0].text).to.be('hej');
  367. expect(scenario.variable.options[1].value).to.be('hop');
  368. });
  369. });
  370. });