variable_srv.jest.ts 19 KB

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