|
1 |
| -# frozen_string_literal: true |
2 |
| - |
3 |
| -require 'spec_helper' |
4 | 1 | require 'facter'
|
5 |
| -require 'open3' |
6 | 2 |
|
7 | 3 | describe 'puppetdb_version' do
|
8 |
| - subject(:fact) { Facter.fact(:puppetdb_version) } |
9 |
| - |
10 | 4 | before(:each) do
|
11 | 5 | Facter.clear
|
12 | 6 | end
|
13 | 7 |
|
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 |
17 | 12 |
|
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 |
20 | 17 |
|
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 |
26 | 24 |
|
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 |
29 | 75 |
|
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) |
35 | 78 |
|
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 |
37 | 82 | end
|
38 | 83 |
|
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 |
41 | 88 |
|
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 |
43 | 92 | end
|
44 | 93 | end
|
0 commit comments