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
|
/*
* SonarQube
* Copyright (C) 2009-2018 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.sonarqube.tests.startup;
import com.sonar.orchestrator.Orchestrator;
import com.sonar.orchestrator.util.NetworkUtils;
import java.io.File;
import java.net.InetAddress;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.DisableOnDebug;
import org.junit.rules.TemporaryFolder;
import org.junit.rules.TestRule;
import org.junit.rules.Timeout;
import org.sonarqube.qa.util.LogsTailer;
import org.sonarqube.qa.util.Tester;
import org.sonarqube.ws.Users;
import org.sonarqube.ws.client.users.SearchRequest;
import static org.assertj.core.api.Assertions.assertThat;
import static util.ItUtils.newOrchestratorBuilder;
import static util.ItUtils.pluginArtifact;
public class StartupIndexationTest {
@Rule
public TemporaryFolder temp = new TemporaryFolder();
@Rule
public TestRule safeguard = new DisableOnDebug(Timeout.seconds(600));
@Test
public void elasticsearch_error_at_startup_must_shutdown_node() throws Exception {
try (SonarQube sonarQube = new SonarQube();
LogsTailer.Watch failedInitialization = sonarQube.logsTailer.watch("Background initialization failed. Stopping SonarQube");
LogsTailer.Watch stopWatcher = sonarQube.logsTailer.watch("SonarQube is stopped")) {
sonarQube.lockAllElasticsearchWrites();
sonarQube.resume();
stopWatcher.waitForLog(10, TimeUnit.SECONDS);
assertThat(stopWatcher.getLog()).isPresent();
assertThat(failedInitialization.getLog()).isPresent();
}
// Restarting is recreating the indexes
try (SonarQube sonarQube = new SonarQube();
LogsTailer.Watch sonarQubeIsUpWatcher = sonarQube.logsTailer.watch("SonarQube is up")) {
sonarQube.resume();
sonarQubeIsUpWatcher.waitForLog(20, TimeUnit.SECONDS);
SearchRequest searchRequest = new SearchRequest().setQ("admin");
Users.SearchWsResponse searchWsResponse = sonarQube.tester.wsClient().users().search(searchRequest);
assertThat(searchWsResponse.getUsersCount()).isEqualTo(1);
assertThat(searchWsResponse.getUsers(0).getName()).isEqualTo("Administrator");
}
}
private class SonarQube implements AutoCloseable {
private final Orchestrator orchestrator;
private final Tester tester;
private final File pauseFile;
private final LogsTailer logsTailer;
private final int esHttpPort = NetworkUtils.getNextAvailablePort(InetAddress.getLoopbackAddress());
SonarQube() throws Exception {
pauseFile = temp.newFile();
FileUtils.touch(pauseFile);
orchestrator = newOrchestratorBuilder()
.setServerProperty("sonar.web.pause.path", pauseFile.getAbsolutePath())
.addPlugin(pluginArtifact("wait-at-platform-level4-plugin"))
.setStartupLogWatcher(l -> l.contains("PlatformLevel4 initialization phase is paused"))
.setServerProperty("sonar.search.httpPort", "" + esHttpPort)
.build();
tester = new Tester(orchestrator);
orchestrator.start();
tester.before();
logsTailer = LogsTailer.builder()
.addFile(orchestrator.getServer().getWebLogs())
.addFile(orchestrator.getServer().getCeLogs())
.addFile(orchestrator.getServer().getAppLogs())
.build();
}
LogsTailer logs() {
return logsTailer;
}
void resume() throws Exception {
FileUtils.forceDelete(pauseFile);
}
void lockElasticsearchWritesOn(String index) throws Exception {
tester.elasticsearch().lockWrites(index);
}
void lockAllElasticsearchWrites() throws Exception {
for (String index : Arrays.asList("metadatas", "components", "tests", "projectmeasures", "rules", "issues", "users", "views")) {
lockElasticsearchWritesOn(index);
}
}
@Override
public void close() {
if (tester != null) {
try {
tester.after();
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
if (orchestrator != null) {
orchestrator.stop();
}
if (logsTailer != null) {
logsTailer.close();
}
}
}
}
|