Skip to content

Commit

Permalink
feat: Add Facter fact for PuppetDB version
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rwaffen committed May 3, 2024
1 parent 99cbeea commit 31e2d20
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/facter/puppetdb_version.rb
Original file line number Diff line number Diff line change
@@ -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
32 changes: 32 additions & 0 deletions spec/unit/facter/puppetdb_version_spec.rb
Original file line number Diff line number Diff line change
@@ -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

Check failure on line 13 in spec/unit/facter/puppetdb_version_spec.rb

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)

RSpec/ExampleWording: Do not use should when describing your tests. (https://rspec.rubystyle.guide/#should-in-example-docstrings, https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ExampleWording)

Check failure on line 13 in spec/unit/facter/puppetdb_version_spec.rb

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 8.0, Ruby Ver: 3.2)

RSpec/ExampleWording: Do not use should when describing your tests. (https://rspec.rubystyle.guide/#should-in-example-docstrings, https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ExampleWording)
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

Check failure on line 20 in spec/unit/facter/puppetdb_version_spec.rb

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)

RSpec/ExampleWording: Do not use should when describing your tests. (https://rspec.rubystyle.guide/#should-in-example-docstrings, https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ExampleWording)

Check failure on line 20 in spec/unit/facter/puppetdb_version_spec.rb

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 8.0, Ruby Ver: 3.2)

RSpec/ExampleWording: Do not use should when describing your tests. (https://rspec.rubystyle.guide/#should-in-example-docstrings, https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ExampleWording)
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

Check failure on line 26 in spec/unit/facter/puppetdb_version_spec.rb

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)

RSpec/ExampleWording: Do not use should when describing your tests. (https://rspec.rubystyle.guide/#should-in-example-docstrings, https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ExampleWording)

Check failure on line 26 in spec/unit/facter/puppetdb_version_spec.rb

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 8.0, Ruby Ver: 3.2)

RSpec/ExampleWording: Do not use should when describing your tests. (https://rspec.rubystyle.guide/#should-in-example-docstrings, https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ExampleWording)
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

0 comments on commit 31e2d20

Please sign in to comment.