File: frontend_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 (372 lines) | stat: -rw-r--r-- 11,382 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
# frozen_string_literal: true

require 'spec_helper'

describe 'haproxy::frontend' 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: 18_140
      }
    end

    it {
      is_expected.to contain_concat__fragment('haproxy-croy_frontend_block').with(
        'order' => '15-croy-00',
        'target' => '/etc/haproxy/haproxy.cfg',
        'content' => "\nfrontend croy\n  bind 1.1.1.1:18140 \n  option tcplog\n",
      )
    }
    context 'with stringy port' do
      let(:params) do
        super().merge(ports: '18140')
      end

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

  # C9948 C9947
  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_frontend_block').with(
        'order' => '15-apache-00',
        'target' => '/etc/haproxy/haproxy.cfg',
        'content' => "\nfrontend apache\n  bind 23.23.23.23:80 \n  bind 23.23.23.23:443 \n  option tcplog\n",
      )
    }
    context 'with stringy port' do
      let(:params) do
        super().merge(ports: ['80', '443'])
      end

      it {
        is_expected.to contain_concat__fragment('haproxy-apache_frontend_block').with(
          'order' => '15-apache-00',
          'target' => '/etc/haproxy/haproxy.cfg',
          'content' => "\nfrontend apache\n  bind 23.23.23.23:80 \n  bind 23.23.23.23:443 \n  option tcplog\n",
        )
      }
    end
    # C9948
    context 'with a comma-seperated list of ports' do
      let(:params) do
        super().merge(ports: '80,443')
      end

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

  # C9971
  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_frontend_block').with(
        'order' => '15-apache-00',
        'target' => '/etc/haproxy/haproxy.cfg',
        'content' => "\nfrontend apache\n  option tcplog\n",
      )
    }
  end

  # C9972
  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

  # C9946
  context 'when multiple ports are provided greater than 65535' do
    let(:params) do
      {
        name: 'apache',
        ipaddress: '23.23.23.23',
        ports: ['80443', '80444']
      }
    end

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

  # C9973
  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

  # C9949
  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 multiple IPs are provided' do
    let(:params) do
      {
        name: 'apache',
        ipaddress: ['23.23.23.23', '23.23.23.24'],
        ports: '80'
      }
    end

    it {
      is_expected.to contain_concat__fragment('haproxy-apache_frontend_block').with(
        'order' => '15-apache-00',
        'target' => '/etc/haproxy/haproxy.cfg',
        'content' => "\nfrontend apache\n  bind 23.23.23.23:80 \n  bind 23.23.23.24:80 \n  option tcplog\n",
      )
    }
  end

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

    it {
      is_expected.to contain_concat__fragment('haproxy-apache_frontend_block').with(
        'order' => '15-apache-00',
        'target' => '/etc/haproxy/haproxy.cfg',
        'content' => "\nfrontend apache\n  bind 1.1.1.1:80 the options go here\n  bind 1.1.1.1:8080 the options go here\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_frontend_block').with(
        'order' => '15-apache-00',
        'target' => '/etc/haproxy/haproxy.cfg',
        'content' => "\nfrontend apache\n  bind 1.1.1.1:80 \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_frontend_block').with(
        'order' => '15-apache-00',
        'target' => '/etc/haproxy/haproxy.cfg',
        'content' => "\nfrontend 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  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' => [],
          '8.252.206.100:80' => [],
          '8.252.206.101:80' => [],
          '8.252.206.99:80' => [],
          '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']
        }
      }
    end

    it {
      is_expected.to contain_concat__fragment('haproxy-apache_frontend_block').with(
        'order' => '15-apache-00',
        'target' => '/etc/haproxy/haproxy.cfg',
        'content' => "\nfrontend apache\n  bind :443,:8443 ssl crt public.puppetlabs.com no-sslv3\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 \n  bind 8.252.206.100:80 \n  bind 8.252.206.101:80 \n  bind 10.1.3.21:80 \n  option tcplog\n", # rubocop:disable Layout/LineLength
      )
    }
  end

  context 'when frontend 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_frontend_block').with(
        'order' => '15-apache-00',
        'target' => '/etc/haproxy/haproxy.cfg',
        'content' => "\nfrontend 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 defaults parameter is used with option default_backend' do
    let(:params) do
      {
        name: 'apache',
        bind: { '1.1.1.1:80' => [] },
        defaults: 'test',
        options: { 'default_backend' => 'b1' }
      }
    end

    it {
      is_expected.to contain_concat__fragment('haproxy-apache_frontend_block').with(
        'order' => '25-test-b1-00-apache',
        'target' => '/etc/haproxy/haproxy.cfg',
        'content' => "\nfrontend apache\n  bind 1.1.1.1:80 \n  default_backend b1\n",
      )
    }
  end

  context 'when configurung custom options for stick-tables' do
    let(:title) { 'baz' }
    let(:buzz) { 'type string len 180 size 32m expire 5m store http_req_rate(10s)' }
    let(:params) do
      { options: [
        { 'stick-table' => buzz },
        { 'stick' => 'on dst' },
      ] }
    end

    it {
      is_expected.to contain_concat__fragment('haproxy-baz_frontend_block').with(
        'order' => '15-baz-00',
        'target' => '/etc/haproxy/haproxy.cfg',
        'content' => "\nfrontend baz\n  stick-table #{buzz}\n  stick on dst\n",
      )
    }
  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',
          ]
        }
      }
    end

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

  # C9950 C9951 C9952 WONTFIX
end