exporter_specs.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import { describe, beforeEach, it, sinon, expect } from 'test/lib/common';
  2. import _ from 'lodash';
  3. import config from 'app/core/config';
  4. import { DashboardExporter } from '../export/exporter';
  5. import { DashboardModel } from '../dashboard_model';
  6. describe('given dashboard with repeated panels', function() {
  7. var dash, exported;
  8. beforeEach(done => {
  9. dash = {
  10. templating: { list: [] },
  11. annotations: { list: [] },
  12. };
  13. config.buildInfo = {
  14. version: '3.0.2',
  15. };
  16. dash.templating.list.push({
  17. name: 'apps',
  18. type: 'query',
  19. datasource: 'gfdb',
  20. current: { value: 'Asd', text: 'Asd' },
  21. options: [{ value: 'Asd', text: 'Asd' }],
  22. });
  23. dash.templating.list.push({
  24. name: 'prefix',
  25. type: 'constant',
  26. current: { value: 'collectd', text: 'collectd' },
  27. options: [],
  28. });
  29. dash.templating.list.push({
  30. name: 'ds',
  31. type: 'datasource',
  32. query: 'testdb',
  33. current: { value: 'prod', text: 'prod' },
  34. options: [],
  35. });
  36. dash.annotations.list.push({
  37. name: 'logs',
  38. datasource: 'gfdb',
  39. });
  40. dash.panels = [
  41. { id: 6, datasource: 'gfdb', type: 'graph' },
  42. { id: 7 },
  43. {
  44. id: 8,
  45. datasource: '-- Mixed --',
  46. targets: [{ datasource: 'other' }],
  47. },
  48. { id: 9, datasource: '$ds' },
  49. ];
  50. dash.panels.push({
  51. id: 2,
  52. repeat: 'apps',
  53. datasource: 'gfdb',
  54. type: 'graph',
  55. });
  56. dash.panels.push({ id: 3, repeat: null, repeatPanelId: 2 });
  57. var datasourceSrvStub = { get: sinon.stub() };
  58. datasourceSrvStub.get.withArgs('gfdb').returns(
  59. Promise.resolve({
  60. name: 'gfdb',
  61. meta: { id: 'testdb', info: { version: '1.2.1' }, name: 'TestDB' },
  62. })
  63. );
  64. datasourceSrvStub.get.withArgs('other').returns(
  65. Promise.resolve({
  66. name: 'other',
  67. meta: { id: 'other', info: { version: '1.2.1' }, name: 'OtherDB' },
  68. })
  69. );
  70. datasourceSrvStub.get.withArgs('-- Mixed --').returns(
  71. Promise.resolve({
  72. name: 'mixed',
  73. meta: {
  74. id: 'mixed',
  75. info: { version: '1.2.1' },
  76. name: 'Mixed',
  77. builtIn: true,
  78. },
  79. })
  80. );
  81. datasourceSrvStub.get.withArgs('-- Grafana --').returns(
  82. Promise.resolve({
  83. name: '-- Grafana --',
  84. meta: {
  85. id: 'grafana',
  86. info: { version: '1.2.1' },
  87. name: 'grafana',
  88. builtIn: true,
  89. },
  90. })
  91. );
  92. config.panels['graph'] = {
  93. id: 'graph',
  94. name: 'Graph',
  95. info: { version: '1.1.0' },
  96. };
  97. dash = new DashboardModel(dash, {});
  98. var exporter = new DashboardExporter(datasourceSrvStub);
  99. exporter.makeExportable(dash).then(clean => {
  100. exported = clean;
  101. done();
  102. });
  103. });
  104. it('should replace datasource refs', function() {
  105. var panel = exported.panels[0];
  106. expect(panel.datasource).to.be('${DS_GFDB}');
  107. });
  108. it('should replace datasource in variable query', function() {
  109. expect(exported.templating.list[0].datasource).to.be('${DS_GFDB}');
  110. expect(exported.templating.list[0].options.length).to.be(0);
  111. expect(exported.templating.list[0].current.value).to.be(undefined);
  112. expect(exported.templating.list[0].current.text).to.be(undefined);
  113. });
  114. it('should replace datasource in annotation query', function() {
  115. expect(exported.annotations.list[1].datasource).to.be('${DS_GFDB}');
  116. });
  117. it('should add datasource as input', function() {
  118. expect(exported.__inputs[0].name).to.be('DS_GFDB');
  119. expect(exported.__inputs[0].pluginId).to.be('testdb');
  120. expect(exported.__inputs[0].type).to.be('datasource');
  121. });
  122. it('should add datasource to required', function() {
  123. var require = _.find(exported.__requires, { name: 'TestDB' });
  124. expect(require.name).to.be('TestDB');
  125. expect(require.id).to.be('testdb');
  126. expect(require.type).to.be('datasource');
  127. expect(require.version).to.be('1.2.1');
  128. });
  129. it('should not add built in datasources to required', function() {
  130. var require = _.find(exported.__requires, { name: 'Mixed' });
  131. expect(require).to.be(undefined);
  132. });
  133. it('should add datasources used in mixed mode', function() {
  134. var require = _.find(exported.__requires, { name: 'OtherDB' });
  135. expect(require).to.not.be(undefined);
  136. });
  137. it('should add panel to required', function() {
  138. var require = _.find(exported.__requires, { name: 'Graph' });
  139. expect(require.name).to.be('Graph');
  140. expect(require.id).to.be('graph');
  141. expect(require.version).to.be('1.1.0');
  142. });
  143. it('should add grafana version', function() {
  144. var require = _.find(exported.__requires, { name: 'Grafana' });
  145. expect(require.type).to.be('grafana');
  146. expect(require.id).to.be('grafana');
  147. expect(require.version).to.be('3.0.2');
  148. });
  149. it('should add constant template variables as inputs', function() {
  150. var input = _.find(exported.__inputs, { name: 'VAR_PREFIX' });
  151. expect(input.type).to.be('constant');
  152. expect(input.label).to.be('prefix');
  153. expect(input.value).to.be('collectd');
  154. });
  155. it('should templatize constant variables', function() {
  156. var variable = _.find(exported.templating.list, { name: 'prefix' });
  157. expect(variable.query).to.be('${VAR_PREFIX}');
  158. expect(variable.current.text).to.be('${VAR_PREFIX}');
  159. expect(variable.current.value).to.be('${VAR_PREFIX}');
  160. expect(variable.options[0].text).to.be('${VAR_PREFIX}');
  161. expect(variable.options[0].value).to.be('${VAR_PREFIX}');
  162. });
  163. });