aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-main/src/test/java/org/sonar/application/command/EsJvmOptionsTest.java
blob: 81140a6b9d9b82a3d43990fb2c68ecd79600b678 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
 * 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.application.command;

import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Properties;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.sonar.process.Props;

import static org.apache.commons.lang3.RandomStringUtils.secure;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

@RunWith(DataProviderRunner.class)
public class EsJvmOptionsTest {
  @Rule
  public final TemporaryFolder temporaryFolder = new TemporaryFolder();

  private final Properties properties = new Properties();

  @Before
  public void before() {
    properties.put("sonar.path.logs", "path_to_logs");
  }

  @Test
  public void constructor_sets_mandatory_JVM_options() throws IOException {
    File tmpDir = temporaryFolder.newFolder();
    EsJvmOptions underTest = new EsJvmOptions(new Props(properties), tmpDir);

    assertThat(underTest.getAll())
      .containsExactlyInAnyOrder(
        "-XX:+UseG1GC",
        "-Djava.io.tmpdir=" + tmpDir.getAbsolutePath(),
        "-XX:ErrorFile=" + Paths.get("path_to_logs/es_hs_err_pid%p.log").toAbsolutePath(),
        "-Des.networkaddress.cache.ttl=60",
        "-Des.networkaddress.cache.negative.ttl=10",
        "-XX:+AlwaysPreTouch",
        "-Xss1m",
        "-Djava.awt.headless=true",
        "-Dfile.encoding=UTF-8",
        "-Djna.nosys=true",
        "-Djna.tmpdir=" + tmpDir.getAbsolutePath(),
        "-XX:-OmitStackTraceInFastThrow",
        "-Dio.netty.noUnsafe=true",
        "-Dio.netty.noKeySetOptimization=true",
        "-Dio.netty.recycler.maxCapacityPerThread=0",
        "-Dio.netty.allocator.numDirectArenas=0",
        "-Dlog4j.shutdownHookEnabled=false",
        "-Dlog4j2.disable.jmx=true",
        "-Dlog4j2.formatMsgNoLookups=true",
        "-Djava.locale.providers=COMPAT",
        "-Des.enforce.bootstrap.checks=true",
        "-Xlog:disable");
  }

  @Test
  public void constructor_does_not_force_boostrap_checks_if_sonarqube_property_is_true() throws IOException {
    properties.put("sonar.es.bootstrap.checks.disable", "true");
    File tmpDir = temporaryFolder.newFolder();
    EsJvmOptions underTest = new EsJvmOptions(new Props(properties), tmpDir);

    assertThat(underTest.getAll())
      .isNotEmpty()
      .doesNotContain("-Des.enforce.bootstrap.checks=true");
  }

  @Test
  public void constructor_forces_boostrap_checks_if_jdbc_url_property_does_not_exist() throws IOException {
    File tmpDir = temporaryFolder.newFolder();
    EsJvmOptions underTest = new EsJvmOptions(new Props(properties), tmpDir);

    assertThat(underTest.getAll())
      .contains("-Des.enforce.bootstrap.checks=true");
  }

  @Test
  public void constructor_forces_boostrap_checks_if_jdbc_url_property_is_not_h2() throws IOException {
    properties.put("sonar.jdbc.url", secure().nextAlphanumeric(53));
    File tmpDir = temporaryFolder.newFolder();
    EsJvmOptions underTest = new EsJvmOptions(new Props(properties), tmpDir);

    assertThat(underTest.getAll())
      .contains("-Des.enforce.bootstrap.checks=true");
  }

  @Test
  public void constructor_does_not_force_boostrap_checks_if_jdbc_url_property_contains_h2() throws IOException {
    properties.put("sonar.jdbc.url", "jdbc:h2:tcp://ffoo:bar/sonar");
    File tmpDir = temporaryFolder.newFolder();
    EsJvmOptions underTest = new EsJvmOptions(new Props(properties), tmpDir);

    assertThat(underTest.getAll())
      .isNotEmpty()
      .doesNotContain("-Des.enforce.bootstrap.checks=true");
  }

  @Test
  public void boostrap_checks_can_be_set_true_if_h2() throws IOException {
    properties.put("sonar.jdbc.url", "jdbc:h2:tcp://ffoo:bar/sonar");
    properties.put("sonar.es.bootstrap.checks.disable", "true");

    File tmpDir = temporaryFolder.newFolder();
    EsJvmOptions underTest = new EsJvmOptions(new Props(properties), tmpDir);

    assertThat(underTest.getAll())
      .isNotEmpty()
      .doesNotContain("-Des.enforce.bootstrap.checks=true");
  }

