Skip to content

Commit

Permalink
Merge pull request #2106 from nwops/ec2
Browse files Browse the repository at this point in the history
 (FACT-2816) - Fix ec2 fact issues when on non ec2 systems
  • Loading branch information
oanatmaria authored Sep 28, 2020
2 parents cc54da5 + f69c181 commit 0a2f78f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/facter/facts/linux/ec2_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def check_product_name
product_name = Facter::Resolvers::Linux::DmiBios.resolve(:product_name)
return unless product_name

Facter::FactsUtils::HYPERVISORS_HASH.each { |key, value| return value if product_name.include?(key) }
_, value = Facter::FactsUtils::HYPERVISORS_HASH.find { |key, _value| product_name.include?(key) }
value
end

def check_bios_vendor
Expand Down
3 changes: 2 additions & 1 deletion lib/facter/facts/linux/ec2_userdata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ def check_product_name
product_name = Facter::Resolvers::Linux::DmiBios.resolve(:product_name)
return unless product_name

Facter::FactsUtils::HYPERVISORS_HASH.each { |key, value| return value if product_name.include?(key) }
_, value = Facter::FactsUtils::HYPERVISORS_HASH.find { |key, _value| product_name.include?(key) }
value
end

def check_bios_vendor
Expand Down
19 changes: 19 additions & 0 deletions spec/facter/facts/linux/ec2_metadata_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,25 @@
allow(Facter::Resolvers::Linux::DmiBios).to receive(:resolve).with(:bios_vendor).and_return(nil)
end

context 'when physical machine with no hypervisor' do
let(:hypervisor) { nil }
let(:value) { nil }

before do
allow(Facter::Resolvers::Linux::DmiBios).to receive(:resolve).with(:product_name).and_return('MS-7A71')
end

it 'returns ec2 metadata fact as nil' do
expect(fact.call_the_resolver).to be_an_instance_of(Facter::ResolvedFact).and \
have_attributes(name: 'ec2_metadata', value: nil)
end

it "doesn't call Ec2 resolver" do
fact.call_the_resolver
expect(Facter::Resolvers::Ec2).not_to have_received(:resolve).with(:metadata)
end
end

context 'when hypervisor is not kvm or xen' do
let(:hypervisor) { nil }
let(:value) { nil }
Expand Down
19 changes: 19 additions & 0 deletions spec/facter/facts/linux/ec2_userdata_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,25 @@
allow(Facter::Resolvers::Linux::DmiBios).to receive(:resolve).with(:bios_vendor).and_return(nil)
end

context 'when physical machine with no hypervisor' do
let(:hypervisor) { nil }
let(:value) { nil }

before do
allow(Facter::Resolvers::Linux::DmiBios).to receive(:resolve).with(:product_name).and_return('MS-7A71')
end

it 'returns ec2 metadata fact as nil' do
expect(fact.call_the_resolver).to be_an_instance_of(Facter::ResolvedFact).and \
have_attributes(name: 'ec2_userdata', value: nil)
end

it "doesn't call Ec2 resolver" do
fact.call_the_resolver
expect(Facter::Resolvers::Ec2).not_to have_received(:resolve).with(:userdata)
end
end

context 'when hypervisor is not kvm or xen' do
let(:hypervisor) { nil }
let(:value) { nil }
Expand Down

0 comments on commit 0a2f78f

Please sign in to comment.