Skip to content
This repository was archived by the owner on Dec 2, 2020. It is now read-only.

Commit c147d1e

Browse files
committed
Don't blow up when Java 8 JRE is installed.
Previously (#54) I handled the case where Java 8 was installed but this just looked at the version for the JDK and tried to install the JRE assuming they had the same version. Instead let's add a `java_jre_version` fact and query that for the JRE and allow installing one or the other depending on the installed version.
1 parent afa4631 commit c147d1e

File tree

4 files changed

+64
-3
lines changed

4 files changed

+64
-3
lines changed

lib/facter/java_jre_version.rb

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Fact: java_jre_version
2+
#
3+
# Purpose: store java jre versions in the config DB
4+
#
5+
# Resolution:
6+
# Tests for presence of java jre, returns nil if not present
7+
# returns output of "java -version" and splits on \n + '"'
8+
#
9+
# Caveats:
10+
# none
11+
#
12+
# Notes:
13+
# None
14+
Facter.add(:java_jre_version) do
15+
setcode do
16+
java_jre = "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java"
17+
next unless File.exist? java_jre
18+
t_java_jre = Facter::Util::Resolution.exec("'#{java_jre}' -version 2>&1")
19+
java_jre_version = t_java_jre.to_s.lines.first.strip.split(/version/)[1].gsub(/"/, "").strip
20+
end
21+
end

manifests/init.pp

+6-1
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,18 @@
2626
mode => '0755'
2727
}
2828

29-
if (versioncmp($::java_version, '1.8.0') < 0) {
29+
if (versioncmp($::java_jre_version, '1.8.0') < 0) {
3030
package {
3131
"jre-7u${update_version}.dmg":
3232
ensure => present,
3333
alias => 'java-jre',
3434
provider => pkgdmg,
3535
source => $jre_url ;
36+
}
37+
}
38+
39+
if (versioncmp($::java_version, '1.8.0') < 0) {
40+
package {
3641
"jdk-7u${update_version}.dmg":
3742
ensure => present,
3843
alias => 'java',
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
require "spec_helper"
2+
3+
describe Facter::Util::Fact do
4+
before {
5+
Facter.clear
6+
allow(Facter::Util::Resolution).to receive(:exec).with(anything()).and_return(nil)
7+
allow(Facter.fact(:kernel)).to receive(:value).and_return("Darwin")
8+
}
9+
10+
describe "java_jre_version" do
11+
context 'returns java jre version when java jre present' do
12+
it do
13+
allow(File).to receive(:exist?).and_return(true)
14+
java_jre_version_output = <<-EOS
15+
java version "1.7.0_71"
16+
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
17+
Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)
18+
EOS
19+
allow(Facter::Util::Resolution).to receive(:exec).with("'/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java' -version 2>&1").
20+
and_return(java_jre_version_output)
21+
Facter.fact(:java_jre_version).value.should == "1.7.0_71"
22+
end
23+
end
24+
25+
context 'returns nil when java jre not present' do
26+
it do
27+
allow(File).to receive(:exist?).and_return(false)
28+
java_jre_version_output = "bash: /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java: No such file or directory"
29+
allow(Facter::Util::Resolution).to receive(:exec).with("'/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java' -version 2>&1").
30+
and_return(java_jre_version_output)
31+
Facter.fact(:java_jre_version).value.should be_nil
32+
end
33+
end
34+
end
35+
end

spec/unit/facter/java_version_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
end
2222
end
2323

24-
context 'returns nil when java present' do
24+
context 'returns nil when java not present' do
2525
it do
2626
java_version_output = "bash: java: command not found"
2727
allow(Facter::Util::Resolution).to receive(:exec).with("java -version 2>&1").
@@ -30,4 +30,4 @@
3030
end
3131
end
3232
end
33-
end
33+
end

0 commit comments

Comments
 (0)