DashboardExporter.test.ts 7.0 KB

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