Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(FACT-2690) Added Hyper-V fact for Linux #1968

Merged
merged 1 commit into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, but product name is Virtual Machine' do
let(:manufacturer) { 'unknown' }
let(:product_name) { 'Virtual Machine' }
let(:value) { {} }

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 manufacturer is Microsoft and product name is not Virtual Machine' do
let(:manufacturer) { 'Microsoft' }
let(:product_name) { 'something_else' }
let(:value) { {} }

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
end
end