template_srv.test.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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('slash should be properly escaped in regex format', () => {
  234. const result = _templateSrv.formatValue('Gi3/14', 'regex');
  235. expect(result).toBe('Gi3\\/14');
  236. });
  237. });
  238. describe('can check if variable exists', () => {
  239. beforeEach(() => {
  240. initTemplateSrv([{ type: 'query', name: 'test', current: { value: 'oogle' } }]);
  241. });
  242. it('should return true if $test exists', () => {
  243. const result = _templateSrv.variableExists('$test');
  244. expect(result).toBe(true);
  245. });
  246. it('should return true if $test exists in string', () => {
  247. const result = _templateSrv.variableExists('something $test something');
  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:csv]] exists in string', () => {
  255. const result = _templateSrv.variableExists('something [[test:csv]] something');
  256. expect(result).toBe(true);
  257. });
  258. it('should return true if ${test} exists in string', () => {
  259. const result = _templateSrv.variableExists('something ${test} something');
  260. expect(result).toBe(true);
  261. });
  262. it('should return true if ${test:raw} exists in string', () => {
  263. const result = _templateSrv.variableExists('something ${test:raw} something');
  264. expect(result).toBe(true);
  265. });
  266. it('should return null if there are no variables in string', () => {
  267. const result = _templateSrv.variableExists('string without variables');
  268. expect(result).toBe(null);
  269. });
  270. });
  271. describe('can highlight variables in string', () => {
  272. beforeEach(() => {
  273. initTemplateSrv([{ type: 'query', name: 'test', current: { value: 'oogle' } }]);
  274. });
  275. it('should insert html', () => {
  276. const result = _templateSrv.highlightVariablesAsHtml('$test');
  277. expect(result).toBe('<span class="template-variable">$test</span>');
  278. });
  279. it('should insert html anywhere in string', () => {
  280. const result = _templateSrv.highlightVariablesAsHtml('this $test ok');
  281. expect(result).toBe('this <span class="template-variable">$test</span> ok');
  282. });
  283. it('should ignore if variables does not exist', () => {
  284. const result = _templateSrv.highlightVariablesAsHtml('this $google ok');
  285. expect(result).toBe('this $google ok');
  286. });
  287. });
  288. describe('updateTemplateData with simple value', () => {
  289. beforeEach(() => {
  290. initTemplateSrv([{ type: 'query', name: 'test', current: { value: 'muuuu' } }]);
  291. });
  292. it('should set current value and update template data', () => {
  293. const target = _templateSrv.replace('this.[[test]].filters');
  294. expect(target).toBe('this.muuuu.filters');
  295. });
  296. });
  297. describe('fillVariableValuesForUrl with multi value', () => {
  298. beforeEach(() => {
  299. initTemplateSrv([
  300. {
  301. type: 'query',
  302. name: 'test',
  303. current: { value: ['val1', 'val2'] },
  304. getValueForUrl: function() {
  305. return this.current.value;
  306. },
  307. },
  308. ]);
  309. });
  310. it('should set multiple url params', () => {
  311. const params = {};
  312. _templateSrv.fillVariableValuesForUrl(params);
  313. expect(params['var-test']).toMatchObject(['val1', 'val2']);
  314. });
  315. });
  316. describe('fillVariableValuesForUrl skip url sync', () => {
  317. beforeEach(() => {
  318. initTemplateSrv([
  319. {
  320. name: 'test',
  321. skipUrlSync: true,
  322. current: { value: 'value' },
  323. getValueForUrl: function() {
  324. return this.current.value;
  325. },
  326. },
  327. ]);
  328. });
  329. it('should not include template variable value in url', () => {
  330. const params = {};
  331. _templateSrv.fillVariableValuesForUrl(params);
  332. expect(params['var-test']).toBe(undefined);
  333. });
  334. });
  335. describe('fillVariableValuesForUrl with multi value with skip url sync', () => {
  336. beforeEach(() => {
  337. initTemplateSrv([
  338. {
  339. type: 'query',
  340. name: 'test',
  341. skipUrlSync: true,
  342. current: { value: ['val1', 'val2'] },
  343. getValueForUrl: function() {
  344. return this.current.value;
  345. },
  346. },
  347. ]);
  348. });
  349. it('should not include template variable value in url', () => {
  350. const params = {};
  351. _templateSrv.fillVariableValuesForUrl(params);
  352. expect(params['var-test']).toBe(undefined);
  353. });
  354. });
  355. describe('fillVariableValuesForUrl with multi value and scopedVars', () => {
  356. beforeEach(() => {
  357. initTemplateSrv([{ type: 'query', name: 'test', current: { value: ['val1', 'val2'] } }]);
  358. });
  359. it('should set scoped value as url params', () => {
  360. const params = {};
  361. _templateSrv.fillVariableValuesForUrl(params, {
  362. test: { value: 'val1' },
  363. });
  364. expect(params['var-test']).toBe('val1');
  365. });
  366. });
  367. describe('fillVariableValuesForUrl with multi value, scopedVars and skip url sync', () => {
  368. beforeEach(() => {
  369. initTemplateSrv([{ type: 'query', name: 'test', current: { value: ['val1', 'val2'] } }]);
  370. });
  371. it('should not set scoped value as url params', () => {
  372. const params = {};
  373. _templateSrv.fillVariableValuesForUrl(params, {
  374. test: { name: 'test', value: 'val1', skipUrlSync: true },
  375. });
  376. expect(params['var-test']).toBe(undefined);
  377. });
  378. });
  379. describe('replaceWithText', () => {
  380. beforeEach(() => {
  381. initTemplateSrv([
  382. {
  383. type: 'query',
  384. name: 'server',
  385. current: { value: '{asd,asd2}', text: 'All' },
  386. },
  387. {
  388. type: 'interval',
  389. name: 'period',
  390. current: { value: '$__auto_interval_interval', text: 'auto' },
  391. },
  392. {
  393. type: 'textbox',
  394. name: 'empty_on_init',
  395. current: { value: '', text: '' },
  396. },
  397. ]);
  398. _templateSrv.setGrafanaVariable('$__auto_interval_interval', '13m');
  399. _templateSrv.updateTemplateData();
  400. });
  401. it('should replace with text except for grafanaVariables', () => {
  402. const target = _templateSrv.replaceWithText('Server: $server, period: $period');
  403. expect(target).toBe('Server: All, period: 13m');
  404. });
  405. it('should replace empty string-values with an empty string', () => {
  406. const target = _templateSrv.replaceWithText('Hello $empty_on_init');
  407. expect(target).toBe('Hello ');
  408. });
  409. });
  410. describe('built in interval variables', () => {
  411. beforeEach(() => {
  412. initTemplateSrv([]);
  413. });
  414. it('should replace $__interval_ms with interval milliseconds', () => {
  415. const target = _templateSrv.replace('10 * $__interval_ms', {
  416. __interval_ms: { text: '100', value: '100' },
  417. });
  418. expect(target).toBe('10 * 100');
  419. });
  420. });
  421. });