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

Collect facts iteratively #152

Merged
merged 1 commit into from
Jul 20, 2023
Merged
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
11 changes: 5 additions & 6 deletions lib/rspec-puppet-facts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,18 @@ def on_supported_os_implementation(opts = {})
strict_requirement = RspecPuppetFacts::facter_version_to_strict_requirement(facterversion)

loose_requirement = RspecPuppetFacts::facter_version_to_loose_requirement(facterversion)
received_facts = []

# 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.
filter.each do |filter_spec|
versions = FacterDB.get_facts(filter_spec).map { |facts| Gem::Version.new(facts[:facterversion]) }.sort.reverse
next unless versions.any?
versions = FacterDB.get_facts(filter_spec).to_h { |facts| [Gem::Version.new(facts[:facterversion]), facts] }

version = versions.detect { |v| strict_requirement =~ v }
version, facts = versions.select { |v, _f| strict_requirement =~ v }.max_by { |v, _f| v }

unless version
version = versions.detect { |v| loose_requirement =~ v } if loose_requirement
version, facts = versions.select { |v, _f| loose_requirement =~ v }.max_by { |v, _f| v } if loose_requirement
next unless version

if RspecPuppetFacts.spec_facts_strict?
Expand All @@ -137,10 +137,9 @@ def on_supported_os_implementation(opts = {})
RspecPuppetFacts.warning "No facts were found in the FacterDB for Facter v#{facterversion} on #{filter_spec}, using v#{version} instead"
end

filter_spec[:facterversion] = "/\\A#{Regexp.escape(version.to_s)}\\Z/"
received_facts << facts
end

received_facts = FacterDB::get_facts(filter)
unless received_facts.any?
RspecPuppetFacts.warning "No facts were found in the FacterDB for: #{filter.inspect}"
return {}
Expand Down
15 changes: 6 additions & 9 deletions spec/rspec_puppet_facts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -662,19 +662,16 @@

before do
allow(described_class).to receive(:warning).with(a_string_matching(/no facts were found/i))
allow(FacterDB).to receive(:get_facts).and_call_original
end

it 'escapes the parens in the filter' do
filter = [
include(
:operatingsystem => "IOS",
:operatingsystemrelease => "/^12\\.2\\(25\\)EWA9/",
:hardwaremodel => "x86_64",
),
]
filter = {
:operatingsystem => "IOS",
:operatingsystemrelease => "/^12\\.2\\(25\\)EWA9/",
:hardwaremodel => "x86_64",
}

expect(FacterDB).to receive(:get_facts).with(filter)
expect(FacterDB).to receive(:get_facts).with(filter).once
subject
end

Expand Down