  @Test
  public void boostrap_checks_can_be_set_false_if_h2() throws IOException {
    properties.put("sonar.jdbc.url", "jdbc:h2:tcp://ffoo:bar/sonar");
    properties.put("sonar.es.bootstrap.checks.disable", "false");

    File tmpDir = temporaryFolder.newFolder();
    EsJvmOptions underTest = new EsJvmOptions(new Props(properties), tmpDir);

    assertThat(underTest.getAll())
      .isNotEmpty()
      .contains("-Des.enforce.bootstrap.checks=true");
  }

  @Test
  public void boostrap_checks_can_be_set_true_if_jdbc_other_than_h2() throws IOException {
    properties.put("sonar.jdbc.url", secure().nextAlphanumeric(53));
    properties.put("sonar.es.bootstrap.checks.disable", "true");

    File tmpDir = temporaryFolder.newFolder();
    EsJvmOptions underTest = new EsJvmOptions(new Props(properties), tmpDir);

    assertThat(underTest.getAll())
      .isNotEmpty()
      .doesNotContain("-Des.enforce.bootstrap.checks=true");
  }

  @Test
  public void boostrap_checks_can_be_set_false_if_jdbc_other_than_h2() throws IOException {
    properties.put("sonar.jdbc.url", secure().nextAlphanumeric(53));
    properties.put("sonar.es.bootstrap.checks.disable", "false");

    File tmpDir = temporaryFolder.newFolder();
    EsJvmOptions underTest = new EsJvmOptions(new Props(properties), tmpDir);

    assertThat(underTest.getAll())
      .isNotEmpty()
      .contains("-Des.enforce.bootstrap.checks=true");
  }

  /**
   * This test may fail if SQ's test are not executed with target Java version 8.
   */
  @Test
  public void writeToJvmOptionFile_writes_all_JVM_options_to_file_with_warning_header() throws IOException {
    File tmpDir = temporaryFolder.newFolder("with space");
    File file = temporaryFolder.newFile();
    EsJvmOptions underTest = new EsJvmOptions(new Props(properties), tmpDir)
      .add("-foo")
      .add("-bar");

    underTest.writeToJvmOptionFile(file);

    assertThat(file).hasContent(
      "# This file has been automatically generated by SonarQube during startup.\n" +
        "# Please use sonar.search.javaOpts and/or sonar.search.javaAdditionalOpts in sonar.properties to specify jvm options for Elasticsearch\n" +
        "\n" +
        "# DO NOT EDIT THIS FILE\n" +
        "\n" +
        "-XX:+UseG1GC\n" +
        "-Djava.io.tmpdir=" + tmpDir.getAbsolutePath() + "\n" +
        "-XX:ErrorFile=" + Paths.get("path_to_logs/es_hs_err_pid%p.log").toAbsolutePath() + "\n" +
        "-Xlog:disable\n" +
        "-Des.networkaddress.cache.ttl=60\n" +
        "-Des.networkaddress.cache.negative.ttl=10\n" +
        "-XX:+AlwaysPreTouch\n" +
        "-Xss1m\n" +
        "-Djava.awt.headless=true\n" +
        "-Dfile.encoding=UTF-8\n" +
        "-Djna.nosys=true\n" +
        "-Djna.tmpdir=" + tmpDir.getAbsolutePath() + "\n" +
        "-XX:-OmitStackTraceInFastThrow\n" +
        "-Dio.netty.noUnsafe=true\n" +
        "-Dio.netty.noKeySetOptimization=true\n" +
        "-Dio.netty.recycler.maxCapacityPerThread=0\n" +
        "-Dio.netty.allocator.numDirectArenas=0\n" +
        "-Dlog4j.shutdownHookEnabled=false\n" +
        "-Dlog4j2.disable.jmx=true\n" +
        "-Dlog4j2.formatMsgNoLookups=true\n" +
        "-Djava.locale.providers=COMPAT\n" +
        "-Des.enforce.bootstrap.checks=true\n" +
        "-foo\n" +
        "-bar");

  }

  @Test
  public void writeToJvmOptionFile_throws_ISE_in_case_of_IOException() throws IOException {
    File notAFile = temporaryFolder.newFolder();
    EsJvmOptions underTest = new EsJvmOptions(new Props(properties), temporaryFolder.newFolder());

    assertThatThrownBy(() -> underTest.writeToJvmOptionFile(notAFile))
      .isInstanceOf(IllegalStateException.class)
      .hasMessage("Cannot write Elasticsearch jvm options file")
      .hasRootCauseInstanceOf(IOException.class);
  }
}