do not use unportable readlink utility to find the java_default_home,

use Ruby in a more portable fashion.

adapt tests as well
This commit is contained in:
Sebastian Reitenbach
2017-09-26 23:35:15 +02:00
committed by Will Meek
parent bdfc567c18
commit e50eb64614
4 changed files with 36 additions and 45 deletions

View File

@@ -3,27 +3,22 @@
# Purpose: get absolute path of java system home
#
# Resolution:
# Uses `readlink` to resolve the path of `/usr/bin/java` then returns subsubdir
# Find the real java binary, and return the subsubdir
#
# Caveats:
# Requires readlink
# java binary has to be found in $PATH
#
# Notes:
# None
Facter.add(:java_default_home) do
confine :kernel => [ 'Linux', 'OpenBSD' ]
setcode do
if Facter::Util::Resolution.which('readlink')
java_bin = Facter::Util::Resolution.exec('readlink -e /usr/bin/java').to_s.strip
if java_bin.empty?
java_bin = Facter::Util::Resolution.exec('readlink -e /usr/local/bin/java').to_s.strip
if java_bin.empty?
java_bin = Facter::Util::Resolution.which('java').to_s.strip
if java_bin.empty?
nil
end
end
end
else
# We might have found a symlink instead of the real binary
java_bin = File.realpath(java_bin)
if java_bin =~ %r(/jre/)
java_default_home = File.dirname(File.dirname(File.dirname(java_bin)))
else

View File

@@ -236,7 +236,7 @@ describe 'java', :type => :class do
context 'select jdk for OpenBSD' do
let(:facts) { {:osfamily => 'OpenBSD', :architecture => 'x86_64'} }
it { is_expected.to contain_package('java').with_name('jdk') }
it { is_expected.to contain_file_line('java-home-environment').with_line('JAVA_HOME=/usr/local/jdk/') }
it { is_expected.to_not contain_file_line('java-home-environment') }
end
context 'select jre for OpenBSD' do

View File

@@ -1,47 +1,43 @@
require "spec_helper"
describe Facter::Util::Fact do
before {
describe "java_default_home" do
before(:each) {
Facter.clear
Facter.fact(:kernel).stubs(:value).returns('Linux')
}
describe "java_default_home" do
context 'returns java home path when readlink present' do
context 'when java is in HOME/jre/bin/java' do
context 'returns java home path when java found in PATH' do
context "when java is in /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java" do
it do
java_path_output = <<-EOS
/usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java
EOS
Facter::Util::Resolution.expects(:which).with("readlink").returns(true)
Facter::Util::Resolution.expects(:exec).with("readlink -e /usr/bin/java").returns(java_path_output)
expect(Facter.value(:java_default_home)).to eql "/usr/lib/jvm/java-7-openjdk-amd64"
end
end
context 'when java is in HOME/bin/java' do
it do
java_path_output = <<-EOS
/usr/lib/jvm/oracle-java8-jre-amd64/bin/java
EOS
Facter::Util::Resolution.expects(:which).with("readlink").returns(true)
Facter::Util::Resolution.expects(:exec).with("readlink -e /usr/bin/java").returns(java_path_output)
expect(Facter.value(:java_default_home)).to eql "/usr/lib/jvm/oracle-java8-jre-amd64"
end
end
end
context 'returns nil when readlink is present but java is not' do
it do
java_path_output = ""
Facter::Util::Resolution.expects(:which).with("readlink").returns(true)
Facter::Util::Resolution.expects(:exec).with("readlink -e /usr/bin/java").returns(java_path_output)
expect(Facter.value(:java_default_home)).to be_nil
File.delete('./java') if File.exist?('./java')
File.symlink('/usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java', './java')
Facter::Util::Resolution.expects(:which).with("java").returns("./java")
expect(File.readlink('./java')).to eq('/usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java')
expect(Facter.value(:java_default_home)).to eql '/usr/lib/jvm/java-7-openjdk-amd64'
File.delete('./java') if File.exist?('./java')
end
end
context 'returns nil when readlink not present' do
context "when java is in /usr/lib/jvm/oracle-java8-jre-amd64/bin/java" do
it do
File.delete('./java') if File.exist?('./java')
File.symlink('/usr/lib/jvm/oracle-java8-jre-amd64/bin/java', './java')
Facter::Util::Resolution.expects(:which).with("java").returns("./java")
expect(File.readlink('./java')).to eq('/usr/lib/jvm/oracle-java8-jre-amd64/bin/java')
expect(Facter.value(:java_default_home)).to eql '/usr/lib/jvm/oracle-java8-jre-amd64'
File.delete('./java') if File.exist?('./java')
end
end
end
context 'returns nil when java not present' do
it do
Facter::Util::Resolution.stubs(:exec)
Facter::Util::Resolution.expects(:which).with("readlink").at_least(1).returns(false)
Facter::Util::Resolution.expects(:which).with("java").at_least(1).returns(false)
expect(Facter.value(:java_default_home)).to be_nil
end
end

View File

@@ -7,7 +7,7 @@ describe Facter::Util::Fact do
describe "java_version" do
context 'returns java version when java present' do
context 'on OpenBSD', :with_env => true do
context 'on OpenBSD' do
before do
Facter.fact(:operatingsystem).stubs(:value).returns("OpenBSD")
end
@@ -61,7 +61,7 @@ Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)
end
context 'returns nil when java not present' do
context 'on OpenBSD', :with_env => true do
context 'on OpenBSD' do
before do
Facter.fact(:operatingsystem).stubs(:value).returns("OpenBSD")
end