This repository has been archived by the owner on Jun 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
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
Showing
1 changed file
with
104 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,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 |