Skip to content

Commit

Permalink
Collect facts iteratively
Browse files Browse the repository at this point in the history
Rather than getting the facts for every filter spec and then discarding
that, this stores the found facts in an array and uses that.

This eliminates a call to FacterDB::get_facts with a very complex
filter.

A quick test in puppet-nginx reduces loading of tests by about 2 seconds
(from ~9 to ~7).
  • Loading branch information
ekohl committed Nov 9, 2020
1 parent 5452fe9 commit 4d80729
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions lib/rspec-puppet-facts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,18 @@ def on_supported_os_implementation(opts = {})
end

facterversion_obj = Gem::Version.new(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|
version = FacterDB.get_facts(filter_spec).map { |facts| Gem::Version.new(facts[:facterversion]) }.sort.reverse.detect { |v| v <= facterversion_obj }
next unless version
version = version.to_s
facts = FacterDB.get_facts(filter_spec)
.select { |facts| Gem::Version.new(facts[:facterversion]) <= facterversion_obj }
.max_by { |facts| Gem::Version.new(facts[:facterversion]) }

next unless facts
version = facts[:facterversion]

unless version == facterversion
if RspecPuppetFacts.spec_facts_strict?
Expand All @@ -130,10 +134,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)}/"
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

0 comments on commit 4d80729

Please sign in to comment.