templateValuesSrv-specs.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. define([
  2. 'mocks/dashboard-mock',
  3. 'helpers',
  4. 'moment',
  5. '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'}]
  48. };
  49. beforeEach(function() {
  50. var dashboard = { templating: { list: [variable] } };
  51. var urlParams = {};
  52. urlParams["var-apps"] = ["val1", "val2"];
  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("val1");
  59. expect(variable.current.value[1]).to.be("val2");
  60. expect(variable.current.text).to.be("val1 + val2");
  61. expect(variable.options[0].selected).to.be(true);
  62. expect(variable.options[1].selected).to.be(true);
  63. });
  64. });
  65. function describeUpdateVariable(desc, fn) {
  66. describe(desc, function() {
  67. var scenario = {};
  68. scenario.setup = function(setupFn) {
  69. scenario.setupFn = setupFn;
  70. };
  71. beforeEach(function() {
  72. scenario.setupFn();
  73. var ds = {};
  74. ds.metricFindQuery = sinon.stub().returns(ctx.$q.when(scenario.queryResult));
  75. ctx.datasourceSrv.get = sinon.stub().returns(ctx.$q.when(ds));
  76. ctx.service.updateOptions(scenario.variable);
  77. ctx.$rootScope.$digest();
  78. });
  79. fn(scenario);
  80. });
  81. }
  82. describeUpdateVariable('interval variable without auto', function(scenario) {
  83. scenario.setup(function() {
  84. scenario.variable = { type: 'interval', query: '1s,2h,5h,1d', name: 'test' };
  85. });
  86. it('should update options array', function() {
  87. expect(scenario.variable.options.length).to.be(4);
  88. expect(scenario.variable.options[0].text).to.be('1s');
  89. expect(scenario.variable.options[0].value).to.be('1s');
  90. });
  91. });
  92. describeUpdateVariable('query variable with empty current object and refresh', function(scenario) {
  93. scenario.setup(function() {
  94. scenario.variable = { type: 'query', query: '', name: 'test', current: {} };
  95. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}];
  96. });
  97. it('should set current value to first option', function() {
  98. expect(scenario.variable.options.length).to.be(2);
  99. expect(scenario.variable.current.value).to.be('backend1');
  100. });
  101. });
  102. describeUpdateVariable('interval variable without auto', function(scenario) {
  103. scenario.setup(function() {
  104. scenario.variable = { type: 'interval', query: '1s,2h,5h,1d', name: 'test' };
  105. });
  106. it('should update options array', function() {
  107. expect(scenario.variable.options.length).to.be(4);
  108. expect(scenario.variable.options[0].text).to.be('1s');
  109. expect(scenario.variable.options[0].value).to.be('1s');
  110. });
  111. });
  112. describeUpdateVariable('interval variable with auto', function(scenario) {
  113. scenario.setup(function() {
  114. scenario.variable = { type: 'interval', query: '1s,2h,5h,1d', name: 'test', auto: true, auto_count: 10 };
  115. var range = {
  116. from: moment(new Date()).subtract(7, 'days').toDate(),
  117. to: new Date()
  118. };
  119. ctx.timeSrv.timeRange = sinon.stub().returns(range);
  120. ctx.templateSrv.setGrafanaVariable = sinon.spy();
  121. });
  122. it('should update options array', function() {
  123. expect(scenario.variable.options.length).to.be(5);
  124. expect(scenario.variable.options[0].text).to.be('auto');
  125. expect(scenario.variable.options[0].value).to.be('$__auto_interval');
  126. });
  127. it('should set $__auto_interval', function() {
  128. var call = ctx.templateSrv.setGrafanaVariable.getCall(0);
  129. expect(call.args[0]).to.be('$__auto_interval');
  130. expect(call.args[1]).to.be('12h');
  131. });
  132. });
  133. describeUpdateVariable('update custom variable', function(scenario) {
  134. scenario.setup(function() {
  135. scenario.variable = { type: 'custom', query: 'hej, hop, asd', name: 'test'};
  136. });
  137. it('should update options array', function() {
  138. expect(scenario.variable.options.length).to.be(3);
  139. expect(scenario.variable.options[0].text).to.be('hej');
  140. expect(scenario.variable.options[1].value).to.be('hop');
  141. });
  142. it('should set $__auto_interval', function() {
  143. var call = ctx.templateSrv.setGrafanaVariable.getCall(0);
  144. expect(call.args[0]).to.be('$__auto_interval');
  145. expect(call.args[1]).to.be('12h');
  146. });
  147. });
  148. describeUpdateVariable('basic query variable', function(scenario) {
  149. scenario.setup(function() {
  150. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  151. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}];
  152. });
  153. it('should update options array', function() {
  154. expect(scenario.variable.options.length).to.be(2);
  155. expect(scenario.variable.options[0].text).to.be('backend1');
  156. expect(scenario.variable.options[0].value).to.be('backend1');
  157. expect(scenario.variable.options[1].value).to.be('backend2');
  158. });
  159. it('should select first option as value', function() {
  160. expect(scenario.variable.current.value).to.be('backend1');
  161. });
  162. });
  163. describeUpdateVariable('and existing value still exists in options', function(scenario) {
  164. scenario.setup(function() {
  165. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  166. scenario.variable.current = { value: 'backend2', text: 'backend2'};
  167. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}];
  168. });
  169. it('should keep variable value', function() {
  170. expect(scenario.variable.current.text).to.be('backend2');
  171. });
  172. });
  173. describeUpdateVariable('and regex pattern exists', function(scenario) {
  174. scenario.setup(function() {
  175. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  176. scenario.variable.regex = '/apps.*(backend_[0-9]+)/';
  177. scenario.queryResult = [{text: 'apps.backend.backend_01.counters.req'}, {text: 'apps.backend.backend_02.counters.req'}];
  178. });
  179. it('should extract and use match group', function() {
  180. expect(scenario.variable.options[0].value).to.be('backend_01');
  181. });
  182. });
  183. describeUpdateVariable('and regex pattern exists and no match', function(scenario) {
  184. scenario.setup(function() {
  185. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  186. scenario.variable.regex = '/apps.*(backendasd[0-9]+)/';
  187. scenario.queryResult = [{text: 'apps.backend.backend_01.counters.req'}, {text: 'apps.backend.backend_02.counters.req'}];
  188. });
  189. it('should not add non matching items, None option should be added instead', function() {
  190. expect(scenario.variable.options.length).to.be(1);
  191. expect(scenario.variable.options[0].isNone).to.be(true);
  192. });
  193. });
  194. describeUpdateVariable('regex pattern without slashes', function(scenario) {
  195. scenario.setup(function() {
  196. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  197. scenario.variable.regex = 'backend_01';
  198. scenario.queryResult = [{text: 'apps.backend.backend_01.counters.req'}, {text: 'apps.backend.backend_02.counters.req'}];
  199. });
  200. it('should return matches options', function() {
  201. expect(scenario.variable.options.length).to.be(1);
  202. });
  203. });
  204. describeUpdateVariable('regex pattern remove duplicates', function(scenario) {
  205. scenario.setup(function() {
  206. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  207. scenario.variable.regex = 'backend_01';
  208. scenario.queryResult = [{text: 'apps.backend.backend_01.counters.req'}, {text: 'apps.backend.backend_01.counters.req'}];
  209. });
  210. it('should return matches options', function() {
  211. expect(scenario.variable.options.length).to.be(1);
  212. });
  213. });
  214. describeUpdateVariable('with include All glob syntax', function(scenario) {
  215. scenario.setup(function() {
  216. scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'glob' };
  217. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
  218. });
  219. it('should add All Glob option', function() {
  220. expect(scenario.variable.options[0].value).to.be('{backend1,backend2,backend3}');
  221. });
  222. });
  223. describeUpdateVariable('with include all wildcard', function(scenario) {
  224. scenario.setup(function() {
  225. scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'wildcard' };
  226. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
  227. });
  228. it('should add All wildcard option', function() {
  229. expect(scenario.variable.options[0].value).to.be('*');
  230. });
  231. });
  232. describeUpdateVariable('with include all wildcard', function(scenario) {
  233. scenario.setup(function() {
  234. scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'regex wildcard' };
  235. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
  236. });
  237. it('should add All wildcard option', function() {
  238. expect(scenario.variable.options[0].value).to.be('.*');
  239. });
  240. });
  241. describeUpdateVariable('with include all regex values', function(scenario) {
  242. scenario.setup(function() {
  243. scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'wildcard' };
  244. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
  245. });
  246. it('should add All wildcard option', function() {
  247. expect(scenario.variable.options[0].value).to.be('*');
  248. });
  249. });
  250. describeUpdateVariable('with include all glob no values', function(scenario) {
  251. scenario.setup(function() {
  252. scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'glob' };
  253. scenario.queryResult = [];
  254. });
  255. it('should add empty glob', function() {
  256. expect(scenario.variable.options[0].value).to.be('{}');
  257. });
  258. });
  259. describeUpdateVariable('with include all regex all values', function(scenario) {
  260. scenario.setup(function() {
  261. scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'regex values' };
  262. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
  263. });
  264. it('should add empty glob', function() {
  265. expect(scenario.variable.options[0].value).to.be('(backend1|backend2|backend3)');
  266. });
  267. });
  268. describeUpdateVariable('with include all regex values and values require escaping', function(scenario) {
  269. scenario.setup(function() {
  270. scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'regex values' };
  271. scenario.queryResult = [{text: '/root'}, {text: '/var'}, { text: '/lib'}];
  272. });
  273. it('should regex escape options', function() {
  274. expect(scenario.variable.options[0].value).to.be('(\\/lib|\\/root|\\/var)');
  275. expect(scenario.variable.options[1].value).to.be('\\/lib');
  276. expect(scenario.variable.options[1].text).to.be('/lib');
  277. });
  278. });
  279. });
  280. });