Skip to content

Commit 17ff4e2

Browse files
committed
Refactor code again:
- use Facter-functions Signed-off-by: Robert Waffen <[email protected]>
1 parent 0faf2ce commit 17ff4e2

File tree

2 files changed

+91
-43
lines changed

2 files changed

+91
-43
lines changed

lib/facter/puppetdb_version.rb

+15-16
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,25 @@
22
confine { Facter::Util::Resolution.which('puppetdb') }
33

44
setcode do
5-
require 'open3'
5+
command = 'puppetdb --version'
6+
splitter = ':'
7+
postion = 'last'
68

7-
# check if os is debian/ubuntu and the package is not from puppetlabs
8-
if Facter.value(:os)('family') == 'Debian'
9+
if Facter.value(:os)['family'] == 'Debian'
910
package_maintainer = Facter::Core::Execution.execute('apt-cache show puppetdb | grep "Maintainer:" | head -1')
11+
1012
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
13+
command = 'dpkg-query --showformat=\'${Version}\' --show puppetdb'
14+
splitter = '-'
15+
postion = 'first'
2416
end
2517
end
18+
19+
begin
20+
output = Facter::Core::Execution.execute(command)
21+
output.split(splitter).send(postion).strip
22+
rescue Facter::Core::Execution::ExecutionFailure
23+
nil
24+
end
2625
end
2726
end
+76-27
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,93 @@
1-
# frozen_string_literal: true
2-
3-
require 'spec_helper'
41
require 'facter'
5-
require 'open3'
62

73
describe 'puppetdb_version' do
8-
subject(:fact) { Facter.fact(:puppetdb_version) }
9-
104
before(:each) do
115
Facter.clear
126
end
137

14-
it 'returns a version on non-Debian family with puppetlabs package' do
15-
allow(Facter::Util::Resolution).to receive(:which).with('puppetdb').and_return('/usr/bin/puppetdb')
16-
allow(Open3).to receive(:capture2).with('puppetdb --version').and_return("puppetdb version: 7.18.0\n")
8+
context 'when puppetdb is available' do
9+
before do
10+
allow(Facter::Util::Resolution).to receive(:which).with('puppetdb').and_return('/usr/bin/puppetdb')
11+
end
1712

18-
expect(Facter.fact(:puppetdb_version).value).to eq('7.18.0')
19-
end
13+
context 'on a Debian-based system' do
14+
before do
15+
allow(Facter).to receive(:value).with(:os).and_return({ 'family' => 'Debian' })
16+
end
2017

21-
it 'returns a version on Debian family with non-puppetlabs package' do
22-
allow(Facter::Util::Resolution).to receive(:which).with('puppetdb').and_return('/usr/sbin/puppetdb')
23-
allow(Facter).to receive(:value).with(:osfamily).and_return('Debian')
24-
allow(Facter::Core::Execution).to receive(:execute).with('apt-cache show puppetdb | grep "Maintainer:" | head -1').and_return('Maintainer: Ubuntu Developers')
25-
allow(Open3).to receive(:capture2).with('dpkg-query --showformat=\'${Version}\' --show puppetdb').and_return("6.2.0-5")
18+
context 'when Puppet Labs is the maintainer' do
19+
before do
20+
allow(Facter::Core::Execution).to receive(:execute)
21+
.with('apt-cache show puppetdb | grep "Maintainer:" | head -1')
22+
.and_return('Maintainer: Puppet Labs')
23+
end
2624

27-
expect(Facter.fact(:puppetdb_version).value).to eq('6.2.0')
28-
end
25+
it 'returns the correct version from puppetdb --version' do
26+
expect(Facter::Core::Execution).to receive(:execute)
27+
.with('puppetdb --version')
28+
.and_return('puppetdb version: 7.19.0')
29+
30+
expect(Facter.fact(:puppetdb_version).value).to eq('7.19.0')
31+
end
32+
33+
it 'returns nil if the command execution fails' do
34+
allow(Facter::Core::Execution).to receive(:execute).with('puppetdb --version').and_raise(Facter::Core::Execution::ExecutionFailure)
35+
36+
expect(Facter.fact(:puppetdb_version).value).to be_nil
37+
end
38+
end
39+
40+
context 'when Puppet Labs is not the maintainer' do
41+
before do
42+
allow(Facter::Core::Execution).to receive(:execute)
43+
.with('apt-cache show puppetdb | grep "Maintainer:" | head -1')
44+
.and_return('Maintainer: Other Maintainer')
45+
end
46+
47+
it 'returns the correct version from dpkg-query' do
48+
expect(Facter::Core::Execution).to receive(:execute)
49+
.with("dpkg-query --showformat='${Version}' --show puppetdb")
50+
.and_return('7.9.0-1ubuntu1')
51+
52+
expect(Facter.fact(:puppetdb_version).value).to eq('7.9.0')
53+
end
54+
55+
it 'returns nil if the command execution fails' do
56+
allow(Facter::Core::Execution).to receive(:execute).with("dpkg-query --showformat='${Version}' --show puppetdb").and_raise(Facter::Core::Execution::ExecutionFailure)
57+
58+
expect(Facter.fact(:puppetdb_version).value).to be_nil
59+
end
60+
end
61+
end
62+
63+
context 'on a non-Debian-based system' do
64+
before do
65+
allow(Facter).to receive(:value).with(:os).and_return({ 'family' => 'RedHat' })
66+
end
67+
68+
it 'returns the correct version from puppetdb --version' do
69+
expect(Facter::Core::Execution).to receive(:execute)
70+
.with('puppetdb --version')
71+
.and_return('puppetdb version: 7.19.0')
72+
73+
expect(Facter.fact(:puppetdb_version).value).to eq('7.19.0')
74+
end
2975

30-
it 'returns a version on Debian family with puppetlabs package' do
31-
allow(Facter::Util::Resolution).to receive(:which).with('puppetdb').and_return('/usr/sbin/puppetdb')
32-
allow(Facter).to receive(:value).with(:osfamily).and_return('Debian')
33-
allow(Facter::Core::Execution).to receive(:execute).with('apt-cache show puppetdb | grep "Maintainer:" | head -1').and_return('Maintainer: Puppet Labs')
34-
allow(Open3).to receive(:capture2).with('dpkg-query --showformat=\'${Version}\' --show puppetdb').and_return("7.19.0-1jammy")
76+
it 'returns nil if the command execution fails' do
77+
allow(Facter::Core::Execution).to receive(:execute).with('puppetdb --version').and_raise(Facter::Core::Execution::ExecutionFailure)
3578

36-
expect(Facter.fact(:puppetdb_version).value).to eq('7.19.0')
79+
expect(Facter.fact(:puppetdb_version).value).to be_nil
80+
end
81+
end
3782
end
3883

39-
it 'returns nil if puppetdb command is not available' do
40-
allow(Facter::Util::Resolution).to receive(:which).with('puppetdb').and_return(nil)
84+
context 'when puppetdb is not available' do
85+
before do
86+
allow(Facter::Util::Resolution).to receive(:which).with('puppetdb').and_return(nil)
87+
end
4188

42-
expect(Facter.fact(:puppetdb_version).value).to be_nil
89+
it 'returns nil' do
90+
expect(Facter.fact(:puppetdb_version).value).to be_nil
91+
end
4392
end
4493
end

0 commit comments

Comments
 (0)