bd00c33f99aae43be55d00eb18869fb9dcee0c5e
[mirror/dsa-puppet.git] / 3rdparty / modules / rabbitmq / spec / classes / rabbitmq_spec.rb
1 require 'spec_helper'
2
3 describe 'rabbitmq' do
4   context 'on unsupported distributions' do
5     let(:facts) do
6       {
7         os: { family: 'Unsupported' }
8       }
9     end
10
11     it 'we fail' do
12       expect { catalogue }.to raise_error(Puppet::Error, %r{not supported on an Unsupported})
13     end
14   end
15
16   on_supported_os.each do |os, facts|
17     context "on #{os}" do
18       systemd_facts = os_specific_facts(facts)
19       facts = facts.merge(systemd_facts)
20       let :facts do
21         facts
22       end
23
24       name = case facts[:osfamily]
25              when 'Archlinux', 'OpenBSD', 'FreeBSD'
26                'rabbitmq'
27              else
28                'rabbitmq-server'
29              end
30
31       it { is_expected.to compile.with_all_deps }
32       it { is_expected.to contain_class('rabbitmq::install') }
33       it { is_expected.to contain_class('rabbitmq::config').that_notifies('Class[rabbitmq::service]') }
34       it { is_expected.to contain_class('rabbitmq::service') }
35
36       it { is_expected.to contain_package(name).with_ensure('installed').with_name(name) }
37       if facts[:os]['family'] == 'Suse'
38         it { is_expected.to contain_package('rabbitmq-server-plugins') }
39       end
40
41       context 'with default params' do
42         it { is_expected.not_to contain_class('rabbitmq::repo::apt') }
43         it { is_expected.not_to contain_apt__source('rabbitmq') }
44         it { is_expected.not_to contain_class('rabbitmq::repo::rhel') }
45         it { is_expected.not_to contain_yumrepo('rabbitmq') }
46       end
47
48       context 'with service_restart => false' do
49         let(:params) { { service_restart: false } }
50
51         it { is_expected.not_to contain_class('rabbitmq::config').that_notifies('Class[rabbitmq::service]') }
52       end
53
54       context 'with repos_ensure => true' do
55         let(:params) { { repos_ensure: true } }
56
57         if facts[:os]['family'] == 'Debian'
58           it 'includes rabbitmq::repo::apt' do
59             is_expected.to contain_class('rabbitmq::repo::apt').
60               with_key_source('https://packagecloud.io/gpg.key').
61               with_key_content(nil)
62           end
63
64           it 'adds a repo with default values' do
65             is_expected.to contain_apt__source('rabbitmq').
66               with_ensure('present').
67               with_location("https://packagecloud.io/rabbitmq/rabbitmq-server/#{facts[:os]['name'].downcase}").
68               with_release(nil).
69               with_repos('main')
70           end
71         else
72           it { is_expected.not_to contain_class('rabbitmq::repo::apt') }
73           it { is_expected.not_to contain_apt__souce('rabbitmq') }
74         end
75
76         if facts[:os]['family'] == 'RedHat'
77           it { is_expected.to contain_class('rabbitmq::repo::rhel') }
78
79           it 'the repo should be present, and contain the expected values' do
80             is_expected.to contain_yumrepo('rabbitmq').
81               with_ensure('present').
82               with_baseurl(%r{https://packagecloud.io/rabbitmq/rabbitmq-server/el/\d+/\$basearch$}).
83               with_gpgkey('https://www.rabbitmq.com/rabbitmq-release-signing-key.asc')
84           end
85         else
86           it { is_expected.not_to contain_class('rabbitmq::repo::rhel') }
87           it { is_expected.not_to contain_yumrepo('rabbitmq') }
88         end
89       end
90
91       context 'with no pin', if: facts[:os]['family'] == 'Debian' do
92         let(:params) { { repos_ensure: true, package_apt_pin: '' } }
93
94         describe 'it sets up an apt::source' do
95           it {
96             is_expected.to contain_apt__source('rabbitmq').with(
97               'location'    => "https://packagecloud.io/rabbitmq/rabbitmq-server/#{facts[:os]['name'].downcase}",
98               'repos'       => 'main',
99               'key'         => '{"id"=>"418A7F2FB0E1E6E7EABF6FE8C2E73424D59097AB", "source"=>"https://packagecloud.io/gpg.key", "content"=>:undef}'
100             )
101           }
102         end
103       end
104
105       context 'with pin', if: facts[:os]['family'] == 'Debian' do
106         let(:params) { { repos_ensure: true, package_apt_pin: '700' } }
107
108         describe 'it sets up an apt::source and pin' do
109           it {
110             is_expected.to contain_apt__source('rabbitmq').with(
111               'location'    => "https://packagecloud.io/rabbitmq/rabbitmq-server/#{facts[:os]['name'].downcase}",
112               'repos'       => 'main',
113               'key'         => '{"id"=>"418A7F2FB0E1E6E7EABF6FE8C2E73424D59097AB", "source"=>"https://packagecloud.io/gpg.key", "content"=>:undef}'
114             )
115           }
116
117           it {
118             is_expected.to contain_apt__pin('rabbitmq').with(
119               'packages' => '*',
120               'priority' => '700',
121               'origin'   => 'packagecloud.io'
122             )
123           }
124         end
125       end
126
127       ['infinity', -1, 1234].each do |value|
128         context "with file_limit => '#{value}'" do
129           let(:params) { { file_limit: value } }
130
131           if facts[:os]['family'] == 'RedHat'
132             it do
133               is_expected.to contain_file('/etc/security/limits.d/rabbitmq-server.conf').
134                 with_owner('0').
135                 with_group('0').
136                 with_mode('0644').
137                 that_notifies('Class[Rabbitmq::Service]').
138                 with_content("rabbitmq soft nofile #{value}\nrabbitmq hard nofile #{value}\n")
139             end
140           else
141             it { is_expected.not_to contain_file('/etc/security/limits.d/rabbitmq-server.conf') }
142           end
143
144           if facts[:os]['family'] == 'Debian'
145             it { is_expected.to contain_file('/etc/default/rabbitmq-server').with_content(%r{ulimit -n #{value}}) }
146           else
147             it { is_expected.not_to contain_file('/etc/default/rabbitmq-server') }
148           end
149
150           if facts[:systemd]
151             it do
152               is_expected.to contain_systemd__service_limits("#{name}.service").
153                 with_limits('LimitNOFILE' => value).
154                 with_restart_service(false)
155             end
156           else
157             it { is_expected.not_to contain_systemd__service_limits("#{name}.service") }
158           end
159         end
160       end
161
162       [-42, '-42', 'foo'].each do |value|
163         context "with file_limit => '#{value}'" do
164           let(:params) { { file_limit: value } }
165
166           it 'does not compile' do
167             expect { catalogue }.to raise_error(Puppet::PreformattedError, %r{Error while evaluating a Resource Statement})
168           end
169         end
170       end
171
172       context 'on systems with systemd', if: facts[:systemd] do
173         it do
174           is_expected.to contain_systemd__service_limits("#{name}.service").
175             with_restart_service(false)
176         end
177       end
178
179       context 'on systems without systemd', unless: facts[:systemd] do
180         it { is_expected.not_to contain_systemd__service_limits("#{name}.service") }
181       end
182
183       context 'with admin_enable set to true' do
184         let(:params) { { admin_enable: true, management_ip_address: '1.1.1.1' } }
185
186         context 'with service_manage set to true' do
187           let(:params) { { admin_enable: true, management_ip_address: '1.1.1.1', service_manage: true } }
188
189           context 'with rabbitmqadmin_package set to blub' do
190             let(:params) { { rabbitmqadmin_package: 'blub' } }
191
192             it 'installs a package called blub' do
193               is_expected.to contain_package('rabbitmqadmin').with_name('blub')
194             end
195           end
196           if facts[:os]['family'] == 'Archlinux'
197             it 'installs a package called rabbitmqadmin' do
198               is_expected.to contain_package('rabbitmqadmin').with_name('rabbitmqadmin')
199             end
200           else
201             it 'we enable the admin interface by default' do
202               is_expected.to contain_class('rabbitmq::install::rabbitmqadmin')
203               is_expected.to contain_rabbitmq_plugin('rabbitmq_management').with(
204                 notify: 'Class[Rabbitmq::Service]'
205               )
206               is_expected.to contain_archive('rabbitmqadmin').with_source('http://1.1.1.1:15672/cli/rabbitmqadmin')
207             end
208           end
209           if %w[RedHat Debian SUSE].include?(facts[:os]['family'])
210             it { is_expected.to contain_package('python') }
211           end
212           if %w[FreeBSD OpenBSD].include?(facts[:os]['family'])
213             it { is_expected.to contain_package('python2') }
214           end
215         end
216         context 'with manage_python false' do
217           let(:params) { { manage_python: false } }
218
219           it do
220             is_expected.to contain_class('rabbitmq::install::rabbitmqadmin')
221             is_expected.not_to contain_package('python')
222             is_expected.not_to contain_package('python2')
223           end
224         end
225
226         context 'with $management_ip_address undef and service_manage set to true', unless: facts[:osfamily] == 'Archlinux' do
227           let(:params) { { admin_enable: true, management_ip_address: :undef } }
228
229           it 'we enable the admin interface by default' do
230             is_expected.to contain_class('rabbitmq::install::rabbitmqadmin')
231             is_expected.to contain_rabbitmq_plugin('rabbitmq_management').with(
232               notify: 'Class[Rabbitmq::Service]'
233             )
234             is_expected.to contain_archive('rabbitmqadmin').with_source('http://127.0.0.1:15672/cli/rabbitmqadmin')
235           end
236         end
237         context 'with service_manage set to true, node_ip_address = undef, and default user/pass specified', unless: facts[:osfamily] == 'Archlinux' do
238           let(:params) { { admin_enable: true, default_user: 'foobar', default_pass: 'hunter2', node_ip_address: :undef } }
239
240           it 'we use the correct URL to rabbitmqadmin' do
241             is_expected.to contain_archive('rabbitmqadmin').with(
242               source: 'http://127.0.0.1:15672/cli/rabbitmqadmin',
243               username: 'foobar',
244               password: 'hunter2'
245             )
246           end
247         end
248         context 'with service_manage set to true and default user/pass specified', unless: facts[:osfamily] == 'Archlinux' do
249           let(:params) { { admin_enable: true, default_user: 'foobar', default_pass: 'hunter2', management_ip_address: '1.1.1.1' } }
250
251           it 'we use the correct URL to rabbitmqadmin' do
252             is_expected.to contain_archive('rabbitmqadmin').with(
253               source: 'http://1.1.1.1:15672/cli/rabbitmqadmin',
254               username: 'foobar',
255               password: 'hunter2'
256             )
257           end
258         end
259         context 'with service_manage set to true and archive_options set', unless: facts[:osfamily] == 'Archlinux' do
260           let(:params) do
261             {
262               admin_enable: true,
263               management_ip_address: '1.1.1.1',
264               archive_options: %w[fizz pop]
265             }
266           end
267
268           it 'we use the correct archive_options to rabbitmqadmin' do
269             is_expected.to contain_archive('rabbitmqadmin').with(
270               source: 'http://1.1.1.1:15672/cli/rabbitmqadmin',
271               download_options: %w[fizz pop]
272             )
273           end
274         end
275         context 'with service_manage set to true and management port specified', unless: facts[:osfamily] == 'Archlinux' do
276           # note that the 2.x management port is 55672 not 15672
277           let(:params) { { admin_enable: true, management_port: 55_672, management_ip_address: '1.1.1.1' } }
278
279           it 'we use the correct URL to rabbitmqadmin' do
280             is_expected.to contain_archive('rabbitmqadmin').with(
281               source: 'http://1.1.1.1:55672/cli/rabbitmqadmin',
282               username: 'guest',
283               password: 'guest'
284             )
285           end
286         end
287         context 'with ipv6, service_manage set to true and management port specified', unless: facts[:osfamily] == 'Archlinux' do
288           # note that the 2.x management port is 55672 not 15672
289           let(:params) { { admin_enable: true, management_port: 55_672, management_ip_address: '::1' } }
290
291           it 'we use the correct URL to rabbitmqadmin' do
292             is_expected.to contain_archive('rabbitmqadmin').with(
293               source: 'http://[::1]:55672/cli/rabbitmqadmin',
294               username: 'guest',
295               password: 'guest'
296             )
297           end
298         end
299         context 'with service_manage set to false' do
300           let(:params) { { admin_enable: true, service_manage: false } }
301
302           it 'does nothing' do
303             is_expected.not_to contain_class('rabbitmq::install::rabbitmqadmin')
304             is_expected.not_to contain_rabbitmq_plugin('rabbitmq_management')
305           end
306         end
307       end
308
309       describe 'manages configuration directory correctly' do
310         it {
311           is_expected.to contain_file('/etc/rabbitmq').with(
312             'ensure' => 'directory',
313             'mode'   => '0755'
314           )
315         }
316       end
317
318       describe 'manages configuration file correctly' do
319         it {
320           is_expected.to contain_file('rabbitmq.config').with(
321             'owner' => '0',
322             'group' => 'rabbitmq',
323             'mode'  => '0640'
324           )
325         }
326       end
327
328       describe 'does not contain pre-ranch settings with default config' do
329         it do
330           is_expected.to contain_file('rabbitmq.config'). \
331             without_content(%r{binary,}).                 \
332             without_content(%r{\{packet,        raw\},}). \
333             without_content(%r{\{reuseaddr,     true\},})
334         end
335       end
336
337       describe 'contains pre-ranch settings with config_ranch set to false' do
338         let(:params) { { config_ranch: false } }
339
340         it do
341           is_expected.to contain_file('rabbitmq.config'). \
342             with_content(%r{binary,}).                 \
343             with_content(%r{\{packet,        raw\},}). \
344             with_content(%r{\{reuseaddr,     true\},})
345         end
346       end
347
348       context 'configures config_cluster' do
349         let(:params) do
350           {
351             config_cluster: true,
352             cluster_nodes: ['hare-1', 'hare-2'],
353             cluster_node_type: 'ram',
354             wipe_db_on_cookie_change: false
355           }
356         end
357
358         describe 'with erlang_cookie set' do
359           let(:params) do
360             {
361               config_cluster: true,
362               cluster_nodes: ['hare-1', 'hare-2'],
363               cluster_node_type: 'ram',
364               erlang_cookie: 'TESTCOOKIE',
365               wipe_db_on_cookie_change: true
366             }
367           end
368
369           it 'contains the rabbitmq_erlang_cookie' do
370             is_expected.to contain_rabbitmq_erlang_cookie('/var/lib/rabbitmq/.erlang.cookie')
371           end
372         end
373
374         describe 'with erlang_cookie set but without config_cluster' do
375           let(:params) do
376             {
377               config_cluster: false,
378               erlang_cookie: 'TESTCOOKIE'
379             }
380           end
381
382           it 'contains the rabbitmq_erlang_cookie' do
383             is_expected.to contain_rabbitmq_erlang_cookie('/var/lib/rabbitmq/.erlang.cookie')
384           end
385         end
386
387         describe 'without erlang_cookie and without config_cluster' do
388           let(:params) do
389             {
390               config_cluster: false
391             }
392           end
393
394           it 'contains the rabbitmq_erlang_cookie' do
395             is_expected.not_to contain_rabbitmq_erlang_cookie('/var/lib/rabbitmq/.erlang.cookie')
396           end
397         end
398
399         describe 'and sets appropriate configuration' do
400           let(:params) do
401             {
402               config_cluster: true,
403               cluster_nodes: ['hare-1', 'hare-2'],
404               cluster_node_type: 'ram',
405               erlang_cookie: 'ORIGINAL',
406               wipe_db_on_cookie_change: true
407             }
408           end
409
410           it 'for cluster_nodes' do
411             is_expected.to contain_file('rabbitmq.config').with('content' => %r{cluster_nodes.*\['rabbit@hare-1', 'rabbit@hare-2'\], ram})
412           end
413         end
414       end
415
416       describe 'rabbitmq-env configuration' do
417         context 'with default params' do
418           it 'sets environment variables' do
419             is_expected.to contain_file('rabbitmq-env.config'). \
420               with_content(%r{ERL_INETRC=/etc/rabbitmq/inetrc})
421           end
422         end
423
424         context 'with environment_variables set' do
425           let(:params) do
426             { environment_variables: {
427               'NODE_IP_ADDRESS' => '1.1.1.1',
428               'NODE_PORT'          => '5656',
429               'NODENAME'           => 'HOSTNAME',
430               'SERVICENAME'        => 'RabbitMQ',
431               'CONSOLE_LOG'        => 'RabbitMQ.debug',
432               'CTL_ERL_ARGS'       => 'verbose',
433               'SERVER_ERL_ARGS'    => 'v',
434               'SERVER_START_ARGS'  => 'debug'
435             } }
436           end
437
438           it 'sets environment variables' do
439             is_expected.to contain_file('rabbitmq-env.config'). \
440               with_content(%r{NODE_IP_ADDRESS=1.1.1.1}). \
441               with_content(%r{NODE_PORT=5656}). \
442               with_content(%r{NODENAME=HOSTNAME}). \
443               with_content(%r{SERVICENAME=RabbitMQ}). \
444               with_content(%r{CONSOLE_LOG=RabbitMQ.debug}). \
445               with_content(%r{CTL_ERL_ARGS=verbose}). \
446               with_content(%r{SERVER_ERL_ARGS=v}). \
447               with_content(%r{SERVER_START_ARGS=debug})
448           end
449         end
450       end
451
452       context 'delete_guest_user' do
453         describe 'should do nothing by default' do
454           it { is_expected.not_to contain_rabbitmq_user('guest') }
455         end
456
457         describe 'delete user when delete_guest_user set' do
458           let(:params) { { delete_guest_user: true } }
459
460           it 'removes the user' do
461             is_expected.to contain_rabbitmq_user('guest').with(
462               'ensure'   => 'absent',
463               'provider' => 'rabbitmqctl'
464             )
465           end
466         end
467       end
468
469       context 'configuration setting' do
470         describe 'node_ip_address when set' do
471           let(:params) { { node_ip_address: '172.0.0.1' } }
472
473           it 'sets NODE_IP_ADDRESS to specified value' do
474             is_expected.to contain_file('rabbitmq-env.config').
475               with_content(%r{NODE_IP_ADDRESS=172\.0\.0\.1})
476           end
477         end
478
479         describe 'stomp by default' do
480           it 'does not specify stomp parameters in rabbitmq.config' do
481             is_expected.to contain_file('rabbitmq.config').without('content' => %r{stomp})
482           end
483         end
484         describe 'stomp when set' do
485           let(:params) { { config_stomp: true, stomp_port: 5679 } }
486
487           it 'specifies stomp port in rabbitmq.config' do
488             is_expected.to contain_file('rabbitmq.config').with('content' => %r{rabbitmq_stomp.*tcp_listeners, \[5679\]}m)
489           end
490         end
491         describe 'stomp when set ssl port w/o ssl enabled' do
492           let(:params) { { config_stomp: true, stomp_port: 5679, ssl: false, ssl_stomp_port: 5680 } }
493
494           it 'does not configure ssl_listeners in rabbitmq.config' do
495             is_expected.to contain_file('rabbitmq.config').without('content' => %r{rabbitmq_stomp.*ssl_listeners, \[5680\]}m)
496           end
497         end
498         describe 'stomp when set with ssl' do
499           let(:params) { { config_stomp: true, stomp_port: 5679, ssl: true, ssl_stomp_port: 5680 } }
500
501           it 'specifies stomp port and ssl stomp port in rabbitmq.config' do
502             is_expected.to contain_file('rabbitmq.config').with('content' => %r{rabbitmq_stomp.*tcp_listeners, \[5679\].*ssl_listeners, \[5680\]}m)
503           end
504         end
505       end
506
507       describe 'configuring ldap authentication' do
508         let :params do
509           { config_stomp: true,
510             ldap_auth: true,
511             ldap_server: 'ldap.example.com',
512             ldap_user_dn_pattern: 'ou=users,dc=example,dc=com',
513             ldap_other_bind: 'as_user',
514             ldap_use_ssl: false,
515             ldap_port: 389,
516             ldap_log: true,
517             ldap_config_variables: { 'foo' => 'bar' } }
518         end
519
520         it { is_expected.to contain_rabbitmq_plugin('rabbitmq_auth_backend_ldap') }
521
522         it 'contains ldap parameters' do
523           verify_contents(catalogue, 'rabbitmq.config',
524                           ['[', '  {rabbit, [', '    {auth_backends, [rabbit_auth_backend_internal, rabbit_auth_backend_ldap]},', '  ]}',
525                            '  {rabbitmq_auth_backend_ldap, [', '    {other_bind, as_user},',
526                            '    {servers, ["ldap.example.com"]},',
527                            '    {user_dn_pattern, "ou=users,dc=example,dc=com"},', '    {use_ssl, false},',
528                            '    {port, 389},', '    {foo, bar},', '    {log, true}'])
529         end
530       end
531
532       describe 'configuring ldap authentication' do
533         let :params do
534           { config_stomp: false,
535             ldap_auth: true,
536             ldap_server: 'ldap.example.com',
537             ldap_user_dn_pattern: 'ou=users,dc=example,dc=com',
538             ldap_other_bind: 'as_user',
539             ldap_use_ssl: false,
540             ldap_port: 389,
541             ldap_log: true,
542             ldap_config_variables: { 'foo' => 'bar' } }
543         end
544
545         it { is_expected.to contain_rabbitmq_plugin('rabbitmq_auth_backend_ldap') }
546
547         it 'contains ldap parameters' do
548           verify_contents(catalogue, 'rabbitmq.config',
549                           ['[', '  {rabbit, [', '    {auth_backends, [rabbit_auth_backend_internal, rabbit_auth_backend_ldap]},', '  ]}',
550                            '  {rabbitmq_auth_backend_ldap, [', '    {other_bind, as_user},',
551                            '    {servers, ["ldap.example.com"]},',
552                            '    {user_dn_pattern, "ou=users,dc=example,dc=com"},', '    {use_ssl, false},',
553                            '    {port, 389},', '    {foo, bar},', '    {log, true}'])
554         end
555       end
556
557       describe 'configuring ldap authentication' do
558         let :params do
559           { config_stomp: false,
560             ldap_auth: true,
561             ldap_server: 'ldap.example.com',
562             ldap_other_bind: 'as_user',
563             ldap_use_ssl: false,
564             ldap_port: 389,
565             ldap_log: true,
566             ldap_config_variables: { 'foo' => 'bar' } }
567         end
568
569         it { is_expected.to contain_rabbitmq_plugin('rabbitmq_auth_backend_ldap') }
570
571         it 'does not set user_dn_pattern when none is specified' do
572           verify_contents(catalogue, 'rabbitmq.config',
573                           ['[', '  {rabbit, [', '    {auth_backends, [rabbit_auth_backend_internal, rabbit_auth_backend_ldap]},', '  ]}',
574                            '  {rabbitmq_auth_backend_ldap, [', '    {other_bind, as_user},',
575                            '    {servers, ["ldap.example.com"]},',
576                            '    {use_ssl, false},',
577                            '    {port, 389},', '    {foo, bar},', '    {log, true}'])
578           content = catalogue.resource('file', 'rabbitmq.config').send(:parameters)[:content]
579           expect(content).not_to include 'user_dn_pattern'
580         end
581       end
582
583       describe 'configuring auth_backends' do
584         let :params do
585           { auth_backends: ['{baz, foo}', 'bar'] }
586         end
587
588         it 'contains auth_backends' do
589           verify_contents(catalogue, 'rabbitmq.config',
590                           ['    {auth_backends, [{baz, foo}, bar]},'])
591         end
592       end
593
594       describe 'auth_backends overrides ldap_auth' do
595         let :params do
596           { auth_backends: ['{baz, foo}', 'bar'],
597             ldap_auth: true }
598         end
599
600         it 'contains auth_backends' do
601           verify_contents(catalogue, 'rabbitmq.config',
602                           ['    {auth_backends, [{baz, foo}, bar]},'])
603         end
604       end
605
606       describe 'configuring shovel plugin' do
607         let :params do
608           {
609             config_shovel: true
610           }
611         end
612
613         it { is_expected.to contain_rabbitmq_plugin('rabbitmq_shovel') }
614
615         it { is_expected.to contain_rabbitmq_plugin('rabbitmq_shovel_management') }
616
617         describe 'with admin_enable false' do
618           let :params do
619             {
620               config_shovel: true,
621               admin_enable: false
622             }
623           end
624
625           it { is_expected.not_to contain_rabbitmq_plugin('rabbitmq_shovel_management') }
626         end
627
628         describe 'with static shovels' do
629           let :params do
630             {
631               config_shovel: true,
632               config_shovel_statics: {
633                 'shovel_first' => '{sources,[{broker,"amqp://"}]},
634         {destinations,[{broker,"amqp://site1.example.com"}]},
635         {queue,<<"source_one">>}',
636                 'shovel_second' => '{sources,[{broker,"amqp://"}]},
637         {destinations,[{broker,"amqp://site2.example.com"}]},
638         {queue,<<"source_two">>}'
639               }
640             }
641           end
642
643           it 'generates correct configuration' do
644             verify_contents(catalogue, 'rabbitmq.config', [
645                               '  {rabbitmq_shovel,',
646                               '    [{shovels,[',
647                               '      {shovel_first,[{sources,[{broker,"amqp://"}]},',
648                               '        {destinations,[{broker,"amqp://site1.example.com"}]},',
649                               '        {queue,<<"source_one">>}]},',
650                               '      {shovel_second,[{sources,[{broker,"amqp://"}]},',
651                               '        {destinations,[{broker,"amqp://site2.example.com"}]},',
652                               '        {queue,<<"source_two">>}]}',
653                               '    ]}]}'
654                             ])
655           end
656         end
657       end
658
659       describe 'configuring shovel plugin' do
660         let :params do
661           {
662             config_shovel: true
663           }
664         end
665
666         it { is_expected.to contain_rabbitmq_plugin('rabbitmq_shovel') }
667
668         it { is_expected.to contain_rabbitmq_plugin('rabbitmq_shovel_management') }
669
670         describe 'with admin_enable false' do
671           let :params do
672             {
673               config_shovel: true,
674               admin_enable: false
675             }
676           end
677
678           it { is_expected.not_to contain_rabbitmq_plugin('rabbitmq_shovel_management') }
679         end
680
681         describe 'with static shovels' do
682           let :params do
683             {
684               config_shovel: true,
685               config_shovel_statics: {
686                 'shovel_first' => '{sources,[{broker,"amqp://"}]},
687         {destinations,[{broker,"amqp://site1.example.com"}]},
688         {queue,<<"source_one">>}',
689                 'shovel_second' => '{sources,[{broker,"amqp://"}]},
690         {destinations,[{broker,"amqp://site2.example.com"}]},
691         {queue,<<"source_two">>}'
692               }
693             }
694           end
695
696           it 'generates correct configuration' do
697             verify_contents(catalogue, 'rabbitmq.config', [
698                               '  {rabbitmq_shovel,',
699                               '    [{shovels,[',
700                               '      {shovel_first,[{sources,[{broker,"amqp://"}]},',
701                               '        {destinations,[{broker,"amqp://site1.example.com"}]},',
702                               '        {queue,<<"source_one">>}]},',
703                               '      {shovel_second,[{sources,[{broker,"amqp://"}]},',
704                               '        {destinations,[{broker,"amqp://site2.example.com"}]},',
705                               '        {queue,<<"source_two">>}]}',
706                               '    ]}]}'
707                             ])
708           end
709         end
710       end
711
712       describe 'default_user and default_pass set' do
713         let(:params) { { default_user: 'foo', default_pass: 'bar' } }
714
715         it 'sets default_user and default_pass to specified values' do
716           is_expected.to contain_file('rabbitmq.config').with('content' => %r{default_user, <<"foo">>.*default_pass, <<"bar">>}m)
717         end
718       end
719
720       describe 'interfaces option with no ssl' do
721         let(:params) do
722           { interface: '0.0.0.0' }
723         end
724
725         it 'sets ssl options to specified values' do
726           is_expected.to contain_file('rabbitmq.config').with_content(%r{tcp_listeners, \[\{"0.0.0.0", 5672\}\]})
727         end
728       end
729
730       describe 'ssl options and mangament_ssl false' do
731         let(:params) do
732           { ssl: true,
733             ssl_port: 3141,
734             ssl_cacert: '/path/to/cacert',
735             ssl_cert: '/path/to/cert',
736             ssl_key: '/path/to/key',
737             ssl_secure_renegotiate: true,
738             ssl_reuse_sessions: true,
739             ssl_honor_cipher_order: true,
740             ssl_dhfile: :undef,
741             management_ssl: false,
742             management_port: 13_142 }
743         end
744
745         it 'sets ssl options to specified values' do
746           is_expected.to contain_file('rabbitmq.config').with_content(
747             %r{ssl_listeners, \[3141\]}
748           )
749           is_expected.to contain_file('rabbitmq.config').with_content(
750             %r{ssl_options, \[}
751           )
752           is_expected.to contain_file('rabbitmq.config').with_content(
753             %r{cacertfile,"/path/to/cacert"}
754           )
755           is_expected.to contain_file('rabbitmq.config').with_content(
756             %r{certfile,"/path/to/cert"}
757           )
758           is_expected.to contain_file('rabbitmq.config').with_content(
759             %r{keyfile,"/path/to/key"}
760           )
761           is_expected.to contain_file('rabbitmq.config').with_content(
762             %r{secure_renegotiate,true}
763           )
764           is_expected.to contain_file('rabbitmq.config').with_content(
765             %r{reuse_sessions,true}
766           )
767           is_expected.to contain_file('rabbitmq.config').with_content(
768             %r{honor_cipher_order,true}
769           )
770           is_expected.to contain_file('rabbitmq.config').without_content(
771             %r{dhfile,}
772           )
773         end
774         it 'sets non ssl port for management port' do
775           is_expected.to contain_file('rabbitmq.config').with_content(
776             %r{port, 13142}
777           )
778           is_expected.to contain_file('rabbitmqadmin.conf').with_content(
779             %r{port\s=\s13142}
780           )
781         end
782       end
783
784       describe 'ssl options and mangament_ssl true' do
785         let(:params) do
786           { ssl: true,
787             ssl_port: 3141,
788             ssl_cacert: '/path/to/cacert',
789             ssl_cert: '/path/to/cert',
790             ssl_key: '/path/to/key',
791             ssl_secure_renegotiate: true,
792             ssl_reuse_sessions: true,
793             ssl_honor_cipher_order: true,
794             ssl_dhfile: :undef,
795
796             management_ssl: true,
797             ssl_management_port: 13_141 }
798         end
799
800         it 'sets ssl options to specified values' do
801           is_expected.to contain_file('rabbitmq.config').with_content(
802             %r{ssl_listeners, \[3141\]}
803           )
804           is_expected.to contain_file('rabbitmq.config').with_content(
805             %r{ssl_opts, }
806           )
807           is_expected.to contain_file('rabbitmq.config').with_content(
808             %r{ssl_options, \[}
809           )
810           is_expected.to contain_file('rabbitmq.config').with_content(
811             %r{cacertfile,"/path/to/cacert"}
812           )
813           is_expected.to contain_file('rabbitmq.config').with_content(
814             %r{certfile,"/path/to/cert"}
815           )
816           is_expected.to contain_file('rabbitmq.config').with_content(
817             %r{keyfile,"/path/to/key"}
818           )
819           is_expected.to contain_file('rabbitmq.config').with_content(
820             %r{secure_renegotiate,true}
821           )
822           is_expected.to contain_file('rabbitmq.config').with_content(
823             %r{reuse_sessions,true}
824           )
825           is_expected.to contain_file('rabbitmq.config').with_content(
826             %r{honor_cipher_order,true}
827           )
828           is_expected.to contain_file('rabbitmq.config').without_content(
829             %r{dhfile,}
830           )
831         end
832         it 'sets ssl managment port to specified values' do
833           is_expected.to contain_file('rabbitmq.config').with_content(
834             %r{port, 13141}
835           )
836         end
837         it 'sets ssl options in the rabbitmqadmin.conf' do
838           is_expected.to contain_file('rabbitmqadmin.conf').with_content(
839             %r{ssl_ca_cert_file\s=\s/path/to/cacert}
840           )
841           is_expected.to contain_file('rabbitmqadmin.conf').with_content(
842             %r{ssl_cert_file\s=\s/path/to/cert}
843           )
844           is_expected.to contain_file('rabbitmqadmin.conf').with_content(
845             %r{ssl_key_file\s=\s/path/to/key}
846           )
847           is_expected.to contain_file('rabbitmqadmin.conf').with_content(
848             %r{hostname\s=\s}
849           )
850           is_expected.to contain_file('rabbitmqadmin.conf').with_content(
851             %r{port\s=\s13141}
852           )
853         end
854       end
855
856       describe 'ssl options' do
857         let(:params) do
858           { ssl: true,
859             ssl_port: 3141,
860             ssl_cacert: '/path/to/cacert',
861             ssl_cert: '/path/to/cert',
862             ssl_key: '/path/to/key',
863             ssl_secure_renegotiate: true,
864             ssl_reuse_sessions: true,
865             ssl_honor_cipher_order: true,
866             ssl_dhfile: :undef }
867         end
868
869         it 'sets ssl options to specified values' do
870           is_expected.to contain_file('rabbitmq.config').with_content(
871             %r{ssl_listeners, \[3141\]}
872           )
873           is_expected.to contain_file('rabbitmq.config').with_content(
874             %r{ssl_options, \[}
875           )
876           is_expected.to contain_file('rabbitmq.config').with_content(
877             %r{cacertfile,"/path/to/cacert"}
878           )
879           is_expected.to contain_file('rabbitmq.config').with_content(
880             %r{certfile,"/path/to/cert"}
881           )
882           is_expected.to contain_file('rabbitmq.config').with_content(
883             %r{keyfile,"/path/to/key"}
884           )
885           is_expected.to contain_file('rabbitmq.config').with_content(
886             %r{secure_renegotiate,true}
887           )
888           is_expected.to contain_file('rabbitmq.config').with_content(
889             %r{reuse_sessions,true}
890           )
891           is_expected.to contain_file('rabbitmq.config').with_content(
892             %r{honor_cipher_order,true}
893           )
894           is_expected.to contain_file('rabbitmq.config').without_content(
895             %r{dhfile,}
896           )
897         end
898       end
899
900       describe 'ssl options with ssl_interfaces' do
901         let(:params) do
902           { ssl: true,
903             ssl_port: 3141,
904             ssl_interface: '0.0.0.0',
905             ssl_cacert: '/path/to/cacert',
906             ssl_cert: '/path/to/cert',
907             ssl_key: '/path/to/key' }
908         end
909
910         it 'sets ssl options to specified values' do
911           is_expected.to contain_file('rabbitmq.config').with_content(%r{ssl_listeners, \[\{"0.0.0.0", 3141\}\]})
912           is_expected.to contain_file('rabbitmq.config').with_content(%r{cacertfile,"/path/to/cacert"})
913           is_expected.to contain_file('rabbitmq.config').with_content(%r{certfile,"/path/to/cert"})
914           is_expected.to contain_file('rabbitmq.config').with_content(%r{keyfile,"/path/to/key})
915         end
916       end
917
918       describe 'ssl options with ssl_only' do
919         let(:params) do
920           { ssl: true,
921             ssl_only: true,
922             ssl_port: 3141,
923             ssl_cacert: '/path/to/cacert',
924             ssl_cert: '/path/to/cert',
925             ssl_key: '/path/to/key' }
926         end
927
928         it 'sets ssl options to specified values' do
929           is_expected.to contain_file('rabbitmq.config').with_content(%r{tcp_listeners, \[\]})
930           is_expected.to contain_file('rabbitmq.config').with_content(%r{ssl_listeners, \[3141\]})
931           is_expected.to contain_file('rabbitmq.config').with_content(%r{ssl_options, \[})
932           is_expected.to contain_file('rabbitmq.config').with_content(%r{cacertfile,"/path/to/cacert"})
933           is_expected.to contain_file('rabbitmq.config').with_content(%r{certfile,"/path/to/cert"})
934           is_expected.to contain_file('rabbitmq.config').with_content(%r{keyfile,"/path/to/key})
935         end
936         it 'does not set TCP listener environment defaults' do
937           is_expected.to contain_file('rabbitmq-env.config'). \
938             without_content(%r{NODE_PORT=}). \
939             without_content(%r{NODE_IP_ADDRESS=})
940         end
941       end
942
943       describe 'ssl options with ssl_only and ssl_interfaces' do
944         let(:params) do
945           { ssl: true,
946             ssl_only: true,
947             ssl_port: 3141,
948             ssl_interface: '0.0.0.0',
949             ssl_cacert: '/path/to/cacert',
950             ssl_cert: '/path/to/cert',
951             ssl_key: '/path/to/key' }
952         end
953
954         it 'sets ssl options to specified values' do
955           is_expected.to contain_file('rabbitmq.config').with_content(%r{tcp_listeners, \[\]})
956           is_expected.to contain_file('rabbitmq.config').with_content(%r{ssl_listeners, \[\{"0.0.0.0", 3141\}\]})
957           is_expected.to contain_file('rabbitmq.config').with_content(%r{cacertfile,"/path/to/cacert"})
958           is_expected.to contain_file('rabbitmq.config').with_content(%r{certfile,"/path/to/cert"})
959           is_expected.to contain_file('rabbitmq.config').with_content(%r{keyfile,"/path/to/key})
960         end
961       end
962
963       describe 'ssl options with specific ssl versions' do
964         let(:params) do
965           { ssl: true,
966             ssl_port: 3141,
967             ssl_cacert: '/path/to/cacert',
968             ssl_cert: '/path/to/cert',
969             ssl_key: '/path/to/key',
970             ssl_versions: ['tlsv1.2', 'tlsv1.1'] }
971         end
972
973         it 'sets ssl options to specified values' do
974           is_expected.to contain_file('rabbitmq.config').with_content(%r{ssl_listeners, \[3141\]})
975           is_expected.to contain_file('rabbitmq.config').with_content(%r{ssl_options, \[})
976           is_expected.to contain_file('rabbitmq.config').with_content(%r{cacertfile,"/path/to/cacert"})
977           is_expected.to contain_file('rabbitmq.config').with_content(%r{certfile,"/path/to/cert"})
978           is_expected.to contain_file('rabbitmq.config').with_content(%r{keyfile,"/path/to/key})
979           is_expected.to contain_file('rabbitmq.config').with_content(%r{ssl, \[\{versions, \['tlsv1.1', 'tlsv1.2'\]\}\]})
980           is_expected.to contain_file('rabbitmq.config').with_content(%r{versions, \['tlsv1.1', 'tlsv1.2'\]})
981         end
982       end
983
984       describe 'ssl options with ssl_versions and not ssl' do
985         let(:params) do
986           { ssl: false,
987             ssl_port: 3141,
988             ssl_cacert: '/path/to/cacert',
989             ssl_cert: '/path/to/cert',
990             ssl_key: '/path/to/key',
991             ssl_versions: ['tlsv1.2', 'tlsv1.1'] }
992         end
993
994         it 'fails' do
995           expect { catalogue }.to raise_error(Puppet::Error, %r{\$ssl_versions requires that \$ssl => true})
996         end
997       end
998
999       describe 'ssl options with ssl ciphers' do
1000         let(:params) do
1001           { ssl: true,
1002             ssl_port: 3141,
1003             ssl_cacert: '/path/to/cacert',
1004             ssl_cert: '/path/to/cert',
1005             ssl_key: '/path/to/key',
1006             ssl_ciphers: ['ecdhe_rsa,aes_256_cbc,sha', 'dhe_rsa,aes_256_cbc,sha'] }
1007         end
1008
1009         it 'sets ssl ciphers to specified values' do
1010           is_expected.to contain_file('rabbitmq.config').with_content(%r{ciphers,\[[[:space:]]+{dhe_rsa,aes_256_cbc,sha},[[:space:]]+{ecdhe_rsa,aes_256_cbc,sha}[[:space:]]+\]})
1011         end
1012       end
1013
1014       describe 'ssl admin options with specific ssl versions' do
1015         let(:params) do
1016           { ssl: true,
1017             ssl_management_port: 5926,
1018             ssl_cacert: '/path/to/cacert',
1019             ssl_cert: '/path/to/cert',
1020             ssl_key: '/path/to/key',
1021             ssl_versions: ['tlsv1.2', 'tlsv1.1'],
1022             admin_enable: true }
1023         end
1024
1025         it 'sets admin ssl opts to specified values' do
1026           is_expected.to contain_file('rabbitmq.config').with_content(%r{rabbitmq_management, \[})
1027           is_expected.to contain_file('rabbitmq.config').with_content(%r{listener, \[})
1028           is_expected.to contain_file('rabbitmq.config').with_content(%r{port, 5926\}})
1029           is_expected.to contain_file('rabbitmq.config').with_content(%r{ssl, true\}})
1030           is_expected.to contain_file('rabbitmq.config').with_content(%r{ssl_opts, \[})
1031           is_expected.to contain_file('rabbitmq.config').with_content(%r{cacertfile, "/path/to/cacert"\},})
1032           is_expected.to contain_file('rabbitmq.config').with_content(%r{certfile, "/path/to/cert"\},})
1033           is_expected.to contain_file('rabbitmq.config').with_content(%r{keyfile, "/path/to/key"\}})
1034           is_expected.to contain_file('rabbitmq.config').with_content(%r{,\{versions, \['tlsv1.1', 'tlsv1.2'\]\}})
1035         end
1036       end
1037
1038       describe 'ssl with ssl_dhfile' do
1039         let(:params) do
1040           { ssl: true,
1041             ssl_interface: '0.0.0.0',
1042             ssl_dhfile: '/etc/pki/tls/dh-params.pem' }
1043         end
1044
1045         it { is_expected.to contain_file('rabbitmq.config').with_content(%r{dhfile, "/etc/pki/tls/dh-params\.pem}) }
1046       end
1047
1048       describe 'ssl with ssl_dhfile unset' do
1049         let(:params) do
1050           { ssl: true,
1051             ssl_interface: '0.0.0.0',
1052             ssl_dhfile: :undef }
1053         end
1054
1055         it { is_expected.to contain_file('rabbitmq.config').without_content(%r{dhfile,}) }
1056       end
1057
1058       describe 'ssl with ssl_secure_renegotiate false' do
1059         let(:params) do
1060           { ssl: true,
1061             ssl_interface: '0.0.0.0',
1062             ssl_secure_renegotiate: false }
1063         end
1064
1065         it { is_expected.to contain_file('rabbitmq.config').with_content(%r{secure_renegotiate,false}) }
1066       end
1067
1068       describe 'ssl with ssl_reuse_sessions false' do
1069         let(:params) do
1070           { ssl: true,
1071             ssl_interface: '0.0.0.0',
1072             ssl_reuse_sessions: false }
1073         end
1074
1075         it { is_expected.to contain_file('rabbitmq.config').with_content(%r{reuse_sessions,false}) }
1076       end
1077
1078       describe 'ssl with ssl_honor_cipher_order false' do
1079         let(:params) do
1080           { ssl: true,
1081             ssl_interface: '0.0.0.0',
1082             ssl_honor_cipher_order: false }
1083         end
1084
1085         it { is_expected.to contain_file('rabbitmq.config').with_content(%r{honor_cipher_order,false}) }
1086       end
1087
1088       describe 'ssl admin options' do
1089         let(:params) do
1090           { ssl: true,
1091             ssl_management_port: 3141,
1092             ssl_cacert: '/path/to/cacert',
1093             ssl_cert: '/path/to/cert',
1094             ssl_key: '/path/to/key',
1095             ssl_management_verify: 'verify_peer',
1096             ssl_management_fail_if_no_peer_cert: true,
1097             admin_enable: true }
1098         end
1099
1100         it 'sets rabbitmq_management ssl options to specified values' do
1101           is_expected.to contain_file('rabbitmq.config').with_content(%r{rabbitmq_management, \[})
1102           is_expected.to contain_file('rabbitmq.config').with_content(%r{listener, \[})
1103           is_expected.to contain_file('rabbitmq.config').with_content(%r{port, 3141\}})
1104           is_expected.to contain_file('rabbitmq.config').with_content(%r{ssl, true\}})
1105           is_expected.to contain_file('rabbitmq.config').with_content(%r{ssl_opts, \[})
1106           is_expected.to contain_file('rabbitmq.config').with_content(%r{verify,verify_peer\},})
1107           is_expected.to contain_file('rabbitmq.config').with_content(%r{fail_if_no_peer_cert,true\}})
1108           is_expected.to contain_file('rabbitmq.config').with_content(%r{cacertfile, "/path/to/cacert"\},})
1109           is_expected.to contain_file('rabbitmq.config').with_content(%r{certfile, "/path/to/cert"\},})
1110           is_expected.to contain_file('rabbitmq.config').with_content(%r{keyfile, "/path/to/key"\}})
1111         end
1112       end
1113
1114       describe 'admin without ssl' do
1115         let(:params) do
1116           { ssl: false,
1117             management_port: 3141,
1118             admin_enable: true }
1119         end
1120
1121         it 'sets rabbitmq_management options to specified values' do
1122           is_expected.to contain_file('rabbitmq.config').with_content(%r{rabbitmq_management, \[})
1123           is_expected.to contain_file('rabbitmq.config').with_content(%r{listener, \[})
1124           is_expected.to contain_file('rabbitmq.config').with_content(%r{port, 3141\}})
1125         end
1126       end
1127
1128       describe 'ssl admin options' do
1129         let(:params) do
1130           { ssl: true,
1131             ssl_management_port: 3141,
1132             ssl_cacert: '/path/to/cacert',
1133             ssl_cert: '/path/to/cert',
1134             ssl_key: '/path/to/key',
1135             admin_enable: true }
1136         end
1137
1138         it 'sets rabbitmq_management ssl options to specified values' do
1139           is_expected.to contain_file('rabbitmq.config').with_content(%r{rabbitmq_management, \[})
1140           is_expected.to contain_file('rabbitmq.config').with_content(%r{listener, \[})
1141           is_expected.to contain_file('rabbitmq.config').with_content(%r{port, 3141\},})
1142           is_expected.to contain_file('rabbitmq.config').with_content(%r{ssl, true\},})
1143           is_expected.to contain_file('rabbitmq.config').with_content(%r{ssl_opts, \[})
1144           is_expected.to contain_file('rabbitmq.config').with_content(%r{cacertfile, "/path/to/cacert"\},})
1145           is_expected.to contain_file('rabbitmq.config').with_content(%r{certfile, "/path/to/cert"\},})
1146           is_expected.to contain_file('rabbitmq.config').with_content(%r{keyfile, "/path/to/key"\}})
1147         end
1148       end
1149
1150       describe 'admin without ssl' do
1151         let(:params) do
1152           { ssl: false,
1153             management_port: 3141,
1154             admin_enable: true }
1155         end
1156
1157         it 'sets rabbitmq_management options to specified values' do
1158           is_expected.to contain_file('rabbitmq.config'). \
1159             with_content(%r{\{rabbitmq_management, \[}). \
1160             with_content(%r{\{listener, \[}). \
1161             with_content(%r{\{port, 3141\}})
1162         end
1163       end
1164
1165       describe 'ipv6 enabled' do
1166         let(:params) { { ipv6: true } }
1167
1168         it 'enables resolver inet6 in inetrc' do
1169           is_expected.to contain_file('rabbitmq-inetrc').with_content(%r{{inet6, true}.})
1170         end
1171
1172         context 'without other erl args' do
1173           it 'enables inet6 distribution' do
1174             is_expected.to contain_file('rabbitmq-env.config'). \
1175               with_content(%r{^RABBITMQ_SERVER_ERL_ARGS="-proto_dist inet6_tcp"$}). \
1176               with_content(%r{^RABBITMQ_CTL_ERL_ARGS="-proto_dist inet6_tcp"$})
1177           end
1178         end
1179
1180         context 'with other quoted erl args' do
1181           let(:params) do
1182             { ipv6: true,
1183               environment_variables: { 'RABBITMQ_SERVER_ERL_ARGS' => '"some quoted args"',
1184                                        'RABBITMQ_CTL_ERL_ARGS'    => '"other quoted args"' } }
1185           end
1186
1187           it 'enables inet6 distribution and quote properly' do
1188             is_expected.to contain_file('rabbitmq-env.config'). \
1189               with_content(%r{^RABBITMQ_SERVER_ERL_ARGS="some quoted args -proto_dist inet6_tcp"$}). \
1190               with_content(%r{^RABBITMQ_CTL_ERL_ARGS="other quoted args -proto_dist inet6_tcp"$})
1191           end
1192         end
1193
1194         context 'with other unquoted erl args' do
1195           let(:params) do
1196             { ipv6: true,
1197               environment_variables: { 'RABBITMQ_SERVER_ERL_ARGS' => 'foo',
1198                                        'RABBITMQ_CTL_ERL_ARGS'    => 'bar' } }
1199           end
1200
1201           it 'enables inet6 distribution and quote properly' do
1202             is_expected.to contain_file('rabbitmq-env.config'). \
1203               with_content(%r{^RABBITMQ_SERVER_ERL_ARGS="foo -proto_dist inet6_tcp"$}). \
1204               with_content(%r{^RABBITMQ_CTL_ERL_ARGS="bar -proto_dist inet6_tcp"$})
1205           end
1206         end
1207
1208         context 'with SSL and without other erl args' do
1209           let(:params) do
1210             { ipv6: true,
1211               ssl_erl_dist: true }
1212           end
1213
1214           it 'enables inet6 distribution' do
1215             is_expected.to contain_file('rabbitmq-env.config'). \
1216               with_content(%r{^RABBITMQ_SERVER_ERL_ARGS=" -pa /usr/lib64/erlang/lib/ssl-7.3.3.1/ebin -proto_dist inet6_tls"$}). \
1217               with_content(%r{^RABBITMQ_CTL_ERL_ARGS=" -pa /usr/lib64/erlang/lib/ssl-7.3.3.1/ebin -proto_dist inet6_tls"$})
1218           end
1219         end
1220
1221         context 'with SSL and other quoted erl args' do
1222           let(:params) do
1223             { ipv6: true,
1224               ssl_erl_dist: true,
1225               environment_variables: { 'RABBITMQ_SERVER_ERL_ARGS' => '"some quoted args"',
1226                                        'RABBITMQ_CTL_ERL_ARGS'    => '"other quoted args"' } }
1227           end
1228
1229           it 'enables inet6 distribution and quote properly' do
1230             is_expected.to contain_file('rabbitmq-env.config'). \
1231               with_content(%r{^RABBITMQ_SERVER_ERL_ARGS="some quoted args -pa /usr/lib64/erlang/lib/ssl-7.3.3.1/ebin  -proto_dist inet6_tls"$}). \
1232               with_content(%r{^RABBITMQ_CTL_ERL_ARGS="other quoted args -pa /usr/lib64/erlang/lib/ssl-7.3.3.1/ebin  -proto_dist inet6_tls"$})
1233           end
1234         end
1235
1236         context 'with SSL and with other unquoted erl args' do
1237           let(:params) do
1238             { ipv6: true,
1239               ssl_erl_dist: true,
1240               environment_variables: { 'RABBITMQ_SERVER_ERL_ARGS' => 'foo',
1241                                        'RABBITMQ_CTL_ERL_ARGS'    => 'bar' } }
1242           end
1243
1244           it 'enables inet6 distribution and quote properly' do
1245             is_expected.to contain_file('rabbitmq-env.config'). \
1246               with_content(%r{^RABBITMQ_SERVER_ERL_ARGS="foo -pa /usr/lib64/erlang/lib/ssl-7.3.3.1/ebin  -proto_dist inet6_tls"$}). \
1247               with_content(%r{^RABBITMQ_CTL_ERL_ARGS="bar -pa /usr/lib64/erlang/lib/ssl-7.3.3.1/ebin  -proto_dist inet6_tls"$})
1248           end
1249         end
1250       end
1251
1252       describe 'config_variables options' do
1253         let(:params) do
1254           { config_variables: {
1255             'hipe_compile' => true,
1256             'vm_memory_high_watermark'      => 0.4,
1257             'frame_max'                     => 131_072,
1258             'collect_statistics'            => 'none',
1259             'auth_mechanisms'               => "['PLAIN', 'AMQPLAIN']"
1260           } }
1261         end
1262
1263         it 'sets environment variables' do
1264           is_expected.to contain_file('rabbitmq.config'). \
1265             with_content(%r{\{hipe_compile, true\}}). \
1266             with_content(%r{\{vm_memory_high_watermark, 0.4\}}). \
1267             with_content(%r{\{frame_max, 131072\}}). \
1268             with_content(%r{\{collect_statistics, none\}}). \
1269             with_content(%r{\{auth_mechanisms, \['PLAIN', 'AMQPLAIN'\]\}})
1270         end
1271       end
1272
1273       describe 'config_kernel_variables options' do
1274         let(:params) do
1275           { config_kernel_variables: {
1276             'inet_dist_listen_min' => 9100,
1277             'inet_dist_listen_max' => 9105
1278           } }
1279         end
1280
1281         it 'sets config variables' do
1282           is_expected.to contain_file('rabbitmq.config'). \
1283             with_content(%r{\{inet_dist_listen_min, 9100\}}). \
1284             with_content(%r{\{inet_dist_listen_max, 9105\}})
1285         end
1286       end
1287
1288       describe 'config_management_variables' do
1289         let(:params) do
1290           { config_management_variables: {
1291             'rates_mode' => 'none'
1292           } }
1293         end
1294
1295         it 'sets config variables' do
1296           is_expected.to contain_file('rabbitmq.config'). \
1297             with_content(%r{\{rates_mode, none\}})
1298         end
1299       end
1300
1301       describe 'tcp_keepalive enabled' do
1302         let(:params) { { tcp_keepalive: true } }
1303
1304         it 'sets tcp_listen_options keepalive true' do
1305           is_expected.to contain_file('rabbitmq.config'). \
1306             with_content(%r{\{keepalive,     true\}})
1307         end
1308       end
1309
1310       describe 'tcp_keepalive disabled (default)' do
1311         it 'does not set tcp_listen_options' do
1312           is_expected.to contain_file('rabbitmq.config'). \
1313             without_content(%r{\{keepalive,     true\}})
1314         end
1315       end
1316
1317       describe 'tcp_backlog with default value' do
1318         it 'sets tcp_listen_options backlog to 128' do
1319           is_expected.to contain_file('rabbitmq.config'). \
1320             with_content(%r{\{backlog,       128\}})
1321         end
1322       end
1323
1324       describe 'tcp_backlog with non-default value' do
1325         let(:params) do
1326           { tcp_backlog: 256 }
1327         end
1328
1329         it 'sets tcp_listen_options backlog to 256' do
1330           is_expected.to contain_file('rabbitmq.config'). \
1331             with_content(%r{\{backlog,       256\}})
1332         end
1333       end
1334
1335       describe 'tcp_sndbuf with default value' do
1336         it 'does not set tcp_listen_options sndbuf' do
1337           is_expected.to contain_file('rabbitmq.config'). \
1338             without_content(%r{sndbuf})
1339         end
1340       end
1341
1342       describe 'tcp_sndbuf with non-default value' do
1343         let(:params) do
1344           { tcp_sndbuf: 128 }
1345         end
1346
1347         it 'sets tcp_listen_options sndbuf to 128' do
1348           is_expected.to contain_file('rabbitmq.config'). \
1349             with_content(%r{\{sndbuf,       128\}})
1350         end
1351       end
1352
1353       describe 'tcp_recbuf with default value' do
1354         it 'does not set tcp_listen_options recbuf' do
1355           is_expected.to contain_file('rabbitmq.config'). \
1356             without_content(%r{recbuf})
1357         end
1358       end
1359
1360       describe 'tcp_recbuf with non-default value' do
1361         let(:params) do
1362           { tcp_recbuf: 128 }
1363         end
1364
1365         it 'sets tcp_listen_options recbuf to 128' do
1366           is_expected.to contain_file('rabbitmq.config'). \
1367             with_content(%r{\{recbuf,       128\}})
1368         end
1369       end
1370
1371       describe 'rabbitmq-heartbeat options' do
1372         let(:params) { { heartbeat: 60 } }
1373
1374         it 'sets heartbeat paramter in config file' do
1375           is_expected.to contain_file('rabbitmq.config'). \
1376             with_content(%r{\{heartbeat, 60\}})
1377         end
1378       end
1379
1380       context 'delete_guest_user' do
1381         describe 'should do nothing by default' do
1382           it { is_expected.not_to contain_rabbitmq_user('guest') }
1383         end
1384
1385         describe 'delete user when delete_guest_user set' do
1386           let(:params) { { delete_guest_user: true } }
1387
1388           it 'removes the user' do
1389             is_expected.to contain_rabbitmq_user('guest').with(
1390               'ensure'   => 'absent',
1391               'provider' => 'rabbitmqctl'
1392             )
1393           end
1394         end
1395       end
1396
1397       describe 'rabbitmq-loopback_users by default' do
1398         it 'sets the loopback_users parameter in the config file' do
1399           is_expected.to contain_file('rabbitmq.config'). \
1400             with_content(%r{\{loopback_users, \[<<"guest">>\]\}})
1401         end
1402       end
1403
1404       describe 'rabbitmq-loopback_users allow connections via loopback interfaces' do
1405         let(:params) { { loopback_users: [] } }
1406
1407         it 'sets the loopback_users parameter in the config file' do
1408           is_expected.to contain_file('rabbitmq.config'). \
1409             with_content(%r{\{loopback_users, \[\]\}})
1410         end
1411       end
1412
1413       describe 'rabbitmq-loopback_users allow connections via loopback interfaces to a group of users' do
1414         let(:params) { { loopback_users: %w[user1 user2] } }
1415
1416         it 'sets the loopback_users parameter in the config file' do
1417           is_expected.to contain_file('rabbitmq.config'). \
1418             with_content(%r{\{loopback_users, \[<<\"user1\">>, <<\"user2\">>\]\}})
1419         end
1420       end
1421
1422       ##
1423       ## rabbitmq::service
1424       ##
1425       describe 'service with default params' do
1426         it {
1427           is_expected.to contain_service('rabbitmq-server').with(
1428             'ensure'     => 'running',
1429             'enable'     => 'true',
1430             'hasstatus'  => 'true',
1431             'hasrestart' => 'true',
1432             'name'       => name
1433           )
1434         }
1435       end
1436
1437       context 'on systems with systemd', if: facts[:systemd] do
1438         it do
1439           is_expected.to contain_service('rabbitmq-server').
1440             that_requires('Class[systemd::systemctl::daemon_reload]')
1441         end
1442       end
1443
1444       describe 'service with ensure stopped' do
1445         let :params do
1446           { service_ensure: 'stopped' }
1447         end
1448
1449         it {
1450           is_expected.to contain_service('rabbitmq-server').with(
1451             'ensure'    => 'stopped',
1452             'enable'    => false
1453           )
1454         }
1455       end
1456
1457       describe 'service with service_manage equal to false' do
1458         let :params do
1459           { service_manage: false }
1460         end
1461
1462         it { is_expected.not_to contain_service('rabbitmq-server') }
1463       end
1464     end
1465   end
1466 end