template_srv_specs.ts 9.8 KB

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