From 8bef42373363042f5d6707f93558441b21dbd8eb Mon Sep 17 00:00:00 2001 From: Jan Vansteenkiste Date: Tue, 29 Sep 2015 06:44:30 +0200 Subject: [PATCH 1/3] Fix rspec deprecation warnings. .should -> expect().to --- spec/unit/facter/java_major_version_spec.rb | 6 +++--- spec/unit/facter/java_patch_level_spec.rb | 4 ++-- spec/unit/facter/java_version_spec.rb | 8 ++++---- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/spec/unit/facter/java_major_version_spec.rb b/spec/unit/facter/java_major_version_spec.rb index 94d1328..5c83081 100644 --- a/spec/unit/facter/java_major_version_spec.rb +++ b/spec/unit/facter/java_major_version_spec.rb @@ -11,7 +11,7 @@ describe Facter::Util::Fact do Facter.fact(:java_version).stubs(:value).returns('1.7.0_71') end it do - Facter.fact(:java_major_version).value.should == "7" + expect(Facter.fact(:java_major_version).value).to eq("7") end end @@ -20,8 +20,8 @@ describe Facter::Util::Fact do Facter.fact(:java_version).stubs(:value).returns(nil) end it do - Facter.fact(:java_major_version).value.should be_nil + expect(Facter.fact(:java_major_version).value).to be_nil end end end -end \ No newline at end of file +end diff --git a/spec/unit/facter/java_patch_level_spec.rb b/spec/unit/facter/java_patch_level_spec.rb index 0619831..5f99eab 100644 --- a/spec/unit/facter/java_patch_level_spec.rb +++ b/spec/unit/facter/java_patch_level_spec.rb @@ -12,7 +12,7 @@ describe Facter::Util::Fact do Facter.fact(:java_version).stubs(:value).returns('1.7.0_71') end it do - Facter.fact(:java_patch_level).value.should == "71" + expect(Facter.fact(:java_patch_level).value).to eq("71") 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 be_nil + expect(Facter.fact(:java_patch_level).value).to be_nil end end end diff --git a/spec/unit/facter/java_version_spec.rb b/spec/unit/facter/java_version_spec.rb index c5f5ec8..caba175 100644 --- a/spec/unit/facter/java_version_spec.rb +++ b/spec/unit/facter/java_version_spec.rb @@ -20,7 +20,7 @@ OpenJDK 64-Bit Server VM (build 24.71-b01, mixed mode) EOS Facter::Util::Resolution.expects(:which).with("java").returns('/usr/local/jdk-1.7.0/jre/bin/java') Facter::Util::Resolution.expects(:exec).with("java -Xmx8m -version 2>&1").returns(java_version_output) - Facter.value(:java_version).should == "1.7.0_71" + expect(Facter.value(:java_version)).to eq("1.7.0_71") end end context 'on other systems' do @@ -35,7 +35,7 @@ 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(:exec).with("java -Xmx8m -version 2>&1").returns(java_version_output) - Facter.value(:java_version).should == "1.7.0_71" + expect(Facter.value(:java_version)).to eq("1.7.0_71") end end end @@ -48,7 +48,7 @@ Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode) let(:facts) { {:operatingsystem => 'OpenBSD'} } it do Facter::Util::Resolution.stubs(:exec) - Facter.value(:java_version).should be_nil + expect(Facter.value(:java_version)).to be_nil end end context 'on other systems' do @@ -58,7 +58,7 @@ Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode) let(:facts) { {:operatingsystem => 'MyOS'} } it do Facter::Util::Resolution.expects(:which).at_least(1).with("java").returns(false) - Facter.value(:java_version).should be_nil + expect(Facter.value(:java_version)).to be_nil end end end From 3645364e5bde2f837b3fa0ecc0d02bcd0e617adf Mon Sep 17 00:00:00 2001 From: David Schmitt Date: Thu, 1 Oct 2015 19:08:42 +0100 Subject: [PATCH 2/3] (MAINT) declare an exclusion filter for the tests using with_env on facter 1.6 The with_env function is not available in facter 1.6 and OpenBSD is not supported. --- spec/spec_helper.rb | 5 +++++ spec/unit/facter/java_version_spec.rb | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 2c6f566..145dcf5 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1 +1,6 @@ require 'puppetlabs_spec_helper/module_spec_helper' + +RSpec.configure do |c| + # declare an exclusion filter for the tests using with_env on facter 1.6, as the function is not available on 1.6 + c.filter_run_excluding :with_env => true if Facter.version =~ /^1\.6\./ +end diff --git a/spec/unit/facter/java_version_spec.rb b/spec/unit/facter/java_version_spec.rb index caba175..67aa7b3 100644 --- a/spec/unit/facter/java_version_spec.rb +++ b/spec/unit/facter/java_version_spec.rb @@ -7,7 +7,7 @@ describe Facter::Util::Fact do describe "java_version" do context 'returns java version when java present' do - context 'on OpenBSD' do + context 'on OpenBSD', :with_env => true do before do Facter.fact(:operatingsystem).stubs(:value).returns("OpenBSD") end @@ -41,7 +41,7 @@ Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode) end context 'returns nil when java not present' do - context 'on OpenBSD' do + context 'on OpenBSD', :with_env => true do before do Facter.fact(:operatingsystem).stubs(:value).returns("OpenBSD") end From dadc7a6c2271f34ccacd70ba5210810cd2a336ba Mon Sep 17 00:00:00 2001 From: David Schmitt Date: Thu, 1 Oct 2015 18:02:42 +0100 Subject: [PATCH 3/3] (MODULES-2637) Checks java actually installed The other method for running which java doesn't work on OSX, as java is installed as an empty shim when not installed in the base OSX image: ``` vagrant:~ vagrant$ java No Java runtime present, requesting install. vagrant:~ vagrant$ /usr/libexec/java_home --failfast Unable to find any JVMs matching version "(null)". vagrant:~ vagrant$ ``` We can use the `/usr/libexec/java_home --failfast` command to check if java is actually present first. Originally-by: Peter Souter --- lib/facter/java_version.rb | 25 +++++++++++++++++++++++-- spec/unit/facter/java_version_spec.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/lib/facter/java_version.rb b/lib/facter/java_version.rb index bc313df..e3ce186 100644 --- a/lib/facter/java_version.rb +++ b/lib/facter/java_version.rb @@ -12,12 +12,23 @@ # Notes: # None Facter.add(:java_version) do + # the OS-specific overrides need to be able to return nil, + # to indicate "no java available". Usually returning nil + # would mean that facter falls back to a lower priority + # resolution, which would then trigger MODULES-2637. To + # avoid that, we confine the "default" here to not run + # on those OS. + # Additionally, facter versions prior to 2.0.1 only support + # positive matches, so this needs to be done manually in setcode. setcode do - if Facter::Util::Resolution.which('java') - Facter::Util::Resolution.exec('java -Xmx8m -version 2>&1').lines.first.split(/"/)[1].strip + unless [ 'openbsd', 'darwin' ].include? Facter.value(:operatingsystem).downcase + if Facter::Util::Resolution.which('java') + Facter::Util::Resolution.exec('java -Xmx8m -version 2>&1').lines.first.split(/"/)[1].strip + end end end end + Facter.add(:java_version) do confine :operatingsystem => 'OpenBSD' has_weight 100 @@ -29,3 +40,13 @@ Facter.add(:java_version) do end end end + +Facter.add(:java_version) do + confine :operatingsystem => 'Darwin' + has_weight 100 + setcode do + unless /Unable to find any JVMs matching version/ =~ Facter::Util::Resolution.exec('/usr/libexec/java_home --failfast 2>&1') + Facter::Util::Resolution.exec('java -Xmx8m -version 2>&1').lines.first.split(/"/)[1].strip + end + end +end diff --git a/spec/unit/facter/java_version_spec.rb b/spec/unit/facter/java_version_spec.rb index 67aa7b3..7d1aa1b 100644 --- a/spec/unit/facter/java_version_spec.rb +++ b/spec/unit/facter/java_version_spec.rb @@ -23,6 +23,22 @@ OpenJDK 64-Bit Server VM (build 24.71-b01, mixed mode) expect(Facter.value(:java_version)).to eq("1.7.0_71") end end + context 'on Darwin' do + before do + Facter.fact(:operatingsystem).stubs(:value).returns("Darwin") + end + let(:facts) { {:operatingsystem => 'Darwin'} } + it do + java_version_output = <<-EOS +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(:exec).with("/usr/libexec/java_home --failfast 2>&1").returns("/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home") + Facter::Util::Resolution.expects(:exec).with("java -Xmx8m -version 2>&1").returns(java_version_output) + Facter.value(:java_version).should == "1.7.0_71" + end + end context 'on other systems' do before do Facter.fact(:operatingsystem).stubs(:value).returns("MyOS") @@ -51,6 +67,16 @@ Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode) expect(Facter.value(:java_version)).to be_nil end end + context 'on Darwin' do + before do + Facter.fact(:operatingsystem).stubs(:value).returns("Darwin") + end + let(:facts) { {:operatingsystem => 'Darwin'} } + it do + Facter::Util::Resolution.expects(:exec).at_least(1).with("/usr/libexec/java_home --failfast 2>&1").returns('Unable to find any JVMs matching version "(null)".') + Facter.value(:java_version).should be_nil + end + end context 'on other systems' do before do Facter.fact(:operatingsystem).stubs(:value).returns("MyOS")