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 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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 com.google.common.collect.ImmutableSet;
  22. import java.io.File;
  23. import java.io.IOException;
  24. import java.util.Date;
  25. import java.util.Locale;
  26. import java.util.Properties;
  27. import java.util.stream.Collectors;
  28. import org.apache.commons.codec.digest.DigestUtils;
  29. import org.apache.commons.lang.StringUtils;
  30. import org.junit.Before;
  31. import org.junit.Rule;
  32. import org.junit.Test;
  33. import org.junit.rules.TemporaryFolder;
  34. import org.picocontainer.ComponentAdapter;
  35. import org.picocontainer.MutablePicoContainer;
  36. import org.sonar.api.CoreProperties;
  37. import org.sonar.api.utils.DateUtils;
  38. import org.sonar.api.utils.System2;
  39. import org.sonar.ce.CeDistributedInformationImpl;
  40. import org.sonar.ce.StandaloneCeDistributedInformation;
  41. import org.sonar.core.extension.ServiceLoaderWrapper;
  42. import org.sonar.db.DbTester;
  43. import org.sonar.db.property.PropertyDto;
  44. import org.sonar.process.ProcessId;
  45. import org.sonar.process.ProcessProperties;
  46. import org.sonar.process.Props;
  47. import org.sonar.server.property.InternalProperties;
  48. import static java.lang.String.valueOf;
  49. import static org.assertj.core.api.Assertions.assertThat;
  50. import static org.mockito.Mockito.mock;
  51. import static org.mockito.Mockito.when;
  52. import static org.sonar.process.ProcessEntryPoint.PROPERTY_PROCESS_INDEX;
  53. import static org.sonar.process.ProcessEntryPoint.PROPERTY_SHARED_PATH;
  54. import static org.sonar.process.ProcessProperties.Property.JDBC_PASSWORD;
  55. import static org.sonar.process.ProcessProperties.Property.JDBC_URL;
  56. import static org.sonar.process.ProcessProperties.Property.JDBC_USERNAME;
  57. import static org.sonar.process.ProcessProperties.Property.PATH_DATA;
  58. import static org.sonar.process.ProcessProperties.Property.PATH_HOME;
  59. import static org.sonar.process.ProcessProperties.Property.PATH_TEMP;
  60. public class ComputeEngineContainerImplTest {
  61. private static final int CONTAINER_ITSELF = 1;
  62. private static final int COMPONENTS_IN_LEVEL_1_AT_CONSTRUCTION = CONTAINER_ITSELF + 1;
  63. @Rule
  64. public TemporaryFolder tempFolder = new TemporaryFolder();
  65. @Rule
  66. public DbTester db = DbTester.create(System2.INSTANCE);
  67. private ComputeEngineContainerImpl underTest;
  68. private ServiceLoaderWrapper serviceLoaderWrapper = mock(ServiceLoaderWrapper.class);
  69. private ProcessProperties processProperties = new ProcessProperties(serviceLoaderWrapper);
  70. @Before
  71. public void setUp() {
  72. when(serviceLoaderWrapper.load()).thenReturn(ImmutableSet.of());
  73. underTest = new ComputeEngineContainerImpl();
  74. underTest.setComputeEngineStatus(mock(ComputeEngineStatus.class));
  75. }
  76. @Test
  77. public void constructor_does_not_create_container() {
  78. assertThat(underTest.getComponentContainer()).isNull();
  79. }
  80. @Test
  81. public void test_real_start() throws IOException {
  82. Properties properties = getProperties();
  83. // required persisted properties
  84. insertProperty(CoreProperties.SERVER_ID, "a_server_id");
  85. insertProperty(CoreProperties.SERVER_STARTTIME, DateUtils.formatDateTime(new Date()));
  86. insertInternalProperty(InternalProperties.SERVER_ID_CHECKSUM, DigestUtils.sha256Hex("a_server_id|" + cleanJdbcUrl()));
  87. underTest
  88. .start(new Props(properties));
  89. MutablePicoContainer picoContainer = underTest.getComponentContainer().getPicoContainer();
  90. try {
  91. assertThat(picoContainer.getComponentAdapters())
  92. .hasSize(
  93. CONTAINER_ITSELF
  94. + 63 // level 4
  95. + 7 // content of IssuesChangesNotificationModule
  96. + 6 // content of CeConfigurationModule
  97. + 4 // content of CeQueueModule
  98. + 3 // content of CeHttpModule
  99. + 3 // content of CeTaskCommonsModule
  100. + 4 // content of ProjectAnalysisTaskModule
  101. + 9 // content of CeTaskProcessorModule
  102. + 3 // content of ReportAnalysisFailureNotificationModule
  103. + 3 // CeCleaningModule + its content
  104. + 4 // WebhookModule
  105. + 1 // CeDistributedInformation
  106. );
  107. assertThat(picoContainer.getParent().getComponentAdapters()).hasSize(
  108. CONTAINER_ITSELF
  109. + 8 // level 3
  110. );
  111. assertThat(picoContainer.getParent().getParent().getComponentAdapters()).hasSize(
  112. CONTAINER_ITSELF
  113. + 6 // MigrationConfigurationModule
  114. + 15 // level 2
  115. );
  116. assertThat(picoContainer.getParent().getParent().getParent().getComponentAdapters()).hasSize(
  117. COMPONENTS_IN_LEVEL_1_AT_CONSTRUCTION
  118. + 27 // level 1
  119. + 63 // content of DaoModule
  120. + 3 // content of EsModule
  121. + 51 // content of CorePropertyDefinitions
  122. + 1 // StopFlagContainer
  123. );
  124. assertThat(
  125. picoContainer.getComponentAdapters().stream()
  126. .map(ComponentAdapter::getComponentImplementation)
  127. .collect(Collectors.toList())).doesNotContain(
  128. (Class) CeDistributedInformationImpl.class).contains(
  129. (Class) StandaloneCeDistributedInformation.class);
  130. assertThat(picoContainer.getParent().getParent().getParent().getParent()).isNull();
  131. } finally {
  132. underTest.stop();
  133. }
  134. assertThat(picoContainer.getLifecycleState().isStarted()).isFalse();
  135. assertThat(picoContainer.getLifecycleState().isStopped()).isFalse();
  136. assertThat(picoContainer.getLifecycleState().isDisposed()).isTrue();
  137. }
  138. private String cleanJdbcUrl() {
  139. return StringUtils.lowerCase(StringUtils.substringBefore(db.getUrl(), "?"), Locale.ENGLISH);
  140. }
  141. private Properties getProperties() throws IOException {
  142. Properties properties = new Properties();
  143. Props props = new Props(properties);
  144. processProperties.completeDefaults(props);
  145. properties = props.rawProperties();
  146. File homeDir = tempFolder.newFolder();
  147. File dataDir = new File(homeDir, "data");
  148. dataDir.mkdirs();
  149. File tmpDir = new File(homeDir, "tmp");
  150. tmpDir.mkdirs();
  151. properties.setProperty(PATH_HOME.getKey(), homeDir.getAbsolutePath());
  152. properties.setProperty(PATH_DATA.getKey(), dataDir.getAbsolutePath());
  153. properties.setProperty(PATH_TEMP.getKey(), tmpDir.getAbsolutePath());
  154. properties.setProperty(PROPERTY_PROCESS_INDEX, valueOf(ProcessId.COMPUTE_ENGINE.getIpcIndex()));
  155. properties.setProperty(PROPERTY_SHARED_PATH, tmpDir.getAbsolutePath());
  156. properties.setProperty(JDBC_URL.getKey(), db.getUrl());
  157. properties.setProperty(JDBC_USERNAME.getKey(), "sonar");
  158. properties.setProperty(JDBC_PASSWORD.getKey(), "sonar");
  159. return properties;
  160. }
  161. private void insertProperty(String key, String value) {
  162. PropertyDto dto = new PropertyDto().setKey(key).setValue(value);
  163. db.getDbClient().propertiesDao().saveProperty(db.getSession(), dto);
  164. db.commit();
  165. }
  166. private void insertInternalProperty(String key, String value) {
  167. db.getDbClient().internalPropertiesDao().save(db.getSession(), key, value);
  168. db.commit();
  169. }
  170. }