variable_srv.test.ts 19 KB

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