templateValuesSrv-specs.js 12 KB

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