20
20
package org .elasticsearch .packaging .test ;
21
21
22
22
import com .carrotsearch .randomizedtesting .annotations .TestCaseOrdering ;
23
+ import org .apache .http .client .fluent .Request ;
24
+ import org .elasticsearch .packaging .util .FileUtils ;
23
25
import org .elasticsearch .packaging .util .Shell ;
24
26
import org .elasticsearch .packaging .util .Shell .Result ;
27
+ import org .hamcrest .CoreMatchers ;
25
28
import org .junit .Before ;
26
29
27
30
import java .io .IOException ;
28
31
import java .nio .charset .StandardCharsets ;
29
32
import java .nio .file .Files ;
30
33
import java .nio .file .Path ;
34
+ import java .nio .file .Paths ;
31
35
import java .nio .file .StandardOpenOption ;
32
36
import java .util .regex .Matcher ;
33
37
import java .util .regex .Pattern ;
34
38
39
+ import static org .elasticsearch .packaging .util .FileUtils .append ;
35
40
import static org .elasticsearch .packaging .util .FileUtils .assertPathsDontExist ;
41
+ import static org .elasticsearch .packaging .util .FileUtils .assertPathsExist ;
42
+ import static org .elasticsearch .packaging .util .FileUtils .cp ;
43
+ import static org .elasticsearch .packaging .util .FileUtils .fileWithGlobExist ;
44
+ import static org .elasticsearch .packaging .util .FileUtils .mkdir ;
36
45
import static org .elasticsearch .packaging .util .FileUtils .mv ;
46
+ import static org .elasticsearch .packaging .util .FileUtils .rm ;
47
+ import static org .elasticsearch .packaging .util .FileUtils .slurp ;
37
48
import static org .elasticsearch .packaging .util .Packages .SYSTEMD_SERVICE ;
38
49
import static org .elasticsearch .packaging .util .Packages .assertInstalled ;
39
50
import static org .elasticsearch .packaging .util .Packages .assertRemoved ;
40
51
import static org .elasticsearch .packaging .util .Packages .install ;
41
52
import static org .elasticsearch .packaging .util .Packages .remove ;
53
+ import static org .elasticsearch .packaging .util .Packages .restartElasticsearch ;
42
54
import static org .elasticsearch .packaging .util .Packages .startElasticsearch ;
43
55
import static org .elasticsearch .packaging .util .Packages .stopElasticsearch ;
44
56
import static org .elasticsearch .packaging .util .Packages .verifyPackageInstallation ;
45
57
import static org .elasticsearch .packaging .util .Platforms .getOsRelease ;
46
58
import static org .elasticsearch .packaging .util .Platforms .isSystemd ;
59
+ import static org .elasticsearch .packaging .util .ServerUtils .makeRequest ;
47
60
import static org .elasticsearch .packaging .util .ServerUtils .runElasticsearchTests ;
61
+ import static org .hamcrest .CoreMatchers .equalTo ;
48
62
import static org .hamcrest .CoreMatchers .not ;
49
63
import static org .hamcrest .CoreMatchers .notNullValue ;
50
64
import static org .hamcrest .Matchers .containsString ;
55
69
56
70
@ TestCaseOrdering (TestCaseOrdering .AlphabeticOrder .class )
57
71
public abstract class PackageTestCase extends PackagingTestCase {
72
+ private Shell sh ;
58
73
59
74
@ Before
60
75
public void onlyCompatibleDistributions () {
61
76
assumeTrue ("only compatible distributions" , distribution ().packaging .compatible );
77
+ sh = newShell ();
62
78
}
63
79
64
80
public void test10InstallPackage () throws IOException {
65
81
assertRemoved (distribution ());
66
82
installation = install (distribution ());
67
83
assertInstalled (distribution ());
68
- verifyPackageInstallation (installation , distribution (), newShell () );
84
+ verifyPackageInstallation (installation , distribution (), sh );
69
85
}
70
86
71
87
public void test20PluginsCommandWhenNoPlugins () {
72
88
assumeThat (installation , is (notNullValue ()));
73
89
74
- assertThat (newShell () .run (installation .bin ("elasticsearch-plugin" ) + " list" ).stdout , isEmptyString ());
90
+ assertThat (sh .run (installation .bin ("elasticsearch-plugin" ) + " list" ).stdout , isEmptyString ());
75
91
}
76
92
77
- public void test30InstallDoesNotStartServer () {
93
+ public void test30DaemonIsNotEnabledOnRestart () {
94
+ if (isSystemd ()) {
95
+ sh .run ("systemctl daemon-reload" );
96
+ String isEnabledOutput = sh .runIgnoreExitCode ("systemctl is-enabled elasticsearch.service" ).stdout .trim ();
97
+ assertThat (isEnabledOutput , equalTo ("disabled" ));
98
+ }
99
+ }
100
+
101
+ public void test31InstallDoesNotStartServer () {
78
102
assumeThat (installation , is (notNullValue ()));
79
103
80
- assertThat (newShell () .run ("ps aux" ).stdout , not (containsString ("org.elasticsearch.bootstrap.Elasticsearch" )));
104
+ assertThat (sh .run ("ps aux" ).stdout , not (containsString ("org.elasticsearch.bootstrap.Elasticsearch" )));
81
105
}
82
106
83
107
public void assertRunsWithJavaHome () throws IOException {
84
- Shell sh = newShell ();
85
-
86
108
String systemJavaHome = sh .run ("echo $SYSTEM_JAVA_HOME" ).stdout .trim ();
87
109
byte [] originalEnvFile = Files .readAllBytes (installation .envFile );
88
110
try {
89
111
Files .write (installation .envFile , ("JAVA_HOME=" + systemJavaHome + "\n " ).getBytes (StandardCharsets .UTF_8 ),
90
112
StandardOpenOption .APPEND );
91
- startElasticsearch ();
113
+ startElasticsearch (sh );
92
114
runElasticsearchTests ();
93
- stopElasticsearch ();
115
+ stopElasticsearch (sh );
94
116
} finally {
95
117
Files .write (installation .envFile , originalEnvFile );
96
118
}
@@ -99,7 +121,7 @@ public void assertRunsWithJavaHome() throws IOException {
99
121
assertThat (new String (Files .readAllBytes (log ), StandardCharsets .UTF_8 ), containsString (systemJavaHome ));
100
122
}
101
123
102
- public void test31JavaHomeOverride () throws IOException {
124
+ public void test32JavaHomeOverride () throws IOException {
103
125
assumeThat (installation , is (notNullValue ()));
104
126
// we always run with java home when no bundled jdk is included, so this test would be repetitive
105
127
assumeThat (distribution ().hasJdk , is (true ));
@@ -121,11 +143,20 @@ public void test42BundledJdkRemoved() throws IOException {
121
143
}
122
144
123
145
public void test40StartServer () throws IOException {
146
+ String start = sh .runIgnoreExitCode ("date " ).stdout .trim ();
124
147
assumeThat (installation , is (notNullValue ()));
125
148
126
- startElasticsearch ();
149
+ startElasticsearch (sh );
150
+
151
+ String journalEntries = sh .runIgnoreExitCode ("journalctl _SYSTEMD_UNIT=elasticsearch.service " +
152
+ "--since \" " + start + "\" --output cat | wc -l" ).stdout .trim ();
153
+ assertThat (journalEntries , equalTo ("0" ));
154
+
155
+ assertPathsExist (installation .pidDir .resolve ("elasticsearch.pid" ));
156
+ assertPathsExist (installation .logs .resolve ("elasticsearch_server.json" ));
157
+
127
158
runElasticsearchTests ();
128
- verifyPackageInstallation (installation , distribution (), newShell () ); // check startup script didn't change permissions
159
+ verifyPackageInstallation (installation , distribution (), sh ); // check startup script didn't change permissions
129
160
}
130
161
131
162
public void test50Remove () {
@@ -134,7 +165,6 @@ public void test50Remove() {
134
165
remove (distribution ());
135
166
136
167
// removing must stop the service
137
- final Shell sh = newShell ();
138
168
assertThat (sh .run ("ps aux" ).stdout , not (containsString ("org.elasticsearch.bootstrap.Elasticsearch" )));
139
169
140
170
if (isSystemd ()) {
@@ -184,9 +214,160 @@ public void test60Reinstall() throws IOException {
184
214
185
215
installation = install (distribution ());
186
216
assertInstalled (distribution ());
187
- verifyPackageInstallation (installation , distribution (), newShell () );
217
+ verifyPackageInstallation (installation , distribution (), sh );
188
218
189
219
remove (distribution ());
190
220
assertRemoved (distribution ());
191
221
}
222
+
223
+ public void test70RestartServer () throws IOException {
224
+ try {
225
+ installation = install (distribution ());
226
+ assertInstalled (distribution ());
227
+
228
+ startElasticsearch (sh );
229
+ restartElasticsearch (sh );
230
+ runElasticsearchTests ();
231
+ stopElasticsearch (sh );
232
+ } finally {
233
+ cleanup ();
234
+ }
235
+ }
236
+
237
+
238
+ public void test72TestRuntimeDirectory () throws IOException {
239
+ try {
240
+ installation = install (distribution ());
241
+ FileUtils .rm (installation .pidDir );
242
+ startElasticsearch (sh );
243
+ assertPathsExist (installation .pidDir );
244
+ stopElasticsearch (sh );
245
+ } finally {
246
+ cleanup ();
247
+ }
248
+ }
249
+
250
+ public void test73gcLogsExist () throws IOException {
251
+ installation = install (distribution ());
252
+ startElasticsearch (sh );
253
+ // it can be gc.log or gc.log.0.current
254
+ assertThat (installation .logs , fileWithGlobExist ("gc.log*" ));
255
+ stopElasticsearch (sh );
256
+ }
257
+
258
+ // TEST CASES FOR SYSTEMD ONLY
259
+
260
+
261
+ /**
262
+ * # Simulates the behavior of a system restart:
263
+ * # the PID directory is deleted by the operating system
264
+ * # but it should not block ES from starting
265
+ * # see https://github.com/elastic/elasticsearch/issues/11594
266
+ */
267
+ public void test80DeletePID_DIRandRestart () throws IOException {
268
+ assumeTrue (isSystemd ());
269
+
270
+ rm (installation .pidDir );
271
+
272
+ sh .run ("systemd-tmpfiles --create" );
273
+
274
+ startElasticsearch (sh );
275
+
276
+ final Path pidFile = installation .pidDir .resolve ("elasticsearch.pid" );
277
+
278
+ assertTrue (Files .exists (pidFile ));
279
+
280
+ stopElasticsearch (sh );
281
+ }
282
+
283
+ public void test81CustomPathConfAndJvmOptions () throws IOException {
284
+ assumeTrue (isSystemd ());
285
+
286
+ assumeThat (installation , is (notNullValue ()));
287
+ assertPathsExist (installation .envFile );
288
+
289
+ stopElasticsearch (sh );
290
+
291
+ // The custom config directory is not under /tmp or /var/tmp because
292
+ // systemd's private temp directory functionally means different
293
+ // processes can have different views of what's in these directories
294
+ String temp = sh .runIgnoreExitCode ("mktemp -p /etc -d" ).stdout .trim ();
295
+ final Path tempConf = Paths .get (temp );
296
+
297
+ try {
298
+ mkdir (tempConf );
299
+ cp (installation .config ("elasticsearch.yml" ), tempConf .resolve ("elasticsearch.yml" ));
300
+ cp (installation .config ("log4j2.properties" ), tempConf .resolve ("log4j2.properties" ));
301
+
302
+ // we have to disable Log4j from using JMX lest it will hit a security
303
+ // manager exception before we have configured logging; this will fail
304
+ // startup since we detect usages of logging before it is configured
305
+ final String jvmOptions =
306
+ "-Xms512m\n " +
307
+ "-Xmx512m\n " +
308
+ "-Dlog4j2.disable.jmx=true\n " ;
309
+ append (tempConf .resolve ("jvm.options" ), jvmOptions );
310
+
311
+ sh .runIgnoreExitCode ("chown -R elasticsearch:elasticsearch " + tempConf );
312
+
313
+ final Shell serverShell = newShell ();
314
+ cp (installation .envFile , tempConf .resolve ("elasticsearch.bk" ));//backup
315
+ append (installation .envFile , "ES_PATH_CONF=" + tempConf + "\n " );
316
+ append (installation .envFile , "ES_JAVA_OPTS=-XX:-UseCompressedOops" );
317
+
318
+ startElasticsearch (serverShell );
319
+
320
+ final String nodesResponse = makeRequest (Request .Get ("http://localhost:9200/_nodes" ));
321
+ assertThat (nodesResponse , CoreMatchers .containsString ("\" heap_init_in_bytes\" :536870912" ));
322
+ assertThat (nodesResponse , CoreMatchers .containsString ("\" using_compressed_ordinary_object_pointers\" :\" false\" " ));
323
+
324
+ stopElasticsearch (serverShell );
325
+
326
+ } finally {
327
+ rm (installation .envFile );
328
+ cp (tempConf .resolve ("elasticsearch.bk" ), installation .envFile );
329
+ rm (tempConf );
330
+ cleanup ();
331
+ }
332
+ }
333
+
334
+ public void test82SystemdMask () throws IOException {
335
+ try {
336
+ assumeTrue (isSystemd ());
337
+
338
+ sh .run ("systemctl mask systemd-sysctl.service" );
339
+
340
+ installation = install (distribution ());
341
+
342
+ sh .run ("systemctl unmask systemd-sysctl.service" );
343
+ } finally {
344
+ cleanup ();
345
+ }
346
+ }
347
+
348
+ public void test83serviceFileSetsLimits () throws IOException {
349
+ // Limits are changed on systemd platforms only
350
+ assumeTrue (isSystemd ());
351
+
352
+ installation = install (distribution ());
353
+
354
+ startElasticsearch (sh );
355
+
356
+ final Path pidFile = installation .pidDir .resolve ("elasticsearch.pid" );
357
+ assertTrue (Files .exists (pidFile ));
358
+ String pid = slurp (pidFile ).trim ();
359
+ String maxFileSize = sh .run ("cat /proc/%s/limits | grep \" Max file size\" | awk '{ print $4 }'" , pid ).stdout .trim ();
360
+ assertThat (maxFileSize , equalTo ("unlimited" ));
361
+
362
+ String maxProcesses = sh .run ("cat /proc/%s/limits | grep \" Max processes\" | awk '{ print $3 }'" , pid ).stdout .trim ();
363
+ assertThat (maxProcesses , equalTo ("4096" ));
364
+
365
+ String maxOpenFiles = sh .run ("cat /proc/%s/limits | grep \" Max open files\" | awk '{ print $4 }'" , pid ).stdout .trim ();
366
+ assertThat (maxOpenFiles , equalTo ("65535" ));
367
+
368
+ String maxAddressSpace = sh .run ("cat /proc/%s/limits | grep \" Max address space\" | awk '{ print $4 }'" , pid ).stdout .trim ();
369
+ assertThat (maxAddressSpace , equalTo ("unlimited" ));
370
+
371
+ stopElasticsearch (sh );
372
+ }
192
373
}
0 commit comments