Update rabbitmq module
[mirror/dsa-puppet.git] / 3rdparty / modules / rabbitmq / spec / unit / puppet / type / rabbitmq_policy_spec.rb
1 require 'spec_helper'
2 describe Puppet::Type.type(:rabbitmq_policy) do
3   let(:policy) do
4     Puppet::Type.type(:rabbitmq_policy).new(
5       name: 'ha-all@/',
6       pattern: '.*',
7       definition: {
8         'ha-mode' => 'all'
9       }
10     )
11   end
12
13   it 'accepts a valid name' do
14     policy[:name] = 'ha-all@/'
15     expect(policy[:name]).to eq('ha-all@/')
16   end
17
18   it 'requires a name' do
19     expect do
20       Puppet::Type.type(:rabbitmq_policy).new({})
21     end.to raise_error(Puppet::Error, 'Title or name must be provided')
22   end
23
24   it 'fails when name does not have a @' do
25     expect do
26       policy[:name] = 'ha-all'
27     end.to raise_error(Puppet::Error, %r{Valid values match})
28   end
29
30   it 'accepts a valid regex for pattern' do
31     policy[:pattern] = '.*?'
32     expect(policy[:pattern]).to eq('.*?')
33   end
34
35   it 'accepts an empty string for pattern' do
36     policy[:pattern] = ''
37     expect(policy[:pattern]).to eq('')
38   end
39
40   it 'does not accept invalid regex for pattern' do
41     expect do
42       policy[:pattern] = '*'
43     end.to raise_error(Puppet::Error, %r{Invalid regexp})
44   end
45
46   it 'accepts valid value for applyto' do
47     [:all, :exchanges, :queues].each do |v|
48       policy[:applyto] = v
49       expect(policy[:applyto]).to eq(v)
50     end
51   end
52
53   it 'does not accept invalid value for applyto' do
54     expect do
55       policy[:applyto] = 'me'
56     end.to raise_error(Puppet::Error, %r{Invalid value})
57   end
58
59   it 'accepts a valid hash for definition' do
60     definition = { 'ha-mode' => 'all', 'ha-sync-mode' => 'automatic' }
61     policy[:definition] = definition
62     expect(policy[:definition]).to eq(definition)
63   end
64
65   it 'does not accept a string for definition' do
66     expect do
67       policy[:definition] = 'ha-mode'
68     end.to raise_error(Puppet::Error, %r{Invalid definition})
69   end
70
71   it 'does not accept invalid hash for definition' do
72     expect do
73       policy[:definition] = { 'ha-mode' => %w[a b] }
74     end.to raise_error(Puppet::Error, %r{Invalid definition})
75   end
76
77   it 'accepts valid value for priority' do
78     [0, 10, '0', '10'].each do |v|
79       policy[:priority] = v
80       expect(policy[:priority]).to eq(v)
81     end
82   end
83
84   it 'does not accept invalid value for priority' do
85     ['-1', -1, '1.0', 1.0, 'abc', ''].each do |v|
86       expect do
87         policy[:priority] = v
88       end.to raise_error(Puppet::Error, %r{Invalid value})
89     end
90   end
91
92   it 'accepts and convert ha-params for ha-mode exactly' do
93     definition = { 'ha-mode' => 'exactly', 'ha-params' => '2' }
94     policy[:definition] = definition
95     expect(policy[:definition]['ha-params']).to eq(2)
96   end
97
98   it 'does not accept non-numeric ha-params for ha-mode exactly' do
99     definition = { 'ha-mode' => 'exactly', 'ha-params' => 'nonnumeric' }
100     expect do
101       policy[:definition] = definition
102     end.to raise_error(Puppet::Error, %r{Invalid ha-params.*nonnumeric.*exactly})
103   end
104
105   it 'accepts and convert the expires value' do
106     definition = { 'expires' => '1800000' }
107     policy[:definition] = definition
108     expect(policy[:definition]['expires']).to eq(1_800_000)
109   end
110
111   it 'does not accept non-numeric expires value' do
112     definition = { 'expires' => 'future' }
113     expect do
114       policy[:definition] = definition
115     end.to raise_error(Puppet::Error, %r{Invalid expires value.*future})
116   end
117
118   it 'accepts and convert the message-ttl value' do
119     definition = { 'message-ttl' => '1800000' }
120     policy[:definition] = definition
121     expect(policy[:definition]['message-ttl']).to eq(1_800_000)
122   end
123
124   it 'does not accept non-numeric message-ttl value' do
125     definition = { 'message-ttl' => 'future' }
126     expect do
127       policy[:definition] = definition
128     end.to raise_error(Puppet::Error, %r{Invalid message-ttl value.*future})
129   end
130
131   it 'accepts and convert the max-length value' do
132     definition = { 'max-length' => '1800000' }
133     policy[:definition] = definition
134     expect(policy[:definition]['max-length']).to eq(1_800_000)
135   end
136
137   it 'does not accept non-numeric max-length value' do
138     definition = { 'max-length' => 'future' }
139     expect do
140       policy[:definition] = definition
141     end.to raise_error(Puppet::Error, %r{Invalid max-length value.*future})
142   end
143
144   it 'accepts and convert the max-length-bytes value' do
145     definition = { 'max-length-bytes' => '1800000' }
146     policy[:definition] = definition
147     expect(policy[:definition]['max-length-bytes']).to eq(1_800_000)
148   end
149
150   it 'does not accept non-numeric max-length-bytes value' do
151     definition = { 'max-length-bytes' => 'future' }
152     expect do
153       policy[:definition] = definition
154     end.to raise_error(Puppet::Error, %r{Invalid max-length-bytes value.*future})
155   end
156
157   it 'accepts and convert the shards-per-node value' do
158     definition = { 'shards-per-node' => '1800000' }
159     policy[:definition] = definition
160     expect(policy[:definition]['shards-per-node']).to eq(1_800_000)
161   end
162
163   it 'does not accept non-numeric shards-per-node value' do
164     definition = { 'shards-per-node' => 'future' }
165     expect do
166       policy[:definition] = definition
167     end.to raise_error(Puppet::Error, %r{Invalid shards-per-node value.*future})
168   end
169
170   it 'accepts and convert the ha-sync-batch-size value' do
171     definition = { 'ha-sync-batch-size' => '1800000' }
172     policy[:definition] = definition
173     expect(policy[:definition]['ha-sync-batch-size']).to eq(1_800_000)
174   end
175
176   it 'does not accept non-numeric ha-sync-batch-size value' do
177     definition = { 'ha-sync-batch-size' => 'future' }
178     expect do
179       policy[:definition] = definition
180     end.to raise_error(Puppet::Error, %r{Invalid ha-sync-batch-size value.*future})
181   end
182
183   context 'accepts list value in ha-params when ha-mode = nodes' do
184     before do
185       policy[:definition] = definition
186     end
187
188     let(:definition) { { 'ha-mode' => 'nodes', 'ha-params' => ['rabbit@rabbit-01', 'rabbit@rabbit-02'] } }
189
190     it { expect(policy[:definition]['ha-mode']).to eq('nodes') }
191     it { expect(policy[:definition]['ha-params']).to be_a(Array) }
192     it { expect(policy[:definition]['ha-params'][0]).to eq('rabbit@rabbit-01') }
193     it { expect(policy[:definition]['ha-params'][1]).to eq('rabbit@rabbit-02') }
194   end
195
196   it 'does not accept non-list value in ha-params when ha-mode = nodes' do
197     definition = { 'ha-mode' => 'nodes', 'ha-params' => 'this-will-fail' }
198     expect do
199       policy[:definition] = definition
200     end.to raise_error(Puppet::Error, %r{Invalid definition, value this-will-fail for key ha-params is not an array})
201   end
202 end