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

View File

@@ -236,7 +236,7 @@ describe 'java', :type => :class do
context 'select jdk for OpenBSD' do context 'select jdk for OpenBSD' do
let(:facts) { {:osfamily => 'OpenBSD', :architecture => 'x86_64'} } let(:facts) { {:osfamily => 'OpenBSD', :architecture => 'x86_64'} }
it { is_expected.to contain_package('java').with_name('jdk') } 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 end
context 'select jre for OpenBSD' do context 'select jre for OpenBSD' do

View File

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

View File

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