From 221ffb5c700b2369788f76af1f7c8735bc43206e Mon Sep 17 00:00:00 2001 From: Kilian Engelhardt Date: Wed, 3 May 2023 21:16:37 +0200 Subject: [PATCH 1/2] confine custom fact to running lldpd daemon --- lib/facter/lldp.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/facter/lldp.rb b/lib/facter/lldp.rb index af2aa76..44f4e40 100644 --- a/lib/facter/lldp.rb +++ b/lib/facter/lldp.rb @@ -7,7 +7,8 @@ class MalformedDataError < StandardError; end Facter.add(:lldp) do confine do - Facter::Util::Resolution.which('lldpctl') + Facter::Util::Resolution.which('lldpctl') && + File.exist?('/run/lldpd/lldpd.socket') end setcode do From ea0c308e52d7389adc28802902866734f5742674 Mon Sep 17 00:00:00 2001 From: Kilian Engelhardt Date: Wed, 3 May 2023 21:19:08 +0200 Subject: [PATCH 2/2] add unit test for 'lldpd' not running --- spec/unit/facter/lldp_spec.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/spec/unit/facter/lldp_spec.rb b/spec/unit/facter/lldp_spec.rb index d3a10e2..624ca1c 100644 --- a/spec/unit/facter/lldp_spec.rb +++ b/spec/unit/facter/lldp_spec.rb @@ -170,14 +170,25 @@ context 'lldpctl not in path' do before do + allow(File).to receive(:exist?).with('/run/lldpd/lldpd.socket').and_return(true) allow(Facter::Util::Resolution).to receive(:which).with('lldpctl').and_return(nil) end it { expect(Facter.fact(:lldp).value).to eq(nil) } end + context 'lldpd not running' do + before do + allow(File).to receive(:exist?).with('/run/lldpd/lldpd.socket').and_return(false) + allow(Facter::Util::Resolution).to receive(:which).with('lldpctl').and_return('/usr/sbin/lldpctl') + end + + it { expect(Facter.fact(:lldp).value).to eq(nil) } + end + context 'valid run' do before do + allow(File).to receive(:exist?).with('/run/lldpd/lldpd.socket').and_return(true) allow(Facter::Util::Resolution).to receive(:which).with('lldpctl').and_return('/usr/sbin/lldpctl') end