forked from rodjek/rspec-puppet
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add setting to use custom Facter implementation
Starting with versions 7.12.0/6.25.0, Puppet was changed not to directly depend on Facter anymore, but to use a `Puppet::Runtime` implementation instead (e.g. calls to `Facter` were changed to `Puppet.runtime[:facter]` to allow for pluggable Facter backends). rspec-puppet stubs facts from facterdb by setting custom facts with higher weights, meaning that Facter 4 will still resolve the underlying core facts (which is by design), leading to noticeable performance hits when compiling catalogs with Facter 4 as opposed to Facter 2 (which just returned the custom fact). This means we can achieve a pretty big performance improvement with rspec-puppet by registering a custom Facter implementation that bypasses Facter altogether and just saves facts to hash. This behavior cand be activated by setting `facter_implementation` to `rspec` in `RSpec.configure`. By default, the setting has the value of `facter` which maintains the old behavior of going through Facter. If `rspec` is set but the Puppet version does not support Facter implementations, rspec will warn and fall back to using Facter.
- Loading branch information
1 parent
84a9cfe
commit 48c5605
Showing
7 changed files
with
268 additions
and
5 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
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
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
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,49 @@ | ||
module RSpec::Puppet | ||
|
||
# Implements a simple hash-based version of Facter to be used in module tests | ||
# that use rspec-puppet. | ||
class FacterTestImpl | ||
def initialize | ||
@facts = {} | ||
end | ||
|
||
def value(fact_name) | ||
@facts[fact_name.to_s] | ||
end | ||
|
||
def clear | ||
@facts.clear | ||
end | ||
|
||
def to_hash | ||
@facts | ||
end | ||
|
||
def add(name, options = {}, &block) | ||
raise 'Facter.add expects a block' unless block_given? | ||
@facts[name.to_s] = instance_eval(&block) | ||
end | ||
|
||
# noop methods | ||
def debugging(arg); end | ||
|
||
def reset; end | ||
|
||
def search(*paths); end | ||
|
||
def setup_logging; end | ||
|
||
private | ||
|
||
def setcode(string = nil, &block) | ||
if block_given? | ||
value = block.call | ||
else | ||
value = string | ||
end | ||
|
||
value | ||
end | ||
end | ||
end | ||
|
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
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
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,84 @@ | ||
require 'spec_helper' | ||
require 'rspec-puppet/facter_impl' | ||
|
||
describe RSpec::Puppet::FacterTestImpl do | ||
subject(:facter_impl) { RSpec::Puppet::FacterTestImpl.new } | ||
let(:fact_hash) do | ||
{ | ||
'string_fact' => 'string_value', | ||
'hash_fact' => { 'key' => 'value' }, | ||
'int_fact' => 3, | ||
'true_fact' => true, | ||
'false_fact' => false, | ||
} | ||
end | ||
|
||
before do | ||
facter_impl.add(:string_fact) { setcode { 'string_value' } } | ||
facter_impl.add(:hash_fact) { setcode { { 'key' => 'value' } } } | ||
facter_impl.add(:int_fact) { setcode { 3 } } | ||
facter_impl.add(:true_fact) { setcode { true } } | ||
facter_impl.add(:false_fact) { setcode { false } } | ||
end | ||
|
||
describe 'noop methods' do | ||
[:debugging, :reset, :search, :setup_logging].each do |method| | ||
it "implements ##{method}" do | ||
expect(facter_impl).to respond_to(method) | ||
end | ||
end | ||
end | ||
|
||
describe '#value' do | ||
it 'retrieves a fact of type String' do | ||
expect(facter_impl.value(:string_fact)).to eq('string_value') | ||
end | ||
|
||
it 'retrieves a fact of type Hash' do | ||
expect(facter_impl.value(:hash_fact)).to eq({ 'key' => 'value' }) | ||
end | ||
|
||
it 'retrieves a fact of type Integer' do | ||
expect(facter_impl.value(:int_fact)).to eq(3) | ||
end | ||
|
||
it 'retrieves a fact of type TrueClass' do | ||
expect(facter_impl.value(:true_fact)).to eq(true) | ||
end | ||
|
||
it 'retrieves a fact of type FalseClass' do | ||
expect(facter_impl.value(:false_fact)).to eq(false) | ||
end | ||
end | ||
|
||
describe '#to_hash' do | ||
it 'returns a hash with all added facts' do | ||
expect(facter_impl.to_hash).to eq(fact_hash) | ||
end | ||
end | ||
|
||
describe '#clear' do | ||
it 'clears the fact hash' do | ||
facter_impl.clear | ||
expect(facter_impl.to_hash).to be_empty | ||
end | ||
end | ||
|
||
describe '#add' do | ||
before { facter_impl.clear } | ||
|
||
it 'adds a fact with a setcode block' do | ||
facter_impl.add(:setcode_block) { setcode { 'value' } } | ||
expect(facter_impl.value(:setcode_block)).to eq('value') | ||
end | ||
|
||
it 'adds a fact with a setcode string' do | ||
facter_impl.add(:setcode_string) { setcode 'value' } | ||
expect(facter_impl.value(:setcode_string)).to eq('value') | ||
end | ||
|
||
it 'fails when not given a block' do | ||
expect { facter_impl.add(:something) }.to raise_error(RuntimeError, 'Facter.add expects a block') | ||
end | ||
end | ||
end |