Skip to content

Commit

Permalink
(FACT-2735) When no hypervisor is detected, return nil.
Browse files Browse the repository at this point in the history
  • Loading branch information
BogdanIrimie committed Jul 31, 2020
1 parent 1bf4470 commit c13aa57
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/facter/facts/linux/virtual.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,59 @@ module Linux
class Virtual
FACT_NAME = 'virtual'

def initialize
@log = Facter::Log.new(self)
end

def call_the_resolver # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
@log.debug('Linux Virtual Resolver')

fact_value = check_docker_lxc || check_gce || retrieve_from_virt_what || check_vmware
fact_value ||= check_open_vz || check_vserver || check_xen || check_other_facts || check_lspci || 'physical'

@log.debug("Fact value is: #{fact_value}")

Facter::ResolvedFact.new(FACT_NAME, fact_value)
end

def check_gce
@log.debug('Checking GCE')
bios_vendor = Facter::Resolvers::Linux::DmiBios.resolve(:bios_vendor)
'gce' if bios_vendor&.include?('Google')
end

def check_docker_lxc
@log.debug('Checking Docker and LXC')
Facter::Resolvers::Containers.resolve(:vm)
end

def check_vmware
@log.debug('Checking VMware')
Facter::Resolvers::Vmware.resolve(:vm)
end

def retrieve_from_virt_what
@log.debug('Checking virtual_what')
Facter::Resolvers::VirtWhat.resolve(:vm)
end

def check_open_vz
@log.debug('Checking OpenVZ')
Facter::Resolvers::OpenVz.resolve(:vm)
end

def check_vserver
@log.debug('Checking VServer')
Facter::Resolvers::VirtWhat.resolve(:vserver)
end

def check_xen
@log.debug('Checking XEN')
Facter::Resolvers::Xen.resolve(:vm)
end

def check_other_facts
@log.debug('Checking others')
product_name = Facter::Resolvers::Linux::DmiBios.resolve(:product_name)
bios_vendor = Facter::Resolvers::Linux::DmiBios.resolve(:bios_vendor)
return 'kvm' if bios_vendor&.include?('Amazon EC2')
Expand All @@ -53,6 +69,7 @@ def check_other_facts
end

def check_lspci
@log.debug('Checking lspci')
Facter::Resolvers::Lspci.resolve(:vm)
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/facter/resolvers/lspci.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def lspci_command(fact_name)

def retrieve_vm(output)
output.each_line { |line| REGEX_VALUES.each { |key, value| return value if line =~ /#{key}/ } }

nil
end
end
end
Expand Down
8 changes: 8 additions & 0 deletions spec/facter/resolvers/lspci_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,12 @@
expect(lspci_resolver.resolve(:vm)).to eq('xenhvm')
end
end

context 'when lspci does not detect any hypervisor' do
let(:output) { 'lspci output with no hypervisor' }

it 'returns nil' do
expect(lspci_resolver.resolve(:vm)).to be_nil
end
end
end

0 comments on commit c13aa57

Please sign in to comment.