templateValuesSrv-specs.js 15 KB

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