template_srv.test.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. import { TemplateSrv } from '../template_srv';
  2. describe('templateSrv', () => {
  3. let _templateSrv: any;
  4. function initTemplateSrv(variables: any) {
  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. describe('when the globbed variable only has one value', () => {
  98. beforeEach(() => {
  99. initTemplateSrv([
  100. {
  101. type: 'query',
  102. name: 'test',
  103. current: { value: ['value1'] },
  104. },
  105. ]);
  106. });
  107. it('should not glob the value', () => {
  108. const target = _templateSrv.replace('this.$test.filters', {}, 'glob');
  109. expect(target).toBe('this.value1.filters');
  110. });
  111. });
  112. it('should replace ${test} with globbed value', () => {
  113. const target = _templateSrv.replace('this.${test}.filters', {}, 'glob');
  114. expect(target).toBe('this.{value1,value2}.filters');
  115. });
  116. it('should replace ${test:glob} with globbed value', () => {
  117. const target = _templateSrv.replace('this.${test:glob}.filters', {});
  118. expect(target).toBe('this.{value1,value2}.filters');
  119. });
  120. it('should replace $test with piped value', () => {
  121. const target = _templateSrv.replace('this=$test', {}, 'pipe');
  122. expect(target).toBe('this=value1|value2');
  123. });
  124. it('should replace ${test} with piped value', () => {
  125. const target = _templateSrv.replace('this=${test}', {}, 'pipe');
  126. expect(target).toBe('this=value1|value2');
  127. });
  128. it('should replace ${test:pipe} with piped value', () => {
  129. const target = _templateSrv.replace('this=${test:pipe}', {});
  130. expect(target).toBe('this=value1|value2');
  131. });
  132. it('should replace ${test:pipe} with piped value and $test with globbed value', () => {
  133. const target = _templateSrv.replace('${test:pipe},$test', {}, 'glob');
  134. expect(target).toBe('value1|value2,{value1,value2}');
  135. });
  136. });
  137. describe('variable with all option', () => {
  138. beforeEach(() => {
  139. initTemplateSrv([
  140. {
  141. type: 'query',
  142. name: 'test',
  143. current: { value: '$__all' },
  144. options: [{ value: '$__all' }, { value: 'value1' }, { value: 'value2' }],
  145. },
  146. ]);
  147. });
  148. it('should replace $test with formatted all value', () => {
  149. const target = _templateSrv.replace('this.$test.filters', {}, 'glob');
  150. expect(target).toBe('this.{value1,value2}.filters');
  151. });
  152. it('should replace ${test} with formatted all value', () => {
  153. const target = _templateSrv.replace('this.${test}.filters', {}, 'glob');
  154. expect(target).toBe('this.{value1,value2}.filters');
  155. });
  156. it('should replace ${test:glob} with formatted all value', () => {
  157. const target = _templateSrv.replace('this.${test:glob}.filters', {});
  158. expect(target).toBe('this.{value1,value2}.filters');
  159. });
  160. it('should replace ${test:pipe} with piped value and $test with globbed value', () => {
  161. const target = _templateSrv.replace('${test:pipe},$test', {}, 'glob');
  162. expect(target).toBe('value1|value2,{value1,value2}');
  163. });
  164. });
  165. describe('variable with all option and custom value', () => {
  166. beforeEach(() => {
  167. initTemplateSrv([
  168. {
  169. type: 'query',
  170. name: 'test',
  171. current: { value: '$__all' },
  172. allValue: '*',
  173. options: [{ value: 'value1' }, { value: 'value2' }],
  174. },
  175. ]);
  176. });
  177. it('should replace $test with formatted all value', () => {
  178. const target = _templateSrv.replace('this.$test.filters', {}, 'glob');
  179. expect(target).toBe('this.*.filters');
  180. });
  181. it('should replace ${test} with formatted all value', () => {
  182. const target = _templateSrv.replace('this.${test}.filters', {}, 'glob');
  183. expect(target).toBe('this.*.filters');
  184. });
  185. it('should replace ${test:glob} with formatted all value', () => {
  186. const target = _templateSrv.replace('this.${test:glob}.filters', {});
  187. expect(target).toBe('this.*.filters');
  188. });
  189. it('should not escape custom all value', () => {
  190. const target = _templateSrv.replace('this.$test', {}, 'regex');
  191. expect(target).toBe('this.*');
  192. });
  193. });
  194. describe('lucene format', () => {
  195. it('should properly escape $test with lucene escape sequences', () => {
  196. initTemplateSrv([{ type: 'query', name: 'test', current: { value: 'value/4' } }]);
  197. const target = _templateSrv.replace('this:$test', {}, 'lucene');
  198. expect(target).toBe('this:value\\/4');
  199. });
  200. it('should properly escape ${test} with lucene escape sequences', () => {
  201. initTemplateSrv([{ type: 'query', name: 'test', current: { value: 'value/4' } }]);
  202. const target = _templateSrv.replace('this:${test}', {}, 'lucene');
  203. expect(target).toBe('this:value\\/4');
  204. });
  205. it('should properly escape ${test:lucene} with lucene escape sequences', () => {
  206. initTemplateSrv([{ type: 'query', name: 'test', current: { value: 'value/4' } }]);
  207. const target = _templateSrv.replace('this:${test:lucene}', {});
  208. expect(target).toBe('this:value\\/4');
  209. });
  210. });
  211. describe('format variable to string values', () => {
  212. it('single value should return value', () => {
  213. const result = _templateSrv.formatValue('test');
  214. expect(result).toBe('test');
  215. });
  216. it('multi value and glob format should render glob string', () => {
  217. const result = _templateSrv.formatValue(['test', 'test2'], 'glob');
  218. expect(result).toBe('{test,test2}');
  219. });
  220. it('multi value and lucene should render as lucene expr', () => {
  221. const result = _templateSrv.formatValue(['test', 'test2'], 'lucene');
  222. expect(result).toBe('("test" OR "test2")');
  223. });
  224. it('multi value and regex format should render regex string', () => {
  225. const result = _templateSrv.formatValue(['test.', 'test2'], 'regex');
  226. expect(result).toBe('(test\\.|test2)');
  227. });
  228. it('multi value and pipe should render pipe string', () => {
  229. const result = _templateSrv.formatValue(['test', 'test2'], 'pipe');
  230. expect(result).toBe('test|test2');
  231. });
  232. it('multi value and distributed should render distributed string', () => {
  233. const result = _templateSrv.formatValue(['test', 'test2'], 'distributed', {
  234. name: 'build',
  235. });
  236. expect(result).toBe('test,build=test2');
  237. });
  238. it('multi value and distributed should render when not string', () => {
  239. const result = _templateSrv.formatValue(['test'], 'distributed', {
  240. name: 'build',
  241. });
  242. expect(result).toBe('test');
  243. });
  244. it('multi value and csv format should render csv string', () => {
  245. const result = _templateSrv.formatValue(['test', 'test2'], 'csv');
  246. expect(result).toBe('test,test2');
  247. });
  248. it('multi value and percentencode format should render percent-encoded string', () => {
  249. const result = _templateSrv.formatValue(['foo()bar BAZ', 'test2'], 'percentencode');
  250. expect(result).toBe('%7Bfoo%28%29bar%20BAZ%2Ctest2%7D');
  251. });
  252. it('slash should be properly escaped in regex format', () => {
  253. const result = _templateSrv.formatValue('Gi3/14', 'regex');
  254. expect(result).toBe('Gi3\\/14');
  255. });
  256. });
  257. describe('can check if variable exists', () => {
  258. beforeEach(() => {
  259. initTemplateSrv([{ type: 'query', name: 'test', current: { value: 'oogle' } }]);
  260. });
  261. it('should return true if $test exists', () => {
  262. const result = _templateSrv.variableExists('$test');
  263. expect(result).toBe(true);
  264. });
  265. it('should return true if $test exists in string', () => {
  266. const result = _templateSrv.variableExists('something $test something');
  267. expect(result).toBe(true);
  268. });
  269. it('should return true if [[test]] exists in string', () => {
  270. const result = _templateSrv.variableExists('something [[test]] something');
  271. expect(result).toBe(true);
  272. });
  273. it('should return true if [[test:csv]] exists in string', () => {
  274. const result = _templateSrv.variableExists('something [[test:csv]] something');
  275. expect(result).toBe(true);
  276. });
  277. it('should return true if ${test} exists in string', () => {
  278. const result = _templateSrv.variableExists('something ${test} something');
  279. expect(result).toBe(true);
  280. });
  281. it('should return true if ${test:raw} exists in string', () => {
  282. const result = _templateSrv.variableExists('something ${test:raw} something');
  283. expect(result).toBe(true);
  284. });
  285. it('should return null if there are no variables in string', () => {
  286. const result = _templateSrv.variableExists('string without variables');
  287. expect(result).toBe(null);
  288. });
  289. });
  290. describe('can highlight variables in string', () => {
  291. beforeEach(() => {
  292. initTemplateSrv([{ type: 'query', name: 'test', current: { value: 'oogle' } }]);
  293. });
  294. it('should insert html', () => {
  295. const result = _templateSrv.highlightVariablesAsHtml('$test');
  296. expect(result).toBe('<span class="template-variable">$test</span>');
  297. });
  298. it('should insert html anywhere in string', () => {
  299. const result = _templateSrv.highlightVariablesAsHtml('this $test ok');
  300. expect(result).toBe('this <span class="template-variable">$test</span> ok');
  301. });
  302. it('should ignore if variables does not exist', () => {
  303. const result = _templateSrv.highlightVariablesAsHtml('this $google ok');
  304. expect(result).toBe('this $google ok');
  305. });
  306. });
  307. describe('updateIndex with simple value', () => {
  308. beforeEach(() => {
  309. initTemplateSrv([{ type: 'query', name: 'test', current: { value: 'muuuu' } }]);
  310. });
  311. it('should set current value and update template data', () => {
  312. const target = _templateSrv.replace('this.[[test]].filters');
  313. expect(target).toBe('this.muuuu.filters');
  314. });
  315. });
  316. describe('fillVariableValuesForUrl with multi value', () => {
  317. beforeEach(() => {
  318. initTemplateSrv([
  319. {
  320. type: 'query',
  321. name: 'test',
  322. current: { value: ['val1', 'val2'] },
  323. getValueForUrl: function() {
  324. return this.current.value;
  325. },
  326. },
  327. ]);
  328. });
  329. it('should set multiple url params', () => {
  330. const params: any = {};
  331. _templateSrv.fillVariableValuesForUrl(params);
  332. expect(params['var-test']).toMatchObject(['val1', 'val2']);
  333. });
  334. });
  335. describe('fillVariableValuesForUrl skip url sync', () => {
  336. beforeEach(() => {
  337. initTemplateSrv([
  338. {
  339. name: 'test',
  340. skipUrlSync: true,
  341. current: { value: 'value' },
  342. getValueForUrl: function() {
  343. return this.current.value;
  344. },
  345. },
  346. ]);
  347. });
  348. it('should not include template variable value in url', () => {
  349. const params: any = {};
  350. _templateSrv.fillVariableValuesForUrl(params);
  351. expect(params['var-test']).toBe(undefined);
  352. });
  353. });
  354. describe('fillVariableValuesForUrl with multi value with skip url sync', () => {
  355. beforeEach(() => {
  356. initTemplateSrv([
  357. {
  358. type: 'query',
  359. name: 'test',
  360. skipUrlSync: true,
  361. current: { value: ['val1', 'val2'] },
  362. getValueForUrl: function() {
  363. return this.current.value;
  364. },
  365. },
  366. ]);
  367. });
  368. it('should not include template variable value in url', () => {
  369. const params: any = {};
  370. _templateSrv.fillVariableValuesForUrl(params);
  371. expect(params['var-test']).toBe(undefined);
  372. });
  373. });
  374. describe('fillVariableValuesForUrl with multi value and scopedVars', () => {
  375. beforeEach(() => {
  376. initTemplateSrv([{ type: 'query', name: 'test', current: { value: ['val1', 'val2'] } }]);
  377. });
  378. it('should set scoped value as url params', () => {
  379. const params: any = {};
  380. _templateSrv.fillVariableValuesForUrl(params, {
  381. test: { value: 'val1' },
  382. });
  383. expect(params['var-test']).toBe('val1');
  384. });
  385. });
  386. describe('fillVariableValuesForUrl with multi value, scopedVars and skip url sync', () => {
  387. beforeEach(() => {
  388. initTemplateSrv([{ type: 'query', name: 'test', current: { value: ['val1', 'val2'] } }]);
  389. });
  390. it('should not set scoped value as url params', () => {
  391. const params: any = {};
  392. _templateSrv.fillVariableValuesForUrl(params, {
  393. test: { name: 'test', value: 'val1', skipUrlSync: true },
  394. });
  395. expect(params['var-test']).toBe(undefined);
  396. });
  397. });
  398. describe('replaceWithText', () => {
  399. beforeEach(() => {
  400. initTemplateSrv([
  401. {
  402. type: 'query',
  403. name: 'server',
  404. current: { value: '{asd,asd2}', text: 'All' },
  405. },
  406. {
  407. type: 'interval',
  408. name: 'period',
  409. current: { value: '$__auto_interval_interval', text: 'auto' },
  410. },
  411. {
  412. type: 'textbox',
  413. name: 'empty_on_init',
  414. current: { value: '', text: '' },
  415. },
  416. {
  417. type: 'custom',
  418. name: 'foo',
  419. current: { value: 'constructor', text: 'constructor' },
  420. },
  421. ]);
  422. _templateSrv.setGrafanaVariable('$__auto_interval_interval', '13m');
  423. _templateSrv.updateIndex();
  424. });
  425. it('should replace with text except for grafanaVariables', () => {
  426. const target = _templateSrv.replaceWithText('Server: $server, period: $period');
  427. expect(target).toBe('Server: All, period: 13m');
  428. });
  429. it('should replace empty string-values with an empty string', () => {
  430. const target = _templateSrv.replaceWithText('Hello $empty_on_init');
  431. expect(target).toBe('Hello ');
  432. });
  433. it('should not return a string representation of a constructor property', () => {
  434. const target = _templateSrv.replaceWithText('$foo');
  435. expect(target).not.toBe('function Object() { [native code] }');
  436. expect(target).toBe('constructor');
  437. });
  438. });
  439. describe('built in interval variables', () => {
  440. beforeEach(() => {
  441. initTemplateSrv([]);
  442. });
  443. it('should be possible to fetch value with getBuilInIntervalValue', () => {
  444. const val = _templateSrv.getBuiltInIntervalValue();
  445. expect(val).toBe('1s');
  446. });
  447. it('should replace $__interval_ms with interval milliseconds', () => {
  448. const target = _templateSrv.replace('10 * $__interval_ms', {
  449. __interval_ms: { text: '100', value: '100' },
  450. });
  451. expect(target).toBe('10 * 100');
  452. });
  453. });
  454. });