Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions lib/facter/java_default_home.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,25 @@
# 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'
confine :kernel => [ 'Linux', 'OpenBSD' ]
setcode do
if Facter::Util::Resolution.which('readlink')
java_bin = Facter::Util::Resolution.exec('readlink -e /usr/bin/java').strip
if java_bin.empty?
nil
elsif java_bin =~ %r(/jre/)
java_default_home = File.dirname(File.dirname(File.dirname(java_bin)))
java_bin = Facter::Util::Resolution.which('java').to_s.strip
if java_bin.empty?
nil
else
java_path = File.realpath(java_bin)
if java_path =~ %r(/jre/)
java_default_home = File.dirname(File.dirname(File.dirname(java_path)))
else
java_default_home = File.dirname(File.dirname(java_bin))
java_default_home = File.dirname(File.dirname(java_path))
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/facter/java_libjvm_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# Notes:
# None
Facter.add(:java_libjvm_path) do
confine :kernel => "Linux"
confine :kernel => [ "Linux", "OpenBSD" ]
setcode do
java_default_home = Facter.value(:java_default_home)
java_libjvm_file = Dir.glob("#{java_default_home}/jre/lib/**/libjvm.so")
Expand Down
16 changes: 1 addition & 15 deletions lib/facter/java_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,7 @@
# Additionally, facter versions prior to 2.0.1 only support
# positive matches, so this needs to be done manually in setcode.
setcode do
unless [ 'openbsd', 'darwin' ].include? Facter.value(:operatingsystem).downcase
version = nil
if Facter::Util::Resolution.which('java')
Facter::Util::Resolution.exec('java -Xmx12m -version 2>&1').lines.each { |line| version = $~[1] if /^.+ version \"(.+)\"$/ =~ line }
end
version
end
end
end

Facter.add(:java_version) do
confine :operatingsystem => 'OpenBSD'
has_weight 100
setcode do
Facter::Util::Resolution.with_env("PATH" => '/usr/local/jdk-1.7.0/jre/bin:/usr/local/jre-1.7.0/bin') do
unless [ 'darwin' ].include? Facter.value(:operatingsystem).downcase
version = nil
if Facter::Util::Resolution.which('java')
Facter::Util::Resolution.exec('java -Xmx12m -version 2>&1').lines.each { |line| version = $~[1] if /^.+ version \"(.+)\"$/ =~ line }
Expand Down
2 changes: 1 addition & 1 deletion manifests/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
}
}
}
'OpenBSD', 'FreeBSD', 'Suse': {
'FreeBSD', 'Suse': {
if $java::use_java_home != undef {
file_line { 'java-home-environment':
path => '/etc/environment',
Expand Down
2 changes: 1 addition & 1 deletion spec/classes/java_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@
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
Expand Down
61 changes: 32 additions & 29 deletions spec/unit/facter/java_default_home_spec.rb
Original file line number Diff line number Diff line change
@@ -1,47 +1,50 @@
require "spec_helper"

def unlink_and_delete(filename)
if File.symlink?(filename)
File.unlink(filename)
end
if File.exist?(filename)
File.delete(filename)
end
end

describe Facter::Util::Fact do
before {
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
before(:each) {
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
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"
unlink_and_delete('./java_test')
File.symlink('/usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java', './java_test')
Facter::Util::Resolution.expects(:which).with("java").returns("./java_test")
File.expects(:realpath).with('./java_test').returns('/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'
unlink_and_delete('./java_test')
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
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"
unlink_and_delete('./java_test')
File.symlink('/usr/lib/jvm/oracle-java8-jre-amd64/bin/java', './java_test')
Facter::Util::Resolution.expects(:which).with("java").returns("./java_test")
File.expects(:realpath).with('./java_test').returns('/usr/lib/jvm/oracle-java8-jre-amd64/bin/java')
expect(Facter.value(:java_default_home)).to eql '/usr/lib/jvm/oracle-java8-jre-amd64'
unlink_and_delete('./java_test')
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
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
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/facter/java_version_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

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
Expand Down Expand Up @@ -61,7 +61,7 @@
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
Expand Down