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.0KB

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