]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/rabbitmq/spec/unit/puppet/provider/rabbitmq_policy/rabbitmqctl_spec.rb
move to puppetlabs rabbitmq module
[dsa-puppet.git] / 3rdparty / modules / rabbitmq / spec / unit / puppet / provider / rabbitmq_policy / rabbitmqctl_spec.rb
1 require 'puppet'
2 require 'mocha'
3
4 RSpec.configure do |config|
5   config.mock_with :mocha
6 end
7
8 describe Puppet::Type.type(:rabbitmq_policy).provider(:rabbitmqctl) do
9
10   let(:resource) do
11     Puppet::Type.type(:rabbitmq_policy).new(
12       :name       => 'ha-all@/',
13       :pattern    => '.*',
14       :definition => {
15         'ha-mode' => 'all'
16       },
17       :provider => described_class.name
18     )
19   end
20
21   let(:provider) { resource.provider }
22
23   after(:each) do
24     described_class.instance_variable_set(:@policies, nil)
25   end
26
27   it 'should accept @ in policy name' do
28     resource = Puppet::Type.type(:rabbitmq_policy).new(
29       :name       => 'ha@home@/',
30       :pattern    => '.*',
31       :definition => {
32         'ha-mode' => 'all'
33       },
34       :provider => described_class.name
35     )
36     provider = described_class.new(resource)
37     provider.should_policy.should == 'ha@home'
38     provider.should_vhost.should == '/'
39   end
40
41   it 'should fail with invalid output from list' do
42     provider.class.expects(:rabbitmqctl).with('list_policies', '-q', '-p', '/').returns 'foobar'
43     expect { provider.exists? }.to raise_error(Puppet::Error, /cannot parse line from list_policies/)
44   end
45
46   it 'should match policies from list (>=3.2.0)' do
47     provider.class.expects(:rabbitmqctl).with('list_policies', '-q', '-p', '/').returns <<-EOT
48 / ha-all all .* {"ha-mode":"all","ha-sync-mode":"automatic"} 0
49 / test exchanges .* {"ha-mode":"all"} 0
50 EOT
51     provider.exists?.should == {
52       :applyto    => 'all',
53       :pattern    => '.*',
54       :priority   => '0',
55       :definition => {
56         'ha-mode'      => 'all',
57         'ha-sync-mode' => 'automatic'}
58       }
59   end
60
61   it 'should match policies from list (<3.2.0)' do
62     provider.class.expects(:rabbitmqctl).with('list_policies', '-q', '-p', '/').returns <<-EOT
63 / ha-all .* {"ha-mode":"all","ha-sync-mode":"automatic"} 0
64 / test .* {"ha-mode":"all"} 0
65 EOT
66     provider.exists?.should == {
67       :applyto    => 'all',
68       :pattern    => '.*',
69       :priority   => '0',
70       :definition => {
71         'ha-mode'      => 'all',
72         'ha-sync-mode' => 'automatic'}
73       }
74   end
75
76   it 'should not match an empty list' do
77     provider.class.expects(:rabbitmqctl).with('list_policies', '-q', '-p', '/').returns ''
78     provider.exists?.should == nil
79   end
80
81   it 'should destroy policy' do
82     provider.expects(:rabbitmqctl).with('clear_policy', '-p', '/', 'ha-all')
83     provider.destroy
84   end
85
86   it 'should only call set_policy once (<3.2.0)' do
87     provider.class.expects(:rabbitmq_version).returns '3.1.0'
88     provider.resource[:priority] = '10'
89     provider.resource[:applyto] = 'exchanges'
90     provider.expects(:rabbitmqctl).with('set_policy',
91       '-p', '/',
92       'ha-all',
93       '.*',
94       '{"ha-mode":"all"}',
95       '10').once
96     provider.priority = '10'
97     provider.applyto = 'exchanges'
98   end
99
100   it 'should only call set_policy once (>=3.2.0)' do
101     provider.class.expects(:rabbitmq_version).returns '3.2.0'
102     provider.resource[:priority] = '10'
103     provider.resource[:applyto] = 'exchanges'
104     provider.expects(:rabbitmqctl).with('set_policy',
105       '-p', '/',
106       '--priority', '10',
107       '--apply-to', 'exchanges',
108       'ha-all',
109       '.*',
110       '{"ha-mode":"all"}').once
111     provider.priority = '10'
112     provider.applyto = 'exchanges'
113   end
114 end