From ac69504513afe91cf53221bf0d6098c2c1da9d2f Mon Sep 17 00:00:00 2001 From: Tim Meusel Date: Fri, 7 Jun 2024 20:09:52 +0200 Subject: [PATCH] work with symbolized strings --- lib/rspec-puppet-facts.rb | 35 +++++++++------------------------ spec/rspec_puppet_facts_spec.rb | 10 +++++----- 2 files changed, 14 insertions(+), 31 deletions(-) diff --git a/lib/rspec-puppet-facts.rb b/lib/rspec-puppet-facts.rb index 18141f72..57b315ab 100644 --- a/lib/rspec-puppet-facts.rb +++ b/lib/rspec-puppet-facts.rb @@ -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 } @@ -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 if RspecPuppetFacts.spec_facts_os_filter && !os.start_with?(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 @@ -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 @@ -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 diff --git a/spec/rspec_puppet_facts_spec.rb b/spec/rspec_puppet_facts_spec.rb index fe06b33e..c34787c6 100644 --- a/spec/rspec_puppet_facts_spec.rb +++ b/spec/rspec_puppet_facts_spec.rb @@ -428,7 +428,7 @@ it 'returns supported OS' do expect(subject.keys.sort).to eq [ - 'openbsd-7.5-amd64', + 'openbsd-7-amd64', ] end end @@ -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 @@ -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