variable_srv_specs.ts 15 KB

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