template_srv_specs.ts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. import {describe, beforeEach, it, sinon, expect, angularMocks} from 'test/lib/common';
  2. import '../all';
  3. import {Emitter} from 'app/core/core';
  4. describe('templateSrv', function() {
  5. var _templateSrv, _variableSrv;
  6. beforeEach(angularMocks.module('grafana.core'));
  7. beforeEach(angularMocks.module('grafana.services'));
  8. beforeEach(angularMocks.module($provide => {
  9. $provide.value('timeSrv', {});
  10. }));
  11. beforeEach(angularMocks.inject(function(variableSrv, templateSrv) {
  12. _templateSrv = templateSrv;
  13. _variableSrv = variableSrv;
  14. }));
  15. function initTemplateSrv(variables) {
  16. _variableSrv.init({
  17. templating: {list: variables},
  18. events: new Emitter(),
  19. });
  20. }
  21. describe('init', function() {
  22. beforeEach(function() {
  23. initTemplateSrv([{type: 'query', name: 'test', current: {value: 'oogle'}}]);
  24. });
  25. it('should initialize template data', function() {
  26. var target = _templateSrv.replace('this.[[test]].filters');
  27. expect(target).to.be('this.oogle.filters');
  28. });
  29. });
  30. describe('replace can pass scoped vars', function() {
  31. beforeEach(function() {
  32. initTemplateSrv([{type: 'query', name: 'test', current: {value: 'oogle' }}]);
  33. });
  34. it('should replace $test with scoped value', function() {
  35. var target = _templateSrv.replace('this.$test.filters', {'test': {value: 'mupp', text: 'asd'}});
  36. expect(target).to.be('this.mupp.filters');
  37. });
  38. it('should replace $test with scoped text', function() {
  39. var target = _templateSrv.replaceWithText('this.$test.filters', {'test': {value: 'mupp', text: 'asd'}});
  40. expect(target).to.be('this.asd.filters');
  41. });
  42. });
  43. describe('getAdhocFilters', function() {
  44. beforeEach(function() {
  45. initTemplateSrv([
  46. {type: 'datasource', name: 'ds', current: {value: 'logstash', text: 'logstash'}},
  47. {type: 'adhoc', name: 'test', datasource: 'oogle', filters: [1]},
  48. {type: 'adhoc', name: 'test2', datasource: '$ds', filters: [2]},
  49. ]);
  50. });
  51. it('should return filters if datasourceName match', function() {
  52. var filters = _templateSrv.getAdhocFilters('oogle');
  53. expect(filters).to.eql([1]);
  54. });
  55. it('should return empty array if datasourceName does not match', function() {
  56. var filters = _templateSrv.getAdhocFilters('oogleasdasd');
  57. expect(filters).to.eql([]);
  58. });
  59. it('should return filters when datasourceName match via data source variable', function() {
  60. var filters = _templateSrv.getAdhocFilters('logstash');
  61. expect(filters).to.eql([2]);
  62. });
  63. });
  64. describe('replace can pass multi / all format', function() {
  65. beforeEach(function() {
  66. initTemplateSrv([{type: 'query', name: 'test', current: {value: ['value1', 'value2'] }}]);
  67. });
  68. it('should replace $test with globbed value', function() {
  69. var target = _templateSrv.replace('this.$test.filters', {}, 'glob');
  70. expect(target).to.be('this.{value1,value2}.filters');
  71. });
  72. it('should replace $test with piped value', function() {
  73. var target = _templateSrv.replace('this=$test', {}, 'pipe');
  74. expect(target).to.be('this=value1|value2');
  75. });
  76. it('should replace $test with piped value', function() {
  77. var target = _templateSrv.replace('this=$test', {}, 'pipe');
  78. expect(target).to.be('this=value1|value2');
  79. });
  80. });
  81. describe('variable with all option', function() {
  82. beforeEach(function() {
  83. initTemplateSrv([{
  84. type: 'query',
  85. name: 'test',
  86. current: {value: '$__all' },
  87. options: [
  88. {value: '$__all'}, {value: 'value1'}, {value: 'value2'}
  89. ]
  90. }]);
  91. });
  92. it('should replace $test with formatted all value', function() {
  93. var target = _templateSrv.replace('this.$test.filters', {}, 'glob');
  94. expect(target).to.be('this.{value1,value2}.filters');
  95. });
  96. });
  97. describe('variable with all option and custom value', function() {
  98. beforeEach(function() {
  99. initTemplateSrv([{
  100. type: 'query',
  101. name: 'test',
  102. current: {value: '$__all' },
  103. allValue: '*',
  104. options: [
  105. {value: 'value1'}, {value: 'value2'}
  106. ]
  107. }]);
  108. });
  109. it('should replace $test with formatted all value', function() {
  110. var target = _templateSrv.replace('this.$test.filters', {}, 'glob');
  111. expect(target).to.be('this.*.filters');
  112. });
  113. it('should not escape custom all value', function() {
  114. var target = _templateSrv.replace('this.$test', {}, 'regex');
  115. expect(target).to.be('this.*');
  116. });
  117. });
  118. describe('lucene format', function() {
  119. it('should properly escape $test with lucene escape sequences', function() {
  120. initTemplateSrv([{type: 'query', name: 'test', current: {value: 'value/4' }}]);
  121. var target = _templateSrv.replace('this:$test', {}, 'lucene');
  122. expect(target).to.be("this:value\\\/4");
  123. });
  124. });
  125. describe('format variable to string values', function() {
  126. it('single value should return value', function() {
  127. var result = _templateSrv.formatValue('test');
  128. expect(result).to.be('test');
  129. });
  130. it('multi value and glob format should render glob string', function() {
  131. var result = _templateSrv.formatValue(['test','test2'], 'glob');
  132. expect(result).to.be('{test,test2}');
  133. });
  134. it('multi value and lucene should render as lucene expr', function() {
  135. var result = _templateSrv.formatValue(['test','test2'], 'lucene');
  136. expect(result).to.be('("test" OR "test2")');
  137. });
  138. it('multi value and regex format should render regex string', function() {
  139. var result = _templateSrv.formatValue(['test.','test2'], 'regex');
  140. expect(result).to.be('(test\\.|test2)');
  141. });
  142. it('multi value and pipe should render pipe string', function() {
  143. var result = _templateSrv.formatValue(['test','test2'], 'pipe');
  144. expect(result).to.be('test|test2');
  145. });
  146. it('multi value and distributed should render distributed string', function() {
  147. var result = _templateSrv.formatValue(['test','test2'], 'distributed', { name: 'build' });
  148. expect(result).to.be('test,build=test2');
  149. });
  150. it('multi value and distributed should render when not string', function() {
  151. var result = _templateSrv.formatValue(['test'], 'distributed', { name: 'build' });
  152. expect(result).to.be('test');
  153. });
  154. it('slash should be properly escaped in regex format', function() {
  155. var result = _templateSrv.formatValue('Gi3/14', 'regex');
  156. expect(result).to.be('Gi3\\/14');
  157. });
  158. });
  159. describe('can check if variable exists', function() {
  160. beforeEach(function() {
  161. initTemplateSrv([{type: 'query', name: 'test', current: { value: 'oogle' } }]);
  162. });
  163. it('should return true if exists', function() {
  164. var result = _templateSrv.variableExists('$test');
  165. expect(result).to.be(true);
  166. });
  167. });
  168. describe('can hightlight variables in string', function() {
  169. beforeEach(function() {
  170. initTemplateSrv([{type: 'query', name: 'test', current: { value: 'oogle' } }]);
  171. });
  172. it('should insert html', function() {
  173. var result = _templateSrv.highlightVariablesAsHtml('$test');
  174. expect(result).to.be('<span class="template-variable">$test</span>');
  175. });
  176. it('should insert html anywhere in string', function() {
  177. var result = _templateSrv.highlightVariablesAsHtml('this $test ok');
  178. expect(result).to.be('this <span class="template-variable">$test</span> ok');
  179. });
  180. it('should ignore if variables does not exist', function() {
  181. var result = _templateSrv.highlightVariablesAsHtml('this $google ok');
  182. expect(result).to.be('this $google ok');
  183. });
  184. });
  185. describe('updateTemplateData with simple value', function() {
  186. beforeEach(function() {
  187. initTemplateSrv([{type: 'query', name: 'test', current: { value: 'muuuu' } }]);
  188. });
  189. it('should set current value and update template data', function() {
  190. var target = _templateSrv.replace('this.[[test]].filters');
  191. expect(target).to.be('this.muuuu.filters');
  192. });
  193. });
  194. describe('fillVariableValuesForUrl with multi value', function() {
  195. beforeEach(function() {
  196. initTemplateSrv([{type: 'query', name: 'test', current: { value: ['val1', 'val2'] }}]);
  197. });
  198. it('should set multiple url params', function() {
  199. var params = {};
  200. _templateSrv.fillVariableValuesForUrl(params);
  201. expect(params['var-test']).to.eql(['val1', 'val2']);
  202. });
  203. });
  204. describe('fillVariableValuesForUrl with multi value and scopedVars', function() {
  205. beforeEach(function() {
  206. initTemplateSrv([{type: 'query', name: 'test', current: { value: ['val1', 'val2'] }}]);
  207. });
  208. it('should set scoped value as url params', function() {
  209. var params = {};
  210. _templateSrv.fillVariableValuesForUrl(params, {'test': {value: 'val1'}});
  211. expect(params['var-test']).to.eql('val1');
  212. });
  213. });
  214. describe('replaceWithText', function() {
  215. beforeEach(function() {
  216. initTemplateSrv([
  217. {type: 'query', name: 'server', current: { value: '{asd,asd2}', text: 'All' } },
  218. {type: 'interval', name: 'period', current: { value: '$__auto_interval', text: 'auto' } }
  219. ]);
  220. _templateSrv.setGrafanaVariable('$__auto_interval', '13m');
  221. _templateSrv.updateTemplateData();
  222. });
  223. it('should replace with text except for grafanaVariables', function() {
  224. var target = _templateSrv.replaceWithText('Server: $server, period: $period');
  225. expect(target).to.be('Server: All, period: 13m');
  226. });
  227. });
  228. describe('built in interval variables', function() {
  229. beforeEach(function() {
  230. initTemplateSrv([]);
  231. });
  232. it('should replace $__interval_ms with interval milliseconds', function() {
  233. var target = _templateSrv.replace('10 * $__interval_ms', {"__interval_ms": {text: "100", value: "100"}});
  234. expect(target).to.be('10 * 100');
  235. });
  236. });
  237. });