variable_srv.test.ts 19 KB

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