variable_srv.test.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. import '../all';
  2. import { VariableSrv } from '../variable_srv';
  3. import { DashboardModel } from '../../dashboard/state/DashboardModel';
  4. import $q from 'q';
  5. import { dateTime } from '@grafana/ui/src/utils/moment_wrapper';
  6. describe('VariableSrv', function(this: any) {
  7. const ctx = {
  8. datasourceSrv: {},
  9. timeSrv: {
  10. timeRange: () => {
  11. return { from: '2018-01-29', to: '2019-01-29' };
  12. },
  13. },
  14. $rootScope: {
  15. $on: () => {},
  16. },
  17. $injector: {
  18. instantiate: (ctr, obj) => new ctr(obj.model),
  19. },
  20. templateSrv: {
  21. setGrafanaVariable: jest.fn(),
  22. init: vars => {
  23. this.variables = vars;
  24. },
  25. updateIndex: () => {},
  26. replace: str =>
  27. str.replace(this.regex, match => {
  28. return match;
  29. }),
  30. },
  31. $location: {
  32. search: () => {},
  33. },
  34. } as any;
  35. function describeUpdateVariable(desc, fn) {
  36. describe(desc, () => {
  37. const scenario: any = {};
  38. scenario.setup = setupFn => {
  39. scenario.setupFn = setupFn;
  40. };
  41. beforeEach(async () => {
  42. scenario.setupFn();
  43. const ds: any = {};
  44. ds.metricFindQuery = () => Promise.resolve(scenario.queryResult);
  45. ctx.variableSrv = new VariableSrv($q, ctx.$location, ctx.$injector, ctx.templateSrv, ctx.timeSrv);
  46. ctx.variableSrv.timeSrv = ctx.timeSrv;
  47. ctx.datasourceSrv = {
  48. get: () => Promise.resolve(ds),
  49. getMetricSources: () => scenario.metricSources,
  50. };
  51. ctx.$injector.instantiate = (ctr, model) => {
  52. return getVarMockConstructor(ctr, model, ctx);
  53. };
  54. ctx.variableSrv.init(
  55. new DashboardModel({
  56. templating: { list: [] },
  57. updateSubmenuVisibility: () => {},
  58. })
  59. );
  60. scenario.variable = ctx.variableSrv.createVariableFromModel(scenario.variableModel);
  61. ctx.variableSrv.addVariable(scenario.variable);
  62. await ctx.variableSrv.updateOptions(scenario.variable);
  63. });
  64. fn(scenario);
  65. });
  66. }
  67. describeUpdateVariable('interval variable without auto', scenario => {
  68. scenario.setup(() => {
  69. scenario.variableModel = {
  70. type: 'interval',
  71. query: '1s,2h,5h,1d',
  72. name: 'test',
  73. };
  74. });
  75. it('should update options array', () => {
  76. expect(scenario.variable.options.length).toBe(4);
  77. expect(scenario.variable.options[0].text).toBe('1s');
  78. expect(scenario.variable.options[0].value).toBe('1s');
  79. });
  80. });
  81. //
  82. // Interval variable update
  83. //
  84. describeUpdateVariable('interval variable with auto', scenario => {
  85. scenario.setup(() => {
  86. scenario.variableModel = {
  87. type: 'interval',
  88. query: '1s,2h,5h,1d',
  89. name: 'test',
  90. auto: true,
  91. auto_count: 10,
  92. };
  93. const range = {
  94. from: dateTime(new Date())
  95. .subtract(7, 'days')
  96. .toDate(),
  97. to: new Date(),
  98. };
  99. ctx.timeSrv.timeRange = () => range;
  100. // ctx.templateSrv.setGrafanaVariable = jest.fn();
  101. });
  102. it('should update options array', () => {
  103. expect(scenario.variable.options.length).toBe(5);
  104. expect(scenario.variable.options[0].text).toBe('auto');
  105. expect(scenario.variable.options[0].value).toBe('$__auto_interval_test');
  106. });
  107. it('should set $__auto_interval_test', () => {
  108. const call = ctx.templateSrv.setGrafanaVariable.mock.calls[0];
  109. expect(call[0]).toBe('$__auto_interval_test');
  110. expect(call[1]).toBe('12h');
  111. });
  112. // updateAutoValue() gets called twice: once directly once via VariableSrv.validateVariableSelectionState()
  113. // So use lastCall instead of a specific call number
  114. it('should set $__auto_interval', () => {
  115. const call = ctx.templateSrv.setGrafanaVariable.mock.calls.pop();
  116. expect(call[0]).toBe('$__auto_interval');
  117. expect(call[1]).toBe('12h');
  118. });
  119. });
  120. //
  121. // Query variable update
  122. //
  123. describeUpdateVariable('query variable with empty current object and refresh', scenario => {
  124. scenario.setup(() => {
  125. scenario.variableModel = {
  126. type: 'query',
  127. query: '',
  128. name: 'test',
  129. current: {},
  130. };
  131. scenario.queryResult = [{ text: 'backend1' }, { text: 'backend2' }];
  132. });
  133. it('should set current value to first option', () => {
  134. expect(scenario.variable.options.length).toBe(2);
  135. expect(scenario.variable.current.value).toBe('backend1');
  136. });
  137. });
  138. describeUpdateVariable(
  139. 'query variable with multi select and new options does not contain some selected values',
  140. scenario => {
  141. scenario.setup(() => {
  142. scenario.variableModel = {
  143. type: 'query',
  144. query: '',
  145. name: 'test',
  146. current: {
  147. value: ['val1', 'val2', 'val3'],
  148. text: 'val1 + val2 + val3',
  149. },
  150. };
  151. scenario.queryResult = [{ text: 'val2' }, { text: 'val3' }];
  152. });
  153. it('should update current value', () => {
  154. expect(scenario.variable.current.value).toEqual(['val2', 'val3']);
  155. expect(scenario.variable.current.text).toEqual('val2 + val3');
  156. });
  157. }
  158. );
  159. describeUpdateVariable(
  160. 'query variable with multi select and new options does not contain any selected values',
  161. scenario => {
  162. scenario.setup(() => {
  163. scenario.variableModel = {
  164. type: 'query',
  165. query: '',
  166. name: 'test',
  167. current: {
  168. value: ['val1', 'val2', 'val3'],
  169. text: 'val1 + val2 + val3',
  170. },
  171. };
  172. scenario.queryResult = [{ text: 'val5' }, { text: 'val6' }];
  173. });
  174. it('should update current value with first one', () => {
  175. expect(scenario.variable.current.value).toEqual('val5');
  176. expect(scenario.variable.current.text).toEqual('val5');
  177. });
  178. }
  179. );
  180. describeUpdateVariable('query variable with multi select and $__all selected', scenario => {
  181. scenario.setup(() => {
  182. scenario.variableModel = {
  183. type: 'query',
  184. query: '',
  185. name: 'test',
  186. includeAll: true,
  187. current: {
  188. value: ['$__all'],
  189. text: 'All',
  190. },
  191. };
  192. scenario.queryResult = [{ text: 'val5' }, { text: 'val6' }];
  193. });
  194. it('should keep current All value', () => {
  195. expect(scenario.variable.current.value).toEqual(['$__all']);
  196. expect(scenario.variable.current.text).toEqual('All');
  197. });
  198. });
  199. describeUpdateVariable('query variable with numeric results', scenario => {
  200. scenario.setup(() => {
  201. scenario.variableModel = {
  202. type: 'query',
  203. query: '',
  204. name: 'test',
  205. current: {},
  206. };
  207. scenario.queryResult = [{ text: 12, value: 12 }];
  208. });
  209. it('should set current value to first option', () => {
  210. expect(scenario.variable.current.value).toBe('12');
  211. expect(scenario.variable.options[0].value).toBe('12');
  212. expect(scenario.variable.options[0].text).toBe('12');
  213. });
  214. });
  215. describeUpdateVariable('basic query variable', scenario => {
  216. scenario.setup(() => {
  217. scenario.variableModel = { type: 'query', query: 'apps.*', name: 'test' };
  218. scenario.queryResult = [{ text: 'backend1' }, { text: 'backend2' }];
  219. });
  220. it('should update options array', () => {
  221. expect(scenario.variable.options.length).toBe(2);
  222. expect(scenario.variable.options[0].text).toBe('backend1');
  223. expect(scenario.variable.options[0].value).toBe('backend1');
  224. expect(scenario.variable.options[1].value).toBe('backend2');
  225. });
  226. it('should select first option as value', () => {
  227. expect(scenario.variable.current.value).toBe('backend1');
  228. });
  229. });
  230. describeUpdateVariable('and existing value still exists in options', scenario => {
  231. scenario.setup(() => {
  232. scenario.variableModel = { type: 'query', query: 'apps.*', name: 'test' };
  233. scenario.variableModel.current = { value: 'backend2', text: 'backend2' };
  234. scenario.queryResult = [{ text: 'backend1' }, { text: 'backend2' }];
  235. });
  236. it('should keep variable value', () => {
  237. expect(scenario.variable.current.text).toBe('backend2');
  238. });
  239. });
  240. describeUpdateVariable('and regex pattern exists', scenario => {
  241. scenario.setup(() => {
  242. scenario.variableModel = { type: 'query', query: 'apps.*', name: 'test' };
  243. scenario.variableModel.regex = '/apps.*(backend_[0-9]+)/';
  244. scenario.queryResult = [
  245. { text: 'apps.backend.backend_01.counters.req' },
  246. { text: 'apps.backend.backend_02.counters.req' },
  247. ];
  248. });
  249. it('should extract and use match group', () => {
  250. expect(scenario.variable.options[0].value).toBe('backend_01');
  251. });
  252. });
  253. describeUpdateVariable('and regex pattern exists and no match', scenario => {
  254. scenario.setup(() => {
  255. scenario.variableModel = { type: 'query', query: 'apps.*', name: 'test' };
  256. scenario.variableModel.regex = '/apps.*(backendasd[0-9]+)/';
  257. scenario.queryResult = [
  258. { text: 'apps.backend.backend_01.counters.req' },
  259. { text: 'apps.backend.backend_02.counters.req' },
  260. ];
  261. });
  262. it('should not add non matching items, None option should be added instead', () => {
  263. expect(scenario.variable.options.length).toBe(1);
  264. expect(scenario.variable.options[0].isNone).toBe(true);
  265. });
  266. });
  267. describeUpdateVariable('regex pattern without slashes', scenario => {
  268. scenario.setup(() => {
  269. scenario.variableModel = { type: 'query', query: 'apps.*', name: 'test' };
  270. scenario.variableModel.regex = 'backend_01';
  271. scenario.queryResult = [
  272. { text: 'apps.backend.backend_01.counters.req' },
  273. { text: 'apps.backend.backend_02.counters.req' },
  274. ];
  275. });
  276. it('should return matches options', () => {
  277. expect(scenario.variable.options.length).toBe(1);
  278. });
  279. });
  280. describeUpdateVariable('regex pattern remove duplicates', scenario => {
  281. scenario.setup(() => {
  282. scenario.variableModel = { type: 'query', query: 'apps.*', name: 'test' };
  283. scenario.variableModel.regex = '/backend_01/';
  284. scenario.queryResult = [
  285. { text: 'apps.backend.backend_01.counters.req' },
  286. { text: 'apps.backend.backend_01.counters.req' },
  287. ];
  288. });
  289. it('should return matches options', () => {
  290. expect(scenario.variable.options.length).toBe(1);
  291. });
  292. });
  293. describeUpdateVariable('with include All', scenario => {
  294. scenario.setup(() => {
  295. scenario.variableModel = {
  296. type: 'query',
  297. query: 'apps.*',
  298. name: 'test',
  299. includeAll: true,
  300. };
  301. scenario.queryResult = [{ text: 'backend1' }, { text: 'backend2' }, { text: 'backend3' }];
  302. });
  303. it('should add All option', () => {
  304. expect(scenario.variable.options[0].text).toBe('All');
  305. expect(scenario.variable.options[0].value).toBe('$__all');
  306. });
  307. });
  308. describeUpdateVariable('with include all and custom value', scenario => {
  309. scenario.setup(() => {
  310. scenario.variableModel = {
  311. type: 'query',
  312. query: 'apps.*',
  313. name: 'test',
  314. includeAll: true,
  315. allValue: '*',
  316. };
  317. scenario.queryResult = [{ text: 'backend1' }, { text: 'backend2' }, { text: 'backend3' }];
  318. });
  319. it('should add All option with custom value', () => {
  320. expect(scenario.variable.options[0].value).toBe('$__all');
  321. });
  322. });
  323. describeUpdateVariable('without sort', scenario => {
  324. scenario.setup(() => {
  325. scenario.variableModel = {
  326. type: 'query',
  327. query: 'apps.*',
  328. name: 'test',
  329. sort: 0,
  330. };
  331. scenario.queryResult = [{ text: 'bbb2' }, { text: 'aaa10' }, { text: 'ccc3' }];
  332. });
  333. it('should return options without sort', () => {
  334. expect(scenario.variable.options[0].text).toBe('bbb2');
  335. expect(scenario.variable.options[1].text).toBe('aaa10');
  336. expect(scenario.variable.options[2].text).toBe('ccc3');
  337. });
  338. });
  339. describeUpdateVariable('with alphabetical sort (asc)', scenario => {
  340. scenario.setup(() => {
  341. scenario.variableModel = {
  342. type: 'query',
  343. query: 'apps.*',
  344. name: 'test',
  345. sort: 1,
  346. };
  347. scenario.queryResult = [{ text: 'bbb2' }, { text: 'aaa10' }, { text: 'ccc3' }];
  348. });
  349. it('should return options with alphabetical sort', () => {
  350. expect(scenario.variable.options[0].text).toBe('aaa10');
  351. expect(scenario.variable.options[1].text).toBe('bbb2');
  352. expect(scenario.variable.options[2].text).toBe('ccc3');
  353. });
  354. });
  355. describeUpdateVariable('with alphabetical sort (desc)', scenario => {
  356. scenario.setup(() => {
  357. scenario.variableModel = {
  358. type: 'query',
  359. query: 'apps.*',
  360. name: 'test',
  361. sort: 2,
  362. };
  363. scenario.queryResult = [{ text: 'bbb2' }, { text: 'aaa10' }, { text: 'ccc3' }];
  364. });
  365. it('should return options with alphabetical sort', () => {
  366. expect(scenario.variable.options[0].text).toBe('ccc3');
  367. expect(scenario.variable.options[1].text).toBe('bbb2');
  368. expect(scenario.variable.options[2].text).toBe('aaa10');
  369. });
  370. });
  371. describeUpdateVariable('with numerical sort (asc)', scenario => {
  372. scenario.setup(() => {
  373. scenario.variableModel = {
  374. type: 'query',
  375. query: 'apps.*',
  376. name: 'test',
  377. sort: 3,
  378. };
  379. scenario.queryResult = [{ text: 'bbb2' }, { text: 'aaa10' }, { text: 'ccc3' }];
  380. });
  381. it('should return options with numerical sort', () => {
  382. expect(scenario.variable.options[0].text).toBe('bbb2');
  383. expect(scenario.variable.options[1].text).toBe('ccc3');
  384. expect(scenario.variable.options[2].text).toBe('aaa10');
  385. });
  386. });
  387. describeUpdateVariable('with numerical sort (desc)', scenario => {
  388. scenario.setup(() => {
  389. scenario.variableModel = {
  390. type: 'query',
  391. query: 'apps.*',
  392. name: 'test',
  393. sort: 4,
  394. };
  395. scenario.queryResult = [{ text: 'bbb2' }, { text: 'aaa10' }, { text: 'ccc3' }];
  396. });
  397. it('should return options with numerical sort', () => {
  398. expect(scenario.variable.options[0].text).toBe('aaa10');
  399. expect(scenario.variable.options[1].text).toBe('ccc3');
  400. expect(scenario.variable.options[2].text).toBe('bbb2');
  401. });
  402. });
  403. //
  404. // datasource variable update
  405. //
  406. describeUpdateVariable('datasource variable with regex filter', scenario => {
  407. scenario.setup(() => {
  408. scenario.variableModel = {
  409. type: 'datasource',
  410. query: 'graphite',
  411. name: 'test',
  412. current: { value: 'backend4_pee', text: 'backend4_pee' },
  413. regex: '/pee$/',
  414. };
  415. scenario.metricSources = [
  416. { name: 'backend1', meta: { id: 'influx' } },
  417. { name: 'backend2_pee', meta: { id: 'graphite' } },
  418. { name: 'backend3', meta: { id: 'graphite' } },
  419. { name: 'backend4_pee', meta: { id: 'graphite' } },
  420. ];
  421. });
  422. it('should set only contain graphite ds and filtered using regex', () => {
  423. expect(scenario.variable.options.length).toBe(2);
  424. expect(scenario.variable.options[0].value).toBe('backend2_pee');
  425. expect(scenario.variable.options[1].value).toBe('backend4_pee');
  426. });
  427. it('should keep current value if available', () => {
  428. expect(scenario.variable.current.value).toBe('backend4_pee');
  429. });
  430. });
  431. //
  432. // Custom variable update
  433. //
  434. describeUpdateVariable('update custom variable', scenario => {
  435. scenario.setup(() => {
  436. scenario.variableModel = {
  437. type: 'custom',
  438. query: 'hej, hop, asd, escaped\\,var',
  439. name: 'test',
  440. };
  441. });
  442. it('should update options array', () => {
  443. expect(scenario.variable.options.length).toBe(4);
  444. expect(scenario.variable.options[0].text).toBe('hej');
  445. expect(scenario.variable.options[1].value).toBe('hop');
  446. expect(scenario.variable.options[2].value).toBe('asd');
  447. expect(scenario.variable.options[3].value).toBe('escaped,var');
  448. });
  449. });
  450. describe('multiple interval variables with auto', () => {
  451. let variable1, variable2;
  452. beforeEach(() => {
  453. const range = {
  454. from: dateTime(new Date())
  455. .subtract(7, 'days')
  456. .toDate(),
  457. to: new Date(),
  458. };
  459. ctx.timeSrv.timeRange = () => range;
  460. ctx.templateSrv.setGrafanaVariable = jest.fn();
  461. const variableModel1 = {
  462. type: 'interval',
  463. query: '1s,2h,5h,1d',
  464. name: 'variable1',
  465. auto: true,
  466. auto_count: 10,
  467. };
  468. variable1 = ctx.variableSrv.createVariableFromModel(variableModel1);
  469. ctx.variableSrv.addVariable(variable1);
  470. const variableModel2 = {
  471. type: 'interval',
  472. query: '1s,2h,5h',
  473. name: 'variable2',
  474. auto: true,
  475. auto_count: 1000,
  476. };
  477. variable2 = ctx.variableSrv.createVariableFromModel(variableModel2);
  478. ctx.variableSrv.addVariable(variable2);
  479. ctx.variableSrv.updateOptions(variable1);
  480. ctx.variableSrv.updateOptions(variable2);
  481. // ctx.$rootScope.$digest();
  482. });
  483. it('should update options array', () => {
  484. expect(variable1.options.length).toBe(5);
  485. expect(variable1.options[0].text).toBe('auto');
  486. expect(variable1.options[0].value).toBe('$__auto_interval_variable1');
  487. expect(variable2.options.length).toBe(4);
  488. expect(variable2.options[0].text).toBe('auto');
  489. expect(variable2.options[0].value).toBe('$__auto_interval_variable2');
  490. });
  491. it('should correctly set $__auto_interval_variableX', () => {
  492. let variable1Set,
  493. variable2Set,
  494. legacySet,
  495. unknownSet = false;
  496. // updateAutoValue() gets called repeatedly: once directly once via VariableSrv.validateVariableSelectionState()
  497. // So check that all calls are valid rather than expect a specific number and/or ordering of calls
  498. for (let i = 0; i < ctx.templateSrv.setGrafanaVariable.mock.calls.length; i++) {
  499. const call = ctx.templateSrv.setGrafanaVariable.mock.calls[i];
  500. switch (call[0]) {
  501. case '$__auto_interval_variable1':
  502. expect(call[1]).toBe('12h');
  503. variable1Set = true;
  504. break;
  505. case '$__auto_interval_variable2':
  506. expect(call[1]).toBe('10m');
  507. variable2Set = true;
  508. break;
  509. case '$__auto_interval':
  510. expect(call[1]).toEqual(expect.stringMatching(/^(12h|10m)$/));
  511. legacySet = true;
  512. break;
  513. default:
  514. unknownSet = true;
  515. break;
  516. }
  517. }
  518. expect(variable1Set).toEqual(true);
  519. expect(variable2Set).toEqual(true);
  520. expect(legacySet).toEqual(true);
  521. expect(unknownSet).toEqual(false);
  522. });
  523. });
  524. });
  525. function getVarMockConstructor(variable, model, ctx) {
  526. switch (model.model.type) {
  527. case 'datasource':
  528. return new variable(model.model, ctx.datasourceSrv, ctx.variableSrv, ctx.templateSrv);
  529. case 'query':
  530. return new variable(model.model, ctx.datasourceSrv, ctx.templateSrv, ctx.variableSrv);
  531. case 'interval':
  532. return new variable(model.model, ctx.timeSrv, ctx.templateSrv, ctx.variableSrv);
  533. case 'custom':
  534. return new variable(model.model, ctx.variableSrv);
  535. default:
  536. return new variable(model.model);
  537. }
  538. }