Skip to content

Commit bac44f7

Browse files
committed
Extend fact to handle debian packages too
Signed-off-by: Robert Waffen <[email protected]>
1 parent d161fc1 commit bac44f7

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

lib/facter/puppetdb_version.rb

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,26 @@
22
confine { Facter::Util::Resolution.which('puppetdb') }
33

44
setcode do
5-
output = Facter::Core::Execution.execute('puppetdb --version')
6-
output.split(':').last.strip
5+
require 'open3'
6+
7+
# check if os is debian/ubuntu and the package is not from puppetlabs
8+
if Facter.value(:osfamily) == 'Debian'
9+
package_maintainer = Facter::Core::Execution.execute('apt-cache show puppetdb | grep "Maintainer:" | head -1')
10+
unless package_maintainer.include? 'Puppet Labs'
11+
output, status = Open3.capture2('dpkg-query --showformat=\'${Version}\' --show puppetdb')
12+
if status.success?
13+
output.strip.split('-').first
14+
else
15+
nil
16+
end
17+
end
18+
else
19+
output, status = Open3.capture2('puppetdb --version')
20+
if status.success?
21+
output.split(':').last.strip
22+
else
23+
nil
24+
end
25+
end
726
end
827
end

spec/unit/facter/puppetdb_version_spec.rb

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,31 @@
1010
Facter.clear
1111
end
1212

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

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

20+
it 'returns a version on Debian family with non-puppetlabs package' do
21+
allow(Facter::Util::Resolution).to receive(:which).with('puppetdb').and_return('/usr/sbin/puppetdb')
22+
allow(Facter).to receive(:value).with(:osfamily).and_return('Debian')
23+
allow(Facter::Core::Execution).to receive(:execute).with('apt-cache show puppetdb | grep "Maintainer:" | head -1').and_return('Maintainer: Ubuntu Developers')
24+
allow(Open3).to receive(:capture2).with('dpkg-query --showformat=\'${Version}\' --show puppetdb').and_return("6.2.0-5")
25+
26+
expect(Facter.fact(:puppetdb_version).value).to eq('6.2.0')
27+
end
28+
29+
it 'returns a version on Debian family with puppetlabs package' do
30+
allow(Facter::Util::Resolution).to receive(:which).with('puppetdb').and_return('/usr/sbin/puppetdb')
31+
allow(Facter).to receive(:value).with(:osfamily).and_return('Debian')
32+
allow(Facter::Core::Execution).to receive(:execute).with('apt-cache show puppetdb | grep "Maintainer:" | head -1').and_return('Maintainer: Puppet Labs')
33+
allow(Open3).to receive(:capture2).with('dpkg-query --showformat=\'${Version}\' --show puppetdb').and_return("7.19.0-1jammy")
34+
35+
expect(Facter.fact(:puppetdb_version).value).to eq('7.19.0')
36+
end
37+
2038
it 'returns nil if puppetdb command is not available' do
2139
allow(Facter::Util::Resolution).to receive(:which).with('puppetdb').and_return(nil)
2240

0 commit comments

Comments
 (0)