Skip to content

Commit 37cbe8c

Browse files
committed
Add package pre-install check for java binary (#31343)
The package installation relies on java being in the path. If java is not in the path, the tests fail at post-install time. This commit adds a pre-install check to validate that java exists, and if it fails, the package is never installed, and thus keeps a system clean, rather than aborting at post-install and leaving behind a mess. Closes #29665
1 parent a605726 commit 37cbe8c

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

distribution/packages/src/common/scripts/preinst

+12
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@
99
# $1=1 : indicates an new install
1010
# $1=2 : indicates an upgrade
1111

12+
# Check for these at preinst time due to failures in postinst if they do not exist
13+
if [ -x "$JAVA_HOME/bin/java" ]; then
14+
JAVA="$JAVA_HOME/bin/java"
15+
else
16+
JAVA=`which java`
17+
fi
18+
19+
if [ -z "$JAVA" ]; then
20+
echo "could not find java; set JAVA_HOME or ensure java is in PATH"
21+
exit 1
22+
fi
23+
1224
case "$1" in
1325

1426
# Debian ####################################################

qa/vagrant/src/test/resources/packaging/tests/30_deb_package.bats

+8
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ setup() {
7272
[ "$status" -eq 1 ]
7373
}
7474

75+
@test "[DEB] temporarily remove java and ensure the install fails" {
76+
move_java
77+
run dpkg -i elasticsearch-oss-$(cat version).deb
78+
output=$status
79+
unmove_java
80+
[ "$output" -eq 1 ]
81+
}
82+
7583
@test "[DEB] install package" {
7684
dpkg -i elasticsearch-oss-$(cat version).deb
7785
}

qa/vagrant/src/test/resources/packaging/tests/40_rpm_package.bats

+8
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ setup() {
7171
[ "$status" -eq 1 ]
7272
}
7373

74+
@test "[RPM] temporarily remove java and ensure the install fails" {
75+
move_java
76+
run rpm -i elasticsearch-oss-$(cat version).rpm
77+
output=$status
78+
unmove_java
79+
[ "$output" -eq 1 ]
80+
}
81+
7482
@test "[RPM] install package" {
7583
rpm -i elasticsearch-oss-$(cat version).rpm
7684
}

qa/vagrant/src/test/resources/packaging/utils/utils.bash

+19-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,11 @@ if [ ! -x "`which unzip 2>/dev/null`" ]; then
6868
fi
6969

7070
if [ ! -x "`which java 2>/dev/null`" ]; then
71-
echo "'java' command is mandatory to run the tests"
72-
exit 1
71+
# there are some tests that move java temporarily
72+
if [ ! -x "`command -v java.bak 2>/dev/null`" ]; then
73+
echo "'java' command is mandatory to run the tests"
74+
exit 1
75+
fi
7376
fi
7477

7578
# Returns 0 if the 'dpkg' command is available
@@ -525,3 +528,17 @@ file_privileges_for_user_from_umask() {
525528

526529
echo $((0777 & ~$(sudo -E -u $user sh -c umask) & ~0111))
527530
}
531+
532+
# move java to simulate it not being in the path
533+
move_java() {
534+
which_java=`command -v java`
535+
assert_file_exist $which_java
536+
mv $which_java ${which_java}.bak
537+
}
538+
539+
# move java back to its original location
540+
unmove_java() {
541+
which_java=`command -v java.bak`
542+
assert_file_exist $which_java
543+
mv $which_java `dirname $which_java`/java
544+
}

0 commit comments

Comments
 (0)