Debian Codegraph @

spec/defines/listen_spec.rb

raw
1 # frozen_string_literal: true
2
3 require 'spec_helper'
4
5 describe 'haproxy::listen' do
6 let(:pre_condition) { 'include haproxy' }
7 let(:title) { 'tyler' }
8 let(:facts) do
9 {
10 networking: {
11 ip: '1.1.1.1'
12 },
13 os: {
14 family: 'RedHat'
15 },
16 concat_basedir: '/dne'
17 }
18 end
19
20 context 'when only one port is provided' do
21 let(:params) do
22 {
23 name: 'croy',
24 ipaddress: '1.1.1.1',
25 ports: '18140'
26 }
27 end
28
29 it {
30 is_expected.to contain_concat__fragment('haproxy-croy_listen_block').with(
31 'order' => '20-croy-00',
32 'target' => '/etc/haproxy/haproxy.cfg',
33 'content' => "\nlisten croy\n bind 1.1.1.1:18140 \n balance roundrobin\n option tcplog\n",
34 )
35 }
36 end
37
38 # C9940
39 context 'when an array of ports is provided' do
40 let(:params) do
41 {
42 name: 'apache',
43 ipaddress: '23.23.23.23',
44 ports: ['80', '443']
45 }
46 end
47
48 it {
49 is_expected.to contain_concat__fragment('haproxy-apache_listen_block').with(
50 'order' => '20-apache-00',
51 'target' => '/etc/haproxy/haproxy.cfg',
52 'content' => "\nlisten apache\n bind 23.23.23.23:80 \n bind 23.23.23.23:443 \n balance roundrobin\n option tcplog\n",
53 )
54 }
55 end
56
57 # C9940
58 context 'when a comma-separated list of ports is provided' do
59 let(:params) do
60 {
61 name: 'apache',
62 ipaddress: '23.23.23.23',
63 ports: '80,443'
64 }
65 end
66
67 it {
68 is_expected.to contain_concat__fragment('haproxy-apache_listen_block').with(
69 'order' => '20-apache-00',
70 'target' => '/etc/haproxy/haproxy.cfg',
71 'content' => "\nlisten apache\n bind 23.23.23.23:80 \n bind 23.23.23.23:443 \n balance roundrobin\n option tcplog\n",
72 )
73 }
74 end
75
76 # C9962
77 context 'when empty list of ports is provided' do
78 let(:params) do
79 {
80 name: 'apache',
81 ipaddress: '23.23.23.23',
82 ports: []
83 }
84 end
85
86 it {
87 is_expected.to contain_concat__fragment('haproxy-apache_listen_block').with(
88 'order' => '20-apache-00',
89 'target' => '/etc/haproxy/haproxy.cfg',
90 'content' => "\nlisten apache\n balance roundrobin\n option tcplog\n",
91 )
92 }
93 end
94
95 # C9963
96 context 'when a port is provided greater than 65535' do
97 let(:params) do
98 {
99 name: 'apache',
100 ipaddress: '23.23.23.23',
101 ports: '80443'
102 }
103 end
104
105 it 'raises error' do
106 expect { catalogue }.to raise_error Puppet::Error, %r{outside of range}
107 end
108 end
109
110 # C9974
111 context 'when an invalid ipv4 address is passed' do
112 let(:params) do
113 {
114 name: 'apache',
115 ipaddress: '2323.23.23',
116 ports: '80'
117 }
118 end
119
120 it 'raises error' do
121 expect { catalogue }.to raise_error Puppet::Error, %r{Invalid IP address}
122 end
123 end
124
125 # C9977
126 context 'when a valid hostname is passed' do
127 let(:params) do
128 {
129 name: 'apache',
130 ipaddress: 'some-hostname',
131 ports: '80'
132 }
133 end
134
135 it {
136 is_expected.to contain_concat__fragment('haproxy-apache_listen_block').with(
137 'order' => '20-apache-00',
138 'target' => '/etc/haproxy/haproxy.cfg',
139 'content' => "\nlisten apache\n bind some-hostname:80 \n balance roundrobin\n option tcplog\n",
140 )
141 }
142 end
143
144 context 'when a * is passed for ip address' do
145 let(:params) do
146 {
147 name: 'apache',
148 ipaddress: '*',
149 ports: '80'
150 }
151 end
152
153 it {
154 is_expected.to contain_concat__fragment('haproxy-apache_listen_block').with(
155 'order' => '20-apache-00',
156 'target' => '/etc/haproxy/haproxy.cfg',
157 'content' => "\nlisten apache\n bind *:80 \n balance roundrobin\n option tcplog\n",
158 )
159 }
160 end
161
162 context 'when a bind parameter hash is passed' do
163 let(:params) do
164 {
165 name: 'apache',
166 bind: { '10.0.0.1:333' => ['ssl', 'crt', 'public.puppetlabs.com'], '192.168.122.1:8082' => [] }
167 }
168 end
169
170 it {
171 is_expected.to contain_concat__fragment('haproxy-apache_listen_block').with(
172 'order' => '20-apache-00',
173 'target' => '/etc/haproxy/haproxy.cfg',
174 'content' => "\nlisten apache\n bind 10.0.0.1:333 ssl crt public.puppetlabs.com\n bind 192.168.122.1:8082 \n balance roundrobin\n option tcplog\n",
175 )
176 }
177 end
178
179 context 'when a ports parameter and a bind parameter are passed' do
180 let(:params) do
181 {
182 name: 'apache',
183 bind: { '192.168.0.1:80' => ['ssl'] },
184 ports: '80'
185 }
186 end
187
188 it 'raises error' do
189 expect { catalogue }.to raise_error Puppet::Error, %r{mutually exclusive}
190 end
191 end
192
193 context 'when an ipaddress parameter and a bind parameter are passed' do
194 let(:params) do
195 {
196 name: 'apache',
197 ipaddress: '1.1.1.1',
198 bind: { '192.168.0.1:80' => ['ssl'] }
199 }
200 end
201
202 it 'raises error' do
203 expect { catalogue }.to raise_error Puppet::Error, %r{mutually exclusive}
204 end
205 end
206
207 context 'when no ipaddress or bind parameter are passed' do
208 let(:params) do
209 {
210 name: 'apache'
211 }
212 end
213
214 it 'raises error' do
215 expect { catalogue }.to raise_error Puppet::Error, %r{is needed}
216 end
217 end
218
219 # C9977
220 context 'when an invalid hostname is passed' do
221 let(:params) do
222 {
223 name: 'apache',
224 ipaddress: '$some_hostname',
225 ports: '80'
226 }
227 end
228
229 it 'raises error' do
230 expect { catalogue }.to raise_error Puppet::Error, %r{Invalid IP address}
231 end
232 end
233
234 # C9974
235 context 'when an invalid ipv6 address is passed' do
236 let(:params) do
237 {
238 name: 'apache',
239 ipaddress: ':::6',
240 ports: '80'
241 }
242 end
243
244 it 'raises error' do
245 expect { catalogue }.to raise_error Puppet::Error, %r{Invalid IP address}
246 end
247 end
248
249 context 'when bind options are provided' do
250 let(:params) do
251 {
252 name: 'apache',
253 ipaddress: '1.1.1.1',
254 ports: '80',
255 bind_options: ['the options', 'go here']
256 }
257 end
258
259 it {
260 is_expected.to contain_concat__fragment('haproxy-apache_listen_block').with(
261 'order' => '20-apache-00',
262 'target' => '/etc/haproxy/haproxy.cfg',
263 'content' => "\nlisten apache\n bind 1.1.1.1:80 the options go here\n balance roundrobin\n option tcplog\n",
264 )
265 }
266 end
267
268 context 'when bind parameter is used without ipaddress parameter' do
269 let(:params) do
270 {
271 name: 'apache',
272 bind: { '1.1.1.1:80' => [] }
273 }
274 end
275
276 it {
277 is_expected.to contain_concat__fragment('haproxy-apache_listen_block').with(
278 'order' => '20-apache-00',
279 'target' => '/etc/haproxy/haproxy.cfg',
280 'content' => "\nlisten apache\n bind 1.1.1.1:80 \n balance roundrobin\n option tcplog\n",
281 )
282 }
283 end
284
285 context 'when bind parameter is used with more complex address constructs' do
286 let(:params) do
287 {
288 name: 'apache',
289 bind: {
290 '1.1.1.1:80' => [],
291 ':443,:8443' => ['ssl', 'crt public.puppetlabs.com', 'no-sslv3'],
292 '2.2.2.2:8000-8010' => ['ssl', 'crt public.puppetlabs.com'],
293 'fd@${FD_APP1}' => [],
294 '/var/run/ssl-frontend.sock' => ['user root', 'mode 600', 'accept-proxy']
295 }
296 }
297 end
298
299 it {
300 is_expected.to contain_concat__fragment('haproxy-apache_listen_block').with(
301 'order' => '20-apache-00',
302 'target' => '/etc/haproxy/haproxy.cfg',
303 'content' => "\nlisten apache\n bind /var/run/ssl-frontend.sock user root mode 600 accept-proxy\n bind :443,:8443 ssl crt public.puppetlabs.com no-sslv3\n bind fd@${FD_APP1} \n bind 1.1.1.1:80 \n bind 2.2.2.2:8000-8010 ssl crt public.puppetlabs.com\n balance roundrobin\n option tcplog\n", # rubocop:disable Layout/LineLength
304 )
305 }
306 end
307
308 context 'when bind parameter is used with ip addresses that sort wrong lexigraphically' do
309 let(:params) do
310 {
311 name: 'apache',
312 bind: {
313 '10.1.3.21:80' => 'name input21',
314 '8.252.206.100:80' => 'name input100',
315 '8.252.206.101:80' => 'name input101',
316 '8.252.206.99:80' => 'name input99',
317 '1.1.1.1:80' => [],
318 ':443,:8443' => ['ssl', 'crt public.puppetlabs.com', 'no-sslv3'],
319 '2.2.2.2:8000-8010' => ['ssl', 'crt public.puppetlabs.com'],
320 'fd@${FD_APP1}' => []
321 }
322 }
323 end
324
325 it {
326 is_expected.to contain_concat__fragment('haproxy-apache_listen_block').with(
327 'order' => '20-apache-00',
328 'target' => '/etc/haproxy/haproxy.cfg',
329 'content' => "\nlisten apache\n bind :443,:8443 ssl crt public.puppetlabs.com no-sslv3\n bind fd@${FD_APP1} \n bind 1.1.1.1:80 \n bind 2.2.2.2:8000-8010 ssl crt public.puppetlabs.com\n bind 8.252.206.99:80 name input99\n bind 8.252.206.100:80 name input100\n bind 8.252.206.101:80 name input101\n bind 10.1.3.21:80 name input21\n balance roundrobin\n option tcplog\n", # rubocop:disable Layout/LineLength
330 )
331 }
332 end
333
334 context 'when listen options are specified as an array of hashes' do
335 let(:params) do
336 {
337 name: 'apache',
338 bind: {
339 '0.0.0.0:48001-48003' => []
340 },
341 mode: 'http',
342 options: [
343 { 'reqadd' => 'X-Forwarded-Proto:\ https' },
344 { 'default_backend' => 'dev00_webapp' },
345 { 'capture request header' => ['X-Forwarded-For len 50', 'Host len 15', 'Referrer len 15'] },
346 { 'acl' => ['dst_dev01 dst_port 48001', 'dst_dev02 dst_port 48002', 'dst_dev03 dst_port 48003'] },
347 { 'use_backend' => ['dev01_webapp if dst_dev01', 'dev02_webapp if dst_dev02', 'dev03_webapp if dst_dev03'] },
348 { 'option' => ['httplog', 'http-server-close', 'forwardfor except 127.0.0.1'] },
349 { 'compression' => 'algo gzip',
350 'bind-process' => 'all' },
351 ]
352 }
353 end
354
355 it {
356 is_expected.to contain_concat__fragment('haproxy-apache_listen_block').with(
357 'order' => '20-apache-00',
358 'target' => '/etc/haproxy/haproxy.cfg',
359 'content' => "\nlisten apache\n bind 0.0.0.0:48001-48003 \n mode http\n reqadd X-Forwarded-Proto:\\ https\n default_backend dev00_webapp\n capture request header X-Forwarded-For len 50\n capture request header Host len 15\n capture request header Referrer len 15\n acl dst_dev01 dst_port 48001\n acl dst_dev02 dst_port 48002\n acl dst_dev03 dst_port 48003\n use_backend dev01_webapp if dst_dev01\n use_backend dev02_webapp if dst_dev02\n use_backend dev03_webapp if dst_dev03\n option httplog\n option http-server-close\n option forwardfor except 127.0.0.1\n bind-process all\n compression algo gzip\n", # rubocop:disable Layout/LineLength
360 )
361 }
362 end
363
364 context 'when listen options are specified with sort_options_alphabetic' do
365 let(:params) do
366 {
367 name: 'apache',
368 bind: {
369 '0.0.0.0:48001-48003' => []
370 },
371 mode: 'http',
372 options: {
373 'reqadd' => 'X-Forwarded-Proto:\ https',
374 'reqidel' => '^X-Forwarded-For:.*',
375 'default_backend' => 'dev00_webapp',
376 'capture request header' => ['X-Forwarded-For len 50', 'Host len 15', 'Referrer len 15'],
377 'acl' => ['dst_dev01 dst_port 48001', 'dst_dev02 dst_port 48002', 'dst_dev03 dst_port 48003'],
378 'use_backend' => ['dev01_webapp if dst_dev01', 'dev02_webapp if dst_dev02', 'dev03_webapp if dst_dev03'],
379 'option' => ['httplog', 'http-server-close', 'forwardfor except 127.0.0.1'],
380 'compression' => 'algo gzip',
381 'bind-process' => 'all'
382 }
383 }
384 end
385
386 it {
387 is_expected.to contain_concat__fragment('haproxy-apache_listen_block').with(
388 'order' => '20-apache-00',
389 'target' => '/etc/haproxy/haproxy.cfg',
390 'content' => "\nlisten apache\n bind 0.0.0.0:48001-48003 \n mode http\n acl dst_dev01 dst_port 48001\n acl dst_dev02 dst_port 48002\n acl dst_dev03 dst_port 48003\n bind-process all\n capture request header X-Forwarded-For len 50\n capture request header Host len 15\n capture request header Referrer len 15\n compression algo gzip\n default_backend dev00_webapp\n option httplog\n option http-server-close\n option forwardfor except 127.0.0.1\n reqadd X-Forwarded-Proto:\\ https\n reqidel ^X-Forwarded-For:.*\n use_backend dev01_webapp if dst_dev01\n use_backend dev02_webapp if dst_dev02\n use_backend dev03_webapp if dst_dev03\n", # rubocop:disable Layout/LineLength
391 )
392 }
393 end
394
395 context 'when listen options are specified without sort_options_alphabetic' do
396 let(:params) do
397 {
398 name: 'apache',
399 bind: {
400 '0.0.0.0:48001-48003' => []
401 },
402 mode: 'http',
403 sort_options_alphabetic: false,
404 options: {
405 'reqadd' => 'X-Forwarded-Proto:\ https',
406 'reqidel' => '^X-Forwarded-For:.*',
407 'default_backend' => 'dev00_webapp',
408 'capture request header' => ['X-Forwarded-For len 50', 'Host len 15', 'Referrer len 15'],
409 'acl' => ['dst_dev01 dst_port 48001', 'dst_dev02 dst_port 48002', 'dst_dev03 dst_port 48003'],
410 'use_backend' => ['dev01_webapp if dst_dev01', 'dev02_webapp if dst_dev02', 'dev03_webapp if dst_dev03'],
411 'option' => ['httpchk', 'httplog', 'http-server-close', 'forwardfor except 127.0.0.1'],
412 'compression' => 'algo gzip',
413 'bind-process' => 'all',
414 'http-check' => ['send hdr Host test.example.com meth GET uri /health', 'expect status 204']
415 }
416 }
417 end
418
419 it {
420 is_expected.to contain_concat__fragment('haproxy-apache_listen_block').with(
421 'order' => '20-apache-00',
422 'target' => '/etc/haproxy/haproxy.cfg',
423 'content' => "\nlisten apache\n bind 0.0.0.0:48001-48003 \n mode http\n acl dst_dev01 dst_port 48001\n acl dst_dev02 dst_port 48002\n acl dst_dev03 dst_port 48003\n bind-process all\n capture request header X-Forwarded-For len 50\n capture request header Host len 15\n capture request header Referrer len 15\n compression algo gzip\n reqidel ^X-Forwarded-For:.*\n reqadd X-Forwarded-Proto:\\ https\n use_backend dev01_webapp if dst_dev01\n use_backend dev02_webapp if dst_dev02\n use_backend dev03_webapp if dst_dev03\n default_backend dev00_webapp\n option httpchk\n option httplog\n option http-server-close\n option forwardfor except 127.0.0.1\n http-check send hdr Host test.example.com meth GET uri /health\n http-check expect status 204\n", # rubocop:disable Layout/LineLength
424 )
425 }
426 end
427
428 context 'when a non-default config file is used' do
429 let(:pre_condition) { 'class { "haproxy": config_file => "/etc/non-default.cfg" }' }
430 let(:params) do
431 {
432 name: 'bar',
433 bind: {
434 '*:5000' => []
435 },
436 options: {
437 'option' => [
438 'tcplog',
439 ],
440 'balance' => 'roundrobin'
441 }
442 }
443 end
444
445 it {
446 is_expected.to contain_concat__fragment('haproxy-bar_listen_block').with(
447 'order' => '20-bar-00',
448 'target' => '/etc/non-default.cfg',
449 'content' => "\nlisten bar\n bind *:5000 \n balance roundrobin\n option tcplog\n",
450 )
451 }
452 end
453 end