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

setcode do
output = Facter::Core::Execution.execute('puppetdb --version')
output.split(':').last.strip
require 'open3'

# check if os is debian/ubuntu and the package is not from puppetlabs
if Facter.value(:osfamily) == 'Debian'
Comment thread
rwaffen marked this conversation as resolved.
Outdated
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'
output, status = Open3.capture2('dpkg-query --showformat=\'${Version}\' --show puppetdb')
Comment thread
rwaffen marked this conversation as resolved.
Outdated
if status.success?
output.strip.split('-').first
else
nil
end
end
else
output, status = Open3.capture2('puppetdb --version')
if status.success?
output.split(':').last.strip
else
nil
end
end
end
end
23 changes: 21 additions & 2 deletions spec/unit/facter/puppetdb_version_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'spec_helper'
require 'facter'
require 'open3'

describe 'puppetdb_version' do
subject(:fact) { Facter.fact(:puppetdb_version) }
Expand All @@ -10,13 +11,31 @@
Facter.clear
end

it 'returns the correct puppetdb version' do
it 'returns a version on non-Debian family with puppetlabs package' 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")
allow(Open3).to receive(:capture2).with('puppetdb --version').and_return("puppetdb version: 7.18.0\n")

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

it 'returns a version on Debian family with non-puppetlabs package' do
allow(Facter::Util::Resolution).to receive(:which).with('puppetdb').and_return('/usr/sbin/puppetdb')
allow(Facter).to receive(:value).with(:osfamily).and_return('Debian')
allow(Facter::Core::Execution).to receive(:execute).with('apt-cache show puppetdb | grep "Maintainer:" | head -1').and_return('Maintainer: Ubuntu Developers')
allow(Open3).to receive(:capture2).with('dpkg-query --showformat=\'${Version}\' --show puppetdb').and_return("6.2.0-5")

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

it 'returns a version on Debian family with puppetlabs package' do
allow(Facter::Util::Resolution).to receive(:which).with('puppetdb').and_return('/usr/sbin/puppetdb')
allow(Facter).to receive(:value).with(:osfamily).and_return('Debian')
allow(Facter::Core::Execution).to receive(:execute).with('apt-cache show puppetdb | grep "Maintainer:" | head -1').and_return('Maintainer: Puppet Labs')
allow(Open3).to receive(:capture2).with('dpkg-query --showformat=\'${Version}\' --show puppetdb').and_return("7.19.0-1jammy")

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

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

Expand Down