You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ComputeEngineContainerImplTest.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.ce.container;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.util.Date;
  24. import java.util.Properties;
  25. import java.util.stream.Collectors;
  26. import org.junit.Before;
  27. import org.junit.Rule;
  28. import org.junit.Test;
  29. import org.junit.rules.TemporaryFolder;
  30. import org.picocontainer.ComponentAdapter;
  31. import org.picocontainer.MutablePicoContainer;
  32. import org.sonar.api.CoreProperties;
  33. import org.sonar.api.utils.DateUtils;
  34. import org.sonar.api.utils.System2;
  35. import org.sonar.ce.CeDistributedInformationImpl;
  36. import org.sonar.ce.StandaloneCeDistributedInformation;
  37. import org.sonar.db.DbTester;
  38. import org.sonar.db.property.PropertyDto;
  39. import org.sonar.process.ProcessId;
  40. import org.sonar.process.ProcessProperties;
  41. import org.sonar.process.Props;
  42. import org.sonar.server.platform.ServerIdChecksum;
  43. import org.sonar.server.property.InternalProperties;
  44. import static java.lang.String.valueOf;
  45. import static org.assertj.core.api.Assertions.assertThat;
  46. import static org.mockito.Mockito.mock;
  47. import static org.sonar.process.ProcessEntryPoint.PROPERTY_PROCESS_INDEX;
  48. import static org.sonar.process.ProcessEntryPoint.PROPERTY_SHARED_PATH;
  49. import static org.sonar.process.ProcessProperties.Property.JDBC_PASSWORD;
  50. import static org.sonar.process.ProcessProperties.Property.JDBC_URL;
  51. import static org.sonar.process.ProcessProperties.Property.JDBC_USERNAME;
  52. import static org.sonar.process.ProcessProperties.Property.PATH_DATA;
  53. import static org.sonar.process.ProcessProperties.Property.PATH_HOME;
  54. import static org.sonar.process.ProcessProperties.Property.PATH_TEMP;
  55. public class ComputeEngineContainerImplTest {
  56. private static final int CONTAINER_ITSELF = 1;
  57. private static final int COMPONENTS_IN_LEVEL_1_AT_CONSTRUCTION = CONTAINER_ITSELF + 1;
  58. @Rule
  59. public TemporaryFolder tempFolder = new TemporaryFolder();
  60. @Rule
  61. public DbTester db = DbTester.create(System2.INSTANCE);
  62. private ComputeEngineContainerImpl underTest;
  63. @Before
  64. public void setUp() {
  65. underTest = new ComputeEngineContainerImpl();
  66. underTest.setComputeEngineStatus(mock(ComputeEngineStatus.class));
  67. }
  68. @Test
  69. public void constructor_does_not_create_container() {
  70. assertThat(underTest.getComponentContainer()).isNull();
  71. }
  72. @Test
  73. public void test_real_start() throws IOException {
  74. Properties properties = getProperties();
  75. // required persisted properties
  76. insertProperty(CoreProperties.SERVER_ID, "a_server_id");
  77. insertProperty(CoreProperties.SERVER_STARTTIME, DateUtils.formatDateTime(new Date()));
  78. insertInternalProperty(InternalProperties.SERVER_ID_CHECKSUM, ServerIdChecksum.of("a_server_id", db.getUrl()));
  79. underTest
  80. .start(new Props(properties));
  81. MutablePicoContainer picoContainer = underTest.getComponentContainer().getPicoContainer();
  82. assertThat(picoContainer.getComponentAdapters())
  83. .hasSize(
  84. CONTAINER_ITSELF
  85. + 78 // level 4
  86. + 21 // content of QualityGateModule
  87. + 6 // content of CeConfigurationModule
  88. + 4 // content of CeQueueModule
  89. + 4 // content of CeHttpModule
  90. + 3 // content of CeTaskCommonsModule
  91. + 4 // content of ProjectAnalysisTaskModule
  92. + 7 // content of CeTaskProcessorModule
  93. + 4 // content of ReportAnalysisFailureNotificationModule
  94. + 3 // CeCleaningModule + its content
  95. + 4 // WebhookModule
  96. + 1 // CeDistributedInformation
  97. );
  98. assertThat(picoContainer.getParent().getComponentAdapters()).hasSize(
  99. CONTAINER_ITSELF
  100. + 7 // level 3
  101. );
  102. assertThat(picoContainer.getParent().getParent().getComponentAdapters()).hasSize(
  103. CONTAINER_ITSELF
  104. + 15 // MigrationConfigurationModule
  105. + 17 // level 2
  106. );
  107. assertThat(picoContainer.getParent().getParent().getParent().getComponentAdapters()).hasSize(
  108. COMPONENTS_IN_LEVEL_1_AT_CONSTRUCTION
  109. + 26 // level 1
  110. + 53 // content of DaoModule
  111. + 3 // content of EsModule
  112. + 59 // content of CorePropertyDefinitions
  113. + 1 // StopFlagContainer
  114. );
  115. assertThat(
  116. picoContainer.getComponentAdapters().stream()
  117. .map(ComponentAdapter::getComponentImplementation)
  118. .collect(Collectors.toList())).doesNotContain(
  119. (Class) CeDistributedInformationImpl.class).contains(
  120. (Class) StandaloneCeDistributedInformation.class);
  121. assertThat(picoContainer.getParent().getParent().getParent().getParent()).isNull();
  122. underTest.stop();
  123. assertThat(picoContainer.getLifecycleState().isStarted()).isFalse();
  124. assertThat(picoContainer.getLifecycleState().isStopped()).isFalse();
  125. assertThat(picoContainer.getLifecycleState().isDisposed()).isTrue();
  126. }
  127. private Properties getProperties() throws IOException {
  128. Properties properties = ProcessProperties.defaults();
  129. File homeDir = tempFolder.newFolder();
  130. File dataDir = new File(homeDir, "data");
  131. File tmpDir = new File(homeDir, "tmp");
  132. properties.setProperty(PATH_HOME.getKey(), homeDir.getAbsolutePath());
  133. properties.setProperty(PATH_DATA.getKey(), dataDir.getAbsolutePath());
  134. properties.setProperty(PATH_TEMP.getKey(), tmpDir.getAbsolutePath());
  135. properties.setProperty(PROPERTY_PROCESS_INDEX, valueOf(ProcessId.COMPUTE_ENGINE.getIpcIndex()));
  136. properties.setProperty(PROPERTY_SHARED_PATH, tmpDir.getAbsolutePath());
  137. properties.setProperty(JDBC_URL.getKey(), db.getUrl());
  138. properties.setProperty(JDBC_USERNAME.getKey(), "sonar");
  139. properties.setProperty(JDBC_PASSWORD.getKey(), "sonar");
  140. return properties;
  141. }
  142. private void insertProperty(String key, String value) {
  143. PropertyDto dto = new PropertyDto().setKey(key).setValue(value);
  144. db.getDbClient().propertiesDao().saveProperty(db.getSession(), dto);
  145. db.commit();
  146. }
  147. private void insertInternalProperty(String key, String value) {
  148. db.getDbClient().internalPropertiesDao().save(db.getSession(), key, value);
  149. db.commit();
  150. }
  151. }