templateValuesSrv-specs.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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('interval variable with auto', function(scenario) {
  91. scenario.setup(function() {
  92. scenario.variable = { type: 'interval', query: '1s,2h,5h,1d', name: 'test', auto: true, auto_count: 10 };
  93. var range = {
  94. from: moment(new Date()).subtract(7, 'days').toDate(),
  95. to: new Date()
  96. };
  97. ctx.timeSrv.timeRange = sinon.stub().returns(range);
  98. ctx.templateSrv.setGrafanaVariable = sinon.spy();
  99. });
  100. it('should update options array', function() {
  101. expect(scenario.variable.options.length).to.be(5);
  102. expect(scenario.variable.options[0].text).to.be('auto');
  103. expect(scenario.variable.options[0].value).to.be('$__auto_interval');
  104. });
  105. it('should set $__auto_interval', function() {
  106. var call = ctx.templateSrv.setGrafanaVariable.getCall(0);
  107. expect(call.args[0]).to.be('$__auto_interval');
  108. expect(call.args[1]).to.be('12h');
  109. });
  110. });
  111. describeUpdateVariable('update custom variable', function(scenario) {
  112. scenario.setup(function() {
  113. scenario.variable = { type: 'custom', query: 'hej, hop, asd', name: 'test'};
  114. });
  115. it('should update options array', function() {
  116. expect(scenario.variable.options.length).to.be(3);
  117. expect(scenario.variable.options[0].text).to.be('hej');
  118. expect(scenario.variable.options[1].value).to.be('hop');
  119. });
  120. it('should set $__auto_interval', function() {
  121. var call = ctx.templateSrv.setGrafanaVariable.getCall(0);
  122. expect(call.args[0]).to.be('$__auto_interval');
  123. expect(call.args[1]).to.be('12h');
  124. });
  125. });
  126. describeUpdateVariable('basic query variable', function(scenario) {
  127. scenario.setup(function() {
  128. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  129. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}];
  130. });
  131. it('should update options array', function() {
  132. expect(scenario.variable.options.length).to.be(2);
  133. expect(scenario.variable.options[0].text).to.be('backend1');
  134. expect(scenario.variable.options[0].value).to.be('backend1');
  135. expect(scenario.variable.options[1].value).to.be('backend2');
  136. });
  137. it('should select first option as value', function() {
  138. expect(scenario.variable.current.value).to.be('backend1');
  139. });
  140. });
  141. describeUpdateVariable('and existing value still exists in options', function(scenario) {
  142. scenario.setup(function() {
  143. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  144. scenario.variable.current = { text: 'backend2'};
  145. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}];
  146. });
  147. it('should keep variable value', function() {
  148. expect(scenario.variable.current.text).to.be('backend2');
  149. });
  150. });
  151. describeUpdateVariable('and regex pattern exists', function(scenario) {
  152. scenario.setup(function() {
  153. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  154. scenario.variable.regex = '/apps.*(backend_[0-9]+)/';
  155. scenario.queryResult = [{text: 'apps.backend.backend_01.counters.req'}, {text: 'apps.backend.backend_02.counters.req'}];
  156. });
  157. it('should extract and use match group', function() {
  158. expect(scenario.variable.options[0].value).to.be('backend_01');
  159. });
  160. });
  161. describeUpdateVariable('and regex pattern exists and no match', function(scenario) {
  162. scenario.setup(function() {
  163. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  164. scenario.variable.regex = '/apps.*(backendasd[0-9]+)/';
  165. scenario.queryResult = [{text: 'apps.backend.backend_01.counters.req'}, {text: 'apps.backend.backend_02.counters.req'}];
  166. });
  167. it('should not add non matching items', function() {
  168. expect(scenario.variable.options.length).to.be(0);
  169. });
  170. });
  171. describeUpdateVariable('regex pattern without slashes', function(scenario) {
  172. scenario.setup(function() {
  173. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  174. scenario.variable.regex = 'backend_01';
  175. scenario.queryResult = [{text: 'apps.backend.backend_01.counters.req'}, {text: 'apps.backend.backend_02.counters.req'}];
  176. });
  177. it('should return matches options', function() {
  178. expect(scenario.variable.options.length).to.be(1);
  179. });
  180. });
  181. describeUpdateVariable('regex pattern remove duplicates', function(scenario) {
  182. scenario.setup(function() {
  183. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  184. scenario.variable.regex = 'backend_01';
  185. scenario.queryResult = [{text: 'apps.backend.backend_01.counters.req'}, {text: 'apps.backend.backend_01.counters.req'}];
  186. });
  187. it('should return matches options', function() {
  188. expect(scenario.variable.options.length).to.be(1);
  189. });
  190. });
  191. describeUpdateVariable('with include All glob syntax', function(scenario) {
  192. scenario.setup(function() {
  193. scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'glob' };
  194. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
  195. });
  196. it('should add All Glob option', function() {
  197. expect(scenario.variable.options[0].value).to.be('{backend1,backend2,backend3}');
  198. });
  199. });
  200. describeUpdateVariable('with include all wildcard', function(scenario) {
  201. scenario.setup(function() {
  202. scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'wildcard' };
  203. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
  204. });
  205. it('should add All wildcard option', function() {
  206. expect(scenario.variable.options[0].value).to.be('*');
  207. });
  208. });
  209. describeUpdateVariable('with include all wildcard', function(scenario) {
  210. scenario.setup(function() {
  211. scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'regex wildcard' };
  212. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
  213. });
  214. it('should add All wildcard option', function() {
  215. expect(scenario.variable.options[0].value).to.be('.*');
  216. });
  217. });
  218. describeUpdateVariable('with include all regex values', function(scenario) {
  219. scenario.setup(function() {
  220. scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'wildcard' };
  221. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
  222. });
  223. it('should add All wildcard option', function() {
  224. expect(scenario.variable.options[0].value).to.be('*');
  225. });
  226. });
  227. describeUpdateVariable('with include all glob no values', function(scenario) {
  228. scenario.setup(function() {
  229. scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'glob' };
  230. scenario.queryResult = [];
  231. });
  232. it('should add empty glob', function() {
  233. expect(scenario.variable.options[0].value).to.be('{}');
  234. });
  235. });
  236. describeUpdateVariable('with include all regex all values', function(scenario) {
  237. scenario.setup(function() {
  238. scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'regex values' };
  239. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
  240. });
  241. it('should add empty glob', function() {
  242. expect(scenario.variable.options[0].value).to.be('(backend1|backend2|backend3)');
  243. });
  244. });
  245. });
  246. });