-
Notifications
You must be signed in to change notification settings - Fork 495
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
Aug 21, 2020
1 parent
0a03f68
commit 106c066
Showing
5 changed files
with
198 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,28 @@ | ||
# frozen_string_literal: true | ||
|
||
module Facts | ||
module Linux | ||
class Xen | ||
FACT_NAME = 'xen' | ||
ALIASES = 'xendomains' | ||
|
||
def call_the_resolver | ||
xen_type = check_virt_what || check_xen | ||
return Facter::ResolvedFact.new(FACT_NAME, nil) if !xen_type || xen_type != 'xen0' | ||
|
||
domains = Facter::Resolvers::Xen.resolve(:domains) || [] | ||
|
||
[Facter::ResolvedFact.new(FACT_NAME, { domains: domains }), | ||
Facter::ResolvedFact.new(ALIASES, domains.entries.join(','), :legacy)] | ||
end | ||
|
||
def check_virt_what | ||
Facter::Resolvers::VirtWhat.resolve(:vm) | ||
end | ||
|
||
def check_xen | ||
Facter::Resolvers::Xen.resolve(:vm) | ||
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,117 @@ | ||
# frozen_string_literal: true | ||
|
||
describe Facts::Linux::Xen do | ||
describe '#call_the_resolver' do | ||
subject(:fact) { Facts::Linux::Xen.new } | ||
|
||
before do | ||
allow(Facter::Resolvers::VirtWhat).to receive(:resolve).with(:vm).and_return(vm) | ||
allow(Facter::Resolvers::Xen).to receive(:resolve).with(:domains).and_return(domains) | ||
end | ||
|
||
context 'when is xen privileged' do | ||
let(:vm) { 'xen0' } | ||
let(:domains) { %w[domain1 domain2] } | ||
|
||
it 'calls Facter::Resolvers::VirtWhat' do | ||
fact.call_the_resolver | ||
expect(Facter::Resolvers::VirtWhat).to have_received(:resolve).with(:vm) | ||
end | ||
|
||
it 'calls Facter::Resolvers::Xen with :domains' do | ||
fact.call_the_resolver | ||
expect(Facter::Resolvers::Xen).to have_received(:resolve).with(:domains) | ||
end | ||
|
||
it 'returns xen fact' do | ||
expect(fact.call_the_resolver).to be_an_instance_of(Array).and \ | ||
contain_exactly(an_object_having_attributes(name: 'xen', value: { 'domains' => domains }), | ||
an_object_having_attributes(name: 'xendomains', | ||
value: domains.entries.join(','), type: :legacy)) | ||
end | ||
end | ||
|
||
context 'when is xen privileged but there are no domains' do | ||
let(:vm) { nil } | ||
let(:domains) { [] } | ||
|
||
before do | ||
allow(Facter::Resolvers::Xen).to receive(:resolve).with(:vm).and_return('xen0') | ||
end | ||
|
||
it 'calls Facter::Resolvers::VirtWhat' do | ||
fact.call_the_resolver | ||
expect(Facter::Resolvers::VirtWhat).to have_received(:resolve).with(:vm) | ||
end | ||
|
||
it 'calls Facter::Resolvers::Xen with :vm' do | ||
fact.call_the_resolver | ||
expect(Facter::Resolvers::Xen).to have_received(:resolve).with(:vm) | ||
end | ||
|
||
it 'calls Facter::Resolvers::Xen with :domains' do | ||
fact.call_the_resolver | ||
expect(Facter::Resolvers::Xen).to have_received(:resolve).with(:domains) | ||
end | ||
|
||
it 'returns xen fact' do | ||
expect(fact.call_the_resolver).to be_an_instance_of(Array).and \ | ||
contain_exactly(an_object_having_attributes(name: 'xen', value: { 'domains' => domains }), | ||
an_object_having_attributes(name: 'xendomains', | ||
value: domains.entries.join(','), type: :legacy)) | ||
end | ||
end | ||
|
||
context 'when is xen privileged but domains are nil' do | ||
let(:vm) { nil } | ||
let(:domains) { nil } | ||
let(:result) { [] } | ||
|
||
before do | ||
allow(Facter::Resolvers::Xen).to receive(:resolve).with(:vm).and_return('xen0') | ||
end | ||
|
||
it 'calls Facter::Resolvers::VirtWhat' do | ||
fact.call_the_resolver | ||
expect(Facter::Resolvers::VirtWhat).to have_received(:resolve).with(:vm) | ||
end | ||
|
||
it 'calls Facter::Resolvers::Xen with :vm' do | ||
fact.call_the_resolver | ||
expect(Facter::Resolvers::Xen).to have_received(:resolve).with(:vm) | ||
end | ||
|
||
it 'calls Facter::Resolvers::Xen with :domains' do | ||
fact.call_the_resolver | ||
expect(Facter::Resolvers::Xen).to have_received(:resolve).with(:domains) | ||
end | ||
|
||
it 'returns xen fact' do | ||
expect(fact.call_the_resolver).to be_an_instance_of(Array).and \ | ||
contain_exactly(an_object_having_attributes(name: 'xen', value: { 'domains' => result }), | ||
an_object_having_attributes(name: 'xendomains', | ||
value: result.entries.join(','), type: :legacy)) | ||
end | ||
end | ||
|
||
context 'when is xen unprivileged' do | ||
let(:vm) { 'xenhvm' } | ||
let(:domains) { nil } | ||
|
||
it 'calls Facter::Resolvers::VirtWhat' do | ||
fact.call_the_resolver | ||
expect(Facter::Resolvers::VirtWhat).to have_received(:resolve).with(:vm) | ||
end | ||
|
||
it 'does not call Facter::Resolvers::Xen with :domains' do | ||
fact.call_the_resolver | ||
expect(Facter::Resolvers::Xen).not_to have_received(:resolve).with(:domains) | ||
end | ||
|
||
it 'returns nil fact' do | ||
expect(fact.call_the_resolver).to be_an_instance_of(Facter::ResolvedFact).and \ | ||
have_attributes(name: 'xen', value: domains) | ||
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,4 @@ | ||
Name ID Mem VCPUs State Time(s) | ||
Domain-0 0 750 4 r----- 11794.3 | ||
win 1 1019 1 r----- 0.3 | ||
linux 2 2048 2 r----- 5624.2 |