Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
22 changes: 20 additions & 2 deletions lib/facter/puppetdb_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,25 @@
confine { Facter::Util::Resolution.which('puppetdb') }

setcode do
output = Facter::Core::Execution.execute('puppetdb --version')
output.split(':').last.strip
command = 'puppetdb --version'
splitter = ':'
postion = 'last'

if Facter.value(:os)['family'] == 'Debian'
package_maintainer = Facter::Core::Execution.execute('apt-cache show puppetdb | grep "Maintainer:" | head -1')
Comment thread
rwaffen marked this conversation as resolved.
Outdated

unless package_maintainer.include? 'Puppet Labs'
command = 'dpkg-query --showformat=\'${Version}\' --show puppetdb'
splitter = '-'
postion = 'first'
end
end

begin
output = Facter::Core::Execution.execute(command)
output.split(splitter).send(postion).strip
rescue Facter::Core::Execution::ExecutionFailure
nil
end
end
end
92 changes: 80 additions & 12 deletions spec/unit/facter/puppetdb_version_spec.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,93 @@
# 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 '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")
context 'when puppetdb is available' do
before do
allow(Facter::Util::Resolution).to receive(:which).with('puppetdb').and_return('/usr/bin/puppetdb')
end

context 'on a Debian-based system' do
before do
allow(Facter).to receive(:value).with(:os).and_return({ 'family' => 'Debian' })
end

context 'when Puppet Labs is the maintainer' do
before do
allow(Facter::Core::Execution).to receive(:execute)
.with('apt-cache show puppetdb | grep "Maintainer:" | head -1')
.and_return('Maintainer: Puppet Labs')
end

it 'returns the correct version from puppetdb --version' do
expect(Facter::Core::Execution).to receive(:execute)
.with('puppetdb --version')
.and_return('puppetdb version: 7.19.0')

expect(Facter.fact(:puppetdb_version).value).to eq('7.19.0')
end

it 'returns nil if the command execution fails' do
allow(Facter::Core::Execution).to receive(:execute).with('puppetdb --version').and_raise(Facter::Core::Execution::ExecutionFailure)

expect(Facter.fact(:puppetdb_version).value).to be_nil
end
end

context 'when Puppet Labs is not the maintainer' do
before do
allow(Facter::Core::Execution).to receive(:execute)
.with('apt-cache show puppetdb | grep "Maintainer:" | head -1')
.and_return('Maintainer: Other Maintainer')
end

it 'returns the correct version from dpkg-query' do
expect(Facter::Core::Execution).to receive(:execute)
.with("dpkg-query --showformat='${Version}' --show puppetdb")
.and_return('7.9.0-1ubuntu1')

expect(Facter.fact(:puppetdb_version).value).to eq('7.9.0')
end

it 'returns nil if the command execution fails' do
allow(Facter::Core::Execution).to receive(:execute).with("dpkg-query --showformat='${Version}' --show puppetdb").and_raise(Facter::Core::Execution::ExecutionFailure)

expect(Facter.fact(:puppetdb_version).value).to be_nil
end
end
end

context 'on a non-Debian-based system' do
before do
allow(Facter).to receive(:value).with(:os).and_return({ 'family' => 'RedHat' })
end

it 'returns the correct version from puppetdb --version' do
expect(Facter::Core::Execution).to receive(:execute)
.with('puppetdb --version')
.and_return('puppetdb version: 7.19.0')

expect(Facter.fact(:puppetdb_version).value).to eq('7.19.0')
end

it 'returns nil if the command execution fails' do
allow(Facter::Core::Execution).to receive(:execute).with('puppetdb --version').and_raise(Facter::Core::Execution::ExecutionFailure)

expect(Facter.fact(:puppetdb_version).value).to eq('7.18.0')
expect(Facter.fact(:puppetdb_version).value).to be_nil
end
end
end

it 'returns nil if puppetdb command is not available' do
allow(Facter::Util::Resolution).to receive(:which).with('puppetdb').and_return(nil)
context 'when puppetdb is not available' do
before do
allow(Facter::Util::Resolution).to receive(:which).with('puppetdb').and_return(nil)
end

expect(Facter.fact(:puppetdb_version).value).to be_nil
it 'returns nil' do
expect(Facter.fact(:puppetdb_version).value).to be_nil
end
end
end