From 14c1371a40ae612d7e09a4e1ac7a2db0fb81f523 Mon Sep 17 00:00:00 2001 From: jordanbreen28 Date: Tue, 29 Nov 2022 12:28:58 +0000 Subject: [PATCH] (1093) - Fix unresolved fact error Prior to this commit, work was carried out on this module to update all instances of the now deprecated Facter::Util::Resolution, and replace all with its newer and supported counterpart Facter::Core::Execution. However, these do not behave exactly the same. Facter::Util::Resolution initially ran a which to locate the binary before executing, preventing any errors from occuring. The newer Facter::Core::Execution method did not do this, instead it attempted to execut> This commit aims to introduce an "on_fail:false" flag to each execute statement, so that a failed execute will return false (boolean) as oppose to an error, which can then be used for further logic. --- lib/facter/ip6tables_version.rb | 9 +++------ lib/facter/iptables_persistent_version.rb | 2 +- lib/facter/iptables_version.rb | 9 +++------ spec/unit/facter/iptables_persistent_version_spec.rb | 8 ++++---- spec/unit/facter/iptables_spec.rb | 10 +++++++--- 5 files changed, 18 insertions(+), 20 deletions(-) diff --git a/lib/facter/ip6tables_version.rb b/lib/facter/ip6tables_version.rb index ec4848a..31b0d52 100644 --- a/lib/facter/ip6tables_version.rb +++ b/lib/facter/ip6tables_version.rb @@ -2,12 +2,9 @@ Facter.add(:ip6tables_version) do confine kernel: :Linux + confine { Facter::Core::Execution.which('ip6tables') } setcode do - version = Facter::Core::Execution.execute('ip6tables --version') - if version - version.match(%r{\d+\.\d+\.\d+}).to_s - else - nil - end + version = Facter::Core::Execution.execute('ip6tables --version', { on_fail: nil }) + version.match(%r{\d+\.\d+\.\d+}).to_s if version end end diff --git a/lib/facter/iptables_persistent_version.rb b/lib/facter/iptables_persistent_version.rb index 5f3598c..ff66f0d 100644 --- a/lib/facter/iptables_persistent_version.rb +++ b/lib/facter/iptables_persistent_version.rb @@ -6,7 +6,7 @@ Facter.add(:iptables_persistent_version) do # Throw away STDERR because dpkg >= 1.16.7 will make some noise if the # package isn't currently installed. cmd = "dpkg-query -Wf '${Version}' netfilter-persistent 2>/dev/null" - version = Facter::Core::Execution.execute(cmd) + version = Facter::Core::Execution.execute(cmd, { on_fail: nil }) if version.nil? || !version.match(%r{\d+\.\d+}) nil diff --git a/lib/facter/iptables_version.rb b/lib/facter/iptables_version.rb index 6093d2e..3cc4931 100644 --- a/lib/facter/iptables_version.rb +++ b/lib/facter/iptables_version.rb @@ -2,12 +2,9 @@ Facter.add(:iptables_version) do confine kernel: :Linux + confine { Facter::Core::Execution.which('iptables') } setcode do - version = Facter::Core::Execution.execute('iptables --version') - if version - version.match(%r{\d+\.\d+\.\d+}).to_s - else - nil - end + version = Facter::Core::Execution.execute('iptables --version', { on_fail: nil }) + version.match(%r{\d+\.\d+\.\d+}).to_s if version end end diff --git a/spec/unit/facter/iptables_persistent_version_spec.rb b/spec/unit/facter/iptables_persistent_version_spec.rb index 91ab608..514e2eb 100644 --- a/spec/unit/facter/iptables_persistent_version_spec.rb +++ b/spec/unit/facter/iptables_persistent_version_spec.rb @@ -17,7 +17,7 @@ describe 'Facter::Util::Fact iptables_persistent_version' do before(:each) do allow(Facter.fact(:operatingsystem)).to receive(:value).and_return(os) allow(Facter.fact(:operatingsystemrelease)).to receive(:value).and_return(os_release) - allow(Facter::Core::Execution).to receive(:execute).with(dpkg_cmd) + allow(Facter::Core::Execution).to receive(:execute).with(dpkg_cmd, { on_fail: nil }) .and_return(ver) end it { expect(Facter.fact(:iptables_persistent_version).value).to eql ver } @@ -28,7 +28,7 @@ describe 'Facter::Util::Fact iptables_persistent_version' do before(:each) do allow(Facter.fact(:operatingsystem)).to receive(:value).and_return('Ubuntu') allow(Facter.fact(:operatingsystemrelease)).to receive(:value).and_return('20.04') - allow(Facter::Core::Execution).to receive(:execute).with(dpkg_cmd) + allow(Facter::Core::Execution).to receive(:execute).with(dpkg_cmd, { on_fail: nil }) .and_return(nil) end it { expect(Facter.fact(:iptables_persistent_version).value).to be_nil } @@ -62,7 +62,7 @@ describe 'Facter::Util::Fact iptables_persistent_version' do before(:each) do allow(Facter.fact(:operatingsystem)).to receive(:value).and_return(os) allow(Facter.fact(:operatingsystemrelease)).to receive(:value).and_return(os_release) - allow(Facter::Core::Execution).to receive(:execute).with(dpkg_cmd) + allow(Facter::Core::Execution).to receive(:execute).with(dpkg_cmd, { on_fail: nil }) .and_return(ver) end it { expect(Facter.fact(:iptables_persistent_version).value).to eql ver } @@ -74,7 +74,7 @@ describe 'Facter::Util::Fact iptables_persistent_version' do before(:each) do allow(Facter.fact(:operatingsystem)).to receive(:value).and_return('Ubuntu') allow(Facter.fact(:operatingsystemrelease)).to receive(:value).and_return(os_release) - allow(Facter::Core::Execution).to receive(:execute).with(dpkg_cmd) + allow(Facter::Core::Execution).to receive(:execute).with(dpkg_cmd, { on_fail: nil }) .and_return(nil) end it { expect(Facter.fact(:iptables_persistent_version).value).to be_nil } diff --git a/spec/unit/facter/iptables_spec.rb b/spec/unit/facter/iptables_spec.rb index 16b8f35..a343cba 100644 --- a/spec/unit/facter/iptables_spec.rb +++ b/spec/unit/facter/iptables_spec.rb @@ -11,16 +11,20 @@ describe 'Facter::Util::Fact' do describe 'iptables_version' do it { - allow(Facter::Core::Execution).to receive(:execute).with('iptables --version') - .and_return('iptables v1.4.7') + allow(Facter::Core::Execution).to receive(:which) + .with('iptables').and_return('/usr/sbin/iptables') + allow(Facter::Core::Execution).to receive(:execute) + .with('iptables --version', { on_fail: nil }).and_return('iptables v1.4.7') expect(Facter.fact(:iptables_version).value).to eql '1.4.7' } end describe 'ip6tables_version' do before(:each) do + allow(Facter::Core::Execution).to receive(:which) + .with('ip6tables').and_return('/usr/sbin/ip6tables') allow(Facter::Core::Execution).to receive(:execute) - .with('ip6tables --version').and_return('ip6tables v1.4.7') + .with('ip6tables --version', { on_fail: nil }).and_return('ip6tables v1.4.7') end it { expect(Facter.fact(:ip6tables_version).value).to eql '1.4.7' } end -- 2.45.2