File: listen_spec.rb

package info (click to toggle)
puppet-module-puppetlabs-haproxy 8.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 816 kB
  • sloc: ruby: 3,979; sh: 14; makefile: 4
file content (453 lines) | stat: -rw-r--r-- 15,451 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# frozen_string_literal: true

require 'spec_helper'

describe 'haproxy::listen' do
  let(:pre_condition) { 'include haproxy' }
  let(:title) { 'tyler' }
  let(:facts) do
    {
      networking: {
        ip: '1.1.1.1'
      },
      os: {
        family: 'RedHat'
      },
      concat_basedir: '/dne'
    }
  end

  context 'when only one port is provided' do
    let(:params) do
      {
        name: 'croy',
        ipaddress: '1.1.1.1',
        ports: '18140'
      }
    end

    it {
      is_expected.to contain_concat__fragment('haproxy-croy_listen_block').with(
        'order' => '20-croy-00',
        'target' => '/etc/haproxy/haproxy.cfg',
        'content' => "\nlisten croy\n  bind 1.1.1.1:18140 \n  balance roundrobin\n  option tcplog\n",
      )
    }
  end

  # C9940
  context 'when an array of ports is provided' do
    let(:params) do
      {
        name: 'apache',
        ipaddress: '23.23.23.23',
        ports: ['80', '443']
      }
    end

    it {
      is_expected.to contain_concat__fragment('haproxy-apache_listen_block').with(
        'order' => '20-apache-00',
        'target' => '/etc/haproxy/haproxy.cfg',
        'content' => "\nlisten apache\n  bind 23.23.23.23:80 \n  bind 23.23.23.23:443 \n  balance roundrobin\n  option tcplog\n",
      )
    }
  end

  # C9940
  context 'when a comma-separated list of ports is provided' do
    let(:params) do
      {
        name: 'apache',
        ipaddress: '23.23.23.23',
        ports: '80,443'
      }
    end

    it {
      is_expected.to contain_concat__fragment('haproxy-apache_listen_block').with(
        'order' => '20-apache-00',
        'target' => '/etc/haproxy/haproxy.cfg',
        'content' => "\nlisten apache\n  bind 23.23.23.23:80 \n  bind 23.23.23.23:443 \n  balance roundrobin\n  option tcplog\n",
      )
    }
  end

  # C9962
  context 'when empty list of ports is provided' do
    let(:params) do
      {
        name: 'apache',
        ipaddress: '23.23.23.23',
        ports: []
      }
    end

    it {
      is_expected.to contain_concat__fragment('haproxy-apache_listen_block').with(
        'order' => '20-apache-00',
        'target' => '/etc/haproxy/haproxy.cfg',
        'content' => "\nlisten apache\n  balance roundrobin\n  option tcplog\n",
      )
    }
  end

  # C9963
  context 'when a port is provided greater than 65535' do
    let(:params) do
      {
        name: 'apache',
        ipaddress: '23.23.23.23',
        ports: '80443'
      }
    end

    it 'raises error' do
      expect { catalogue }.to raise_error Puppet::Error, %r{outside of range}
    end
  end

  # C9974
  context 'when an invalid ipv4 address is passed' do
    let(:params) do
      {
        name: 'apache',
        ipaddress: '2323.23.23',
        ports: '80'
      }
    end

    it 'raises error' do
      expect { catalogue }.to raise_error Puppet::Error, %r{Invalid IP address}
    end
  end

  # C9977
  context 'when a valid hostname is passed' do
    let(:params) do
      {
        name: 'apache',
        ipaddress: 'some-hostname',
        ports: '80'
      }
    end

    it {
      is_expected.to contain_concat__fragment('haproxy-apache_listen_block').with(
        'order' => '20-apache-00',
        'target' => '/etc/haproxy/haproxy.cfg',
        'content' => "\nlisten apache\n  bind some-hostname:80 \n  balance roundrobin\n  option tcplog\n",
      )
    }
  end

  context 'when a * is passed for ip address' do
    let(:params) do
      {
        name: 'apache',
        ipaddress: '*',
        ports: '80'
      }
    end

    it {
      is_expected.to contain_concat__fragment('haproxy-apache_listen_block').with(
        'order' => '20-apache-00',
        'target' => '/etc/haproxy/haproxy.cfg',
        'content' => "\nlisten apache\n  bind *:80 \n  balance roundrobin\n  option tcplog\n",
      )
    }
  end

  context 'when a bind parameter hash is passed' do
    let(:params) do
      {
        name: 'apache',
        bind: { '10.0.0.1:333' => ['ssl', 'crt', 'public.puppetlabs.com'], '192.168.122.1:8082' => [] }
      }
    end

    it {
      is_expected.to contain_concat__fragment('haproxy-apache_listen_block').with(
        'order' => '20-apache-00',
        'target' => '/etc/haproxy/haproxy.cfg',
        '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",
      )
    }
  end

  context 'when a ports parameter and a bind parameter are passed' do
    let(:params) do
      {
        name: 'apache',
        bind: { '192.168.0.1:80' => ['ssl'] },
        ports: '80'
      }
    end

    it 'raises error' do
      expect { catalogue }.to raise_error Puppet::Error, %r{mutually exclusive}
    end
  end

  context 'when an ipaddress parameter and a bind parameter are passed' do
    let(:params) do
      {
        name: 'apache',
        ipaddress: '1.1.1.1',
        bind: { '192.168.0.1:80' => ['ssl'] }
      }
    end

    it 'raises error' do
      expect { catalogue }.to raise_error Puppet::Error, %r{mutually exclusive}
    end
  end

  context 'when no ipaddress or bind parameter are passed' do
    let(:params) do
      {
        name: 'apache'
      }
    end

    it 'raises error' do
      expect { catalogue }.to raise_error Puppet::Error, %r{is needed}
    end
  end

  # C9977
  context 'when an invalid hostname is passed' do
    let(:params) do
      {
        name: 'apache',
        ipaddress: '$some_hostname',
        ports: '80'
      }
    end

    it 'raises error' do
      expect { catalogue }.to raise_error Puppet::Error, %r{Invalid IP address}
    end
  end

  # C9974
  context 'when an invalid ipv6 address is passed' do
    let(:params) do
      {
        name: 'apache',
        ipaddress: ':::6',
        ports: '80'
      }
    end

    it 'raises error' do
      expect { catalogue }.to raise_error Puppet::Error, %r{Invalid IP address}
    end
  end

  context 'when bind options are provided' do
    let(:params) do
      {
        name: 'apache',
        ipaddress: '1.1.1.1',
        ports: '80',
        bind_options: ['the options', 'go here']
      }
    end

    it {
      is_expected.to contain_concat__fragment('haproxy-apache_listen_block').with(
        'order' => '20-apache-00',
        'target' => '/etc/haproxy/haproxy.cfg',
        'content' => "\nlisten apache\n  bind 1.1.1.1:80 the options go here\n  balance roundrobin\n  option tcplog\n",
      )
    }
  end

  context 'when bind parameter is used without ipaddress parameter' do
    let(:params) do
      {
        name: 'apache',
        bind: { '1.1.1.1:80' => [] }
      }
    end

    it {
      is_expected.to contain_concat__fragment('haproxy-apache_listen_block').with(
        'order' => '20-apache-00',
        'target' => '/etc/haproxy/haproxy.cfg',
        'content' => "\nlisten apache\n  bind 1.1.1.1:80 \n  balance roundrobin\n  option tcplog\n",
      )
    }
  end

  context 'when bind parameter is used with more complex address constructs' do
    let(:params) do
      {
        name: 'apache',
        bind: {
          '1.1.1.1:80' => [],
          ':443,:8443' => ['ssl', 'crt public.puppetlabs.com', 'no-sslv3'],
          '2.2.2.2:8000-8010' => ['ssl', 'crt public.puppetlabs.com'],
          'fd@${FD_APP1}' => [],
          '/var/run/ssl-frontend.sock' => ['user root', 'mode 600', 'accept-proxy']
        }
      }
    end

    it {
      is_expected.to contain_concat__fragment('haproxy-apache_listen_block').with(
        'order' => '20-apache-00',
        'target' => '/etc/haproxy/haproxy.cfg',
        '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
      )
    }
  end

  context 'when bind parameter is used with ip addresses that sort wrong lexigraphically' do
    let(:params) do
      {
        name: 'apache',
        bind: {
          '10.1.3.21:80' => 'name input21',
          '8.252.206.100:80' => 'name input100',
          '8.252.206.101:80' => 'name input101',
          '8.252.206.99:80' => 'name input99',
          '1.1.1.1:80' => [],
          ':443,:8443' => ['ssl', 'crt public.puppetlabs.com', 'no-sslv3'],
          '2.2.2.2:8000-8010' => ['ssl', 'crt public.puppetlabs.com'],
          'fd@${FD_APP1}' => []
        }
      }
    end

    it {
      is_expected.to contain_concat__fragment('haproxy-apache_listen_block').with(
        'order' => '20-apache-00',
        'target' => '/etc/haproxy/haproxy.cfg',
        '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
      )
    }
  end

  context 'when listen options are specified as an array of hashes' do
    let(:params) do
      {
        name: 'apache',
        bind: {
          '0.0.0.0:48001-48003' => []
        },
        mode: 'http',
        options: [
          { 'reqadd'                 => 'X-Forwarded-Proto:\ https' },
          { 'default_backend'        => 'dev00_webapp' },
          { 'capture request header' => ['X-Forwarded-For len 50', 'Host len 15', 'Referrer len 15'] },
          { 'acl'                    => ['dst_dev01 dst_port 48001', 'dst_dev02 dst_port 48002', 'dst_dev03 dst_port 48003'] },
          { 'use_backend'            => ['dev01_webapp if dst_dev01', 'dev02_webapp if dst_dev02', 'dev03_webapp if dst_dev03'] },
          { 'option'                 => ['httplog', 'http-server-close', 'forwardfor except 127.0.0.1'] },
          { 'compression' => 'algo gzip',
            'bind-process' => 'all' },
        ]
      }
    end

    it {
      is_expected.to contain_concat__fragment('haproxy-apache_listen_block').with(
        'order' => '20-apache-00',
        'target' => '/etc/haproxy/haproxy.cfg',
        '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
      )
    }
  end

  context 'when listen options are specified with sort_options_alphabetic' do
    let(:params) do
      {
        name: 'apache',
        bind: {
          '0.0.0.0:48001-48003' => []
        },
        mode: 'http',
        options: {
          'reqadd' => 'X-Forwarded-Proto:\ https',
          'reqidel' => '^X-Forwarded-For:.*',
          'default_backend' => 'dev00_webapp',
          'capture request header' => ['X-Forwarded-For len 50', 'Host len 15', 'Referrer len 15'],
          'acl' => ['dst_dev01 dst_port 48001', 'dst_dev02 dst_port 48002', 'dst_dev03 dst_port 48003'],
          'use_backend' => ['dev01_webapp if dst_dev01', 'dev02_webapp if dst_dev02', 'dev03_webapp if dst_dev03'],
          'option' => ['httplog', 'http-server-close', 'forwardfor except 127.0.0.1'],
          'compression' => 'algo gzip',
          'bind-process' => 'all'
        }
      }
    end

    it {
      is_expected.to contain_concat__fragment('haproxy-apache_listen_block').with(
        'order' => '20-apache-00',
        'target' => '/etc/haproxy/haproxy.cfg',
        '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
      )
    }
  end

  context 'when listen options are specified without sort_options_alphabetic' do
    let(:params) do
      {
        name: 'apache',
        bind: {
          '0.0.0.0:48001-48003' => []
        },
        mode: 'http',
        sort_options_alphabetic: false,
        options: {
          'reqadd' => 'X-Forwarded-Proto:\ https',
          'reqidel' => '^X-Forwarded-For:.*',
          'default_backend' => 'dev00_webapp',
          'capture request header' => ['X-Forwarded-For len 50', 'Host len 15', 'Referrer len 15'],
          'acl' => ['dst_dev01 dst_port 48001', 'dst_dev02 dst_port 48002', 'dst_dev03 dst_port 48003'],
          'use_backend' => ['dev01_webapp if dst_dev01', 'dev02_webapp if dst_dev02', 'dev03_webapp if dst_dev03'],
          'option' => ['httpchk', 'httplog', 'http-server-close', 'forwardfor except 127.0.0.1'],
          'compression' => 'algo gzip',
          'bind-process' => 'all',
          'http-check' => ['send hdr Host test.example.com meth GET uri /health', 'expect status 204']
        }
      }
    end

    it {
      is_expected.to contain_concat__fragment('haproxy-apache_listen_block').with(
        'order' => '20-apache-00',
        'target' => '/etc/haproxy/haproxy.cfg',
        '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
      )
    }
  end

  context 'when a non-default config file is used' do
    let(:pre_condition) { 'class { "haproxy": config_file => "/etc/non-default.cfg" }' }
    let(:params) do
      {
        name: 'bar',
        bind: {
          '*:5000' => []
        },
        options: {
          'option' => [
            'tcplog',
          ],
          'balance' => 'roundrobin'
        }
      }
    end

    it {
      is_expected.to contain_concat__fragment('haproxy-bar_listen_block').with(
        'order' => '20-bar-00',
        'target' => '/etc/non-default.cfg',
        'content' => "\nlisten bar\n  bind *:5000 \n  balance roundrobin\n  option tcplog\n",
      )
    }
  end
end