variable_srv.jest.ts 20 KB

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