(MODULES-1570) Add java_major_version fact
Gives the major version of the Java release (ie. Java 1.7.0_71, major version is 7) We also want to specifically return nil when java not present
This commit is contained in:
committed by
Peter Souter
parent
5168f61c26
commit
cd3ac1ee46
20
lib/facter/java_major_version.rb
Normal file
20
lib/facter/java_major_version.rb
Normal file
@@ -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
|
||||
@@ -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
|
||||
@@ -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,9 +11,10 @@
|
||||
#
|
||||
# Notes:
|
||||
# None
|
||||
Facter.add(:java_version) do
|
||||
setcode do
|
||||
java_version = Facter::Util::Resolution.exec("java -version 2>&1")
|
||||
java_version = java_version.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
|
||||
end
|
||||
|
||||
27
spec/unit/facter/java_major_version_spec.rb
Normal file
27
spec/unit/facter/java_major_version_spec.rb
Normal file
@@ -0,0 +1,27 @@
|
||||
require "spec_helper"
|
||||
|
||||
describe Facter::Util::Fact do
|
||||
before {
|
||||
Facter.clear
|
||||
}
|
||||
|
||||
describe "java_major_version" do
|
||||
context 'returns major version when java_version fact present' do
|
||||
before :each do
|
||||
Facter.fact(:java_version).stubs(:value).returns('1.7.0_71')
|
||||
end
|
||||
it do
|
||||
Facter.fact(:java_major_version).value.should == "7"
|
||||
end
|
||||
end
|
||||
|
||||
context 'returns nil when java not present' do
|
||||
before :each do
|
||||
Facter.fact(:java_version).stubs(:value).returns(nil)
|
||||
end
|
||||
it do
|
||||
Facter.fact(:java_major_version).value.should be_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -23,7 +23,7 @@ describe Facter::Util::Fact do
|
||||
Facter.fact(:java_version).stubs(:value).returns(nil)
|
||||
end
|
||||
it do
|
||||
Facter.fact(:java_patch_level).value.should == "JAVA_NOT_INSTALLED"
|
||||
Facter.fact(:java_patch_level).value.should be_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -13,6 +13,7 @@ java version "1.7.0_71"
|
||||
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
|
||||
Facter::Util::Resolution.expects(:which).with("java").returns(true)
|
||||
Facter::Util::Resolution.expects(:exec).with("java -version 2>&1").returns(java_version_output)
|
||||
Facter.fact(:java_version).value.should == "1.7.0_71"
|
||||
end
|
||||
@@ -20,9 +21,8 @@ Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)
|
||||
|
||||
context 'returns nil when java not present' do
|
||||
it do
|
||||
java_version_output = "bash: java: command not found"
|
||||
Facter::Util::Resolution.expects(:exec).with("java -version 2>&1").returns(java_version_output)
|
||||
Facter.fact(:java_version).value.should be_nil
|
||||
Facter::Util::Resolution.expects(:which).with("java").returns(false)
|
||||
Facter.fact(:java_version).should be_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user