-
Notifications
You must be signed in to change notification settings - Fork 496
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(FACT-2690) Added Hyper-V fact for Linux
- Loading branch information
1 parent
37879e3
commit 0769aea
Showing
2 changed files
with
91 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# frozen_string_literal: true | ||
|
||
module Facts | ||
module Linux | ||
module Hypervisors | ||
class HyperV | ||
FACT_NAME = 'hypervisors.hyperv' | ||
|
||
def call_the_resolver | ||
fact_value = check_hyper_v | ||
Facter::ResolvedFact.new(FACT_NAME, fact_value) | ||
end | ||
|
||
def check_hyper_v | ||
manufacturer = Facter::Resolvers::Linux::DmiBios.resolve(:sys_vendor) | ||
product_name = Facter::Resolvers::Linux::DmiBios.resolve(:product_name) | ||
|
||
return {} if manufacturer =~ /Microsoft/ && product_name == 'Virtual Machine' | ||
|
||
nil | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# frozen_string_literal: true | ||
|
||
describe Facts::Linux::Hypervisors::HyperV do | ||
describe '#call_the_resolver' do | ||
subject(:fact) { Facts::Linux::Hypervisors::HyperV.new } | ||
|
||
before do | ||
allow(Facter::Resolvers::Linux::DmiBios).to receive(:resolve).with(:sys_vendor).and_return(manufacturer) | ||
allow(Facter::Resolvers::Linux::DmiBios).to receive(:resolve).with(:product_name).and_return(product_name) | ||
end | ||
|
||
context 'when resolver returns hyper_v' do | ||
let(:manufacturer) { 'Microsoft' } | ||
let(:product_name) { 'Virtual Machine' } | ||
let(:value) { {} } | ||
|
||
it 'calls Facter::Resolvers::DMIBios with :sys_vendor' do | ||
fact.call_the_resolver | ||
expect(Facter::Resolvers::Linux::DmiBios).to have_received(:resolve).with(:sys_vendor) | ||
end | ||
|
||
it 'calls Facter::Resolvers::DMIBios with :product_name' do | ||
fact.call_the_resolver | ||
expect(Facter::Resolvers::Linux::DmiBios).to have_received(:resolve).with(:product_name) | ||
end | ||
|
||
it 'returns hyper_v fact' do | ||
expect(fact.call_the_resolver).to be_an_instance_of(Facter::ResolvedFact).and \ | ||
have_attributes(name: 'hypervisors.hyperv', value: value) | ||
end | ||
end | ||
|
||
context 'when resolver returns nil' do | ||
let(:manufacturer) { nil } | ||
let(:product_name) { nil } | ||
let(:value) { nil } | ||
|
||
it 'returns virtual fact as nil' do | ||
expect(fact.call_the_resolver).to be_an_instance_of(Facter::ResolvedFact).and \ | ||
have_attributes(name: 'hypervisors.hyperv', value: value) | ||
end | ||
end | ||
|
||
context 'when manufacturer is not Microsoft' do | ||
let(:manufacturer) { 'unknown' } | ||
let(:product_name) { 'Virtual Machine' } | ||
let(:value) { nil } | ||
|
||
it 'returns virtual fact as nil' do | ||
expect(fact.call_the_resolver).to be_an_instance_of(Facter::ResolvedFact).and \ | ||
have_attributes(name: 'hypervisors.hyperv', value: value) | ||
end | ||
end | ||
|
||
context 'when manufacturer is Microsoft and product name is not Virtual Machine' do | ||
let(:manufacturer) { 'Microsoft' } | ||
let(:product_name) { 'something_else' } | ||
let(:value) { nil } | ||
|
||
it 'returns virtual fact as nil' do | ||
expect(fact.call_the_resolver).to be_an_instance_of(Facter::ResolvedFact).and \ | ||
have_attributes(name: 'hypervisors.hyperv', value: value) | ||
end | ||
end | ||
end | ||
end |