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-2728) Added hypervisors fact for Solaris #2045

Merged
merged 1 commit into from
Aug 31, 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
24 changes: 24 additions & 0 deletions lib/facter/facts/solaris/hypervisors/ldom.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

module Facts
module Solaris
module Hypervisors
class Ldom
FACT_NAME = 'hypervisors.ldom'

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

def call_the_resolver
fact_value = %i[
chassis_serial control_domain domain_name
domain_uuid role_control role_io role_root role_service
].map! { |key| [key, Facter::Resolvers::Solaris::Ldom.resolve(key)] }.to_h

Facter::ResolvedFact.new(FACT_NAME, fact_value)
end
end
end
end
end
39 changes: 39 additions & 0 deletions lib/facter/facts/solaris/hypervisors/zone.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

module Facts
module Solaris
module Hypervisors
class Zone
FACT_NAME = 'hypervisors.zone'

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

def call_the_resolver
fact_value = current_zone

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

def current_zone
current_zone_name = Facter::Resolvers::Solaris::ZoneName.resolve(:current_zone_name)
return unless current_zone_name

zones = Facter::Resolvers::Solaris::Zone.resolve(:zone)
return nil unless zones

current_zone = zones.find { |r| r[:name] == current_zone_name }

{
brand: current_zone[:brand],
id: current_zone[:id],
ip_type: current_zone[:iptype],
name: current_zone[:name],
uuid: current_zone[:uuid]
}
Comment on lines +28 to +34
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively yo this you could also use Hash#select or something similar to keep only the needed keys.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem here is that one key (ip_type) needs to be changed, so no general rule to map keys would work.

end
end
end
end
end
79 changes: 79 additions & 0 deletions spec/facter/facts/solaris/hypervisors/ldom_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# frozen_string_literal: true

describe Facts::Solaris::Hypervisors::Ldom do
describe '#call_the_resolver' do
subject(:fact) { Facts::Solaris::Hypervisors::Ldom.new }

before do
allow(Facter::Resolvers::Solaris::Ldom).to receive(:resolve)
end

context 'when Ldom resolver returns values' do
let(:value) do
{
'chassis_serial' => 'AK00358110',
'control_domain' => 'opdx-a0-sun2',
'domain_name' => 'sol11-9',
'domain_uuid' => 'd7a3a4df-ce8c-47a9-b396-cb5a5f30c0b2',
'role_control' => 'false',
'role_io' => 'false',
'role_root' => 'false',
'role_service' => 'false'
}
end

before do
allow(Facter::Resolvers::Solaris::Ldom).to receive(:resolve).with(:chassis_serial).and_return('AK00358110')
allow(Facter::Resolvers::Solaris::Ldom).to receive(:resolve).with(:control_domain).and_return('opdx-a0-sun2')
allow(Facter::Resolvers::Solaris::Ldom).to receive(:resolve).with(:domain_name).and_return('sol11-9')
allow(Facter::Resolvers::Solaris::Ldom).to receive(:resolve).with(:role_control).and_return('false')
allow(Facter::Resolvers::Solaris::Ldom).to receive(:resolve).with(:role_io).and_return('false')
allow(Facter::Resolvers::Solaris::Ldom).to receive(:resolve).with(:role_root).and_return('false')
allow(Facter::Resolvers::Solaris::Ldom).to receive(:resolve).with(:role_service).and_return('false')
allow(Facter::Resolvers::Solaris::Ldom)
.to receive(:resolve)
.with(:domain_uuid)
.and_return('d7a3a4df-ce8c-47a9-b396-cb5a5f30c0b2')
end

it 'returns virtual fact as physical' do
expect(fact.call_the_resolver).to be_an_instance_of(Facter::ResolvedFact).and \
have_attributes(name: 'hypervisors.ldom', value: value)
end
end

