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

Commit

Permalink
(FACT-2605) Add vmware resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
Oana Tanasoiu committed May 20, 2020
1 parent 0401b70 commit d5054e5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/facts/linux/virtual.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Virtual
FACT_NAME = 'virtual'

def call_the_resolver
fact_value = check_docker_lxc || check_gce
fact_value = check_docker_lxc || check_gce || check_vmware
Facter::ResolvedFact.new(FACT_NAME, fact_value)
end

Expand All @@ -18,6 +18,10 @@ def check_gce
def check_docker_lxc
Facter::Resolvers::DockerLxc.resolve(:vm)
end

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

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

class << self
private

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

def vmware_command(fact_name)
output = Facter::Core::Execution.execute('vmware -v', logger: log)
return if output.empty?

parts = out.split("\n")
return unless parts.size.equal?(2)

@fact_list[:vm] = "#{parts[0].downcase}_#{parts[1].downcase}"
@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 @@ -35,6 +35,20 @@
end
end

context 'when is vmware' do
let(:vm) { nil }
let(:value) { 'vmware_fusion' }

before do
allow(Facter::Resolvers::Vmware).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

0 comments on commit d5054e5

Please sign in to comment.