variable_srv_specs.ts 15 KB

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