Skip to content

Commit f61724c

Browse files
committed
Refactor rabbitmq_plugins to share common code between exists? and instances
1 parent ac048e0 commit f61724c

File tree

2 files changed

+87
-11
lines changed

2 files changed

+87
-11
lines changed

lib/puppet/provider/rabbitmq_plugin/rabbitmqplugins.rb

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,29 @@
44
Puppet::Type.type(:rabbitmq_plugin).provide(:rabbitmqplugins, parent: Puppet::Provider::RabbitmqCli) do
55
confine feature: :posix
66

7-
def self.instances
8-
plugin_list = run_with_retries do
9-
# Pass in -E for explicitly enabled plugins as well as -e for implicitly enabled plugins
7+
def self.plugin_list
8+
list = run_with_retries do
9+
# Starting in RabbitMQ 3.6.7 pass in -e to list both implicitly and explicitly enabled plugins.
10+
# If you pass in -E instead, then only explicitly enabled plugins are listed.
1011
# Implicitly enabled plugins are those that were enabled as a dependency of another plugin/
1112
# If we do not pass in -e then the order if plugin installation matters within the puppet
1213
# code. Example, if Plugin A depends on Plugin B and we install Plugin B first it will
1314
# implicitly enable Plugin A. Then when we go to run Puppet a second time without the
1415
# -e parameter, we won't see Plugin A as being enabled so we'll try to install it again.
1516
# To preserve idempotency we should get all enabled plugins regardless of implicitly or
1617
# explicitly enabled.
17-
rabbitmqplugins('list', '-E', '-e', '-m')
18+
if Puppet::Util::Package.versioncmp(rabbitmq_version, '3.6.7') >= 0
19+
# also pass in -q to suppress informational messages that break our parsing
20+
rabbitmqplugins('list', '-e', '-m', '-q')
21+
else
22+
rabbitmqplugins('list', '-E', '-m')
23+
end
1824
end
25+
list.split(%r{\n})
26+
end
1927

20-
plugin_list.split(%r{\n}).map do |line|
28+
def self.instances
29+
plugin_list.map do |line|
2130
raise Puppet::Error, "Cannot parse invalid plugins line: #{line}" unless line =~ %r{^(\S+)$}
2231
new(name: Regexp.last_match(1))
2332
end
@@ -40,6 +49,6 @@ def destroy
4049
end
4150

4251
def exists?
43-
run_with_retries { rabbitmqplugins('list', '-E', '-e', '-m') }.split(%r{\n}).include? resource[:name]
52+
self.class.plugin_list.include? resource[:name]
4453
end
4554
end

spec/unit/puppet/provider/rabbitmq_plugin/rabbitmqctl_spec.rb

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,84 @@
99
end
1010
let(:provider) { provider_class.new(resource) }
1111

12-
it 'matches plugins' do
13-
provider.expects(:rabbitmqplugins).with('list', '-E', '-e', '-m').returns("foo\n")
14-
expect(provider.exists?).to eq(true)
15-
end
16-
1712
it 'calls rabbitmqplugins to enable when node not running' do
1813
provider.class.expects(:rabbitmq_running).returns false
1914
provider.expects(:rabbitmqplugins).with('enable', 'foo')
2015
provider.create
2116
end
2217

18+
describe '#instances' do
19+
it 'exists' do
20+
expect(provider_class).to respond_to :instances
21+
end
22+
23+
# rubocop:disable RSpec/MultipleExpectation
24+
it 'retrieves instances' do
25+
provider.class.expects(:plugin_list).returns(%w[foo bar])
26+
instances = provider_class.instances
27+
expect(instances.size).to eq(2)
28+
instances_cmp = instances.map { |prov| { name: prov.get(:name) } }
29+
expect(instances_cmp).to eq(
30+
[
31+
{ name: 'foo' },
32+
{ name: 'bar' }
33+
]
34+
)
35+
end
36+
# rubocop:enable RSpec/MultipleExpectation
37+
38+
it 'raises error on invalid line' do
39+
provider_class.expects(:plugin_list).returns([' '])
40+
expect { provider_class.instances }.to raise_error Puppet::Error, %r{Cannot parse invalid plugins line}
41+
end
42+
end
43+
44+
describe '#plugin_list' do
45+
it 'exists' do
46+
expect(provider_class).to respond_to :instances
47+
end
48+
49+
context 'with RabbitMQ version >=3.6.7' do
50+
it 'returns a list of plugins' do
51+
provider.class.expects(:rabbitmq_version).returns '3.6.7'
52+
provider.class.expects(:rabbitmqplugins).with('list', '-e', '-m', '-q').returns("foo\nbar\nbaz\n")
53+
expect(provider.class.plugin_list).to eq(%w[foo bar baz])
54+
end
55+
56+
it 'handles no training newline properly' do
57+
provider.class.expects(:rabbitmq_version).returns '3.6.7'
58+
provider.class.expects(:rabbitmqplugins).with('list', '-e', '-m', '-q').returns("foo\nbar")
59+
expect(provider.class.plugin_list).to eq(%w[foo bar])
60+
end
61+
end
62+
63+
context 'with RabbitMQ version <3.6.7' do
64+
it 'returns a list of plugins' do
65+
provider.class.expects(:rabbitmq_version).returns '3.6.6'
66+
provider.class.expects(:rabbitmqplugins).with('list', '-E', '-m').returns("foo\nbar\nbaz\n")
67+
expect(provider.class.plugin_list).to eq(%w[foo bar baz])
68+
end
69+
70+
it 'handles no training newline properly' do
71+
provider.class.expects(:rabbitmq_version).returns '3.6.6'
72+
provider.class.expects(:rabbitmqplugins).with('list', '-E', '-m').returns("foo\nbar")
73+
expect(provider.class.plugin_list).to eq(%w[foo bar])
74+
end
75+
end
76+
end
77+
78+
describe '#exists?' do
79+
it 'matches existng plugins' do
80+
provider_class.expects(:plugin_list).returns(%w[foo])
81+
expect(provider.exists?).to eq(true)
82+
end
83+
84+
it 'returns false for missing plugins' do
85+
provider_class.expects(:plugin_list).returns(%w[bar])
86+
expect(provider.exists?).to eq(false)
87+
end
88+
end
89+
2390
context 'with RabbitMQ version >=3.4.0' do
2491
it 'calls rabbitmqplugins to enable' do
2592
provider.class.expects(:rabbitmq_version).returns '3.4.0'

0 commit comments

Comments
 (0)