Skip to content

Commit e5d252e

Browse files
committed
(bugfix) - Fix ec2 fact issues when on non ec2 systems
* previously when ec2 facts are executed the check_product_name was run which incorrectly returned a hash of values when the product name did not exist in the hypervisor hash. This is an edge case on linux systems not in ec2.
1 parent b496e04 commit e5d252e

4 files changed

Lines changed: 42 additions & 3 deletions

File tree

lib/facter/facts/linux/ec2_metadata.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ def check_product_name
3535
product_name = Facter::Resolvers::Linux::DmiBios.resolve(:product_name)
3636
return unless product_name
3737

38-
Facter::FactsUtils::HYPERVISORS_HASH.each { |key, value| return value if product_name.include?(key) }
38+
_, value = Facter::FactsUtils::HYPERVISORS_HASH.find {|key, value| product_name.include?(key) }
39+
value
3940
end
4041

4142
def check_bios_vendor

lib/facter/facts/linux/ec2_userdata.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ def check_xen
3232
def check_product_name
3333
product_name = Facter::Resolvers::Linux::DmiBios.resolve(:product_name)
3434
return unless product_name
35-
36-
Facter::FactsUtils::HYPERVISORS_HASH.each { |key, value| return value if product_name.include?(key) }
35+
_, value = Facter::FactsUtils::HYPERVISORS_HASH.find {|key, value| product_name.include?(key) }
36+
value
3737
end
3838

3939
def check_bios_vendor

spec/facter/facts/linux/ec2_metadata_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,25 @@
1313
allow(Facter::Resolvers::Linux::DmiBios).to receive(:resolve).with(:bios_vendor).and_return(nil)
1414
end
1515

16+
context 'when physical machine with no hypervisor' do
17+
let(:hypervisor) { nil }
18+
let(:value) { nil }
19+
20+
before(:each) do
21+
allow(Facter::Resolvers::Linux::DmiBios).to receive(:resolve).with(:product_name).and_return('MS-7A71')
22+
end
23+
24+
it 'returns ec2 metadata fact as nil' do
25+
expect(fact.call_the_resolver).to be_an_instance_of(Facter::ResolvedFact).and \
26+
have_attributes(name: 'ec2_metadata', value: nil)
27+
end
28+
29+
it "doesn't call Ec2 resolver" do
30+
fact.call_the_resolver
31+
expect(Facter::Resolvers::Ec2).not_to have_received(:resolve).with(:metadata)
32+
end
33+
end
34+
1635
context 'when hypervisor is not kvm or xen' do
1736
let(:hypervisor) { nil }
1837
let(:value) { nil }

spec/facter/facts/linux/ec2_userdata_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,25 @@
1313
allow(Facter::Resolvers::Linux::DmiBios).to receive(:resolve).with(:bios_vendor).and_return(nil)
1414
end
1515

16+
context 'when physical machine with no hypervisor' do
17+
let(:hypervisor) { nil }
18+
let(:value) { nil }
19+
20+
before(:each) do
21+
allow(Facter::Resolvers::Linux::DmiBios).to receive(:resolve).with(:product_name).and_return('MS-7A71')
22+
end
23+
24+
it 'returns ec2 metadata fact as nil' do
25+
expect(fact.call_the_resolver).to be_an_instance_of(Facter::ResolvedFact).and \
26+
have_attributes(name: 'ec2_metadata', value: nil)
27+
end
28+
29+
it "doesn't call Ec2 resolver" do
30+
fact.call_the_resolver
31+
expect(Facter::Resolvers::Ec2).not_to have_received(:resolve).with(:metadata)
32+
end
33+
end
34+
1635
context 'when hypervisor is not kvm or xen' do
1736
let(:hypervisor) { nil }
1837
let(:value) { nil }

0 commit comments

Comments
 (0)