templateValuesSrv-specs.js 12 KB

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