Skip to content

Commit

Permalink
work with symbolized strings
Browse files Browse the repository at this point in the history
  • Loading branch information
bastelfreak committed Jun 8, 2024
1 parent 09d6472 commit 6b7e25f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 31 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
source ENV['GEM_SOURCE'] || 'https://rubygems.org'

gem 'facterdb', git: 'https://github.com/bastelfreak/facterdb', branch: 'optional-symbolize-keys'

gemspec

gem 'facter', ENV['FACTER_GEM_VERSION'], :require => false
Expand Down
35 changes: 9 additions & 26 deletions lib/rspec-puppet-facts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def on_supported_os_implementation(opts = {})
# 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).to_h { |facts| [Gem::Version.new(facts[:facterversion]), facts] }
versions = FacterDB.get_facts(filter_spec, symbolize_keys: !RSpec.configuration.facterdb_string_keys).to_h { |facts| [Gem::Version.new(facts[:facterversion]), facts] }

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

Expand All @@ -148,33 +148,16 @@ def on_supported_os_implementation(opts = {})

os_facts_hash = {}
received_facts.map do |facts|
# Fix facter bug
# Todo: refactor the whole block to rely on structured facts and use legacy facts as fallback
if facts[:operatingsystem] == 'Ubuntu'
operatingsystemmajrelease = facts[:operatingsystemrelease].split('.')[0..1].join('.')
elsif facts[:operatingsystem] == 'OpenBSD'
operatingsystemmajrelease = facts[:operatingsystemrelease]
elsif facts[:operatingsystem] == 'windows' && facts[:operatingsystemrelease].start_with?('10.0.')
operatingsystemmajrelease = '2016'
elsif facts.dig(:os, 'release', 'major')
operatingsystemmajrelease = facts[:os]['release']['major']
elsif facts.dig(:os, 'distro', 'release', 'major')
operatingsystemmajrelease = facts[:os]['distro']['release']['major']
else
if facts[:operatingsystemmajrelease].nil?
operatingsystemmajrelease = facts[:operatingsystemrelease].split('.')[0]
else
operatingsystemmajrelease = facts[:operatingsystemmajrelease]
end
end
os = "#{facts[:operatingsystem].downcase}-#{operatingsystemmajrelease}-#{facts[:hardwaremodel]}"
os = RSpec.configuration.facterdb_string_keys ? facts[:os] : facts['os']
operatingsystemmajrelease = os['release']['major']
operatingsystem = os['name'].downcase
hardwaremodel = os['hardware']
os = "#{operatingsystem}-#{operatingsystemmajrelease}-#{hardwaremodel}"
next unless os.start_with? RspecPuppetFacts.spec_facts_os_filter if RspecPuppetFacts.spec_facts_os_filter
facts.merge! RspecPuppetFacts.common_facts
os_facts_hash[os] = RspecPuppetFacts.with_custom_facts(os, facts)
end

return stringify_keys(os_facts_hash) if RSpec.configuration.facterdb_string_keys

os_facts_hash
end

Expand Down Expand Up @@ -281,6 +264,7 @@ def self.common_facts
if augeas?
@common_facts[:augeasversion] = Augeas.open(nil, nil, Augeas::NO_MODL_AUTOLOAD).get('/augeas/version')
end
@common_facts = stringify_keys(@common_facts) if RSpec.configuration.facterdb_string_keys

@common_facts
end
Expand Down Expand Up @@ -440,7 +424,6 @@ def self.facter_version_for_puppet_version(puppet_version)
end

RSpec.configure do |c|
c.add_setting :default_facter_version,
:default => RspecPuppetFacts.facter_version_for_puppet_version(Puppet.version)
c.add_setting :facterdb_string_keys, :default => false
c.add_setting :default_facter_version, default: RspecPuppetFacts.facter_version_for_puppet_version(Puppet.version)
c.add_setting :facterdb_string_keys, default: false
end
10 changes: 5 additions & 5 deletions spec/rspec_puppet_facts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@

it 'returns supported OS' do
expect(subject.keys.sort).to eq [
'openbsd-7.5-amd64',
'openbsd-7-amd64',
]
end
end
Expand Down Expand Up @@ -674,7 +674,7 @@
:hardwaremodel => "x86_64",
}

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

Expand Down Expand Up @@ -804,18 +804,18 @@
before do
allow(FacterDB).to receive(:get_facts).and_call_original
allow(FacterDB).to receive(:get_facts).with(
{:operatingsystem=>"CentOS", :operatingsystemrelease=>"/^9/", :hardwaremodel=>"x86_64"},
{:operatingsystem=>"CentOS", :operatingsystemrelease=>"/^9/", :hardwaremodel=>"x86_64"}, symbolize_keys: true,
).and_wrap_original do |m, *args|
m.call(*args).reject { |facts| facts[:facterversion].start_with?('4.6.') }
end
end

it 'returns CentOS facts from a facter version matching 4.5' do
is_expected.to include('centos-9-x86_64' => include(:facterversion => '4.5.2'))
is_expected.to include('centos-9-x86_64' => include(facterversion: '4.5.2'))
end

it 'returns Debian facts from a facter version matching 4.6.1' do
is_expected.to include('debian-12-x86_64' => include(:facterversion => '4.6.1'),)
is_expected.to include('debian-12-x86_64' => include(facterversion: '4.6.1'),)
end
end
end
Expand Down

0 comments on commit 6b7e25f

Please sign in to comment.