From caea0b31bd09f569573069a8f144f445a6f80d1f Mon Sep 17 00:00:00 2001 From: Peter Souter Date: Thu, 8 Jan 2015 13:01:20 -0800 Subject: [PATCH 1/3] Take upstream java fixes and put them here Got some advice from some Puppetlabs people on how to do facts better! :) --- lib/facter/java_major_version.rb | 20 +++++++++++++++ lib/facter/java_patch_level.rb | 6 +---- lib/facter/java_version.rb | 13 +++++----- spec/unit/facter/java_major_version_spec.rb | 27 ++++++++++++++++++++ spec/unit/facter/java_patch_level_spec.rb | 28 +++++++++------------ spec/unit/facter/java_version_spec.rb | 15 ++++++----- 6 files changed, 73 insertions(+), 36 deletions(-) create mode 100644 lib/facter/java_major_version.rb create mode 100644 spec/unit/facter/java_major_version_spec.rb diff --git a/lib/facter/java_major_version.rb b/lib/facter/java_major_version.rb new file mode 100644 index 0000000..b0a0aae --- /dev/null +++ b/lib/facter/java_major_version.rb @@ -0,0 +1,20 @@ +# Fact: java_major_version +# +# Purpose: get Java's major version +# +# Resolution: +# Tests for presence of java, returns nil if not present +# returns output of "java -version" and splits on \n + '"' +# eg. +# +# Caveats: +# none +# +# Notes: +# None +Facter.add(:java_major_version) do + setcode do + java_version = Facter.value(:java_version) + java_patch_level = java_version.strip.split('_')[0].split('.')[1] unless java_version.nil? + end +end \ No newline at end of file diff --git a/lib/facter/java_patch_level.rb b/lib/facter/java_patch_level.rb index 3ec97be..1d7921f 100644 --- a/lib/facter/java_patch_level.rb +++ b/lib/facter/java_patch_level.rb @@ -13,10 +13,6 @@ Facter.add(:java_patch_level) do setcode do java_version = Facter.value(:java_version) - if java_version.nil? - "JAVA_NOT_INSTALLED" - else - java_patch_level = java_version.strip.split('_')[1] - end + java_patch_level = java_version.strip.split('_')[1] unless java_version.nil? end end \ No newline at end of file diff --git a/lib/facter/java_version.rb b/lib/facter/java_version.rb index e9e6b01..47d1a66 100644 --- a/lib/facter/java_version.rb +++ b/lib/facter/java_version.rb @@ -1,6 +1,6 @@ # Fact: java_version # -# Purpose: store java versions in the config DB +# Purpose: get full java version string # # Resolution: # Tests for presence of java, returns nil if not present @@ -11,11 +11,10 @@ # # Notes: # None -Facter.add(:java_version) do - setcode do - # This will fail on OS X when Java hasn't been installed yet. - next unless system "/usr/libexec/java_home --failfast &>/dev/null" - t_java = Facter::Util::Resolution.exec("java -version 2>&1") - java_version = t_java.to_s.lines.first.strip.split(/version/)[1].gsub(/"/, "").strip +if Facter::Util::Resolution.which('java') + Facter.add(:java_version) do + setcode do + Facter::Util::Resolution.exec('java -version 2>&1').lines.first.split(/"/)[1].strip + end end end diff --git a/spec/unit/facter/java_major_version_spec.rb b/spec/unit/facter/java_major_version_spec.rb new file mode 100644 index 0000000..b944aad --- /dev/null +++ b/spec/unit/facter/java_major_version_spec.rb @@ -0,0 +1,27 @@ +require "spec_helper" + +describe Facter::Util::Fact do + before { + Facter.clear + } + + describe "java_major_version" do + context 'returns java major version extracted from java_version fact' do + before :each do + allow(Facter.fact(:java_version)).to receive(:value).and_return("1.7.0_71") + end + it do + Facter.fact(:java_major_version).value.should == "7" + end + end + + context 'returns nil when java_version fact not present' do + before :each do + allow(Facter.fact(:java_version)).to receive(:value).and_return(nil) + end + it do + Facter.fact(:java_major_version).value.should be_nil + end + end + end +end \ No newline at end of file diff --git a/spec/unit/facter/java_patch_level_spec.rb b/spec/unit/facter/java_patch_level_spec.rb index 5fd4533..e5cccc9 100644 --- a/spec/unit/facter/java_patch_level_spec.rb +++ b/spec/unit/facter/java_patch_level_spec.rb @@ -6,25 +6,21 @@ } describe "java_patch_level" do - context "if java is installed" do - context 'returns java patch version extracted from java_version fact' do - before :each do - allow(Facter.fact(:java_version)).to receive(:value).and_return("1.7.0_71") - end - it do - Facter.fact(:java_patch_level).value.should == "71" - end + context 'returns java patch version extracted from java_version fact' do + before :each do + allow(Facter.fact(:java_version)).to receive(:value).and_return("1.7.0_71") + end + it do + Facter.fact(:java_patch_level).value.should == "71" end end - context "if java is installed" do - context 'returns java patch version extracted from java_version fact' do - before :each do - allow(Facter.fact(:java_version)).to receive(:value).and_return(nil) - end - it do - Facter.fact(:java_patch_level).value.should == "JAVA_NOT_INSTALLED" - end + context "returns nil when java_version fact not present" do + before :each do + allow(Facter.fact(:java_version)).to receive(:value).and_return(nil) + end + it do + Facter.fact(:java_patch_level).value.should be_nil end end end diff --git a/spec/unit/facter/java_version_spec.rb b/spec/unit/facter/java_version_spec.rb index c715f54..baf9ace 100644 --- a/spec/unit/facter/java_version_spec.rb +++ b/spec/unit/facter/java_version_spec.rb @@ -3,8 +3,6 @@ describe Facter::Util::Fact do before { Facter.clear - allow(Facter::Util::Resolution).to receive(:exec).with(anything()).and_return(nil) - allow(Facter.fact(:kernel)).to receive(:value).and_return("Darwin") } describe "java_version" do @@ -15,18 +13,19 @@ Java(TM) SE Runtime Environment (build 1.7.0_71-b14) Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode) EOS + allow(Facter::Util::Resolution).to receive(:which).with("java"). + and_return(true) allow(Facter::Util::Resolution).to receive(:exec).with("java -version 2>&1"). - and_return(java_version_output) + and_return(java_version_output) Facter.fact(:java_version).value.should == "1.7.0_71" end end - context 'returns nil when java present' do + context 'returns nil when java not present' do it do - java_version_output = "bash: java: command not found" - allow(Facter::Util::Resolution).to receive(:exec).with("java -version 2>&1"). - and_return(java_version_output) - Facter.fact(:java_version).value.should be_nil + allow(Facter::Util::Resolution).to receive(:which).with("java"). + and_return(false) + Facter.fact(:java_version).should be_nil end end end From 6575546b32f968065462327d02c60d23afde6ed4 Mon Sep 17 00:00:00 2001 From: Peter Souter Date: Wed, 6 May 2015 06:03:59 +0100 Subject: [PATCH 2/3] Improve fact to use java_home libexec Catches edge-case where which java is resolved, but Java is not actually installed! --- lib/facter/java_version.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/facter/java_version.rb b/lib/facter/java_version.rb index 47d1a66..a7e26ca 100644 --- a/lib/facter/java_version.rb +++ b/lib/facter/java_version.rb @@ -11,10 +11,10 @@ # # Notes: # None -if Facter::Util::Resolution.which('java') - Facter.add(:java_version) do - setcode do - Facter::Util::Resolution.exec('java -version 2>&1').lines.first.split(/"/)[1].strip +Facter.add(:java_version) do + setcode do + if Facter::Util::Resolution.which('java') && Facter::Util::Resolution.exec('/usr/libexec/java_home --failfast &>/dev/null') + Facter::Util::Resolution.exec('java -Xmx8m -version 2>&1').lines.first.split(/"/)[1].strip end end end From 04d28d513ec4b9a06fae35b7bd61b8f4ff47a6db Mon Sep 17 00:00:00 2001 From: Peter Souter Date: Mon, 9 Mar 2015 15:51:04 +0000 Subject: [PATCH 3/3] Refactors facts about Java version These facts have since been refactored, ported these changes from https://github.com/puppetlabs/puppetlabs-java/ --- lib/facter/java_major_version.rb | 2 +- lib/facter/java_patch_level.rb | 2 +- lib/facter/java_version.rb | 4 +++- spec/unit/facter/java_major_version.rb | 31 ++++++++++++++++++++++++++ spec/unit/facter/java_version_spec.rb | 12 +++++----- 5 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 spec/unit/facter/java_major_version.rb diff --git a/lib/facter/java_major_version.rb b/lib/facter/java_major_version.rb index b0a0aae..bf34d03 100644 --- a/lib/facter/java_major_version.rb +++ b/lib/facter/java_major_version.rb @@ -17,4 +17,4 @@ java_version = Facter.value(:java_version) java_patch_level = java_version.strip.split('_')[0].split('.')[1] unless java_version.nil? end -end \ No newline at end of file +end diff --git a/lib/facter/java_patch_level.rb b/lib/facter/java_patch_level.rb index 1d7921f..6589615 100644 --- a/lib/facter/java_patch_level.rb +++ b/lib/facter/java_patch_level.rb @@ -15,4 +15,4 @@ java_version = Facter.value(:java_version) java_patch_level = java_version.strip.split('_')[1] unless java_version.nil? end -end \ No newline at end of file +end diff --git a/lib/facter/java_version.rb b/lib/facter/java_version.rb index a7e26ca..ed00fa0 100644 --- a/lib/facter/java_version.rb +++ b/lib/facter/java_version.rb @@ -12,8 +12,10 @@ # Notes: # None Facter.add(:java_version) do + # This will fail on OS X when Java hasn't been installed yet. + next unless Kernel.system "/usr/libexec/java_home --failfast &>/dev/null" setcode do - if Facter::Util::Resolution.which('java') && Facter::Util::Resolution.exec('/usr/libexec/java_home --failfast &>/dev/null') + if Facter::Util::Resolution.which('java') Facter::Util::Resolution.exec('java -Xmx8m -version 2>&1').lines.first.split(/"/)[1].strip end end diff --git a/spec/unit/facter/java_major_version.rb b/spec/unit/facter/java_major_version.rb new file mode 100644 index 0000000..7855267 --- /dev/null +++ b/spec/unit/facter/java_major_version.rb @@ -0,0 +1,31 @@ +require "spec_helper" + +describe Facter::Util::Fact do + before { + Facter.clear + } + + describe "java_major_version" do + context "if java is not installed" do + context 'returns java major version extracted from java_version fact' do + before :each do + allow(Facter.fact(:java_version)).to receive(:value).and_return("1.7.0_71") + end + it do + Facter.fact(:java_major_version).value.should == "7" + end + end + end + + context "if java is not installed" do + context 'returns nil' do + before :each do + allow(Facter.fact(:java_version)).to receive(:value).and_return(nil) + end + it do + Facter.fact(:java_major_version).value.should == nil + end + end + end + end +end \ No newline at end of file diff --git a/spec/unit/facter/java_version_spec.rb b/spec/unit/facter/java_version_spec.rb index baf9ace..bd0403b 100644 --- a/spec/unit/facter/java_version_spec.rb +++ b/spec/unit/facter/java_version_spec.rb @@ -13,10 +13,12 @@ Java(TM) SE Runtime Environment (build 1.7.0_71-b14) Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode) EOS - allow(Facter::Util::Resolution).to receive(:which).with("java"). - and_return(true) - allow(Facter::Util::Resolution).to receive(:exec).with("java -version 2>&1"). - and_return(java_version_output) + allow(Facter::Util::Resolution).to receive(:which). + with("java").and_return(true) + expect(Kernel).to receive(:system). + with("/usr/libexec/java_home --failfast &>/dev/null").and_return(true) + allow(Facter::Util::Resolution).to receive(:exec). + with("java -Xmx8m -version 2>&1").and_return(java_version_output) Facter.fact(:java_version).value.should == "1.7.0_71" end end @@ -25,7 +27,7 @@ it do allow(Facter::Util::Resolution).to receive(:which).with("java"). and_return(false) - Facter.fact(:java_version).should be_nil + Facter.fact(:java_version).value.should be_nil end end end