Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Comment thread
rwaffen marked this conversation as resolved.
Outdated
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
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