exporter_specs.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. datasourceSrvStub.get.withArgs('-- Grafana --').returns(Promise.resolve({
  73. name: '-- Grafana --',
  74. meta: {id: "grafana", info: {version: "1.2.1"}, name: "grafana", builtIn: true}
  75. }));
  76. config.panels['graph'] = {
  77. id: "graph",
  78. name: "Graph",
  79. info: {version: "1.1.0"}
  80. };
  81. dash = new DashboardModel(dash, {});
  82. var exporter = new DashboardExporter(datasourceSrvStub);
  83. exporter.makeExportable(dash).then(clean => {
  84. exported = clean;
  85. done();
  86. });
  87. });
  88. it('exported dashboard should not contain repeated panels', function() {
  89. expect(exported.rows[0].panels.length).to.be(3);
  90. });
  91. it('exported dashboard should not contain repeated rows', function() {
  92. expect(exported.rows.length).to.be(1);
  93. });
  94. it('should replace datasource refs', function() {
  95. var panel = exported.rows[0].panels[0];
  96. expect(panel.datasource).to.be("${DS_GFDB}");
  97. });
  98. it('should replace datasource in variable query', function() {
  99. expect(exported.templating.list[0].datasource).to.be("${DS_GFDB}");
  100. expect(exported.templating.list[0].options.length).to.be(0);
  101. expect(exported.templating.list[0].current.value).to.be(undefined);
  102. expect(exported.templating.list[0].current.text).to.be(undefined);
  103. });
  104. it('should replace datasource in annotation query', function() {
  105. expect(exported.annotations.list[1].datasource).to.be("${DS_GFDB}");
  106. });
  107. it('should add datasource as input', function() {
  108. expect(exported.__inputs[0].name).to.be("DS_GFDB");
  109. expect(exported.__inputs[0].pluginId).to.be("testdb");
  110. expect(exported.__inputs[0].type).to.be("datasource");
  111. });
  112. it('should add datasource to required', function() {
  113. var require = _.find(exported.__requires, {name: 'TestDB'});
  114. expect(require.name).to.be("TestDB");
  115. expect(require.id).to.be("testdb");
  116. expect(require.type).to.be("datasource");
  117. expect(require.version).to.be("1.2.1");
  118. });
  119. it('should not add built in datasources to required', function() {
  120. var require = _.find(exported.__requires, {name: 'Mixed'});
  121. expect(require).to.be(undefined);
  122. });
  123. it('should add datasources used in mixed mode', function() {
  124. var require = _.find(exported.__requires, {name: 'OtherDB'});
  125. expect(require).to.not.be(undefined);
  126. });
  127. it('should add panel to required', function() {
  128. var require = _.find(exported.__requires, {name: 'Graph'});
  129. expect(require.name).to.be("Graph");
  130. expect(require.id).to.be("graph");
  131. expect(require.version).to.be("1.1.0");
  132. });
  133. it('should add grafana version', function() {
  134. var require = _.find(exported.__requires, {name: 'Grafana'});
  135. expect(require.type).to.be("grafana");
  136. expect(require.id).to.be("grafana");
  137. expect(require.version).to.be("3.0.2");
  138. });
  139. it('should add constant template variables as inputs', function() {
  140. var input = _.find(exported.__inputs, {name: 'VAR_PREFIX'});
  141. expect(input.type).to.be("constant");
  142. expect(input.label).to.be("prefix");
  143. expect(input.value).to.be("collectd");
  144. });
  145. it('should templatize constant variables', function() {
  146. var variable = _.find(exported.templating.list, {name: 'prefix'});
  147. expect(variable.query).to.be("${VAR_PREFIX}");
  148. expect(variable.current.text).to.be("${VAR_PREFIX}");
  149. expect(variable.current.value).to.be("${VAR_PREFIX}");
  150. expect(variable.options[0].text).to.be("${VAR_PREFIX}");
  151. expect(variable.options[0].value).to.be("${VAR_PREFIX}");
  152. });
  153. });