Skip to content

Commit

Permalink
Rely on modern facts
Browse files Browse the repository at this point in the history
Previously it read legacy facts, which are deprecated and slowly going
away. It drops all the various hacks that were needed for older versions
on the assumption they're no longer needed.

Facter 2.4 introduced the structured os fact while Facter 3.0 added the
os.hardware fact to it. rspec-puppet-facts relies on those, so Facter
versions older than 3.0 are no longer supported.
  • Loading branch information
ekohl committed Jun 9, 2024
1 parent 303c6fc commit b0085f7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 57 deletions.
35 changes: 11 additions & 24 deletions lib/rspec-puppet-facts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
49 changes: 16 additions & 33 deletions spec/rspec_puppet_facts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -377,11 +377,11 @@
{
"operatingsystem" => "FreeBSD",
"operatingsystemrelease" => [
"10",
"13",
],
},
],
:facterversion => '2.4',
:facterversion => '4.5',
},
)
}
Expand All @@ -396,7 +396,7 @@

it 'returns supported OS' do
expect(subject.keys.sort).to eq [
'freebsd-10-amd64',
'freebsd-13-amd64',
]
end
end
Expand All @@ -409,11 +409,11 @@
{
"operatingsystem" => "OpenBSD",
"operatingsystemrelease" => [
"5.7",
"7.5",
],
},
],
:facterversion => '2.4',
:facterversion => '4.7',
},
)
}
Expand All @@ -428,12 +428,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(
{
Expand Down Expand Up @@ -498,7 +498,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(
{
Expand Down Expand Up @@ -616,7 +616,7 @@
"operatingsystem" => "Archlinux",
},
],
:facterversion => '2.4',
:facterversion => '3.14',
},
)
}
Expand Down Expand Up @@ -654,9 +654,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
Expand Down Expand Up @@ -701,19 +701,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
Expand Down Expand Up @@ -797,23 +797,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(
Expand Down Expand Up @@ -844,7 +827,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
Expand Down

0 comments on commit b0085f7

Please sign in to comment.