templateValuesSrv-specs.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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', "$routeParams"]));
  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. function describeUpdateVariable(desc, fn) {
  25. describe(desc, function() {
  26. var scenario = {};
  27. scenario.setup = function(setupFn) {
  28. scenario.setupFn = setupFn;
  29. };
  30. beforeEach(function() {
  31. scenario.setupFn();
  32. var ds = {};
  33. ds.metricFindQuery = sinon.stub().returns(ctx.$q.when(scenario.queryResult));
  34. ctx.datasourceSrv.get = sinon.stub().returns(ctx.$q.when(ds));
  35. ctx.service.updateOptions(scenario.variable);
  36. ctx.$rootScope.$digest();
  37. });
  38. fn(scenario);
  39. });
  40. }
  41. describeUpdateVariable('interval variable without auto', function(scenario) {
  42. scenario.setup(function() {
  43. scenario.variable = { type: 'interval', query: '1s,2h,5h,1d', name: 'test' };
  44. });
  45. it('should update options array', function() {
  46. expect(scenario.variable.options.length).to.be(4);
  47. expect(scenario.variable.options[0].text).to.be('1s');
  48. expect(scenario.variable.options[0].value).to.be('1s');
  49. });
  50. });
  51. describeUpdateVariable('interval variable with auto', function(scenario) {
  52. scenario.setup(function() {
  53. scenario.variable = { type: 'interval', query: '1s,2h,5h,1d', name: 'test', auto: true, auto_count: 10 };
  54. var range = {
  55. from: moment(new Date()).subtract(7, 'days').toDate(),
  56. to: new Date()
  57. };
  58. ctx.timeSrv.timeRange = sinon.stub().returns(range);
  59. ctx.templateSrv.setGrafanaVariable = sinon.spy();
  60. });
  61. it('should update options array', function() {
  62. expect(scenario.variable.options.length).to.be(5);
  63. expect(scenario.variable.options[0].text).to.be('auto');
  64. expect(scenario.variable.options[0].value).to.be('$__auto_interval');
  65. });
  66. it('should set $__auto_interval', function() {
  67. var call = ctx.templateSrv.setGrafanaVariable.getCall(0);
  68. expect(call.args[0]).to.be('$__auto_interval');
  69. expect(call.args[1]).to.be('12h');
  70. });
  71. });
  72. describeUpdateVariable('update custom variable', function(scenario) {
  73. scenario.setup(function() {
  74. scenario.variable = { type: 'custom', query: 'hej, hop, asd', name: 'test'};
  75. });
  76. it('should update options array', function() {
  77. expect(scenario.variable.options.length).to.be(3);
  78. expect(scenario.variable.options[0].text).to.be('hej');
  79. expect(scenario.variable.options[1].value).to.be('hop');
  80. });
  81. it('should set $__auto_interval', function() {
  82. var call = ctx.templateSrv.setGrafanaVariable.getCall(0);
  83. expect(call.args[0]).to.be('$__auto_interval');
  84. expect(call.args[1]).to.be('12h');
  85. });
  86. });
  87. describeUpdateVariable('basic query variable', function(scenario) {
  88. scenario.setup(function() {
  89. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  90. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}];
  91. });
  92. it('should update options array', function() {
  93. expect(scenario.variable.options.length).to.be(2);
  94. expect(scenario.variable.options[0].text).to.be('backend1');
  95. expect(scenario.variable.options[0].value).to.be('backend1');
  96. expect(scenario.variable.options[1].value).to.be('backend2');
  97. });
  98. it('should select first option as value', function() {
  99. expect(scenario.variable.current.value).to.be('backend1');
  100. });
  101. });
  102. describeUpdateVariable('and existing value still exists in options', function(scenario) {
  103. scenario.setup(function() {
  104. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  105. scenario.variable.current = { text: 'backend2'};
  106. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}];
  107. });
  108. it('should keep variable value', function() {
  109. expect(scenario.variable.current.text).to.be('backend2');
  110. });
  111. });
  112. describeUpdateVariable('and regex pattern exists', function(scenario) {
  113. scenario.setup(function() {
  114. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  115. scenario.variable.regex = '/apps.*(backend_[0-9]+)/';
  116. scenario.queryResult = [{text: 'apps.backend.backend_01.counters.req'}, {text: 'apps.backend.backend_02.counters.req'}];
  117. });
  118. it('should extract and use match group', function() {
  119. expect(scenario.variable.options[0].value).to.be('backend_01');
  120. });
  121. });
  122. describeUpdateVariable('and regex pattern exists and no match', function(scenario) {
  123. scenario.setup(function() {
  124. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  125. scenario.variable.regex = '/apps.*(backendasd[0-9]+)/';
  126. scenario.queryResult = [{text: 'apps.backend.backend_01.counters.req'}, {text: 'apps.backend.backend_02.counters.req'}];
  127. });
  128. it('should not add non matching items', function() {
  129. expect(scenario.variable.options.length).to.be(0);
  130. });
  131. });
  132. describeUpdateVariable('regex pattern without slashes', function(scenario) {
  133. scenario.setup(function() {
  134. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  135. scenario.variable.regex = 'backend_01';
  136. scenario.queryResult = [{text: 'apps.backend.backend_01.counters.req'}, {text: 'apps.backend.backend_02.counters.req'}];
  137. });
  138. it('should return matches options', function() {
  139. expect(scenario.variable.options.length).to.be(1);
  140. });
  141. });
  142. describeUpdateVariable('regex pattern remove duplicates', function(scenario) {
  143. scenario.setup(function() {
  144. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  145. scenario.variable.regex = 'backend_01';
  146. scenario.queryResult = [{text: 'apps.backend.backend_01.counters.req'}, {text: 'apps.backend.backend_01.counters.req'}];
  147. });
  148. it('should return matches options', function() {
  149. expect(scenario.variable.options.length).to.be(1);
  150. });
  151. });
  152. describeUpdateVariable('with include All glob syntax', function(scenario) {
  153. scenario.setup(function() {
  154. scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'glob' };
  155. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
  156. });
  157. it('should add All Glob option', function() {
  158. expect(scenario.variable.options[0].value).to.be('{backend1,backend2,backend3}');
  159. });
  160. });
  161. describeUpdateVariable('with include all wildcard', function(scenario) {
  162. scenario.setup(function() {
  163. scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'wildcard' };
  164. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
  165. });
  166. it('should add All wildcard option', function() {
  167. expect(scenario.variable.options[0].value).to.be('*');
  168. });
  169. });
  170. describeUpdateVariable('with include all wildcard', function(scenario) {
  171. scenario.setup(function() {
  172. scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'regex wildcard' };
  173. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
  174. });
  175. it('should add All wildcard option', function() {
  176. expect(scenario.variable.options[0].value).to.be('.*');
  177. });
  178. });
  179. describeUpdateVariable('with include all regex values', function(scenario) {
  180. scenario.setup(function() {
  181. scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'wildcard' };
  182. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
  183. });
  184. it('should add All wildcard option', function() {
  185. expect(scenario.variable.options[0].value).to.be('*');
  186. });
  187. });
  188. describeUpdateVariable('with include all glob no values', function(scenario) {
  189. scenario.setup(function() {
  190. scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'glob' };
  191. scenario.queryResult = [];
  192. });
  193. it('should add empty glob', function() {
  194. expect(scenario.variable.options[0].value).to.be('{}');
  195. });
  196. });
  197. describeUpdateVariable('with include all regex all values', function(scenario) {
  198. scenario.setup(function() {
  199. scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'regex values' };
  200. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
  201. });
  202. it('should add empty glob', function() {
  203. expect(scenario.variable.options[0].value).to.be('(backend1|backend2|backend3)');
  204. });
  205. });
  206. });
  207. });