exporter_specs.ts 5.2 KB

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