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