templateValuesSrv-specs.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 numeric results', function(scenario) {
  109. scenario.setup(function() {
  110. scenario.variable = { type: 'query', query: '', name: 'test', current: {} };
  111. scenario.queryResult = [{text: 12, value: 12}];
  112. });
  113. it('should set current value to first option', function() {
  114. expect(scenario.variable.current.value).to.be('12');
  115. expect(scenario.variable.options[0].value).to.be('12');
  116. expect(scenario.variable.options[0].text).to.be('12');
  117. });
  118. });
  119. describeUpdateVariable('interval variable without auto', function(scenario) {
  120. scenario.setup(function() {
  121. scenario.variable = { type: 'interval', query: '1s,2h,5h,1d', name: 'test' };
  122. });
  123. it('should update options array', function() {
  124. expect(scenario.variable.options.length).to.be(4);
  125. expect(scenario.variable.options[0].text).to.be('1s');
  126. expect(scenario.variable.options[0].value).to.be('1s');
  127. });
  128. });
  129. describeUpdateVariable('interval variable with auto', function(scenario) {
  130. scenario.setup(function() {
  131. scenario.variable = { type: 'interval', query: '1s,2h,5h,1d', name: 'test', auto: true, auto_count: 10 };
  132. var range = {
  133. from: moment(new Date()).subtract(7, 'days').toDate(),
  134. to: new Date()
  135. };
  136. ctx.timeSrv.timeRange = sinon.stub().returns(range);
  137. ctx.templateSrv.setGrafanaVariable = sinon.spy();
  138. });
  139. it('should update options array', function() {
  140. expect(scenario.variable.options.length).to.be(5);
  141. expect(scenario.variable.options[0].text).to.be('auto');
  142. expect(scenario.variable.options[0].value).to.be('$__auto_interval');
  143. });
  144. it('should set $__auto_interval', function() {
  145. var call = ctx.templateSrv.setGrafanaVariable.getCall(0);
  146. expect(call.args[0]).to.be('$__auto_interval');
  147. expect(call.args[1]).to.be('12h');
  148. });
  149. });
  150. describeUpdateVariable('update custom variable', function(scenario) {
  151. scenario.setup(function() {
  152. scenario.variable = {type: 'custom', query: 'hej, hop, asd', name: 'test'};
  153. });
  154. it('should update options array', function() {
  155. expect(scenario.variable.options.length).to.be(3);
  156. expect(scenario.variable.options[0].text).to.be('hej');
  157. expect(scenario.variable.options[1].value).to.be('hop');
  158. });
  159. it('should set $__auto_interval', function() {
  160. var call = ctx.templateSrv.setGrafanaVariable.getCall(0);
  161. expect(call.args[0]).to.be('$__auto_interval');
  162. expect(call.args[1]).to.be('12h');
  163. });
  164. });
  165. describeUpdateVariable('basic query variable', function(scenario) {
  166. scenario.setup(function() {
  167. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  168. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}];
  169. });
  170. it('should update options array', function() {
  171. expect(scenario.variable.options.length).to.be(2);
  172. expect(scenario.variable.options[0].text).to.be('backend1');
  173. expect(scenario.variable.options[0].value).to.be('backend1');
  174. expect(scenario.variable.options[1].value).to.be('backend2');
  175. });
  176. it('should select first option as value', function() {
  177. expect(scenario.variable.current.value).to.be('backend1');
  178. });
  179. });
  180. describeUpdateVariable('and existing value still exists in options', function(scenario) {
  181. scenario.setup(function() {
  182. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  183. scenario.variable.current = { value: 'backend2', text: 'backend2'};
  184. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}];
  185. });
  186. it('should keep variable value', function() {
  187. expect(scenario.variable.current.text).to.be('backend2');
  188. });
  189. });
  190. describeUpdateVariable('and regex pattern exists', function(scenario) {
  191. scenario.setup(function() {
  192. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  193. scenario.variable.regex = '/apps.*(backend_[0-9]+)/';
  194. scenario.queryResult = [{text: 'apps.backend.backend_01.counters.req'}, {text: 'apps.backend.backend_02.counters.req'}];
  195. });
  196. it('should extract and use match group', function() {
  197. expect(scenario.variable.options[0].value).to.be('backend_01');
  198. });
  199. });
  200. describeUpdateVariable('and regex pattern exists and no match', function(scenario) {
  201. scenario.setup(function() {
  202. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  203. scenario.variable.regex = '/apps.*(backendasd[0-9]+)/';
  204. scenario.queryResult = [{text: 'apps.backend.backend_01.counters.req'}, {text: 'apps.backend.backend_02.counters.req'}];
  205. });
  206. it('should not add non matching items, None option should be added instead', function() {
  207. expect(scenario.variable.options.length).to.be(1);
  208. expect(scenario.variable.options[0].isNone).to.be(true);
  209. });
  210. });
  211. describeUpdateVariable('regex pattern without slashes', function(scenario) {
  212. scenario.setup(function() {
  213. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  214. scenario.variable.regex = 'backend_01';
  215. scenario.queryResult = [{text: 'apps.backend.backend_01.counters.req'}, {text: 'apps.backend.backend_02.counters.req'}];
  216. });
  217. it('should return matches options', function() {
  218. expect(scenario.variable.options.length).to.be(1);
  219. });
  220. });
  221. describeUpdateVariable('regex pattern remove duplicates', function(scenario) {
  222. scenario.setup(function() {
  223. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  224. scenario.variable.regex = 'backend_01';
  225. scenario.queryResult = [{text: 'apps.backend.backend_01.counters.req'}, {text: 'apps.backend.backend_01.counters.req'}];
  226. });
  227. it('should return matches options', function() {
  228. expect(scenario.variable.options.length).to.be(1);
  229. });
  230. });
  231. describeUpdateVariable('with include All', function(scenario) {
  232. scenario.setup(function() {
  233. scenario.variable = {type: 'query', query: 'apps.*', name: 'test', includeAll: true};
  234. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
  235. });
  236. it('should add All option', function() {
  237. expect(scenario.variable.options[0].text).to.be('All');
  238. expect(scenario.variable.options[0].value).to.be('$__all');
  239. });
  240. });
  241. describeUpdateVariable('with include all and custom value', function(scenario) {
  242. scenario.setup(function() {
  243. scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allValue: '*' };
  244. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
  245. });
  246. it('should add All option with custom value', function() {
  247. expect(scenario.variable.options[0].value).to.be('$__all');
  248. });
  249. });
  250. describeUpdateVariable('datasource variable with regex filter', function(scenario) {
  251. scenario.setup(function() {
  252. scenario.variable = {
  253. type: 'datasource',
  254. query: 'graphite',
  255. name: 'test',
  256. current: {value: 'backend4_pee', text: 'backend4_pee'},
  257. regex: '/pee$/'
  258. };
  259. scenario.metricSources = [
  260. {name: 'backend1', meta: {id: 'influx'}},
  261. {name: 'backend2_pee', meta: {id: 'graphite'}},
  262. {name: 'backend3', meta: {id: 'graphite'}},
  263. {name: 'backend4_pee', meta: {id: 'graphite'}},
  264. ];
  265. });
  266. it('should set only contain graphite ds and filtered using regex', function() {
  267. expect(scenario.variable.options.length).to.be(2);
  268. expect(scenario.variable.options[0].value).to.be('backend2_pee');
  269. expect(scenario.variable.options[1].value).to.be('backend4_pee');
  270. });
  271. it('should keep current value if available', function() {
  272. expect(scenario.variable.current.value).to.be('backend4_pee');
  273. });
  274. });
  275. });
  276. });