DashboardExporter.test.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. jest.mock('app/core/store', () => {
  2. return {
  3. getBool: jest.fn(),
  4. };
  5. });
  6. // @ts-ignore
  7. import _ from 'lodash';
  8. import config from 'app/core/config';
  9. import { DashboardExporter } from './DashboardExporter';
  10. import { DashboardModel } from '../../state/DashboardModel';
  11. import { DatasourceSrv } from 'app/features/plugins/datasource_srv';
  12. import { PanelPlugin } from 'app/types';
  13. describe('given dashboard with repeated panels', () => {
  14. let dash: any, exported: any;
  15. beforeEach(done => {
  16. dash = {
  17. templating: {
  18. list: [
  19. {
  20. name: 'apps',
  21. type: 'query',
  22. datasource: 'gfdb',
  23. current: { value: 'Asd', text: 'Asd' },
  24. options: [{ value: 'Asd', text: 'Asd' }],
  25. },
  26. {
  27. name: 'prefix',
  28. type: 'constant',
  29. current: { value: 'collectd', text: 'collectd' },
  30. options: [],
  31. },
  32. {
  33. name: 'ds',
  34. type: 'datasource',
  35. query: 'other2',
  36. current: { value: 'other2', text: 'other2' },
  37. options: [],
  38. },
  39. ],
  40. },
  41. annotations: {
  42. list: [
  43. {
  44. name: 'logs',
  45. datasource: 'gfdb',
  46. },
  47. ],
  48. },
  49. panels: [
  50. { id: 6, datasource: 'gfdb', type: 'graph' },
  51. { id: 7 },
  52. {
  53. id: 8,
  54. datasource: '-- Mixed --',
  55. targets: [{ datasource: 'other' }],
  56. },
  57. { id: 9, datasource: '$ds' },
  58. {
  59. id: 2,
  60. repeat: 'apps',
  61. datasource: 'gfdb',
  62. type: 'graph',
  63. },
  64. { id: 3, repeat: null, repeatPanelId: 2 },
  65. {
  66. id: 4,
  67. collapsed: true,
  68. panels: [
  69. { id: 10, datasource: 'gfdb', type: 'table' },
  70. { id: 11 },
  71. {
  72. id: 12,
  73. datasource: '-- Mixed --',
  74. targets: [{ datasource: 'other' }],
  75. },
  76. { id: 13, datasource: '$ds' },
  77. {
  78. id: 14,
  79. repeat: 'apps',
  80. datasource: 'gfdb',
  81. type: 'heatmap',
  82. },
  83. { id: 15, repeat: null, repeatPanelId: 14 },
  84. ],
  85. },
  86. ],
  87. };
  88. config.buildInfo.version = '3.0.2';
  89. //Stubs test function calls
  90. const datasourceSrvStub = ({ get: jest.fn(arg => getStub(arg)) } as any) as DatasourceSrv;
  91. config.panels['graph'] = {
  92. id: 'graph',
  93. name: 'Graph',
  94. info: { version: '1.1.0' },
  95. } as PanelPlugin;
  96. config.panels['table'] = {
  97. id: 'table',
  98. name: 'Table',
  99. info: { version: '1.1.1' },
  100. } as PanelPlugin;
  101. config.panels['heatmap'] = {
  102. id: 'heatmap',
  103. name: 'Heatmap',
  104. info: { version: '1.1.2' },
  105. } as PanelPlugin;
  106. dash = new DashboardModel(dash, {});
  107. const exporter = new DashboardExporter(datasourceSrvStub);
  108. exporter.makeExportable(dash).then(clean => {
  109. exported = clean;
  110. done();
  111. });
  112. });
  113. it('should replace datasource refs', () => {
  114. const panel = exported.panels[0];
  115. expect(panel.datasource).toBe('${DS_GFDB}');
  116. });
  117. it('should replace datasource refs in collapsed row', () => {
  118. const panel = exported.panels[5].panels[0];
  119. expect(panel.datasource).toBe('${DS_GFDB}');
  120. });
  121. it('should replace datasource in variable query', () => {
  122. expect(exported.templating.list[0].datasource).toBe('${DS_GFDB}');
  123. expect(exported.templating.list[0].options.length).toBe(0);
  124. expect(exported.templating.list[0].current.value).toBe(undefined);
  125. expect(exported.templating.list[0].current.text).toBe(undefined);
  126. });
  127. it('should replace datasource in annotation query', () => {
  128. expect(exported.annotations.list[1].datasource).toBe('${DS_GFDB}');
  129. });
  130. it('should add datasource as input', () => {
  131. expect(exported.__inputs[0].name).toBe('DS_GFDB');
  132. expect(exported.__inputs[0].pluginId).toBe('testdb');
  133. expect(exported.__inputs[0].type).toBe('datasource');
  134. });
  135. it('should add datasource to required', () => {
  136. const require = _.find(exported.__requires, { name: 'TestDB' });
  137. expect(require.name).toBe('TestDB');
  138. expect(require.id).toBe('testdb');
  139. expect(require.type).toBe('datasource');
  140. expect(require.version).toBe('1.2.1');
  141. });
  142. it('should not add built in datasources to required', () => {
  143. const require = _.find(exported.__requires, { name: 'Mixed' });
  144. expect(require).toBe(undefined);
  145. });
  146. it('should add datasources used in mixed mode', () => {
  147. const require = _.find(exported.__requires, { name: 'OtherDB' });
  148. expect(require).not.toBe(undefined);
  149. });
  150. it('should add graph panel to required', () => {
  151. const require = _.find(exported.__requires, { name: 'Graph' });
  152. expect(require.name).toBe('Graph');
  153. expect(require.id).toBe('graph');
  154. expect(require.version).toBe('1.1.0');
  155. });
  156. it('should add table panel to required', () => {
  157. const require = _.find(exported.__requires, { name: 'Table' });
  158. expect(require.name).toBe('Table');
  159. expect(require.id).toBe('table');
  160. expect(require.version).toBe('1.1.1');
  161. });
  162. it('should add heatmap panel to required', () => {
  163. const require = _.find(exported.__requires, { name: 'Heatmap' });
  164. expect(require.name).toBe('Heatmap');
  165. expect(require.id).toBe('heatmap');
  166. expect(require.version).toBe('1.1.2');
  167. });
  168. it('should add grafana version', () => {
  169. const require = _.find(exported.__requires, { name: 'Grafana' });
  170. expect(require.type).toBe('grafana');
  171. expect(require.id).toBe('grafana');
  172. expect(require.version).toBe('3.0.2');
  173. });
  174. it('should add constant template variables as inputs', () => {
  175. const input = _.find(exported.__inputs, { name: 'VAR_PREFIX' });
  176. expect(input.type).toBe('constant');
  177. expect(input.label).toBe('prefix');
  178. expect(input.value).toBe('collectd');
  179. });
  180. it('should templatize constant variables', () => {
  181. const variable = _.find(exported.templating.list, { name: 'prefix' });
  182. expect(variable.query).toBe('${VAR_PREFIX}');
  183. expect(variable.current.text).toBe('${VAR_PREFIX}');
  184. expect(variable.current.value).toBe('${VAR_PREFIX}');
  185. expect(variable.options[0].text).toBe('${VAR_PREFIX}');
  186. expect(variable.options[0].value).toBe('${VAR_PREFIX}');
  187. });
  188. it('should add datasources only use via datasource variable to requires', () => {
  189. const require = _.find(exported.__requires, { name: 'OtherDB_2' });
  190. expect(require.id).toBe('other2');
  191. });
  192. });
  193. // Stub responses
  194. const stubs: { [key: string]: {} } = {};
  195. stubs['gfdb'] = {
  196. name: 'gfdb',
  197. meta: { id: 'testdb', info: { version: '1.2.1' }, name: 'TestDB' },
  198. };
  199. stubs['other'] = {
  200. name: 'other',
  201. meta: { id: 'other', info: { version: '1.2.1' }, name: 'OtherDB' },
  202. };
  203. stubs['other2'] = {
  204. name: 'other2',
  205. meta: { id: 'other2', info: { version: '1.2.1' }, name: 'OtherDB_2' },
  206. };
  207. stubs['-- Mixed --'] = {
  208. name: 'mixed',
  209. meta: {
  210. id: 'mixed',
  211. info: { version: '1.2.1' },
  212. name: 'Mixed',
  213. builtIn: true,
  214. },
  215. };
  216. stubs['-- Grafana --'] = {
  217. name: '-- Grafana --',
  218. meta: {
  219. id: 'grafana',
  220. info: { version: '1.2.1' },
  221. name: 'grafana',
  222. builtIn: true,
  223. },
  224. };
  225. function getStub(arg: string) {
  226. return Promise.resolve(stubs[arg || 'gfdb']);
  227. }