Skip to content
This repository has been archived by the owner on Jun 19, 2020. It is now read-only.

(FACT-2245) Add xen resolver #532

Merged
merged 1 commit into from
Jun 2, 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
6 changes: 5 additions & 1 deletion lib/facts/linux/virtual.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Virtual

def call_the_resolver
fact_value = check_docker_lxc || check_gce || retrieve_from_virt_what || check_vmware
fact_value ||= check_open_vz || check_vserver
fact_value ||= check_open_vz || check_vserver || check_xen

Facter::ResolvedFact.new(FACT_NAME, fact_value)
end
Expand Down Expand Up @@ -36,6 +36,10 @@ def check_open_vz
def check_vserver
Facter::Resolvers::VirtWhat.resolve(:vserver)
end

def check_xen
Facter::Resolvers::Xen.resolve(:vm)
end
end
end
end
26 changes: 26 additions & 0 deletions lib/resolvers/xen.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

module Facter
module Resolvers
class Xen < BaseResolver
@semaphore = Mutex.new
@fact_list ||= {}

class << self
private

def post_resolve(fact_name)
@fact_list.fetch(fact_name) { chech_xen_dirs(fact_name) }
end

def chech_xen_dirs(fact_name)
xen_type = 'xen0' if File.exist?('/dev/xen/evtchn')
xen_type = 'xenu' if !xen_type && (File.exist?('/proc/xen') || File.exist?('/dev/xvda1'))

@fact_list[:vm] = xen_type
@fact_list[fact_name]
end
end
end
end
end
14 changes: 14 additions & 0 deletions spec/facter/facts/linux/virtual_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,20 @@
end
end

context 'when is xen priviledged' do
let(:vm) { nil }
let(:value) { 'xen0' }

before do
allow(Facter::Resolvers::Xen).to receive(:resolve).with(:vm).and_return(value)
end

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

context 'when resolver returns nil' do
let(:vm) { nil }

Expand Down
36 changes: 36 additions & 0 deletions spec/facter/resolvers/xen_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

describe Facter::Resolvers::Xen do
subject(:xen_resolver) { Facter::Resolvers::Xen }

let(:proc_xen_file) { false }
let(:xvda1_file) { false }

before do
allow(File).to receive(:exist?).with('/dev/xen/evtchn').and_return(evtchn_file)
allow(File).to receive(:exist?).with('/proc/xen').and_return(proc_xen_file)
allow(File).to receive(:exist?).with('/dev/xvda1').and_return(xvda1_file)
xen_resolver.invalidate_cache
end

context 'when is priviledged xen' do
let(:evtchn_file) { true }

it 'returns xen0' do
expect(xen_resolver.resolve(:vm)).to eq('xen0')
end

it 'does not check other files' do
expect(File).not_to have_received(:exist?).with('/proc/xen')
end
end

context 'when is unpriviledged xen' do
let(:evtchn_file) { false }
let(:xvda1_file) { true }

it 'returns xenu' do
expect(xen_resolver.resolve(:vm)).to eq('xenu')
end
end
end