1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
# frozen_string_literal: true
require 'spec_helper'
openjdk_7_output = "Picked up JAVA_TOOL_OPTIONS: -Djava.net.preferIPv4Stack=true\n" \
"openjdk version \"1.7.0_71\"\n" \
"OpenJDK Runtime Environment (build 1.7.0_71-b14)\n" \
"OpenJDK 64-Bit Server VM (build 24.71-b01, mixed mode)\n"
jdk_7_hotspot_output = "Picked up JAVA_TOOL_OPTIONS: -Djava.net.preferIPv4Stack=true\n" \
"java version \"1.7.0_71\"\n" \
"Java(TM) SE Runtime Environment (build 1.7.0_71-b14)\n" \
"Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)\n"
describe 'java_version' do
before(:each) do
Facter.clear
end
context 'when java present, returns java version' do
context 'when on OpenBSD', with_env: true do
before(:each) do
allow(Facter.fact(:operatingsystem)).to receive(:value).and_return('OpenBSD')
allow(Facter.fact(:kernel)).to receive(:value).and_return('Linux')
end
let(:facts) { { operatingsystem: 'OpenBSD' } }
it do
allow(Facter::Core::Execution).to receive(:which).with('java').and_return('/usr/local/jdk-1.7.0/jre/bin/java')
allow(Facter::Core::Execution).to receive(:execute).with('java -Xmx12m -version 2>&1').and_return(openjdk_7_output)
expect(Facter.value(:java_version)).to eq('1.7.0_71')
end
end
context 'when on Darwin' do
before(:each) do
allow(Facter.fact(:kernel)).to receive(:value).and_return('Darwin')
end
let(:facts) { { kernel: 'Darwin' } }
it do
allow(Facter::Core::Execution).to receive(:execute).with('/usr/libexec/java_home --failfast', { on_fail: false }).and_return('/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home')
allow(Facter::Core::Execution).to receive(:execute).with('java -Xmx12m -version 2>&1').and_return(jdk_7_hotspot_output)
expect(Facter.value(:java_version)).to eq('1.7.0_71')
end
end
context 'when on other systems' do
before(:each) do
allow(Facter.fact(:operatingsystem)).to receive(:value).and_return('MyOS')
allow(Facter.fact(:kernel)).to receive(:value).and_return('Linux')
end
let(:facts) { { operatingsystem: 'MyOS' } }
it do
allow(Facter::Core::Execution).to receive(:which).with('java').and_return('/path/to/java')
allow(Facter::Core::Execution).to receive(:execute).with('java -Xmx12m -version 2>&1').and_return(jdk_7_hotspot_output)
expect(Facter.value(:java_version)).to eq('1.7.0_71')
end
end
end
context 'when java not present, returns nil' do
context 'when on OpenBSD', with_env: true do
before(:each) do
allow(Facter.fact(:operatingsystem)).to receive(:value).and_return('OpenBSD')
allow(Facter.fact(:kernel)).to receive(:value).and_return('Linux')
end
let(:facts) { { operatingsystem: 'OpenBSD' } }
it do
allow(Facter::Core::Execution).to receive(:execute) # Catch all other calls
allow(Facter::Core::Execution).to receive(:which).and_return(nil)
expect(Facter.value(:java_version)).to be_nil
end
end
context 'when on Darwin' do
before(:each) do
allow(Facter.fact(:kernel)).to receive(:value).and_return('Darwin')
end
let(:facts) { { kernel: 'Darwin' } }
it do
expect(Facter::Core::Execution).to receive(:execute).with('/usr/libexec/java_home --failfast', { on_fail: false }).at_least(1).and_return(false)
expect(Facter.value(:java_version)).to be_nil
end
end
context 'when on other systems' do
before(:each) do
allow(Facter.fact(:operatingsystem)).to receive(:value).and_return('MyOS')
allow(Facter.fact(:kernel)).to receive(:value).and_return('Linux')
end
let(:facts) { { operatingsystem: 'MyOS' } }
it do
expect(Facter::Core::Execution).to receive(:which).at_least(1).with('java').and_return(false)
expect(Facter.value(:java_version)).to be_nil
end
end
end
end
|