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 +64
-23
lines changed
Expand file tree Collapse file tree 6 files changed +64
-23
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- t_java = Facter ::Util ::Resolution . exec ( "java -version 2>&1" )
17- 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
1819 end
19- end
20+ end
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 major version when java_version fact present' do
10+ before :each do
11+ Facter . fact ( :java_version ) . stubs ( :value ) . returns ( '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 not present' do
19+ before :each do
20+ Facter . fact ( :java_version ) . stubs ( :value ) . returns ( 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 99 context "if java is installed" do
1010 context 'returns java patch version extracted from java_version fact' do
1111 before :each do
12- allow ( Facter . fact ( :java_version ) ) . to receive ( :value ) . and_return ( " 1.7.0_71" )
12+ Facter . fact ( :java_version ) . stubs ( :value ) . returns ( ' 1.7.0_71' )
1313 end
1414 it do
1515 Facter . fact ( :java_patch_level ) . value . should == "71"
2020 context "if java is installed" do
2121 context 'returns java patch version extracted from java_version fact' do
2222 before :each do
23- allow ( Facter . fact ( :java_version ) ) . to receive ( :value ) . and_return ( nil )
23+ Facter . fact ( :java_version ) . stubs ( :value ) . returns ( nil )
2424 end
2525 it do
26- Facter . fact ( :java_patch_level ) . value . should == "JAVA_NOT_INSTALLED"
26+ Facter . fact ( :java_patch_level ) . value . should be_nil
2727 end
2828 end
2929 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
18- allow ( Facter ::Util ::Resolution ) . to receive ( :exec ) . with ( "java -version 2>&1 " ) .
19- and_return ( java_version_output )
16+ Facter ::Util ::Resolution . expects ( :which ) . with ( "java" ) . returns ( true )
17+ Facter :: Util :: Resolution . expects ( :exec ) . with ( "java -version 2>&1" ) . returns ( java_version_output )
2018 Facter . fact ( :java_version ) . value . should == "1.7.0_71"
2119 end
2220 end
2321
24- context 'returns nil when java present' do
22+ context 'returns nil when java not present' do
2523 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
24+ Facter ::Util ::Resolution . stubs ( :exec )
25+ Facter ::Util ::Resolution . expects ( :which ) . with ( "java" ) . returns ( false )
26+ Facter . fact ( :java_version ) . should be_nil
3027 end
3128 end
3229 end
You can’t perform that action at this time.
0 commit comments