context 'when ldom resolver returns nil' do
before do
allow(Facter::Resolvers::Solaris::Ldom).to receive(:resolve).with(:chassis_serial).and_return(nil)
allow(Facter::Resolvers::Solaris::Ldom).to receive(:resolve).with(:control_domain).and_return(nil)
allow(Facter::Resolvers::Solaris::Ldom).to receive(:resolve).with(:domain_name).and_return(nil)
allow(Facter::Resolvers::Solaris::Ldom).to receive(:resolve).with(:role_control).and_return(nil)
allow(Facter::Resolvers::Solaris::Ldom).to receive(:resolve).with(:role_impl).and_return(nil)
allow(Facter::Resolvers::Solaris::Ldom).to receive(:resolve).with(:role_io).and_return(nil)
allow(Facter::Resolvers::Solaris::Ldom).to receive(:resolve).with(:role_root).and_return(nil)
allow(Facter::Resolvers::Solaris::Ldom).to receive(:resolve).with(:role_service).and_return(nil)
allow(Facter::Resolvers::Solaris::Ldom).to receive(:resolve).with(:domain_uuid).and_return(nil)
end

context 'when role_control is false' do
let(:value) do
{
'chassis_serial' => nil,
'control_domain' => nil,
'domain_name' => nil,
'domain_uuid' => nil,
'role_control' => nil,
'role_io' => nil,
'role_root' => nil,
'role_service' => nil
}
end

it 'returns virtual fact as physical' do
expect(fact.call_the_resolver).to be_an_instance_of(Facter::ResolvedFact).and \
have_attributes(name: 'hypervisors.ldom', value: value)
end
end
end
end
end
91 changes: 91 additions & 0 deletions spec/facter/facts/solaris/hypervisors/zone_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# frozen_string_literal: true

describe Facts::Solaris::Hypervisors::Zone do
describe '#call_the_resolver' do
subject(:fact) { Facts::Solaris::Hypervisors::Zone.new }

before do
allow(Facter::Resolvers::Solaris::ZoneName).to receive(:resolve)
allow(Facter::Resolvers::Solaris::Zone).to receive(:resolve)
end

context 'when current zone name is nil' do
let(:current_zone_name) { nil }

before do
allow(Facter::Resolvers::Solaris::ZoneName)
.to receive(:resolve)
.with(:current_zone_name)
.and_return(current_zone_name)
end

it 'returns virtual fact as physical' do
expect(fact.call_the_resolver).to be_an_instance_of(Facter::ResolvedFact).and \
have_attributes(name: 'hypervisors.zone', value: nil)
end
end

context 'when zone resolver call is nil' do
let(:current_zone_name) { 'global' }
let(:zones) { nil }

before do
allow(Facter::Resolvers::Solaris::ZoneName)
.to receive(:resolve)
.with(:current_zone_name)
.and_return(current_zone_name)
allow(Facter::Resolvers::Solaris::Zone).to receive(:resolve).with(:zone).and_return(zones)
end

it 'returns current zone details' do
expect(fact.call_the_resolver).to be_an_instance_of(Facter::ResolvedFact).and \
have_attributes(name: 'hypervisors.zone', value: nil)
end
end

context 'when current zone name is valid' do
let(:current_zone_name) { 'global' }
let(:zones) do
[
{
brand: 'solaris',
id: 0,
iptype: 'shared',
name: 'global',
uuid: '1234',
status: 'running',
path: 'my/path'
},
{
brand: 'solaris',
id: 1,
iptype: 'not_shared',
name: 'global2',
uuid: '4321',
status: 'running',
path: 'my/path'
}
]
end

before do
allow(Facter::Resolvers::Solaris::ZoneName)
.to receive(:resolve)
.with(:current_zone_name)
.and_return(current_zone_name)
allow(Facter::Resolvers::Solaris::Zone).to receive(:resolve).with(:zone).and_return(zones)
end

it 'returns current zone details' do
expect(fact.call_the_resolver).to be_an_instance_of(Facter::ResolvedFact).and \
have_attributes(name: 'hypervisors.zone', value: {
'brand' => 'solaris',
'id' => 0,
'ip_type' => 'shared',
'name' => 'global',
'uuid' => '1234'
})
oanatmaria marked this conversation as resolved.
Show resolved Hide resolved
end
end
end
end