diff --git a/lib/rspec-puppet-facts.rb b/lib/rspec-puppet-facts.rb index b45d01c9..de1ec930 100644 --- a/lib/rspec-puppet-facts.rb +++ b/lib/rspec-puppet-facts.rb @@ -94,17 +94,17 @@ def on_supported_os_implementation(opts = {}) end filter << { - :operatingsystem => os_sup['operatingsystem'], - :operatingsystemrelease => os_release_filter, - :hardwaremodel => hardwaremodel, + 'os.name' => os_sup['operatingsystem'], + 'os.release.full' => os_release_filter, + 'os.hardware' => hardwaremodel, } end end else opts[:hardwaremodels].each do |hardwaremodel| filter << { - :operatingsystem => os_sup['operatingsystem'], - :hardwaremodel => hardwaremodel, + 'os.name' => os_sup['operatingsystem'], + 'os.hardware' => hardwaremodel, } end end @@ -144,26 +144,13 @@ 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 + os_fact = facts[:os] + unless os_fact + RspecPuppetFacts.warning "No os fact was found in FacterDB for: #{facts}" + next end - os = "#{facts[:operatingsystem].downcase}-#{operatingsystemmajrelease}-#{facts[:hardwaremodel]}" + + os = "#{os_fact['name'].downcase}-#{os_fact['release']['major']}-#{os_fact['hardware']}" 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) diff --git a/spec/rspec_puppet_facts_spec.rb b/spec/rspec_puppet_facts_spec.rb index 6d6a724e..39d60185 100644 --- a/spec/rspec_puppet_facts_spec.rb +++ b/spec/rspec_puppet_facts_spec.rb @@ -161,7 +161,8 @@ end it 'returns a fact set with all the keys as Strings' do - expect(get_keys.call(result['debian-7-x86_64'])).to all(be_a(String)) + expect(result).to include('debian-12-x86_64') + expect(get_keys.call(result['debian-12-x86_64'])).to all(be_a(String)) end end @@ -171,7 +172,8 @@ end it 'returns a fact set with all the keys as Symbols or Strings' do - expect(get_keys.call(result['debian-7-x86_64'])).to all(be_a(Symbol).or(be_a(String))) + expect(result).to include('debian-12-x86_64') + expect(get_keys.call(result['debian-12-x86_64'])).to all(be_a(Symbol).or(be_a(String))) end end end @@ -377,11 +379,11 @@ { "operatingsystem" => "FreeBSD", "operatingsystemrelease" => [ - "10", + "13", ], }, ], - :facterversion => '2.4', + :facterversion => '4.5', }, ) } @@ -396,7 +398,7 @@ it 'returns supported OS' do expect(subject.keys.sort).to eq [ - 'freebsd-10-amd64', + 'freebsd-13-amd64', ] end end @@ -409,11 +411,11 @@ { "operatingsystem" => "OpenBSD", "operatingsystemrelease" => [ - "5.7", + "7.5", ], }, ], - :facterversion => '2.4', + :facterversion => '4.7', }, ) } @@ -428,12 +430,12 @@ it 'returns supported OS' do expect(subject.keys.sort).to eq [ - 'openbsd-5.7-amd64', + 'openbsd-7-amd64', ] end end - context 'When testing Solaris 11', :if => Facter.version.to_f >= 2.0 do + context 'When testing Solaris 11' do subject { on_supported_os( { @@ -498,7 +500,7 @@ end end - context 'When testing Windows', :if => Facter.version.to_f >= 2.4 do + context 'When testing Windows' do subject do on_supported_os( { @@ -616,7 +618,7 @@ "operatingsystem" => "Archlinux", }, ], - :facterversion => '2.4', + :facterversion => '3.14', }, ) } @@ -654,9 +656,9 @@ it 'escapes the parens in the filter' do filter = { - :operatingsystem => "IOS", - :operatingsystemrelease => "/^12\\.2\\(25\\)EWA9/", - :hardwaremodel => "x86_64", + 'os.name' => "IOS", + 'os.release.full' => "/^12\\.2\\(25\\)EWA9/", + 'os.hardware' => "x86_64", } expect(FacterDB).to receive(:get_facts).with(filter).once @@ -701,19 +703,19 @@ supported_os: [ { 'operatingsystem' => 'CentOS', 'operatingsystemrelease' => %w[7] }, ], - facterversion: "2.6", + facterversion: "3.15", ) end before do - allow(Facter).to receive(:version).and_return('2.4.5') + allow(Facter).to receive(:version).and_return('3.14.1') end it 'returns facts from a facter version matching version and below' do is_expected.to match( 'centos-7-x86_64' => include( - :facterversion => /\A2\.[0-6]\./, + :facterversion => /\A3\.14\./, ), ) end @@ -797,23 +799,6 @@ end end - context 'When querying a fact set that does not have an operatingsystemmajrelease fact' do - subject do - on_supported_os( - supported_os: [ - { 'operatingsystem' => 'SLES', 'operatingsystemrelease' => ['11'] }, - ], - facterversion: '2.1.0', - ) - end - - it 'splits the operatingsystemrelease fact value to get the major release' do - is_expected.to match( - 'sles-11-x86_64' => include(:operatingsystemrelease => '11.3'), - ) - end - end - context 'With an invalid facterversion in the options hash' do let(:method_call) do on_supported_os( @@ -844,7 +829,7 @@ before do allow(FacterDB).to receive(:get_facts).and_call_original allow(FacterDB).to receive(:get_facts).with( - {:operatingsystem=>"CentOS", :operatingsystemrelease=>"/^7/", :hardwaremodel=>"x86_64"}, + {'os.name'=>"CentOS", 'os.release.full'=>"/^7/", 'os.hardware'=>"x86_64"}, ).and_wrap_original do |m, *args| m.call(*args).reject { |facts| facts[:facterversion].start_with?('3.9.') } end