24
24
import java .util .HashMap ;
25
25
import java .util .List ;
26
26
import java .util .Map ;
27
+ import java .util .Objects ;
27
28
import java .util .Set ;
28
29
import java .util .stream .Stream ;
29
30
30
31
import static java .nio .file .attribute .PosixFilePermissions .fromString ;
31
32
import static org .elasticsearch .packaging .util .FileMatcher .p644 ;
32
- import static org .elasticsearch .packaging .util .FileMatcher .p660 ;
33
33
import static org .elasticsearch .packaging .util .FileMatcher .p664 ;
34
34
import static org .elasticsearch .packaging .util .FileMatcher .p755 ;
35
35
import static org .elasticsearch .packaging .util .FileMatcher .p770 ;
@@ -84,6 +84,7 @@ public static void ensureImageIsLoaded(Distribution distribution) {
84
84
* successfully.
85
85
*
86
86
* @param distribution details about the docker image being tested
87
+ * @return an installation that models the running container
87
88
*/
88
89
public static Installation runContainer (Distribution distribution ) {
89
90
return runContainer (distribution , DockerRun .builder ());
@@ -95,6 +96,7 @@ public static Installation runContainer(Distribution distribution) {
95
96
*
96
97
* @param distribution details about the docker image being tested
97
98
* @param builder the command to run
99
+ * @return an installation that models the running container
98
100
*/
99
101
public static Installation runContainer (Distribution distribution , DockerRun builder ) {
100
102
executeDockerRun (distribution , builder );
@@ -261,13 +263,17 @@ protected String[] getScriptCommand(String script) {
261
263
262
264
/**
263
265
* Checks whether a path exists in the Docker container.
266
+ * @param path the path that ought to exist
267
+ * @return whether the path exists
264
268
*/
265
269
public static boolean existsInContainer (Path path ) {
266
270
return existsInContainer (path .toString ());
267
271
}
268
272
269
273
/**
270
274
* Checks whether a path exists in the Docker container.
275
+ * @param path the path that ought to exist
276
+ * @return whether the path exists
271
277
*/
272
278
public static boolean existsInContainer (String path ) {
273
279
logger .debug ("Checking whether file " + path + " exists in container" );
@@ -366,6 +372,8 @@ public static void chownWithPrivilegeEscalation(Path localPath, String ownership
366
372
367
373
/**
368
374
* Checks that the specified path's permissions and ownership match those specified.
375
+ * @param path the path to check
376
+ * @param expectedPermissions the unix permissions that the path ought to have
369
377
*/
370
378
public static void assertPermissionsAndOwnership (Path path , Set <PosixFilePermission > expectedPermissions ) {
371
379
logger .debug ("Checking permissions and ownership of [" + path + "]" );
@@ -387,6 +395,7 @@ public static void assertPermissionsAndOwnership(Path path, Set<PosixFilePermiss
387
395
388
396
/**
389
397
* Waits for up to 20 seconds for a path to exist in the container.
398
+ * @param path the path to await
390
399
*/
391
400
public static void waitForPathToExist (Path path ) throws InterruptedException {
392
401
int attempt = 0 ;
@@ -404,6 +413,8 @@ public static void waitForPathToExist(Path path) throws InterruptedException {
404
413
405
414
/**
406
415
* Perform a variety of checks on an installation. If the current distribution is not OSS, additional checks are carried out.
416
+ * @param installation the installation to verify
417
+ * @param distribution the distribution to verify
407
418
*/
408
419
public static void verifyContainerInstallation (Installation installation , Distribution distribution ) {
409
420
verifyOssInstallation (installation );
@@ -424,8 +435,6 @@ private static void verifyOssInstallation(Installation es) {
424
435
425
436
Stream .of (es .modules ).forEach (dir -> assertPermissionsAndOwnership (dir , p755 ));
426
437
427
- assertPermissionsAndOwnership (es .config ("elasticsearch.keystore" ), p660 );
428
-
429
438
Stream .of ("elasticsearch.yml" , "jvm.options" , "log4j2.properties" )
430
439
.forEach (configFile -> assertPermissionsAndOwnership (es .config (configFile ), p664 ));
431
440
@@ -508,14 +517,31 @@ public static String getContainerId() {
508
517
return containerId ;
509
518
}
510
519
520
+ /**
521
+ * Performs an HTTP GET to <code>http://localhost:9200/</code> with the supplied path.
522
+ * @param path the path to fetch, which must start with <code>/</code>
523
+ * @return the parsed response
524
+ */
511
525
public static JsonNode getJson (String path ) throws Exception {
512
- final String pluginsResponse = makeRequest (Request .Get ("http://localhost:9200/" + path ));
526
+ path = Objects .requireNonNull (path ).trim ();
527
+ if (path .isEmpty ()) {
528
+ throw new IllegalArgumentException ("path must be supplied" );
529
+ }
530
+ if (path .startsWith ("/" ) == false ) {
531
+ throw new IllegalArgumentException ("path must start with /" );
532
+ }
533
+ final String pluginsResponse = makeRequest (Request .Get ("http://localhost:9200" + path ));
513
534
514
535
ObjectMapper mapper = new ObjectMapper ();
515
536
516
537
return mapper .readTree (pluginsResponse );
517
538
}
518
539
540
+ /**
541
+ * Fetches all the labels for a Docker image
542
+ * @param distribution required to derive the image name
543
+ * @return a mapping from label name to value
544
+ */
519
545
public static Map <String , String > getImageLabels (Distribution distribution ) throws Exception {
520
546
// The format below extracts the .Config.Labels value, and prints it as json. Without the json
521
547
// modifier, a stringified Go map is printed instead, which isn't helpful.
0 commit comments