variable_srv.test.ts 19 KB

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