Skip to content

Commit

Permalink
(FACT-2690) Added Hyper-V fact for Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
Filipovici-Andrei committed Jul 13, 2020
1 parent 37879e3 commit 0769aea
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lib/facter/facts/linux/hypervisors/hyper_v.rb
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
66 changes: 66 additions & 0 deletions spec/facter/facts/linux/hypervisors/hyper_v_spec.rb
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

0 comments on commit 0769aea

Please sign in to comment.