This repository has been archived by the owner on Jun 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Oana Tanasoiu
committed
Jun 2, 2020
1 parent
67a5f04
commit 7e910a6
Showing
7 changed files
with
136 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
module Facter | ||
module Resolvers | ||
class Lspci < BaseResolver | ||
@semaphore = Mutex.new | ||
@fact_list ||= {} | ||
|
||
REGEX_VALUES = { 'VirtualBox' => 'virtualbox', 'XenSource' => 'xenhvm', | ||
'Microsoft Corporation Hyper-V' => 'hyperv', 'Class 8007: Google, Inc' => 'gce', | ||
'VM[wW]are' => 'vmware', '1ab8:' => 'parallels', '[Pp]arallels' => 'parallels', | ||
'(?i)(virtio)' => 'kvm' }.freeze | ||
|
||
class << self | ||
private | ||
|
||
def post_resolve(fact_name) | ||
@fact_list.fetch(fact_name) { lspci_command(fact_name) } | ||
end | ||
|
||
def lspci_command(fact_name) | ||
output = Facter::Core::Execution.execute('lspci', logger: log) | ||
return if output.empty? | ||
|
||
@fact_list[:vm] = retrieve_vm(output) | ||
@fact_list[fact_name] | ||
end | ||
|
||
def retrieve_vm(output) | ||
output.each_line { |line| REGEX_VALUES.each { |key, value| return value if line =~ /#{key}/ } } | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
describe Facter::Resolvers::Lspci do | ||
subject(:lspci_resolver) { Facter::Resolvers::Lspci } | ||
|
||
let(:log_spy) { instance_spy(Facter::Log) } | ||
|
||
before do | ||
lspci_resolver.instance_variable_set(:@log, log_spy) | ||
allow(Facter::Core::Execution).to receive(:execute).with('lspci', logger: log_spy).and_return(output) | ||
end | ||
|
||
after do | ||
lspci_resolver.invalidate_cache | ||
end | ||
|
||
context 'when lspci fails' do | ||
let(:output) { '' } | ||
|
||
it 'returns nil' do | ||
expect(lspci_resolver.resolve(:vm)).to be_nil | ||
end | ||
end | ||
|
||
context 'when lspci detects vmware' do | ||
let(:output) { load_fixture('lspci_vmware').read } | ||
|
||
it 'returns hypervisor name' do | ||
expect(lspci_resolver.resolve(:vm)).to eq('vmware') | ||
end | ||
end | ||
|
||
context 'when lspci detects xen' do | ||
let(:output) { load_fixture('lspci_aws').read } | ||
|
||
it 'returns hypervisor name' do | ||
expect(lspci_resolver.resolve(:vm)).to eq('xenhvm') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
00:00.0 Host bridge: Intel Corporation FX PMC [Natoma] (rev 02) | ||
00:02.0 VGA compatible controller: Cirrus Logic GD 5446 | ||
00:03.0 Unassigned class [ff80]: XenSource, Inc. Xen Platform Device (rev 01) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
00:10.0 SCSI storage controller: LSI Logic / Symbios Logic 53c1030 PCI-X Fusion-MPT Dual Ultra320 SCSI (rev 01) | ||
00:11.0 PCI bridge: VMware PCI bridge (rev 02) | ||
00:18.5 PCI bridge: VMware PCI Express Root Port (rev 01) | ||
00:18.6 PCI bridge: VMware PCI Express Root Port (rev 01) | ||
00:18.7 PCI bridge: VMware PCI Express Root Port (rev 01) | ||
03:00.0 Ethernet controller: VMware VMXNET3 Ethernet Controller (rev 01) |