From 31e2d20bc08b9f5ece2554eac197c8ea510b652d Mon Sep 17 00:00:00 2001 From: Robert Waffen Date: Fri, 3 May 2024 15:57:06 +0200 Subject: [PATCH 1/3] feat: Add Facter fact for PuppetDB version The code changes add a new Facter fact called `puppetdb_version` that retrieves the version of PuppetDB installed on the system. It uses the `puppetdb --version` command to fetch the version and returns it as a fact value. --- lib/facter/puppetdb_version.rb | 13 +++++++++ spec/unit/facter/puppetdb_version_spec.rb | 32 +++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 lib/facter/puppetdb_version.rb create mode 100644 spec/unit/facter/puppetdb_version_spec.rb diff --git a/lib/facter/puppetdb_version.rb b/lib/facter/puppetdb_version.rb new file mode 100644 index 00000000..04da0504 --- /dev/null +++ b/lib/facter/puppetdb_version.rb @@ -0,0 +1,13 @@ +Facter.add(:puppetdb_version) do + confine { Facter::Util::Resolution.which('puppetdb') } + + setcode do + output = Facter::Core::Execution.execute('puppetdb --version') + + if output.nil? + nil + else + output.split(':').last.strip + end + end +end diff --git a/spec/unit/facter/puppetdb_version_spec.rb b/spec/unit/facter/puppetdb_version_spec.rb new file mode 100644 index 00000000..bbe7c846 --- /dev/null +++ b/spec/unit/facter/puppetdb_version_spec.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +require 'spec_helper' +require 'facter' + +describe 'puppetdb_version' do + subject(:fact) { Facter.fact(:puppetdb_version) } + + before(:each) do + Facter.clear + end + + it 'should return the correct puppetdb version' do + allow(Facter::Util::Resolution).to receive(:which).with('puppetdb').and_return('/usr/bin/puppetdb') + allow(Facter::Core::Execution).to receive(:execute).with('puppetdb --version').and_return("puppetdb version: 7.18.0\n") + + expect(Facter.fact(:puppetdb_version).value).to eq('7.18.0') + end + + it 'should return nil if puppetdb command is not available' do + allow(Facter::Util::Resolution).to receive(:which).with('puppetdb').and_return(nil) + + expect(Facter.fact(:puppetdb_version).value).to be_nil + end + + it 'should return nil if puppetdb version output is nil' do + allow(Facter::Util::Resolution).to receive(:which).with('puppetdb').and_return('/usr/bin/puppetdb') + allow(Facter::Core::Execution).to receive(:execute).with('puppetdb --version').and_return(nil) + + expect(Facter.fact(:puppetdb_version).value).to be_nil + end +end From 9b2a86417b65a1b622cc6fcd61d13c36e305e0ce Mon Sep 17 00:00:00 2001 From: Robert Waffen Date: Fri, 3 May 2024 16:04:42 +0200 Subject: [PATCH 2/3] fix: use correct wording in unit test --- spec/unit/facter/puppetdb_version_spec.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/unit/facter/puppetdb_version_spec.rb b/spec/unit/facter/puppetdb_version_spec.rb index bbe7c846..b68e1ae1 100644 --- a/spec/unit/facter/puppetdb_version_spec.rb +++ b/spec/unit/facter/puppetdb_version_spec.rb @@ -10,20 +10,20 @@ Facter.clear end - it 'should return the correct puppetdb version' do + it 'returns the correct puppetdb version' do allow(Facter::Util::Resolution).to receive(:which).with('puppetdb').and_return('/usr/bin/puppetdb') allow(Facter::Core::Execution).to receive(:execute).with('puppetdb --version').and_return("puppetdb version: 7.18.0\n") expect(Facter.fact(:puppetdb_version).value).to eq('7.18.0') end - it 'should return nil if puppetdb command is not available' do + it 'returns nil if puppetdb command is not available' do allow(Facter::Util::Resolution).to receive(:which).with('puppetdb').and_return(nil) expect(Facter.fact(:puppetdb_version).value).to be_nil end - it 'should return nil if puppetdb version output is nil' do + it 'returns nil if puppetdb version output is nil' do allow(Facter::Util::Resolution).to receive(:which).with('puppetdb').and_return('/usr/bin/puppetdb') allow(Facter::Core::Execution).to receive(:execute).with('puppetdb --version').and_return(nil) From d2db65469ecf7f5f72505330004b6c80f7263d97 Mon Sep 17 00:00:00 2001 From: Robert Waffen Date: Fri, 3 May 2024 16:07:41 +0200 Subject: [PATCH 3/3] fix: remove unnecessary check on output --- lib/facter/puppetdb_version.rb | 7 +------ spec/unit/facter/puppetdb_version_spec.rb | 7 ------- 2 files changed, 1 insertion(+), 13 deletions(-) diff --git a/lib/facter/puppetdb_version.rb b/lib/facter/puppetdb_version.rb index 04da0504..b04fb26c 100644 --- a/lib/facter/puppetdb_version.rb +++ b/lib/facter/puppetdb_version.rb @@ -3,11 +3,6 @@ setcode do output = Facter::Core::Execution.execute('puppetdb --version') - - if output.nil? - nil - else - output.split(':').last.strip - end + output.split(':').last.strip end end diff --git a/spec/unit/facter/puppetdb_version_spec.rb b/spec/unit/facter/puppetdb_version_spec.rb index b68e1ae1..3c2783fe 100644 --- a/spec/unit/facter/puppetdb_version_spec.rb +++ b/spec/unit/facter/puppetdb_version_spec.rb @@ -22,11 +22,4 @@ expect(Facter.fact(:puppetdb_version).value).to be_nil end - - it 'returns nil if puppetdb version output is nil' do - allow(Facter::Util::Resolution).to receive(:which).with('puppetdb').and_return('/usr/bin/puppetdb') - allow(Facter::Core::Execution).to receive(:execute).with('puppetdb --version').and_return(nil) - - expect(Facter.fact(:puppetdb_version).value).to be_nil - end end