template_srv.test.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. import { TemplateSrv } from '../template_srv';
  2. describe('templateSrv', () => {
  3. let _templateSrv;
  4. function initTemplateSrv(variables) {
  5. _templateSrv = new TemplateSrv();
  6. _templateSrv.init(variables);
  7. }
  8. describe('init', () => {
  9. beforeEach(() => {
  10. initTemplateSrv([{ type: 'query', name: 'test', current: { value: 'oogle' } }]);
  11. });
  12. it('should initialize template data', () => {
  13. const target = _templateSrv.replace('this.[[test]].filters');
  14. expect(target).toBe('this.oogle.filters');
  15. });
  16. });
  17. describe('replace can pass scoped vars', () => {
  18. beforeEach(() => {
  19. initTemplateSrv([{ type: 'query', name: 'test', current: { value: 'oogle' } }]);
  20. });
  21. it('should replace $test with scoped value', () => {
  22. const target = _templateSrv.replace('this.$test.filters', {
  23. test: { value: 'mupp', text: 'asd' },
  24. });
  25. expect(target).toBe('this.mupp.filters');
  26. });
  27. it('should replace ${test} with scoped value', () => {
  28. const target = _templateSrv.replace('this.${test}.filters', {
  29. test: { value: 'mupp', text: 'asd' },
  30. });
  31. expect(target).toBe('this.mupp.filters');
  32. });
  33. it('should replace ${test:glob} with scoped value', () => {
  34. const target = _templateSrv.replace('this.${test:glob}.filters', {
  35. test: { value: 'mupp', text: 'asd' },
  36. });
  37. expect(target).toBe('this.mupp.filters');
  38. });
  39. it('should replace $test with scoped text', () => {
  40. const target = _templateSrv.replaceWithText('this.$test.filters', {
  41. test: { value: 'mupp', text: 'asd' },
  42. });
  43. expect(target).toBe('this.asd.filters');
  44. });
  45. it('should replace ${test} with scoped text', () => {
  46. const target = _templateSrv.replaceWithText('this.${test}.filters', {
  47. test: { value: 'mupp', text: 'asd' },
  48. });
  49. expect(target).toBe('this.asd.filters');
  50. });
  51. it('should replace ${test:glob} with scoped text', () => {
  52. const target = _templateSrv.replaceWithText('this.${test:glob}.filters', {
  53. test: { value: 'mupp', text: 'asd' },
  54. });
  55. expect(target).toBe('this.asd.filters');
  56. });
  57. });
  58. describe('getAdhocFilters', () => {
  59. beforeEach(() => {
  60. initTemplateSrv([
  61. {
  62. type: 'datasource',
  63. name: 'ds',
  64. current: { value: 'logstash', text: 'logstash' },
  65. },
  66. { type: 'adhoc', name: 'test', datasource: 'oogle', filters: [1] },
  67. { type: 'adhoc', name: 'test2', datasource: '$ds', filters: [2] },
  68. ]);
  69. });
  70. it('should return filters if datasourceName match', () => {
  71. const filters = _templateSrv.getAdhocFilters('oogle');
  72. expect(filters).toMatchObject([1]);
  73. });
  74. it('should return empty array if datasourceName does not match', () => {
  75. const filters = _templateSrv.getAdhocFilters('oogleasdasd');
  76. expect(filters).toMatchObject([]);
  77. });
  78. it('should return filters when datasourceName match via data source variable', () => {
  79. const filters = _templateSrv.getAdhocFilters('logstash');
  80. expect(filters).toMatchObject([2]);
  81. });
  82. });
  83. describe('replace can pass multi / all format', () => {
  84. beforeEach(() => {
  85. initTemplateSrv([
  86. {
  87. type: 'query',
  88. name: 'test',
  89. current: { value: ['value1', 'value2'] },
  90. },
  91. ]);
  92. });
  93. it('should replace $test with globbed value', () => {
  94. const target = _templateSrv.replace('this.$test.filters', {}, 'glob');
  95. expect(target).toBe('this.{value1,value2}.filters');
  96. });
  97. it('should replace ${test} with globbed value', () => {
  98. const target = _templateSrv.replace('this.${test}.filters', {}, 'glob');
  99. expect(target).toBe('this.{value1,value2}.filters');
  100. });
  101. it('should replace ${test:glob} with globbed value', () => {
  102. const target = _templateSrv.replace('this.${test:glob}.filters', {});
  103. expect(target).toBe('this.{value1,value2}.filters');
  104. });
  105. it('should replace $test with piped value', () => {
  106. const target = _templateSrv.replace('this=$test', {}, 'pipe');
  107. expect(target).toBe('this=value1|value2');
  108. });
  109. it('should replace ${test} with piped value', () => {
  110. const target = _templateSrv.replace('this=${test}', {}, 'pipe');
  111. expect(target).toBe('this=value1|value2');
  112. });
  113. it('should replace ${test:pipe} with piped value', () => {
  114. const target = _templateSrv.replace('this=${test:pipe}', {});
  115. expect(target).toBe('this=value1|value2');
  116. });
  117. it('should replace ${test:pipe} with piped value and $test with globbed value', () => {
  118. const target = _templateSrv.replace('${test:pipe},$test', {}, 'glob');
  119. expect(target).toBe('value1|value2,{value1,value2}');
  120. });
  121. });
  122. describe('variable with all option', () => {
  123. beforeEach(() => {
  124. initTemplateSrv([
  125. {
  126. type: 'query',
  127. name: 'test',
  128. current: { value: '$__all' },
  129. options: [{ value: '$__all' }, { value: 'value1' }, { value: 'value2' }],
  130. },
  131. ]);
  132. });
  133. it('should replace $test with formatted all value', () => {
  134. const target = _templateSrv.replace('this.$test.filters', {}, 'glob');
  135. expect(target).toBe('this.{value1,value2}.filters');
  136. });
  137. it('should replace ${test} with formatted all value', () => {
  138. const target = _templateSrv.replace('this.${test}.filters', {}, 'glob');
  139. expect(target).toBe('this.{value1,value2}.filters');
  140. });
  141. it('should replace ${test:glob} with formatted all value', () => {
  142. const target = _templateSrv.replace('this.${test:glob}.filters', {});
  143. expect(target).toBe('this.{value1,value2}.filters');
  144. });
  145. it('should replace ${test:pipe} with piped value and $test with globbed value', () => {
  146. const target = _templateSrv.replace('${test:pipe},$test', {}, 'glob');
  147. expect(target).toBe('value1|value2,{value1,value2}');
  148. });
  149. });
  150. describe('variable with all option and custom value', () => {
  151. beforeEach(() => {
  152. initTemplateSrv([
  153. {
  154. type: 'query',
  155. name: 'test',
  156. current: { value: '$__all' },
  157. allValue: '*',
  158. options: [{ value: 'value1' }, { value: 'value2' }],
  159. },
  160. ]);
  161. });
  162. it('should replace $test with formatted all value', () => {
  163. const target = _templateSrv.replace('this.$test.filters', {}, 'glob');
  164. expect(target).toBe('this.*.filters');
  165. });
  166. it('should replace ${test} with formatted all value', () => {
  167. const target = _templateSrv.replace('this.${test}.filters', {}, 'glob');
  168. expect(target).toBe('this.*.filters');
  169. });
  170. it('should replace ${test:glob} with formatted all value', () => {
  171. const target = _templateSrv.replace('this.${test:glob}.filters', {});
  172. expect(target).toBe('this.*.filters');
  173. });
  174. it('should not escape custom all value', () => {
  175. const target = _templateSrv.replace('this.$test', {}, 'regex');
  176. expect(target).toBe('this.*');
  177. });
  178. });
  179. describe('lucene format', () => {
  180. it('should properly escape $test with lucene escape sequences', () => {
  181. initTemplateSrv([{ type: 'query', name: 'test', current: { value: 'value/4' } }]);
  182. const target = _templateSrv.replace('this:$test', {}, 'lucene');
  183. expect(target).toBe('this:value\\/4');
  184. });
  185. it('should properly escape ${test} with lucene escape sequences', () => {
  186. initTemplateSrv([{ type: 'query', name: 'test', current: { value: 'value/4' } }]);
  187. const target = _templateSrv.replace('this:${test}', {}, 'lucene');
  188. expect(target).toBe('this:value\\/4');
  189. });
  190. it('should properly escape ${test:lucene} with lucene escape sequences', () => {
  191. initTemplateSrv([{ type: 'query', name: 'test', current: { value: 'value/4' } }]);
  192. const target = _templateSrv.replace('this:${test:lucene}', {});
  193. expect(target).toBe('this:value\\/4');
  194. });
  195. });
  196. describe('format variable to string values', () => {
  197. it('single value should return value', () => {
  198. const result = _templateSrv.formatValue('test');
  199. expect(result).toBe('test');
  200. });
  201. it('multi value and glob format should render glob string', () => {
  202. const result = _templateSrv.formatValue(['test', 'test2'], 'glob');
  203. expect(result).toBe('{test,test2}');
  204. });
  205. it('multi value and lucene should render as lucene expr', () => {
  206. const result = _templateSrv.formatValue(['test', 'test2'], 'lucene');
  207. expect(result).toBe('("test" OR "test2")');
  208. });
  209. it('multi value and regex format should render regex string', () => {
  210. const result = _templateSrv.formatValue(['test.', 'test2'], 'regex');
  211. expect(result).toBe('(test\\.|test2)');
  212. });
  213. it('multi value and pipe should render pipe string', () => {
  214. const result = _templateSrv.formatValue(['test', 'test2'], 'pipe');
  215. expect(result).toBe('test|test2');
  216. });
  217. it('multi value and distributed should render distributed string', () => {
  218. const result = _templateSrv.formatValue(['test', 'test2'], 'distributed', {
  219. name: 'build',
  220. });
  221. expect(result).toBe('test,build=test2');
  222. });
  223. it('multi value and distributed should render when not string', () => {
  224. const result = _templateSrv.formatValue(['test'], 'distributed', {
  225. name: 'build',
  226. });
  227. expect(result).toBe('test');
  228. });
  229. it('multi value and csv format should render csv string', () => {
  230. const result = _templateSrv.formatValue(['test', 'test2'], 'csv');
  231. expect(result).toBe('test,test2');
  232. });
  233. it('multi value and percentencode format should render percent-encoded string', () => {
  234. const result = _templateSrv.formatValue(['foo()bar BAZ', 'test2'], 'percentencode');
  235. expect(result).toBe('%7Bfoo%28%29bar%20BAZ%2Ctest2%7D');
  236. });
  237. it('slash should be properly escaped in regex format', () => {
  238. const result = _templateSrv.formatValue('Gi3/14', 'regex');
  239. expect(result).toBe('Gi3\\/14');
  240. });
  241. });
  242. describe('can check if variable exists', () => {
  243. beforeEach(() => {
  244. initTemplateSrv([{ type: 'query', name: 'test', current: { value: 'oogle' } }]);
  245. });
  246. it('should return true if $test exists', () => {
  247. const result = _templateSrv.variableExists('$test');
  248. expect(result).toBe(true);
  249. });
  250. it('should return true if $test exists in string', () => {
  251. const result = _templateSrv.variableExists('something $test something');
  252. expect(result).toBe(true);
  253. });
  254. it('should return true if [[test]] exists in string', () => {
  255. const result = _templateSrv.variableExists('something [[test]] something');
  256. expect(result).toBe(true);
  257. });
  258. it('should return true if [[test:csv]] exists in string', () => {
  259. const result = _templateSrv.variableExists('something [[test:csv]] something');
  260. expect(result).toBe(true);
  261. });
  262. it('should return true if ${test} exists in string', () => {
  263. const result = _templateSrv.variableExists('something ${test} something');
  264. expect(result).toBe(true);
  265. });
  266. it('should return true if ${test:raw} exists in string', () => {
  267. const result = _templateSrv.variableExists('something ${test:raw} something');
  268. expect(result).toBe(true);
  269. });
  270. it('should return null if there are no variables in string', () => {
  271. const result = _templateSrv.variableExists('string without variables');
  272. expect(result).toBe(null);
  273. });
  274. });
  275. describe('can highlight variables in string', () => {
  276. beforeEach(() => {
  277. initTemplateSrv([{ type: 'query', name: 'test', current: { value: 'oogle' } }]);
  278. });
  279. it('should insert html', () => {
  280. const result = _templateSrv.highlightVariablesAsHtml('$test');
  281. expect(result).toBe('<span class="template-variable">$test</span>');
  282. });
  283. it('should insert html anywhere in string', () => {
  284. const result = _templateSrv.highlightVariablesAsHtml('this $test ok');
  285. expect(result).toBe('this <span class="template-variable">$test</span> ok');
  286. });
  287. it('should ignore if variables does not exist', () => {
  288. const result = _templateSrv.highlightVariablesAsHtml('this $google ok');
  289. expect(result).toBe('this $google ok');
  290. });
  291. });
  292. describe('updateIndex with simple value', () => {
  293. beforeEach(() => {
  294. initTemplateSrv([{ type: 'query', name: 'test', current: { value: 'muuuu' } }]);
  295. });
  296. it('should set current value and update template data', () => {
  297. const target = _templateSrv.replace('this.[[test]].filters');
  298. expect(target).toBe('this.muuuu.filters');
  299. });
  300. });
  301. describe('fillVariableValuesForUrl with multi value', () => {
  302. beforeEach(() => {
  303. initTemplateSrv([
  304. {
  305. type: 'query',
  306. name: 'test',
  307. current: { value: ['val1', 'val2'] },
  308. getValueForUrl: function() {
  309. return this.current.value;
  310. },
  311. },
  312. ]);
  313. });
  314. it('should set multiple url params', () => {
  315. const params = {};
  316. _templateSrv.fillVariableValuesForUrl(params);
  317. expect(params['var-test']).toMatchObject(['val1', 'val2']);
  318. });
  319. });
  320. describe('fillVariableValuesForUrl skip url sync', () => {
  321. beforeEach(() => {
  322. initTemplateSrv([
  323. {
  324. name: 'test',
  325. skipUrlSync: true,
  326. current: { value: 'value' },
  327. getValueForUrl: function() {
  328. return this.current.value;
  329. },
  330. },
  331. ]);
  332. });
  333. it('should not include template variable value in url', () => {
  334. const params = {};
  335. _templateSrv.fillVariableValuesForUrl(params);
  336. expect(params['var-test']).toBe(undefined);
  337. });
  338. });
  339. describe('fillVariableValuesForUrl with multi value with skip url sync', () => {
  340. beforeEach(() => {
  341. initTemplateSrv([
  342. {
  343. type: 'query',
  344. name: 'test',
  345. skipUrlSync: true,
  346. current: { value: ['val1', 'val2'] },
  347. getValueForUrl: function() {
  348. return this.current.value;
  349. },
  350. },
  351. ]);
  352. });
  353. it('should not include template variable value in url', () => {
  354. const params = {};
  355. _templateSrv.fillVariableValuesForUrl(params);
  356. expect(params['var-test']).toBe(undefined);
  357. });
  358. });
  359. describe('fillVariableValuesForUrl with multi value and scopedVars', () => {
  360. beforeEach(() => {
  361. initTemplateSrv([{ type: 'query', name: 'test', current: { value: ['val1', 'val2'] } }]);
  362. });
  363. it('should set scoped value as url params', () => {
  364. const params = {};
  365. _templateSrv.fillVariableValuesForUrl(params, {
  366. test: { value: 'val1' },
  367. });
  368. expect(params['var-test']).toBe('val1');
  369. });
  370. });
  371. describe('fillVariableValuesForUrl with multi value, scopedVars and skip url sync', () => {
  372. beforeEach(() => {
  373. initTemplateSrv([{ type: 'query', name: 'test', current: { value: ['val1', 'val2'] } }]);
  374. });
  375. it('should not set scoped value as url params', () => {
  376. const params = {};
  377. _templateSrv.fillVariableValuesForUrl(params, {
  378. test: { name: 'test', value: 'val1', skipUrlSync: true },
  379. });
  380. expect(params['var-test']).toBe(undefined);
  381. });
  382. });
  383. describe('replaceWithText', () => {
  384. beforeEach(() => {
  385. initTemplateSrv([
  386. {
  387. type: 'query',
  388. name: 'server',
  389. current: { value: '{asd,asd2}', text: 'All' },
  390. },
  391. {
  392. type: 'interval',
  393. name: 'period',
  394. current: { value: '$__auto_interval_interval', text: 'auto' },
  395. },
  396. {
  397. type: 'textbox',
  398. name: 'empty_on_init',
  399. current: { value: '', text: '' },
  400. },
  401. {
  402. type: 'custom',
  403. name: 'foo',
  404. current: { value: 'constructor', text: 'constructor' },
  405. },
  406. ]);
  407. _templateSrv.setGrafanaVariable('$__auto_interval_interval', '13m');
  408. _templateSrv.updateIndex();
  409. });
  410. it('should replace with text except for grafanaVariables', () => {
  411. const target = _templateSrv.replaceWithText('Server: $server, period: $period');
  412. expect(target).toBe('Server: All, period: 13m');
  413. });
  414. it('should replace empty string-values with an empty string', () => {
  415. const target = _templateSrv.replaceWithText('Hello $empty_on_init');
  416. expect(target).toBe('Hello ');
  417. });
  418. it('should not return a string representation of a constructor property', () => {
  419. const target = _templateSrv.replaceWithText('$foo');
  420. expect(target).not.toBe('function Object() { [native code] }');
  421. expect(target).toBe('constructor');
  422. });
  423. });
  424. describe('built in interval variables', () => {
  425. beforeEach(() => {
  426. initTemplateSrv([]);
  427. });
  428. it('should replace $__interval_ms with interval milliseconds', () => {
  429. const target = _templateSrv.replace('10 * $__interval_ms', {
  430. __interval_ms: { text: '100', value: '100' },
  431. });
  432. expect(target).toBe('10 * 100');
  433. });
  434. });
  435. });