This repository was archived by the owner on Dec 2, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 6 files changed +73
-36
lines changed
Expand file tree Collapse file tree 6 files changed +73
-36
lines changed Original file line number Diff line number Diff line change 1+ # Fact: java_major_version
2+ #
3+ # Purpose: get Java's major version
4+ #
5+ # Resolution:
6+ # Tests for presence of java, returns nil if not present
7+ # returns output of "java -version" and splits on \n + '"'
8+ # eg.
9+ #
10+ # Caveats:
11+ # none
12+ #
13+ # Notes:
14+ # None
15+ Facter . add ( :java_major_version ) do
16+ setcode do
17+ java_version = Facter . value ( :java_version )
18+ java_patch_level = java_version . strip . split ( '_' ) [ 0 ] . split ( '.' ) [ 1 ] unless java_version . nil?
19+ end
20+ end
Original file line number Diff line number Diff line change 1313Facter . add ( :java_patch_level ) do
1414 setcode do
1515 java_version = Facter . value ( :java_version )
16- if java_version . nil?
17- "JAVA_NOT_INSTALLED"
18- else
19- java_patch_level = java_version . strip . split ( '_' ) [ 1 ]
20- end
16+ java_patch_level = java_version . strip . split ( '_' ) [ 1 ] unless java_version . nil?
2117 end
2218end
Original file line number Diff line number Diff line change 11# Fact: java_version
22#
3- # Purpose: store java versions in the config DB
3+ # Purpose: get full java version string
44#
55# Resolution:
66# Tests for presence of java, returns nil if not present
1111#
1212# Notes:
1313# None
14- Facter . add ( :java_version ) do
15- setcode do
16- # This will fail on OS X when Java hasn't been installed yet.
17- next unless system "/usr/libexec/java_home --failfast &>/dev/null"
18- t_java = Facter ::Util ::Resolution . exec ( "java -version 2>&1" )
19- java_version = t_java . to_s . lines . first . strip . split ( /version/ ) [ 1 ] . gsub ( /"/ , "" ) . strip
14+ if Facter ::Util ::Resolution . which ( 'java' )
15+ Facter . add ( :java_version ) do
16+ setcode do
17+ Facter ::Util ::Resolution . exec ( 'java -version 2>&1' ) . lines . first . split ( /"/ ) [ 1 ] . strip
18+ end
2019 end
2120end
Original file line number Diff line number Diff line change 1+ require "spec_helper"
2+
3+ describe Facter ::Util ::Fact do
4+ before {
5+ Facter . clear
6+ }
7+
8+ describe "java_major_version" do
9+ context 'returns java major version extracted from java_version fact' do
10+ before :each do
11+ allow ( Facter . fact ( :java_version ) ) . to receive ( :value ) . and_return ( "1.7.0_71" )
12+ end
13+ it do
14+ Facter . fact ( :java_major_version ) . value . should == "7"
15+ end
16+ end
17+
18+ context 'returns nil when java_version fact not present' do
19+ before :each do
20+ allow ( Facter . fact ( :java_version ) ) . to receive ( :value ) . and_return ( nil )
21+ end
22+ it do
23+ Facter . fact ( :java_major_version ) . value . should be_nil
24+ end
25+ end
26+ end
27+ end
Original file line number Diff line number Diff line change 66 }
77
88 describe "java_patch_level" do
9- context "if java is installed" do
10- context 'returns java patch version extracted from java_version fact' do
11- before :each do
12- allow ( Facter . fact ( :java_version ) ) . to receive ( :value ) . and_return ( "1.7.0_71" )
13- end
14- it do
15- Facter . fact ( :java_patch_level ) . value . should == "71"
16- end
9+ context 'returns java patch version extracted from java_version fact' do
10+ before :each do
11+ allow ( Facter . fact ( :java_version ) ) . to receive ( :value ) . and_return ( "1.7.0_71" )
12+ end
13+ it do
14+ Facter . fact ( :java_patch_level ) . value . should == "71"
1715 end
1816 end
1917
20- context "if java is installed" do
21- context 'returns java patch version extracted from java_version fact' do
22- before :each do
23- allow ( Facter . fact ( :java_version ) ) . to receive ( :value ) . and_return ( nil )
24- end
25- it do
26- Facter . fact ( :java_patch_level ) . value . should == "JAVA_NOT_INSTALLED"
27- end
18+ context "returns nil when java_version fact not present" do
19+ before :each do
20+ allow ( Facter . fact ( :java_version ) ) . to receive ( :value ) . and_return ( nil )
21+ end
22+ it do
23+ Facter . fact ( :java_patch_level ) . value . should be_nil
2824 end
2925 end
3026 end
Original file line number Diff line number Diff line change 33describe Facter ::Util ::Fact do
44 before {
55 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" )
86 }
97
108 describe "java_version" do
1513Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
1614Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)
1715 EOS
16+ allow ( Facter ::Util ::Resolution ) . to receive ( :which ) . with ( "java" ) .
17+ and_return ( true )
1818 allow ( Facter ::Util ::Resolution ) . to receive ( :exec ) . with ( "java -version 2>&1" ) .
19- and_return ( java_version_output )
19+ and_return ( java_version_output )
2020 Facter . fact ( :java_version ) . value . should == "1.7.0_71"
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
26- java_version_output = "bash: java: command not found"
27- allow ( Facter ::Util ::Resolution ) . to receive ( :exec ) . with ( "java -version 2>&1" ) .
28- and_return ( java_version_output )
29- Facter . fact ( :java_version ) . value . should be_nil
26+ allow ( Facter ::Util ::Resolution ) . to receive ( :which ) . with ( "java" ) .
27+ and_return ( false )
28+ Facter . fact ( :java_version ) . should be_nil
3029 end
3130 end
3231 end
You can’t perform that action at this time.
0 commit comments