exporter_specs.ts 5.3 KB

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