Skip to content

Commit dadc7a6

Browse files
committed
(MODULES-2637) Checks java actually installed
The other method for running which java doesn't work on OSX, as java is installed as an empty shim when not installed in the base OSX image: ``` vagrant:~ vagrant$ java No Java runtime present, requesting install. vagrant:~ vagrant$ /usr/libexec/java_home --failfast Unable to find any JVMs matching version "(null)". vagrant:~ vagrant$ ``` We can use the `/usr/libexec/java_home --failfast` command to check if java is actually present first. Originally-by: Peter Souter <[email protected]>
1 parent 3645364 commit dadc7a6

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

lib/facter/java_version.rb

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,23 @@
1212
# Notes:
1313
# None
1414
Facter.add(:java_version) do
15+
# the OS-specific overrides need to be able to return nil,
16+
# to indicate "no java available". Usually returning nil
17+
# would mean that facter falls back to a lower priority
18+
# resolution, which would then trigger MODULES-2637. To
19+
# avoid that, we confine the "default" here to not run
20+
# on those OS.
21+
# Additionally, facter versions prior to 2.0.1 only support
22+
# positive matches, so this needs to be done manually in setcode.
1523
setcode do
16-
if Facter::Util::Resolution.which('java')
17-
Facter::Util::Resolution.exec('java -Xmx8m -version 2>&1').lines.first.split(/"/)[1].strip
24+
unless [ 'openbsd', 'darwin' ].include? Facter.value(:operatingsystem).downcase
25+
if Facter::Util::Resolution.which('java')
26+
Facter::Util::Resolution.exec('java -Xmx8m -version 2>&1').lines.first.split(/"/)[1].strip
27+
end
1828
end
1929
end
2030
end
31+
2132
Facter.add(:java_version) do
2233
confine :operatingsystem => 'OpenBSD'
2334
has_weight 100
@@ -29,3 +40,13 @@
2940
end
3041
end
3142
end
43+
44+
Facter.add(:java_version) do
45+
confine :operatingsystem => 'Darwin'
46+
has_weight 100
47+
setcode do
48+
unless /Unable to find any JVMs matching version/ =~ Facter::Util::Resolution.exec('/usr/libexec/java_home --failfast 2>&1')
49+
Facter::Util::Resolution.exec('java -Xmx8m -version 2>&1').lines.first.split(/"/)[1].strip
50+
end
51+
end
52+
end

spec/unit/facter/java_version_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@
2323
expect(Facter.value(:java_version)).to eq("1.7.0_71")
2424
end
2525
end
26+
context 'on Darwin' do
27+
before do
28+
Facter.fact(:operatingsystem).stubs(:value).returns("Darwin")
29+
end
30+
let(:facts) { {:operatingsystem => 'Darwin'} }
31+
it do
32+
java_version_output = <<-EOS
33+
java version "1.7.0_71"
34+
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
35+
Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)
36+
EOS
37+
Facter::Util::Resolution.expects(:exec).with("/usr/libexec/java_home --failfast 2>&1").returns("/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home")
38+
Facter::Util::Resolution.expects(:exec).with("java -Xmx8m -version 2>&1").returns(java_version_output)
39+
Facter.value(:java_version).should == "1.7.0_71"
40+
end
41+
end
2642
context 'on other systems' do
2743
before do
2844
Facter.fact(:operatingsystem).stubs(:value).returns("MyOS")
@@ -51,6 +67,16 @@
5167
expect(Facter.value(:java_version)).to be_nil
5268
end
5369
end
70+
context 'on Darwin' do
71+
before do
72+
Facter.fact(:operatingsystem).stubs(:value).returns("Darwin")
73+
end
74+
let(:facts) { {:operatingsystem => 'Darwin'} }
75+
it do
76+
Facter::Util::Resolution.expects(:exec).at_least(1).with("/usr/libexec/java_home --failfast 2>&1").returns('Unable to find any JVMs matching version "(null)".')
77+
Facter.value(:java_version).should be_nil
78+
end
79+
end
5480
context 'on other systems' do
5581
before do
5682
Facter.fact(:operatingsystem).stubs(:value).returns("MyOS")

0 commit comments

Comments
 (0)