templateValuesSrv-specs.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 numeric results', function(scenario) {
  145. scenario.setup(function() {
  146. scenario.variable = { 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('interval variable without auto', function(scenario) {
  156. scenario.setup(function() {
  157. scenario.variable = { type: 'interval', query: '1s,2h,5h,1d', name: 'test' };
  158. });
  159. it('should update options array', function() {
  160. expect(scenario.variable.options.length).to.be(4);
  161. expect(scenario.variable.options[0].text).to.be('1s');
  162. expect(scenario.variable.options[0].value).to.be('1s');
  163. });
  164. });
  165. describeUpdateVariable('interval variable with auto', function(scenario) {
  166. scenario.setup(function() {
  167. scenario.variable = { type: 'interval', query: '1s,2h,5h,1d', name: 'test', auto: true, auto_count: 10 };
  168. var range = {
  169. from: moment(new Date()).subtract(7, 'days').toDate(),
  170. to: new Date()
  171. };
  172. ctx.timeSrv.timeRange = sinon.stub().returns(range);
  173. ctx.templateSrv.setGrafanaVariable = sinon.spy();
  174. });
  175. it('should update options array', function() {
  176. expect(scenario.variable.options.length).to.be(5);
  177. expect(scenario.variable.options[0].text).to.be('auto');
  178. expect(scenario.variable.options[0].value).to.be('$__auto_interval');
  179. });
  180. it('should set $__auto_interval', function() {
  181. var call = ctx.templateSrv.setGrafanaVariable.getCall(0);
  182. expect(call.args[0]).to.be('$__auto_interval');
  183. expect(call.args[1]).to.be('12h');
  184. });
  185. });
  186. describeUpdateVariable('update custom variable', function(scenario) {
  187. scenario.setup(function() {
  188. scenario.variable = {type: 'custom', query: 'hej, hop, asd', name: 'test'};
  189. });
  190. it('should update options array', function() {
  191. expect(scenario.variable.options.length).to.be(3);
  192. expect(scenario.variable.options[0].text).to.be('hej');
  193. expect(scenario.variable.options[1].value).to.be('hop');
  194. });
  195. it('should set $__auto_interval', function() {
  196. var call = ctx.templateSrv.setGrafanaVariable.getCall(0);
  197. expect(call.args[0]).to.be('$__auto_interval');
  198. expect(call.args[1]).to.be('12h');
  199. });
  200. });
  201. describeUpdateVariable('basic query variable', function(scenario) {
  202. scenario.setup(function() {
  203. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  204. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}];
  205. });
  206. it('should update options array', function() {
  207. expect(scenario.variable.options.length).to.be(2);
  208. expect(scenario.variable.options[0].text).to.be('backend1');
  209. expect(scenario.variable.options[0].value).to.be('backend1');
  210. expect(scenario.variable.options[1].value).to.be('backend2');
  211. });
  212. it('should select first option as value', function() {
  213. expect(scenario.variable.current.value).to.be('backend1');
  214. });
  215. });
  216. describeUpdateVariable('and existing value still exists in options', function(scenario) {
  217. scenario.setup(function() {
  218. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  219. scenario.variable.current = { value: 'backend2', text: 'backend2'};
  220. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}];
  221. });
  222. it('should keep variable value', function() {
  223. expect(scenario.variable.current.text).to.be('backend2');
  224. });
  225. });
  226. describeUpdateVariable('and regex pattern exists', function(scenario) {
  227. scenario.setup(function() {
  228. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  229. scenario.variable.regex = '/apps.*(backend_[0-9]+)/';
  230. scenario.queryResult = [{text: 'apps.backend.backend_01.counters.req'}, {text: 'apps.backend.backend_02.counters.req'}];
  231. });
  232. it('should extract and use match group', function() {
  233. expect(scenario.variable.options[0].value).to.be('backend_01');
  234. });
  235. });
  236. describeUpdateVariable('and regex pattern exists and no match', function(scenario) {
  237. scenario.setup(function() {
  238. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  239. scenario.variable.regex = '/apps.*(backendasd[0-9]+)/';
  240. scenario.queryResult = [{text: 'apps.backend.backend_01.counters.req'}, {text: 'apps.backend.backend_02.counters.req'}];
  241. });
  242. it('should not add non matching items, None option should be added instead', function() {
  243. expect(scenario.variable.options.length).to.be(1);
  244. expect(scenario.variable.options[0].isNone).to.be(true);
  245. });
  246. });
  247. describeUpdateVariable('regex pattern without slashes', function(scenario) {
  248. scenario.setup(function() {
  249. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  250. scenario.variable.regex = 'backend_01';
  251. scenario.queryResult = [{text: 'apps.backend.backend_01.counters.req'}, {text: 'apps.backend.backend_02.counters.req'}];
  252. });
  253. it('should return matches options', function() {
  254. expect(scenario.variable.options.length).to.be(1);
  255. });
  256. });
  257. describeUpdateVariable('regex pattern remove duplicates', function(scenario) {
  258. scenario.setup(function() {
  259. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  260. scenario.variable.regex = 'backend_01';
  261. scenario.queryResult = [{text: 'apps.backend.backend_01.counters.req'}, {text: 'apps.backend.backend_01.counters.req'}];
  262. });
  263. it('should return matches options', function() {
  264. expect(scenario.variable.options.length).to.be(1);
  265. });
  266. });
  267. describeUpdateVariable('with include All', function(scenario) {
  268. scenario.setup(function() {
  269. scenario.variable = {type: 'query', query: 'apps.*', name: 'test', includeAll: true};
  270. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
  271. });
  272. it('should add All option', function() {
  273. expect(scenario.variable.options[0].text).to.be('All');
  274. expect(scenario.variable.options[0].value).to.be('$__all');
  275. });
  276. });
  277. describeUpdateVariable('with include all and custom value', function(scenario) {
  278. scenario.setup(function() {
  279. scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allValue: '*' };
  280. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
  281. });
  282. it('should add All option with custom value', function() {
  283. expect(scenario.variable.options[0].value).to.be('$__all');
  284. });
  285. });
  286. describeUpdateVariable('datasource variable with regex filter', function(scenario) {
  287. scenario.setup(function() {
  288. scenario.variable = {
  289. type: 'datasource',
  290. query: 'graphite',
  291. name: 'test',
  292. current: {value: 'backend4_pee', text: 'backend4_pee'},
  293. regex: '/pee$/'
  294. };
  295. scenario.metricSources = [
  296. {name: 'backend1', meta: {id: 'influx'}},
  297. {name: 'backend2_pee', meta: {id: 'graphite'}},
  298. {name: 'backend3', meta: {id: 'graphite'}},
  299. {name: 'backend4_pee', meta: {id: 'graphite'}},
  300. ];
  301. });
  302. it('should set only contain graphite ds and filtered using regex', function() {
  303. expect(scenario.variable.options.length).to.be(2);
  304. expect(scenario.variable.options[0].value).to.be('backend2_pee');
  305. expect(scenario.variable.options[1].value).to.be('backend4_pee');
  306. });
  307. it('should keep current value if available', function() {
  308. expect(scenario.variable.current.value).to.be('backend4_pee');
  309. });
  310. });
  311. });
  312. });