exporter_specs.ts 5.0 KB

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