Debian Codegraph @

spec/classes/haproxy_spec.rb

raw
1 # frozen_string_literal: true
2
3 require 'spec_helper'
4
5 describe 'haproxy', type: :class do
6 let(:default_facts) do
7 {
8 concat_basedir: '/dne',
9 networking: {
10 ip: '10.10.10.10'
11 }
12 }
13 end
14
15 describe 'for OS-agnostic configuration' do
16 ['Debian', 'RedHat', 'Archlinux', 'FreeBSD', 'Gentoo'].each do |osfamily|
17 context "when on #{osfamily} family operatingsystems" do
18 let(:facts) do
19 { os: { family: osfamily } }.merge default_facts
20 end
21 let(:params) do
22 {
23 'service_ensure' => 'running',
24 'package_ensure' => 'present',
25 'service_manage' => true
26 }
27 end
28
29 it 'installs the haproxy package' do
30 subject.should contain_package('haproxy').with(
31 'ensure' => 'present',
32 )
33 end
34
35 it 'installs the haproxy service' do
36 subject.should contain_service('haproxy').with(
37 'ensure' => 'running', 'enable' => 'true',
38 'hasrestart' => 'true', 'hasstatus' => 'true'
39 )
40 end
41 end
42
43 context "when on #{osfamily} specifying a package version" do
44 let(:facts) do
45 { os: { family: osfamily } }.merge default_facts
46 end
47 let(:params) do
48 {
49 'service_ensure' => 'running',
50 'package_ensure' => '1.7.9-1',
51 'service_manage' => true
52 }
53 end
54
55 it 'installs the haproxy package in a specific version' do
56 subject.should contain_package('haproxy').with(
57 'ensure' => '1.7.9-1',
58 )
59 end
60
61 it 'installs the haproxy service' do
62 subject.should contain_service('haproxy').with(
63 'ensure' => 'running', 'enable' => 'true',
64 'hasrestart' => 'true', 'hasstatus' => 'true'
65 )
66 end
67 end
68
69 # C9938
70 context "when on #{osfamily} when specifying custom content" do
71 let(:facts) do
72 { os: { family: osfamily } }.merge default_facts
73 end
74 let(:params) do
75 { 'custom_fragment' => "listen stats :9090\n mode http\n stats uri /\n stats auth puppet:puppet\n" }
76 end
77
78 it 'sets the haproxy package' do
79 subject.should contain_concat__fragment('haproxy-haproxy-base').with_content(
80 %r{listen stats :9090\n mode http\n stats uri /\n stats auth puppet:puppet\n},
81 )
82 end
83 end
84 end
85 end
86
87 describe 'for linux operating systems' do
88 ['Debian', 'RedHat', 'Archlinux', 'Gentoo'].each do |osfamily|
89 context "when on #{osfamily} family operatingsystems" do
90 let(:facts) do
91 { os: { family: osfamily } }.merge default_facts
92 end
93
94 it 'sets up /etc/haproxy/haproxy.cfg as a concat resource' do
95 subject.should contain_concat('/etc/haproxy/haproxy.cfg').with(
96 'owner' => '0',
97 'group' => '0',
98 'mode' => '0640',
99 )
100 end
101
102 it 'manages the chroot directory' do
103 subject.should contain_file('/var/lib/haproxy').with(
104 'ensure' => 'directory',
105 'owner' => 'haproxy',
106 'group' => 'haproxy',
107 )
108 end
109
110 it 'contains a header concat fragment' do
111 subject.should contain_concat__fragment('haproxy-00-header').with(
112 'target' => '/etc/haproxy/haproxy.cfg',
113 'order' => '01',
114 'content' => "# This file is managed by Puppet\n",
115 )
116 end
117
118 it 'contains a haproxy-haproxy-base concat fragment' do
119 subject.should contain_concat__fragment('haproxy-haproxy-base').with(
120 'target' => '/etc/haproxy/haproxy.cfg',
121 'order' => '10',
122 )
123 end
124
125 describe 'Base concat fragment contents' do
126 let(:contents) { param_value(catalogue, 'concat::fragment', 'haproxy-haproxy-base', 'content').split("\n") }
127
128 # C9936 C9937
129 it 'contains global and defaults sections' do
130 contents.should include('global')
131 contents.should include('defaults')
132 end
133
134 it 'logs to an ip address for local0' do
135 contents.should be_any do |match|
136 match =~ %r{ log \d+(\.\d+){3} local0}
137 end
138 end
139
140 it 'specifies the default chroot' do
141 contents.should include(' chroot /var/lib/haproxy')
142 end
143
144 it 'specifies the correct user' do
145 contents.should include(' user haproxy')
146 end
147
148 it 'specifies the correct group' do
149 contents.should include(' group haproxy')
150 end
151
152 it 'specifies the correct pidfile' do
153 contents.should include(' pidfile /var/run/haproxy.pid')
154 end
155 end
156 end
157
158 context "when on #{osfamily} family operatingsystems with setting haproxy.cfg location" do
159 let(:facts) do
160 { os: { family: osfamily } }.merge default_facts
161 end
162 let(:params) do
163 {
164 'config_file' => '/tmp/haproxy.cfg'
165 }
166 end
167
168 it 'sets up /tmp/haproxy.cfg as a concat resource' do
169 subject.should contain_concat('/tmp/haproxy.cfg').with(
170 'owner' => '0',
171 'group' => '0',
172 'mode' => '0640',
173 )
174 end
175 end
176
177 context "when on #{osfamily} family operatingsystems without managing the service" do
178 let(:facts) do
179 { os: { family: osfamily } }.merge default_facts
180 end
181 let(:params) do
182 {
183 'service_ensure' => true,
184 'package_ensure' => 'present',
185 'service_manage' => false
186 }
187 end
188
189 it 'installs the haproxy package' do
190 subject.should contain_package('haproxy').with(
191 'ensure' => 'present',
192 )
193 end
194
195 it 'does not manage the haproxy service' do
196 subject.should_not contain_service('haproxy')
197 end
198
199 it 'sets up /etc/haproxy/haproxy.cfg as a concat resource' do
200 subject.should contain_concat('/etc/haproxy/haproxy.cfg').with(
201 'owner' => '0',
202 'group' => '0',
203 'mode' => '0640',
204 )
205 end
206
207 it 'manages the chroot directory' do
208 subject.should contain_file('/var/lib/haproxy').with(
209 'ensure' => 'directory',
210 )
211 end
212
213 it 'contains a header concat fragment' do
214 subject.should contain_concat__fragment('haproxy-00-header').with(
215 'target' => '/etc/haproxy/haproxy.cfg',
216 'order' => '01',
217 'content' => "# This file is managed by Puppet\n",
218 )
219 end
220
221 it 'contains a haproxy-base concat fragment' do
222 subject.should contain_concat__fragment('haproxy-haproxy-base').with(
223 'target' => '/etc/haproxy/haproxy.cfg',
224 'order' => '10',
225 )
226 end
227
228 describe 'Base concat fragment contents' do
229 let(:contents) { param_value(catalogue, 'concat::fragment', 'haproxy-haproxy-base', 'content').split("\n") }
230
231 it 'contains global and defaults sections' do
232 contents.should include('global')
233 contents.should include('defaults')
234 end
235
236 it 'logs to an ip address for local0' do
237 contents.should be_any do |match|
238 match =~ %r{ log \d+(\.\d+){3} local0}
239 end
240 end
241
242 it 'specifies the default chroot' do
243 contents.should include(' chroot /var/lib/haproxy')
244 end
245
246 it 'specifies the correct user' do
247 contents.should include(' user haproxy')
248 end
249
250 it 'specifies the correct group' do
251 contents.should include(' group haproxy')
252 end
253
254 it 'specifies the correct pidfile' do
255 contents.should include(' pidfile /var/run/haproxy.pid')
256 end
257 end
258 end
259
260 context "when on #{osfamily} when specifying a restart_command" do
261 let(:facts) do
262 { os: { family: osfamily } }.merge default_facts
263 end
264 let(:params) do
265 {
266 'restart_command' => '/etc/init.d/haproxy reload',
267 'service_manage' => true
268 }
269 end
270
271 it 'sets the haproxy package' do
272 subject.should contain_service('haproxy').with(
273 'restart' => '/etc/init.d/haproxy reload',
274 )
275 end
276 end
277 end
278 end
279
280 describe 'for freebsd' do
281 context 'when on freebsd family operatingsystems' do
282 let(:facts) do
283 { os: { family: 'FreeBSD' } }.merge default_facts
284 end
285
286 it 'sets up /usr/local/etc/haproxy.conf as a concat resource' do
287 subject.should contain_concat('/usr/local/etc/haproxy.conf').with(
288 'owner' => '0',
289 'group' => '0',
290 'mode' => '0640',
291 )
292 end
293
294 it 'manages the chroot directory' do
295 subject.should contain_file('/usr/local/haproxy').with(
296 'ensure' => 'directory',
297 )
298 end
299
300 it 'contains a header concat fragment' do
301 subject.should contain_concat__fragment('haproxy-00-header').with(
302 'target' => '/usr/local/etc/haproxy.conf',
303 'order' => '01',
304 'content' => "# This file is managed by Puppet\n",
305 )
306 end
307
308 it 'contains a haproxy-base concat fragment' do
309 subject.should contain_concat__fragment('haproxy-haproxy-base').with(
310 'target' => '/usr/local/etc/haproxy.conf',
311 'order' => '10',
312 )
313 end
314
315 describe 'Base concat fragment contents' do
316 let(:contents) { param_value(catalogue, 'concat::fragment', 'haproxy-haproxy-base', 'content').split("\n") }
317
318 # C9936 C9937
319 it 'contains global and defaults sections' do
320 contents.should include('global')
321 contents.should include('defaults')
322 end
323
324 it 'logs to an ip address for local0' do
325 contents.should be_any do |match|
326 match =~ %r{ log \d+(\.\d+){3} local0}
327 end
328 end
329
330 it 'specifies the default chroot' do
331 contents.should include(' chroot /usr/local/haproxy')
332 end
333
334 it 'specifies the correct pidfile' do
335 contents.should include(' pidfile /var/run/haproxy.pid')
336 end
337 end
338 end
339
340 context 'when on freebsd family operatingsystems without managing the service' do
341 let(:facts) do
342 { os: { family: 'FreeBSD' } }.merge default_facts
343 end
344 let(:params) do
345 {
346 'service_ensure' => true,
347 'package_ensure' => 'present',
348 'service_manage' => false
349 }
350 end
351
352 it 'installs the haproxy package' do
353 subject.should contain_package('haproxy').with(
354 'ensure' => 'present',
355 )
356 end
357
358 it 'does not manage the haproxy service' do
359 subject.should_not contain_service('haproxy')
360 end
361
362 it 'sets up /usr/local/etc/haproxy.conf as a concat resource' do
363 subject.should contain_concat('/usr/local/etc/haproxy.conf').with(
364 'owner' => '0',
365 'group' => '0',
366 'mode' => '0640',
367 )
368 end
369
370 it 'manages the chroot directory' do
371 subject.should contain_file('/usr/local/haproxy').with(
372 'ensure' => 'directory',
373 )
374 end
375
376 it 'contains a header concat fragment' do
377 subject.should contain_concat__fragment('haproxy-00-header').with(
378 'target' => '/usr/local/etc/haproxy.conf',
379 'order' => '01',
380 'content' => "# This file is managed by Puppet\n",
381 )
382 end
383
384 it 'contains a haproxy-base concat fragment' do
385 subject.should contain_concat__fragment('haproxy-haproxy-base').with(
386 'target' => '/usr/local/etc/haproxy.conf',
387 'order' => '10',
388 )
389 end
390
391 describe 'Base concat fragment contents' do
392 let(:contents) { param_value(catalogue, 'concat::fragment', 'haproxy-haproxy-base', 'content').split("\n") }
393
394 it 'contains global and defaults sections' do
395 contents.should include('global')
396 contents.should include('defaults')
397 end
398
399 it 'logs to an ip address for local0' do
400 contents.should be_any do |match|
401 match =~ %r{ log \d+(\.\d+){3} local0}
402 end
403 end
404
405 it 'specifies the default chroot' do
406 contents.should include(' chroot /usr/local/haproxy')
407 end
408
409 it 'specifies the correct pidfile' do
410 contents.should include(' pidfile /var/run/haproxy.pid')
411 end
412 end
413 end
414
415 context 'when on freebsd when specifying a restart_command' do
416 let(:facts) do
417 { os: { family: 'FreeBSD' } }.merge default_facts
418 end
419 let(:params) do
420 {
421 'restart_command' => '/usr/local/etc/rc.d/haproxy reload',
422 'service_manage' => true
423 }
424 end
425
426 it 'sets the haproxy package' do
427 subject.should contain_service('haproxy').with(
428 'restart' => '/usr/local/etc/rc.d/haproxy reload',
429 )
430 end
431 end
432 end
433
434 describe 'for OS-specific configuration' do
435 context 'when only on Debian family operatingsystems' do
436 let(:facts) do
437 { os: { family: 'Debian' } }.merge default_facts
438 end
439
440 it 'manages haproxy service defaults' do
441 subject.should contain_file('/etc/default/haproxy')
442 verify_contents(catalogue, '/etc/default/haproxy', ['ENABLED=1'])
443 end
444 end
445
446 context 'when only on Gentoo family operatingsystems' do
447 let(:facts) do
448 { os: { family: 'Gentoo' } }.merge default_facts
449 end
450
451 it 'creates directory /etc/haproxy' do
452 subject.should contain_file('/etc/haproxy').with(
453 'ensure' => 'directory',
454 )
455 end
456 end
457 end
458
459 describe 'when merging global and defaults options with user-supplied overrides and additions' do
460 # For testing the merging functionality we restrict ourselves to
461 # Debian OS family so that we don't have to juggle different sets of
462 # global_options and defaults_options (like for FreeBSD).
463 ['Debian'].each do |osfamily|
464 context "when on #{osfamily} family operatingsystems" do
465 let(:facts) do
466 { os: { family: osfamily } }.merge default_facts
467 end
468 let(:contents) { param_value(catalogue, 'concat::fragment', 'haproxy-haproxy-base', 'content').split("\n") }
469 let(:params) do
470 {
471 'merge_options' => true,
472 'global_options' => {
473 'log-send-hostname' => '',
474 'chroot' => '/srv/haproxy-chroot',
475 'maxconn' => nil,
476 'stats' => [
477 'socket /var/lib/haproxy/admin.sock mode 660 level admin',
478 'timeout 30s',
479 ]
480 },
481 'defaults_options' => {
482 'mode' => 'http',
483 'option' => [
484 'abortonclose',
485 'logasap',
486 'dontlognull',
487 'httplog',
488 'http-server-close',
489 'forwardfor except 127.0.0.1',
490 ],
491 'timeout' => [
492 'connect 5s',
493 'client 1m',
494 'server 1m',
495 'check 7s',
496 ]
497 }
498 }
499 end
500
501 it 'manages a custom chroot directory' do
502 subject.should contain_file('/srv/haproxy-chroot').with(
503 'ensure' => 'directory',
504 'owner' => 'haproxy',
505 'group' => 'haproxy',
506 )
507 end
508
509 it 'contains global and defaults sections' do
510 contents.should include('global')
511 contents.should include('defaults')
512 end
513
514 it 'sends hostname with log in global options' do
515 contents.should include(' log-send-hostname ')
516 end
517
518 it 'enables admin stats and stats timeout in global options' do
519 contents.should include(' stats socket /var/lib/haproxy/admin.sock mode 660 level admin')
520 contents.should include(' stats timeout 30s')
521 end
522
523 it 'logs to an ip address for local0' do
524 contents.should be_any do |match|
525 match =~ %r{ log \d+(\.\d+){3} local0}
526 end
527 end
528
529 it 'specifies the correct user' do
530 contents.should include(' user haproxy')
531 end
532
533 it 'specifies the correct group' do
534 contents.should include(' group haproxy')
535 end
536
537 it 'specifies the correct pidfile' do
538 contents.should include(' pidfile /var/run/haproxy.pid')
539 end
540
541 it 'sets mode http in default options' do
542 contents.should include(' mode http')
543 end
544
545 it 'does not set the global parameter "maxconn"' do
546 contents.should_not include(' maxconn 4000')
547 end
548
549 expected_array_one = [' option abortonclose', ' option logasap', ' option dontlognull',
550 ' option httplog', ' option http-server-close', ' option forwardfor except 127.0.0.1']
551 it 'sets various options in defaults, removing the "redispatch" option' do
552 contents.should_not include(' option redispatch')
553 expected_array_one.each do |expected|
554 contents.should include(expected)
555 end
556 end
557
558 expected_array_two = [' timeout connect 5s', ' timeout check 7s', ' timeout client 1m', ' timeout server 1m']
559 it 'sets timeouts in defaults, removing the "http-request 10s" and "queue 1m" timeout' do
560 contents.should_not include(' timeout http-request 10s')
561 contents.should_not include(' timeout queue 1m')
562 expected_array_two.each do |expected|
563 contents.should include(expected)
564 end
565 end
566 end
567 end
568 end
569
570 describe 'when overriding global and defaults options with user-supplied overrides and additions' do
571 # For testing the merging functionality we restrict ourselves to
572 # Debian OS family so that we don't have to juggle different sets of
573 # global_options and defaults_options (like for FreeBSD).
574 ['Debian'].each do |osfamily|
575 context "when on #{osfamily} family operatingsystems" do
576 let(:facts) do
577 { os: { family: osfamily } }.merge default_facts
578 end
579 let(:contents) { param_value(catalogue, 'concat::fragment', 'haproxy-haproxy-base', 'content').split("\n") }
580 let(:params) do
581 {
582 'merge_options' => false,
583 'global_options' => {
584 'log-send-hostname' => '',
585 'chroot' => '/srv/haproxy-chroot',
586 'stats' => [
587 'socket /var/lib/haproxy/admin.sock mode 660 level admin',
588 'timeout 30s',
589 ]
590 },
591 'defaults_options' => {
592 'mode' => 'http',
593 'option' => [
594 'abortonclose',
595 'logasap',
596 'dontlognull',
597 'httplog',
598 'http-server-close',
599 'forwardfor except 127.0.0.1',
600 ],
601 'timeout' => [
602 'connect 5s',
603 'client 1m',
604 'server 1m',
605 'check 7s',
606 ]
607 }
608 }
609 end
610
611 it 'manages a custom chroot directory' do
612 subject.should contain_file('/srv/haproxy-chroot').with(
613 'ensure' => 'directory',
614 )
615 end
616
617 it 'contains global and defaults sections' do
618 contents.should include('global')
619 contents.should include('defaults')
620 end
621
622 it 'sends hostname with log in global options' do
623 contents.should include(' log-send-hostname ')
624 end
625
626 it 'enables admin stats and stats timeout in global options' do
627 contents.should include(' stats socket /var/lib/haproxy/admin.sock mode 660 level admin')
628 contents.should include(' stats timeout 30s')
629 end
630
631 it 'sets mode http in default options' do
632 contents.should include(' mode http')
633 end
634
635 it 'does not set the global parameter "maxconn"' do
636 contents.should_not include(' maxconn 4000')
637 end
638
639 expected_array_one = [' option abortonclose', ' option logasap', ' option dontlognull', ' option httplog',
640 ' option http-server-close', ' option forwardfor except 127.0.0.1']
641 it 'sets various options in defaults, removing the "redispatch" option' do
642 contents.should_not include(' option redispatch')
643 expected_array_one.each do |expected|
644 contents.should include(expected)
645 end
646 end
647
648 expected_array_two = [' timeout connect 5s', ' timeout check 7s', ' timeout client 1m', ' timeout server 1m']
649 it 'sets timeouts in defaults, removing the "http-request 10s" and "queue 1m" timeout' do
650 contents.should_not include(' timeout http-request 10s')
651 contents.should_not include(' timeout queue 1m')
652 expected_array_two.each do |expected|
653 contents.should include(expected)
654 end
655 end
656 end
657 end
658 end
659
660 context 'when on unsupported operatingsystems' do
661 let(:facts) do
662 { os: { family: 'windows' } }.merge default_facts
663 end
664
665 it do
666 expect {
667 is_expected.to contain_service('haproxy')
668 }.to raise_error(Puppet::Error, %r{operating system is not supported with the haproxy module})
669 end
670 end
671 end