aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-ce/src/it/java/org/sonar/ce/container/ComputeEngineContainerImplIT.java
blob: 3259d6707922a338d166527ea4174dba17dce129 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 * SonarQube
 * Copyright (C) 2009-2025 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.ce.container;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Locale;
import java.util.Properties;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.io.TempDir;
import org.sonar.api.CoreProperties;
import org.sonar.api.utils.DateUtils;
import org.sonar.api.utils.System2;
import org.sonar.core.extension.ServiceLoaderWrapper;
import org.sonar.db.DbTester;
import org.sonar.db.property.PropertyDto;
import org.sonar.process.ProcessId;
import org.sonar.process.ProcessProperties;
import org.sonar.process.Props;
import org.sonar.server.extension.TestCoreExtension;
import org.sonar.server.property.InternalProperties;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import static java.lang.String.valueOf;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.sonar.process.ProcessEntryPoint.PROPERTY_PROCESS_INDEX;
import static org.sonar.process.ProcessEntryPoint.PROPERTY_SHARED_PATH;
import static org.sonar.process.ProcessProperties.Property.JDBC_PASSWORD;
import static org.sonar.process.ProcessProperties.Property.JDBC_URL;
import static org.sonar.process.ProcessProperties.Property.JDBC_USERNAME;
import static org.sonar.process.ProcessProperties.Property.PATH_DATA;
import static org.sonar.process.ProcessProperties.Property.PATH_HOME;
import static org.sonar.process.ProcessProperties.Property.PATH_TEMP;

class ComputeEngineContainerImplIT {

  @TempDir
  private File tempFolder;

  @RegisterExtension
  private DbTester db = DbTester.create(System2.INSTANCE);

  private final ServiceLoaderWrapper serviceLoaderWrapper = mock(ServiceLoaderWrapper.class);
  private final ProcessProperties processProperties = new ProcessProperties(serviceLoaderWrapper);
  private final ComputeEngineContainerImpl underTest = new ComputeEngineContainerImpl();

  @BeforeEach
  void setUp() {
    underTest.setComputeEngineStatus(mock(ComputeEngineStatus.class));
  }

  @Test
  void constructor_does_not_create_container() {
    assertThat(underTest.getComponentContainer()).isNull();
  }

  @Test
  void whenStartingCeContainer_thenBeansAndExtensionsLoadTogether() throws IOException {
    Properties properties = getProperties();

    // required persisted properties
    insertProperty(CoreProperties.SERVER_ID, "a_server_id");
    insertProperty(CoreProperties.SERVER_STARTTIME, DateUtils.formatDateTime(new Date()));
    insertInternalProperty(InternalProperties.SERVER_ID_CHECKSUM, DigestUtils.sha256Hex("a_server_id|" + cleanJdbcUrl()));

    underTest.start(new Props(properties));

    AnnotationConfigApplicationContext context = underTest.getComponentContainer().context();
    try {
      assertThat(context.getBeanDefinitionNames()).hasSizeGreaterThan(1)
        .anyMatch(beanName -> beanName.endsWith("TestCoreExtension.TestBean4"));
      assertThat(underTest.getComponentContainer().getOptionalComponentByType(TestCoreExtension.TestBean4.class)).isPresent();
      assertThat(context.getParent().getBeanDefinitionNames()).hasSizeGreaterThan(1)
        .anyMatch(beanName -> beanName.endsWith("TestCoreExtension.TestBean3"));
      assertThat(underTest.getComponentContainer().getOptionalComponentByType(TestCoreExtension.TestBean3.class)).isPresent();
      assertThat(context.getParent().getParent().getBeanDefinitionNames()).hasSizeGreaterThan(1)
        .anyMatch(beanName -> beanName.endsWith("TestCoreExtension.TestBean2"));
      assertThat(underTest.getComponentContainer().getOptionalComponentByType(TestCoreExtension.TestBean2.class)).isPresent();
      assertThat(context.getParent().getParent().getParent().getBeanDefinitionNames()).hasSizeGreaterThan(1)
        .anyMatch(beanName -> beanName.endsWith("TestCoreExtension.TestBean1"));
      assertThat(underTest.getComponentContainer().getOptionalComponentByType(TestCoreExtension.TestBean1.class)).isPresent()
        .get().matches(tb -> tb.called, "mybatis extension has been called");
      assertThat(context.getParent().getParent().getParent().getParent()).isNull();
    } finally {
      underTest.stop();
    }

    assertThat(context.isActive()).isFalse();
  }

  private String cleanJdbcUrl() {
    return StringUtils.lowerCase(StringUtils.substringBefore(db.getUrl(), "?"), Locale.ENGLISH);
  }

  private Properties getProperties() throws IOException {
    Properties properties = new Properties();
    Props props = new Props(properties);
    processProperties.completeDefaults(props);
    properties = props.rawProperties();
    File homeDir = tempFolder;
    File dataDir = new File(tempFolder, "data");
    dataDir.mkdirs();
    File tmpDir = new File(tempFolder, "tmp");
    tmpDir.mkdirs();
    properties.setProperty(PATH_HOME.getKey(), homeDir.getAbsolutePath());
    properties.setProperty(PATH_DATA.getKey(), dataDir.getAbsolutePath());
    properties.setProperty(PATH_TEMP.getKey(), tmpDir.getAbsolutePath());
    properties.setProperty(PROPERTY_PROCESS_INDEX, valueOf(ProcessId.COMPUTE_ENGINE.getIpcIndex()));
    properties.setProperty(PROPERTY_SHARED_PATH, tmpDir.getAbsolutePath());
    properties.setProperty(JDBC_URL.getKey(), db.getUrl());
    properties.setProperty(JDBC_USERNAME.getKey(), "sonar");
    properties.setProperty(JDBC_PASSWORD.getKey(), "sonar");
    return properties;
  }

  private void insertProperty(String key, String value) {
    PropertyDto dto = new PropertyDto().setKey(key).setValue(value);
    db.getDbClient().propertiesDao().saveProperty(db.getSession(), dto, null, null, null, null);
    db.commit();
  }

  private void insertInternalProperty(String key, String value) {
    db.getDbClient().internalPropertiesDao().save(db.getSession(), key, value);
    db.commit();
  }
}