Skip to content

Commit 12e1bc4

Browse files
Refactor utility code in qa:os: tests (elastic#49945)
This refactor bridges some gaps between a long-running feature branch (elastic#49268) and the master branch. First of all, this PR gives our PackagingTestCase class some methods to start and stop Elasticsearch that will switch on packaging type and delegate to the appropriate utility class for deb/RPM packages, archive installations, and Docker. These methods should be very useful as we continue group tests by function rather than by package or platform type. Second, the password-protected keystore tests have a particular need to read the output of Elasticsearch startup commands. In order to make this easer to do, some commands now return Shell.Result objects so that tests can check over output to the shell. To that end, there's also an assertElasticsearchFailure method that will handle checking for startup failures for the various distribution types. There is an update to the Powershell startup script for archives that asynchronously redirects the output of the Powershell process to files that we can read for errors. Finally, we use the ES_STARTUP_SLEEP_TIME environment variable to make sure that our startup commands wait long enough before exiting for errors to make it to the standard output and error streams.
1 parent e520b85 commit 12e1bc4

File tree

7 files changed

+238
-148
lines changed

7 files changed

+238
-148
lines changed

qa/os/src/test/java/org/elasticsearch/packaging/test/ArchiveTests.java

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public void test50StartAndStop() throws Exception {
138138
rm(installation.config("elasticsearch.keystore"));
139139

140140
try {
141-
Archives.runElasticsearch(installation, sh);
141+
startElasticsearch();
142142
} catch (Exception e ){
143143
if (Files.exists(installation.home.resolve("elasticsearch.pid"))) {
144144
String pid = FileUtils.slurp(installation.home.resolve("elasticsearch.pid")).trim();
@@ -151,7 +151,7 @@ public void test50StartAndStop() throws Exception {
151151
assertTrue("gc logs exist", Files.exists(installation.logs.resolve("gc.log")));
152152
ServerUtils.runElasticsearchTests();
153153

154-
Archives.stopElasticsearch(installation);
154+
stopElasticsearch();
155155
}
156156

157157
public void test51JavaHomeOverride() throws Exception {
@@ -164,9 +164,9 @@ public void test51JavaHomeOverride() throws Exception {
164164
sh.getEnv().put("JAVA_HOME", systemJavaHome1);
165165
});
166166

167-
Archives.runElasticsearch(installation, sh);
167+
startElasticsearch();
168168
ServerUtils.runElasticsearchTests();
169-
Archives.stopElasticsearch(installation);
169+
stopElasticsearch();
170170

171171
String systemJavaHome1 = sh.getEnv().get("JAVA_HOME");
172172
assertThat(FileUtils.slurpAllLogs(installation.logs, "elasticsearch.log", "*.log.gz"),
@@ -188,9 +188,9 @@ public void test52BundledJdkRemoved() throws Exception {
188188
sh.getEnv().put("JAVA_HOME", systemJavaHome1);
189189
});
190190

191-
Archives.runElasticsearch(installation, sh);
191+
startElasticsearch();
192192
ServerUtils.runElasticsearchTests();
193-
Archives.stopElasticsearch(installation);
193+
stopElasticsearch();
194194

195195
String systemJavaHome1 = sh.getEnv().get("JAVA_HOME");
196196
assertThat(FileUtils.slurpAllLogs(installation.logs, "elasticsearch.log", "*.log.gz"),
@@ -211,9 +211,9 @@ public void test53JavaHomeWithSpecialCharacters() throws Exception {
211211
sh.getEnv().put("JAVA_HOME", "C:\\Program Files (x86)\\java");
212212

213213
//verify ES can start, stop and run plugin list
214-
Archives.runElasticsearch(installation, sh);
214+
startElasticsearch();
215215

216-
Archives.stopElasticsearch(installation);
216+
stopElasticsearch();
217217

218218
String pluginListCommand = installation.bin + "/elasticsearch-plugin list";
219219
Result result = sh.run(pluginListCommand);
@@ -237,9 +237,9 @@ public void test53JavaHomeWithSpecialCharacters() throws Exception {
237237
sh.getEnv().put("JAVA_HOME", testJavaHome);
238238

239239
//verify ES can start, stop and run plugin list
240-
Archives.runElasticsearch(installation, sh);
240+
startElasticsearch();
241241

242-
Archives.stopElasticsearch(installation);
242+
stopElasticsearch();
243243

244244
String pluginListCommand = installation.bin + "/elasticsearch-plugin list";
245245
Result result = sh.run(pluginListCommand);
@@ -284,13 +284,12 @@ public void test70CustomPathConfAndJvmOptions() throws Exception {
284284
"-Dlog4j2.disable.jmx=true\n";
285285
append(tempConf.resolve("jvm.options"), jvmOptions);
286286

287-
final Shell sh = newShell();
288287
sh.chown(tempConf);
289288

290289
sh.getEnv().put("ES_PATH_CONF", tempConf.toString());
291290
sh.getEnv().put("ES_JAVA_OPTS", "-XX:-UseCompressedOops");
292291

293-
Archives.runElasticsearch(installation, sh);
292+
startElasticsearch();
294293

295294
final String nodesResponse = makeRequest(Request.Get("http://localhost:9200/_nodes"));
296295
assertThat(nodesResponse, containsString("\"heap_init_in_bytes\":536870912"));
@@ -318,17 +317,16 @@ public void test80RelativePathConf() throws Exception {
318317

319318
append(tempConf.resolve("elasticsearch.yml"), "node.name: relative");
320319

321-
final Shell sh = newShell();
322320
sh.chown(temp);
323321

324322
sh.setWorkingDirectory(temp);
325323
sh.getEnv().put("ES_PATH_CONF", "config");
326-
Archives.runElasticsearch(installation, sh);
324+
startElasticsearch();
327325

328326
final String nodesResponse = makeRequest(Request.Get("http://localhost:9200/_nodes"));
329327
assertThat(nodesResponse, containsString("\"name\":\"relative\""));
330328

331-
Archives.stopElasticsearch(installation);
329+
stopElasticsearch();
332330

333331
} finally {
334332
rm(tempConf);
@@ -393,7 +391,7 @@ public void test93ElasticsearchNodeCustomDataPathAndNotEsHomeWorkDir() throws Ex
393391

394392
sh.setWorkingDirectory(getTempDir());
395393

396-
Archives.runElasticsearch(installation, sh);
394+
startElasticsearch();
397395
Archives.stopElasticsearch(installation);
398396

399397
Result result = sh.run("echo y | " + installation.executables().elasticsearchNode + " unsafe-bootstrap");

qa/os/src/test/java/org/elasticsearch/packaging/test/PackageTests.java

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@
5050
import static org.elasticsearch.packaging.util.Packages.installPackage;
5151
import static org.elasticsearch.packaging.util.Packages.remove;
5252
import static org.elasticsearch.packaging.util.Packages.restartElasticsearch;
53-
import static org.elasticsearch.packaging.util.Packages.startElasticsearch;
54-
import static org.elasticsearch.packaging.util.Packages.startElasticsearchIgnoringFailure;
55-
import static org.elasticsearch.packaging.util.Packages.stopElasticsearch;
5653
import static org.elasticsearch.packaging.util.Packages.verifyPackageInstallation;
5754
import static org.elasticsearch.packaging.util.Platforms.getOsRelease;
5855
import static org.elasticsearch.packaging.util.Platforms.isSystemd;
@@ -101,9 +98,9 @@ private void assertRunsWithJavaHome() throws Exception {
10198
try {
10299
Files.write(installation.envFile, ("JAVA_HOME=" + systemJavaHome + "\n").getBytes(StandardCharsets.UTF_8),
103100
StandardOpenOption.APPEND);
104-
startElasticsearch(sh, installation);
101+
startElasticsearch();
105102
runElasticsearchTests();
106-
stopElasticsearch(sh);
103+
stopElasticsearch();
107104
} finally {
108105
Files.write(installation.envFile, originalEnvFile);
109106
}
@@ -129,9 +126,9 @@ public void test33RunsIfJavaNotOnPath() throws Exception {
129126
}
130127

131128
try {
132-
startElasticsearch(sh, installation);
129+
startElasticsearch();
133130
runElasticsearchTests();
134-
stopElasticsearch(sh);
131+
stopElasticsearch();
135132
} finally {
136133
if (Files.exists(Paths.get(backupPath))) {
137134
sh.run("sudo mv " + backupPath + " /usr/bin/java");
@@ -153,7 +150,7 @@ public void test42BundledJdkRemoved() throws Exception {
153150

154151
public void test40StartServer() throws Exception {
155152
String start = sh.runIgnoreExitCode("date ").stdout.trim();
156-
startElasticsearch(sh, installation);
153+
startElasticsearch();
157154

158155
String journalEntries = sh.runIgnoreExitCode("journalctl _SYSTEMD_UNIT=elasticsearch.service " +
159156
"--since \"" + start + "\" --output cat | wc -l").stdout.trim();
@@ -218,7 +215,7 @@ public void test50Remove() throws Exception {
218215
}
219216

220217
public void test60Reinstall() throws Exception {
221-
installation = installPackage(distribution());
218+
install();
222219
assertInstalled(distribution());
223220
verifyPackageInstallation(installation, distribution(), sh);
224221

@@ -228,13 +225,13 @@ public void test60Reinstall() throws Exception {
228225

229226
public void test70RestartServer() throws Exception {
230227
try {
231-
installation = installPackage(distribution());
228+
install();
232229
assertInstalled(distribution());
233230

234-
startElasticsearch(sh, installation);
231+
startElasticsearch();
235232
restartElasticsearch(sh, installation);
236233
runElasticsearchTests();
237-
stopElasticsearch(sh);
234+
stopElasticsearch();
238235
} finally {
239236
cleanup();
240237
}
@@ -243,22 +240,22 @@ public void test70RestartServer() throws Exception {
243240

244241
public void test72TestRuntimeDirectory() throws Exception {
245242
try {
246-
installation = installPackage(distribution());
243+
install();
247244
FileUtils.rm(installation.pidDir);
248-
startElasticsearch(sh, installation);
245+
startElasticsearch();
249246
assertPathsExist(installation.pidDir);
250-
stopElasticsearch(sh);
247+
stopElasticsearch();
251248
} finally {
252249
cleanup();
253250
}
254251
}
255252

256253
public void test73gcLogsExist() throws Exception {
257-
installation = installPackage(distribution());
258-
startElasticsearch(sh, installation);
254+
install();
255+
startElasticsearch();
259256
// it can be gc.log or gc.log.0.current
260257
assertThat(installation.logs, fileWithGlobExist("gc.log*"));
261-
stopElasticsearch(sh);
258+
stopElasticsearch();
262259
}
263260

264261
// TEST CASES FOR SYSTEMD ONLY
@@ -277,26 +274,26 @@ public void test80DeletePID_DIRandRestart() throws Exception {
277274

278275
sh.run("systemd-tmpfiles --create");
279276

280-
startElasticsearch(sh, installation);
277+
startElasticsearch();
281278

282279
final Path pidFile = installation.pidDir.resolve("elasticsearch.pid");
283280

284281
assertTrue(Files.exists(pidFile));
285282

286-
stopElasticsearch(sh);
283+
stopElasticsearch();
287284
}
288285

289286
public void test81CustomPathConfAndJvmOptions() throws Exception {
290287
withCustomConfig(tempConf -> {
291288
append(installation.envFile, "ES_JAVA_OPTS=-XX:-UseCompressedOops");
292289

293-
startElasticsearch(sh, installation);
290+
startElasticsearch();
294291

295292
final String nodesResponse = makeRequest(Request.Get("http://localhost:9200/_nodes"));
296293
assertThat(nodesResponse, containsString("\"heap_init_in_bytes\":536870912"));
297294
assertThat(nodesResponse, containsString("\"using_compressed_ordinary_object_pointers\":\"false\""));
298295

299-
stopElasticsearch(sh);
296+
stopElasticsearch();
300297
});
301298
}
302299

@@ -306,7 +303,7 @@ public void test82SystemdMask() throws Exception {
306303

307304
sh.run("systemctl mask systemd-sysctl.service");
308305

309-
installation = installPackage(distribution());
306+
install();
310307

311308
sh.run("systemctl unmask systemd-sysctl.service");
312309
} finally {
@@ -318,9 +315,9 @@ public void test83serviceFileSetsLimits() throws Exception {
318315
// Limits are changed on systemd platforms only
319316
assumeTrue(isSystemd());
320317

321-
installation = installPackage(distribution());
318+
install();
322319

323-
startElasticsearch(sh, installation);
320+
startElasticsearch();
324321

325322
final Path pidFile = installation.pidDir.resolve("elasticsearch.pid");
326323
assertTrue(Files.exists(pidFile));
@@ -337,7 +334,7 @@ public void test83serviceFileSetsLimits() throws Exception {
337334
String maxAddressSpace = sh.run("cat /proc/%s/limits | grep \"Max address space\" | awk '{ print $4 }'", pid).stdout.trim();
338335
assertThat(maxAddressSpace, equalTo("unlimited"));
339336

340-
stopElasticsearch(sh);
337+
stopElasticsearch();
341338
}
342339

343340
public void test90DoNotCloseStderrWhenQuiet() throws Exception {
@@ -347,7 +344,7 @@ public void test90DoNotCloseStderrWhenQuiet() throws Exception {
347344

348345
// Make sure we don't pick up the journal entries for previous ES instances.
349346
clearJournal(sh);
350-
startElasticsearchIgnoringFailure(sh);
347+
runElasticsearchStartCommand();
351348

352349
final Result logs = sh.run("journalctl -u elasticsearch.service");
353350

@@ -365,7 +362,7 @@ private void withCustomConfig(CustomConfigConsumer pathConsumer) throws Exceptio
365362

366363
assertPathsExist(installation.envFile);
367364

368-
stopElasticsearch(sh);
365+
stopElasticsearch();
369366

370367
// The custom config directory is not under /tmp or /var/tmp because
371368
// systemd's private temp directory functionally means different

0 commit comments

Comments
 (0)