Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a single FacterDB.get_facts call #158

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ group :coverage, optional: ENV['COVERAGE'] != 'yes' do
gem 'codecov', require: false
gem 'simplecov-console', require: false
end

gem 'facterdb', github: 'ekohl/facterdb', branch: 'filter-results'
12 changes: 9 additions & 3 deletions lib/rspec-puppet-facts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,19 @@ def on_supported_os_implementation(opts = {})
loose_requirement = RspecPuppetFacts.facter_version_to_loose_requirement(facterversion)
received_facts = []

db = FacterDB.get_facts(filter, symbolize_keys: false)
unless db.any?
RspecPuppetFacts.warning "No facts were found in the FacterDB for: #{filter.inspect}"
return {}
end

# FacterDB may have newer versions of facter data for which it contains a subset of all possible
# facter data (see FacterDB 0.5.2 for Facter releases 3.8 and 3.9). In this situation we need to
# cycle through and downgrade Facter versions per platform type until we find matching Facter data.
facterversion_key = RSpec.configuration.facterdb_string_keys ? 'facterversion' : :facterversion
filter.each do |filter_spec|
versions = FacterDB.get_facts(filter_spec, symbolize_keys: !RSpec.configuration.facterdb_string_keys).to_h do |facts|
[Gem::Version.new(facts[facterversion_key]), facts]
versions = FacterDB.filter_results(db, filter_spec).to_h do |facts|
result = RSpec.configuration.facterdb_string_keys ? facts : facts.transform_keys(&:to_sym)
[Gem::Version.new(facts['facterversion']), result]
end

version, facts = versions.select { |v, _f| strict_requirement =~ v }.max_by { |v, _f| v }
Expand Down
17 changes: 10 additions & 7 deletions spec/rspec_puppet_facts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -610,13 +610,13 @@
end

it 'escapes the parens in the filter' do
filter = {
filter = [{
'os.name' => 'IOS',
'os.release.full' => '/^12\\.2\\(25\\)EWA9/',
'os.hardware' => 'x86_64',
}
}]

expect(FacterDB).to receive(:get_facts).with(filter, symbolize_keys: true).once
expect(FacterDB).to receive(:get_facts).with(filter, symbolize_keys: false).once.and_return([])
subject
end

Expand Down Expand Up @@ -742,11 +742,14 @@
end

before do
allow(FacterDB).to receive(:get_facts).and_call_original
allow(FacterDB).to receive(:get_facts).with(
{ 'os.name' => 'CentOS', 'os.release.full' => '/^9/', 'os.hardware' => 'x86_64' }, symbolize_keys: true
).and_wrap_original do |m, *args|
m.call(*args).reject { |facts| facts[:facterversion].start_with?('4.6.') }
[
{ 'os.name' => 'CentOS', 'os.release.full' => '/^9/', 'os.hardware' => 'x86_64' },
{ 'os.name' => 'Debian', 'os.release.full' => '/^12/', 'os.hardware' => 'x86_64' },
],
symbolize_keys: false,
).and_wrap_original do |m, *args, **kwargs|
m.call(*args, **kwargs).reject { |facts| facts.dig('os', 'name') == 'CentOS' && facts['facterversion'].start_with?('4.6.') }
end
end

Expand Down
Loading