Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(FACT-2816) - Fix ec2 fact issues when on non ec2 systems #2106

Merged
merged 1 commit into from
Sep 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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