Skip to content

Commit

Permalink
Add SPEC_FACTS_STRICT setting
Browse files Browse the repository at this point in the history
Folks with high confidence needs can use this to elide the permissive
behaviour.
  • Loading branch information
DavidS committed Jul 25, 2017
1 parent e2b1c55 commit 2c983a4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,5 @@ If you want to run the tests against the facts for specific operating systems, y
```bash
SPEC_FACTS_OS='ubuntu-14' rake spec
```

When no facts are available for the specific facter/operating system combination, the library will fall back to facts from earlier versions of the requested operating system, to allow testing to continue when new versions of facter are released. Set `SPEC_FACTS_STRICT=yes` to instead trigger a failure.
15 changes: 15 additions & 0 deletions lib/rspec-puppet-facts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ def on_supported_os(opts = {})
return {}
end

unless version == facterversion
if RspecPuppetFacts.spec_facts_strict?
raise ArgumentError, "No facts were found in the FacterDB for Facter v#{facterversion}, aborting"
else
RspecPuppetFacts.warning "No facts were found in the FacterDB for Facter v#{facterversion}, using v#{version} instead"
end
end

os_facts_hash = {}
received_facts.map do |facts|
# Fix facter bug
Expand Down Expand Up @@ -170,6 +178,13 @@ def self.spec_facts_os_filter
ENV['SPEC_FACTS_OS']
end

# If SPEC_FACTS_STRICT is set to `yes`, RspecPuppetFacts will error on missing FacterDB entries, instead of warning & skipping the tests, or using an older facter version.
# @return [Boolean]
# @api private
def self.spec_facts_strict?
ENV['SPEC_FACTS_STRICT'] == 'yes'
end

# These facts are common for all OS'es and will be
# added to the facts retrieved from the FacterDB
# @api private
Expand Down
13 changes: 11 additions & 2 deletions spec/rspec_puppet_facts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -397,18 +397,27 @@
supported_os: [
{ 'operatingsystem' => 'CentOS', 'operatingsystemrelease' => %w[7] }
],
facterversion: "#{major}.#{minor.to_i + 1}"
facterversion: "#{major}.#{minor.to_i + 100}"
)
end

it 'returns facts from a facter version matching future and below' do
major, minor = Facter.version.split('.')
is_expected.to match(
'centos-7-x86_64' => include(
facterversion: /\A#{major}\.[0-#{minor.to_i + 1}]\./
facterversion: /\A#{major}\.[0-#{minor.to_i + 100}]\./
)
)
end

context 'With SPEC_FACTS_STRICT set to `yes`' do
before(:each) do
allow(RspecPuppetFacts).to receive(:spec_facts_strict?).and_return(true)
end
it 'errors' do
expect { subject }.to raise_error ArgumentError, /No facts were found in the FacterDB.*aborting/
end
end
end

context 'With a custom facterversion (3.1) in the options hash' do
Expand Down

0 comments on commit 2c983a4

Please sign in to comment.