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

Commit

Permalink
Merge f9285e8 into a9d516e
Browse files Browse the repository at this point in the history
  • Loading branch information
Filipovici-Andrei authored Mar 26, 2020
2 parents a9d516e + f9285e8 commit 68a8fad
Showing 1 changed file with 104 additions and 0 deletions.
104 changes: 104 additions & 0 deletions spec/facter/resolvers/base_resolver_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# frozen_string_literal: true

describe Facter::Resolvers::BaseResolver do
let(:fact) { 'fact' }
let(:resolver) do
Class.new(Facter::Resolvers::BaseResolver) do
@fact_list = {}
@semaphore = Mutex.new
def self.post_resolve(fact_name)
@fact_list[fact_name] = 'value'
@fact_list
end
end
end

describe '#log' do
before do
allow(Facter::Log).to receive(:new).with(resolver).and_return('logger')
end

it 'initializes the log' do
resolver.log

expect(Facter::Log).to have_received(:new).with(resolver)
end

it 'initializes the log only once' do
resolver.log
resolver.log

expect(Facter::Log).to have_received(:new).with(resolver).once
end
end

describe '#invalidate_cache' do
it 'clears the facts_list' do
resolver.resolve(fact)

resolver.invalidate_cache

expect(resolver.resolve('fact2')).to eq('fact2' => 'value')
end
end

describe '#subscribe_to_manager' do
before do
allow(Facter::CacheManager).to receive(:subscribe).with(resolver)
end

it 'calls the CacheManager subscribe method' do
resolver.subscribe_to_manager

expect(Facter::CacheManager).to have_received(:subscribe).with(resolver)
end
end

describe '#resolve' do
context 'when fact is resolved successfully' do
before do
allow(resolver).to receive(:post_resolve)
allow(Facter::CacheManager).to receive(:subscribe).with(resolver)
end

it 'calls the CacheManager subscribe method' do
resolver.resolve(fact)

expect(Facter::CacheManager).to have_received(:subscribe).with(resolver)
end

it 'calls the post_resolve method' do
resolver.resolve(fact)

expect(resolver).to have_received(:post_resolve).with(fact)
end
end

context 'when Load Error is raised' do
before do
allow(resolver).to receive(:post_resolve).and_raise(LoadError)
allow(Facter::Log).to receive(:new).with(resolver).and_return(instance_double(Facter::Log, debug: nil))
end

it 'logs the Load Error exception' do
resolver.resolve(fact)

expect(resolver.log).to have_received(:debug).with("resolving fact #{fact}, but LoadError")
end

it 'sets the fact to nil' do
expect(resolver.resolve(fact)).to eq(nil)
end
end
end

describe '#post_resolve' do
let(:resolver) { Class.new(Facter::Resolvers::BaseResolver) }

it 'raises NotImplementedError error' do
expect { resolver.post_resolve(fact) }.to \
raise_error(NotImplementedError,
'You must implement post_resolve(fact_name) method in ')
end
end
end

0 comments on commit 68a8fad

Please sign in to comment.