templateValuesSrv-specs.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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() {
  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);
  36. });
  37. it('should update current value', function() {
  38. expect(variable.current.value).to.be("new");
  39. expect(variable.current.text).to.be("new");
  40. });
  41. });
  42. describe('when template variable is present in url multiple times', function() {
  43. var variable = {
  44. name: 'apps',
  45. multi: true,
  46. current: {text: "val1", value: "val1"},
  47. options: [{text: "val1", value: "val1"}, {text: 'val2', value: 'val2'}, {text: 'val3', value: 'val3', selected: true}]
  48. };
  49. beforeEach(function() {
  50. var dashboard = { templating: { list: [variable] } };
  51. var urlParams = {};
  52. urlParams["var-apps"] = ["val2", "val1"];
  53. ctx.$location.search = sinon.stub().returns(urlParams);
  54. ctx.service.init(dashboard);
  55. });
  56. it('should update current value', function() {
  57. expect(variable.current.value.length).to.be(2);
  58. expect(variable.current.value[0]).to.be("val2");
  59. expect(variable.current.value[1]).to.be("val1");
  60. expect(variable.current.text).to.be("val2 + val1");
  61. expect(variable.options[0].selected).to.be(true);
  62. expect(variable.options[1].selected).to.be(true);
  63. });
  64. it('should set options that are not in value to selected false', function() {
  65. expect(variable.options[2].selected).to.be(false);
  66. });
  67. });
  68. function describeUpdateVariable(desc, fn) {
  69. describe(desc, function() {
  70. var scenario = {};
  71. scenario.setup = function(setupFn) {
  72. scenario.setupFn = setupFn;
  73. };
  74. beforeEach(function() {
  75. scenario.setupFn();
  76. var ds = {};
  77. ds.metricFindQuery = sinon.stub().returns(ctx.$q.when(scenario.queryResult));
  78. ctx.datasourceSrv.get = sinon.stub().returns(ctx.$q.when(ds));
  79. ctx.service.updateOptions(scenario.variable);
  80. ctx.$rootScope.$digest();
  81. });
  82. fn(scenario);
  83. });
  84. }
  85. describeUpdateVariable('interval variable without auto', function(scenario) {
  86. scenario.setup(function() {
  87. scenario.variable = { type: 'interval', query: '1s,2h,5h,1d', name: 'test' };
  88. });
  89. it('should update options array', function() {
  90. expect(scenario.variable.options.length).to.be(4);
  91. expect(scenario.variable.options[0].text).to.be('1s');
  92. expect(scenario.variable.options[0].value).to.be('1s');
  93. });
  94. });
  95. describeUpdateVariable('query variable with empty current object and refresh', function(scenario) {
  96. scenario.setup(function() {
  97. scenario.variable = { type: 'query', query: '', name: 'test', current: {} };
  98. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}];
  99. });
  100. it('should set current value to first option', function() {
  101. expect(scenario.variable.options.length).to.be(2);
  102. expect(scenario.variable.current.value).to.be('backend1');
  103. });
  104. });
  105. describeUpdateVariable('interval variable without auto', function(scenario) {
  106. scenario.setup(function() {
  107. scenario.variable = { type: 'interval', query: '1s,2h,5h,1d', name: 'test' };
  108. });
  109. it('should update options array', function() {
  110. expect(scenario.variable.options.length).to.be(4);
  111. expect(scenario.variable.options[0].text).to.be('1s');
  112. expect(scenario.variable.options[0].value).to.be('1s');
  113. });
  114. });
  115. describeUpdateVariable('interval variable with auto', function(scenario) {
  116. scenario.setup(function() {
  117. scenario.variable = { type: 'interval', query: '1s,2h,5h,1d', name: 'test', auto: true, auto_count: 10 };
  118. var range = {
  119. from: moment(new Date()).subtract(7, 'days').toDate(),
  120. to: new Date()
  121. };
  122. ctx.timeSrv.timeRange = sinon.stub().returns(range);
  123. ctx.templateSrv.setGrafanaVariable = sinon.spy();
  124. });
  125. it('should update options array', function() {
  126. expect(scenario.variable.options.length).to.be(5);
  127. expect(scenario.variable.options[0].text).to.be('auto');
  128. expect(scenario.variable.options[0].value).to.be('$__auto_interval');
  129. });
  130. it('should set $__auto_interval', function() {
  131. var call = ctx.templateSrv.setGrafanaVariable.getCall(0);
  132. expect(call.args[0]).to.be('$__auto_interval');
  133. expect(call.args[1]).to.be('12h');
  134. });
  135. });
  136. describeUpdateVariable('update custom variable', function(scenario) {
  137. scenario.setup(function() {
  138. scenario.variable = { type: 'custom', query: 'hej, hop, asd', name: 'test'};
  139. });
  140. it('should update options array', function() {
  141. expect(scenario.variable.options.length).to.be(3);
  142. expect(scenario.variable.options[0].text).to.be('hej');
  143. expect(scenario.variable.options[1].value).to.be('hop');
  144. });
  145. it('should set $__auto_interval', function() {
  146. var call = ctx.templateSrv.setGrafanaVariable.getCall(0);
  147. expect(call.args[0]).to.be('$__auto_interval');
  148. expect(call.args[1]).to.be('12h');
  149. });
  150. });
  151. describeUpdateVariable('basic query variable', function(scenario) {
  152. scenario.setup(function() {
  153. scenario.variable = { 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.variable = { type: 'query', query: 'apps.*', name: 'test' };
  169. scenario.variable.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.variable = { type: 'query', query: 'apps.*', name: 'test' };
  179. scenario.variable.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.variable = { type: 'query', query: 'apps.*', name: 'test' };
  189. scenario.variable.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.variable = { type: 'query', query: 'apps.*', name: 'test' };
  200. scenario.variable.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.variable = { type: 'query', query: 'apps.*', name: 'test' };
  210. scenario.variable.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 glob syntax', function(scenario) {
  218. scenario.setup(function() {
  219. scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'glob' };
  220. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
  221. });
  222. it('should add All Glob option', function() {
  223. expect(scenario.variable.options[0].value).to.be('{backend1,backend2,backend3}');
  224. });
  225. });
  226. describeUpdateVariable('with include all wildcard', function(scenario) {
  227. scenario.setup(function() {
  228. scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'wildcard' };
  229. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
  230. });
  231. it('should add All wildcard option', function() {
  232. expect(scenario.variable.options[0].value).to.be('*');
  233. });
  234. });
  235. describeUpdateVariable('with include all wildcard', function(scenario) {
  236. scenario.setup(function() {
  237. scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'regex wildcard' };
  238. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
  239. });
  240. it('should add All wildcard option', function() {
  241. expect(scenario.variable.options[0].value).to.be('.*');
  242. });
  243. });
  244. describeUpdateVariable('with include all regex values', function(scenario) {
  245. scenario.setup(function() {
  246. scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'wildcard' };
  247. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
  248. });
  249. it('should add All wildcard option', function() {
  250. expect(scenario.variable.options[0].value).to.be('*');
  251. });
  252. });
  253. describeUpdateVariable('with include all glob no values', function(scenario) {
  254. scenario.setup(function() {
  255. scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'glob' };
  256. scenario.queryResult = [];
  257. });
  258. it('should add empty glob', function() {
  259. expect(scenario.variable.options[0].value).to.be('{}');
  260. });
  261. });
  262. describeUpdateVariable('with include all lucene and values', function(scenario) {
  263. scenario.setup(function() {
  264. scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'lucene' };
  265. scenario.queryResult = [{text: 'backend1'}, { text: 'backend2'}];
  266. });
  267. it('should add lucene glob', function() {
  268. expect(scenario.variable.options[0].value).to.be('(\\\"backend1\\\" OR \\\"backend2\\\")');
  269. });
  270. });
  271. describeUpdateVariable('with include all regex all values', function(scenario) {
  272. scenario.setup(function() {
  273. scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'regex values' };
  274. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
  275. });
  276. it('should add empty glob', function() {
  277. expect(scenario.variable.options[0].value).to.be('(backend1|backend2|backend3)');
  278. });
  279. });
  280. describeUpdateVariable('with include all regex values and values require escaping', function(scenario) {
  281. scenario.setup(function() {
  282. scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'regex values' };
  283. scenario.queryResult = [{text: '/root'}, {text: '/var'}, { text: '/lib'}];
  284. });
  285. it('should regex escape options', function() {
  286. expect(scenario.variable.options[0].value).to.be('(\\/lib|\\/root|\\/var)');
  287. expect(scenario.variable.options[1].value).to.be('\\/lib');
  288. expect(scenario.variable.options[1].text).to.be('/lib');
  289. });
  290. });
  291. describeUpdateVariable('with include all pipe all values', function(scenario) {
  292. scenario.setup(function() {
  293. scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'pipe' };
  294. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
  295. });
  296. it('should add pipe delimited string', function() {
  297. expect(scenario.variable.options[0].value).to.be('backend1|backend2|backend3');
  298. });
  299. });
  300. });
  301. });