Skip to content

Commit 31e2d20

Browse files
committed
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.
1 parent 99cbeea commit 31e2d20

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

lib/facter/puppetdb_version.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Facter.add(:puppetdb_version) do
2+
confine { Facter::Util::Resolution.which('puppetdb') }
3+
4+
setcode do
5+
output = Facter::Core::Execution.execute('puppetdb --version')
6+
7+
if output.nil?
8+
nil
9+
else
10+
output.split(':').last.strip
11+
end
12+
end
13+
end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
require 'facter'
5+
6+
describe 'puppetdb_version' do
7+
subject(:fact) { Facter.fact(:puppetdb_version) }
8+
9+
before(:each) do
10+
Facter.clear
11+
end
12+
13+
it 'should return the correct puppetdb version' do
14+
allow(Facter::Util::Resolution).to receive(:which).with('puppetdb').and_return('/usr/bin/puppetdb')
15+
allow(Facter::Core::Execution).to receive(:execute).with('puppetdb --version').and_return("puppetdb version: 7.18.0\n")
16+
17+
expect(Facter.fact(:puppetdb_version).value).to eq('7.18.0')
18+
end
19+
20+
it 'should return nil if puppetdb command is not available' do
21+
allow(Facter::Util::Resolution).to receive(:which).with('puppetdb').and_return(nil)
22+
23+
expect(Facter.fact(:puppetdb_version).value).to be_nil
24+
end
25+
26+
it 'should return nil if puppetdb version output is nil' do
27+
allow(Facter::Util::Resolution).to receive(:which).with('puppetdb').and_return('/usr/bin/puppetdb')
28+
allow(Facter::Core::Execution).to receive(:execute).with('puppetdb --version').and_return(nil)
29+
30+
expect(Facter.fact(:puppetdb_version).value).to be_nil
31+
end
32+
end

0 commit comments

Comments
 (0)