Skip to content

Commit

Permalink
(FACT-2699) Detect augeas from gem if augparse is not available.
Browse files Browse the repository at this point in the history
  • Loading branch information
BogdanIrimie committed Jul 30, 2020
1 parent 9f2bff0 commit 1ec10ee
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 8 deletions.
23 changes: 21 additions & 2 deletions lib/facter/resolvers/augeas_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,29 @@ def post_resolve(fact_name)
end

def read_augeas_version(fact_name)
output = Facter::Core::Execution.execute('augparse --version 2>&1', logger: log)
@fact_list[:augeas_version] = Regexp.last_match(1) if output =~ /^augparse (\d+\.\d+\.\d+)/
@fact_list[:augeas_version] = read_augeas_from_cli
@fact_list[:augeas_version] ||= read_augeas_from_gem

@fact_list[fact_name]
end

def read_augeas_from_cli
output = Facter::Core::Execution.execute('augparse --version 2>&1', logger: log)
Regexp.last_match(1) if output =~ /^augparse (\d+\.\d+\.\d+)/
end

def read_augeas_from_gem
require 'augeas'

return ::Augeas.create { |aug| aug.get('/augeas/version') } if ::Augeas.respond_to?(:create)

# it is used for legacy augeas <= 0.5.0
return ::Augeas.open { |aug| aug.get('/augeas/version') } if ::Augeas.respond_to?(:open)
rescue StandardError => e
log.debug('ruby-augeas not available')
log.log_exception(e)
nil
end
end
end
end
Expand Down
80 changes: 75 additions & 5 deletions spec/facter/resolvers/augeas_resolver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,82 @@

before do
augeas.instance_variable_set(:@log, log_spy)
allow(Facter::Core::Execution).to receive(:execute)
.with('augparse --version 2>&1', logger: log_spy)
.and_return('augparse 1.12.0 <http://augeas.net/>')
end

it 'returns build' do
expect(augeas.resolve(:augeas_version)).to eq('1.12.0')
after do
augeas.invalidate_cache
end

context 'when augparse is installed' do
before do
allow(Facter::Core::Execution).to receive(:execute)
.with('augparse --version 2>&1', logger: log_spy)
.and_return('augparse 1.12.0 <http://augeas.net/>')
end

it 'returns build' do
expect(augeas.resolve(:augeas_version)).to eq('1.12.0')
end
end

context 'when augparse is not installed' do
before do
allow(Facter::Core::Execution).to receive(:execute)
.with('augparse --version 2>&1', logger: log_spy)
.and_return('sh: augparse: command not found')
end

context 'when augeas gem > 0.5.0 is installed' do
let(:augeas_mock) { instance_spy(Augeas) }

before do
allow(Augeas).to receive(:respond_to?).with(:create).and_return(true)
allow(Augeas).to receive(:respond_to?).with(:create, true).and_return(true)
allow(Augeas).to receive(:create).and_return('1.12.0')
end

it 'returns build' do
expect(augeas.resolve(:augeas_version)).to eq('1.12.0')
end
end

context 'when augeas gem <= 0.5.0 is installed' do
let(:augeas_mock) { instance_spy(Augeas) }

before do
allow(Augeas).to receive(:respond_to?).with(:create).and_return(false)
allow(Augeas).to receive(:respond_to?).with(:open).and_return(true)
allow(Augeas).to receive(:respond_to?).with(:open, true).and_return(true)
allow(Augeas).to receive(:open).and_return('1.12.0')
end

it 'returns build' do
expect(augeas.resolve(:augeas_version)).to eq('1.12.0')
end
end

context 'when augeas gem is not installed' do
let(:exception) { StandardError.new('error_message') }

before do
allow(Facter::Resolvers::Augeas).to receive(:require).with('augeas').and_raise(exception)
end

it 'returns nil' do
expect(augeas.resolve(:augeas_version)).to be_nil
end

it 'logs a debug message' do
augeas.resolve(:augeas_version)

expect(log_spy).to have_received(:debug).with('ruby-augeas not available')
end

it 'logs an exception' do
augeas.resolve(:augeas_version)

expect(log_spy).to have_received(:log_exception).with(exception)
end
end
end
end
7 changes: 7 additions & 0 deletions spec/mocks/augeas.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class Augeas
def self.create(opts = {}, &block); end

def self.open(root = nil, loadpath = nil, flags = NONE, &block); end
end
2 changes: 1 addition & 1 deletion spec/mocks/kernel_mock.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

LIBS_TO_SKIP = %w[win32ole ffi win32/registry sys/filesystem].freeze
LIBS_TO_SKIP = %w[win32ole ffi win32/registry sys/filesystem augeas].freeze
module Kernel
alias old_require require
def require(path)
Expand Down

0 comments on commit 1ec10ee

Please sign in to comment.