File: concat_spec.rb

package info (click to toggle)
puppet-module-puppetlabs-concat 7.3.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 532 kB
  • sloc: ruby: 2,584; sh: 46; makefile: 2
file content (404 lines) | stat: -rw-r--r-- 10,107 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
# frozen_string_literal: true

require 'spec_helper'

describe 'concat' do
  shared_examples 'concat' do |title, params, id|
    params = {} if params.nil?
    id = 'root' if id.nil?

    # default param values
    p = {
      ensure: 'present',
      path: title,
      owner: nil,
      group: nil,
      mode: '0644',
      warn: false,
      backup: 'puppet',
      replace: true,
      force: false,
    }.merge(params)

    file_defaults = {
      backup: p[:backup],
    }

    present_expect = {
      ensure: 'present',
      owner: p[:owner],
      group: p[:group],
      mode: p[:mode],
      path: p[:path],
      backup: p[:backup],
      replace: p[:replace],
      selinux_ignore_defaults: p[:selinux_ignore_defaults],
      selrange: p[:selrange],
      selrole: p[:selrole],
      seltype: p[:seltype],
      seluser: p[:seluser],
      force: p[:force],
    }

    let(:title) { title }
    let(:params) { params }
    let(:facts) do
      {
        id: id,
        osfamily: 'Debian',
        path: '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
        kernel: 'Linux',
        is_pe: false,
      }
    end

    if p[:ensure] == 'present'
      it do
        is_expected.to contain_concat(title).with(file_defaults.merge(present_expect))
      end
    else
      it do
        is_expected.to contain_concat(title).with(file_defaults.merge(ensure: 'absent',
                                                                      backup: p[:backup]))
      end
    end
  end

  context 'when title without path param' do
    # title/name is the default value for the path param. therefore, the
    # title must be an absolute path unless path is specified
    ['/foo', '/foo/bar', '/foo/bar/baz'].each do |title|
      context title do
        it_behaves_like 'concat', '/etc/foo.bar'
      end
    end

    ['./foo', 'foo', 'foo/bar'].each do |title|
      context title do
        let(:title) { title }

        it 'fails' do
          expect { catalogue }.to raise_error(Puppet::Error, %r{Stdlib::Unixpath})
        end
      end
    end
  end

  context 'when title with path param' do
    ['/foo', 'foo', 'foo/bar'].each do |title|
      context title do
        it_behaves_like 'concat', title, path: '/etc/foo.bar'
      end
    end
  end

  context 'when title with special characters in title' do
    ['foo:bar', 'foo*bar', 'foo(bar)', 'foo@bar'].each do |title|
      context title do
        it_behaves_like 'concat', title, path: '/etc/foo.bar'
      end
    end
  end

  context 'when as non-root user' do
    it_behaves_like 'concat', '/etc/foo.bar', {}, 'bob'
  end

  context 'when ensure =>' do
    ['present', 'absent'].each do |ens|
      context ens do
        it_behaves_like 'concat', '/etc/foo.bar', ensure: ens
      end
    end

    context 'when invalid' do
      let(:title) { '/etc/foo.bar' }
      let(:params) { { ensure: 'invalid' } }

      it 'fails' do
        expect { catalogue }.to raise_error(Puppet::Error, %r{expects a match for Enum\['absent', 'present'\]})
      end
    end
  end
  # ensure =>

  context 'when path =>' do
    context 'when /foo' do
      it_behaves_like 'concat', '/etc/foo.bar', path: '/foo'
    end

    context 'when false' do
      let(:title) { '/etc/foo.bar' }
      let(:params) { { path: false } }

      it 'fails' do
        expect { catalogue }.to raise_error(Puppet::Error, %r{Stdlib::Unixpath})
      end
    end

    ['./foo', 'foo', 'foo/bar'].each do |path|
      context path do
        let(:title) { '/etc/foo.bar' }
        let(:params) { { path: path } }

        it 'fails' do
          expect { catalogue }.to raise_error(Puppet::Error, %r{Stdlib::Unixpath})
        end
      end
    end
  end
  # path =>

  context 'when owner =>' do
    ['apenney', 1000, '1001'].each do |owner|
      context owner do
        it_behaves_like 'concat', '/etc/foo.bar', owner: owner
      end
    end

    context 'when false' do
      let(:title) { '/etc/foo.bar' }
      let(:params) { { owner: false } }

      it 'fails' do
        expect { catalogue }.to raise_error(Puppet::Error, %r{Evaluation Error.*expects.*String.*Boolean.*})
      end
    end
  end
  # owner =>

  context 'when group =>' do
    ['apenney', 1000, '1001'].each do |group|
      context group do
        it_behaves_like 'concat', '/etc/foo.bar', group: group
      end
    end

    context 'when false' do
      let(:title) { '/etc/foo.bar' }
      let(:params) { { group: false } }

      it 'fails' do
        expect { catalogue }.to raise_error(Puppet::Error, %r{Evaluation Error.*expects.*String.*Boolean.*})
      end
    end
  end
  # group =>

  context 'when mode =>' do
    context 'when 1755' do
      it_behaves_like 'concat', '/etc/foo.bar', mode: '1755'
    end

    context 'when false' do
      let(:title) { '/etc/foo.bar' }
      let(:params) { { mode: false } }

      it 'fails' do
        expect { catalogue }.to raise_error(Puppet::Error, %r{parameter 'mode' expects .*String.*})
      end
    end
  end
  # mode =>

  context 'when warn =>' do
    [true, false, '# foo'].each do |warn|
      context warn do
        it_behaves_like 'concat', '/etc/foo.bar', warn: warn
      end
    end

    context 'when (stringified boolean)' do
      ['true', 'yes', 'on', 'false', 'no', 'off'].each do |warn|
        define warn do
          it_behaves_like 'concat', '/etc/foo.bar', warn: warn

          it 'creates a warning' do
            skip('rspec-puppet support for testing warning()')
          end
        end
      end
    end

    context 'when 123' do
      let(:title) { '/etc/foo.bar' }
      let(:params) { { warn: 123 } }

      it 'fails' do
        expect { catalogue }.to raise_error(Puppet::Error, %r{parameter 'warn' expects .*Boolean.*String.*})
      end
    end
  end
  # warn =>

  context 'when show_diff =>' do
    [true, false].each do |show_diff|
      context show_diff do
        it_behaves_like 'concat', '/etc/foo.bar', show_diff: show_diff
      end
    end

    context 'when 123' do
      let(:title) { '/etc/foo.bar' }
      let(:params) { { show_diff: 123 } }

      it 'fails' do
        expect { catalogue }.to raise_error(Puppet::Error, %r{parameter 'show_diff' expects .*Boolean.*})
      end
    end
  end
  # show_diff =>

  context 'when backup =>' do
    ['reverse', false, true].each do |backup|
      context backup.to_s do
        it_behaves_like 'concat', '/etc/foo.bar', backup: backup
      end
    end

    context 'when true' do
      let(:title) { '/etc/foo.bar' }
      let(:params) { { backup: [] } }

      it 'fails' do
        expect { catalogue }.to raise_error(Puppet::Error, %r{parameter 'backup' expects .*Boolean.*String.*})
      end
    end
  end
  # backup =>

  context 'when replace =>' do
    [true, false].each do |replace|
      context replace do
        it_behaves_like 'concat', '/etc/foo.bar', replace: replace
      end
    end

    context 'when 123' do
      let(:title) { '/etc/foo.bar' }
      let(:params) { { replace: 123 } }

      it 'fails' do
        expect { catalogue }.to raise_error(Puppet::Error, %r{parameter 'replace' expects .*Boolean.*})
      end
    end
  end
  # replace =>

  context 'when force =>' do
    [true, false].each do |force|
      context force do
        it_behaves_like 'concat', '/etc/foo.bar', force: force
      end
    end

    context 'when 123' do
      let(:title) { '/etc/foo.bar' }
      let(:params) { { force: 123 } }

      it 'fails' do
        expect { catalogue }.to raise_error(Puppet::Error, %r{parameter 'force' expects .*Boolean.*})
      end
    end
  end
  # force =>

  context 'when order =>' do
    ['alpha', 'numeric'].each do |order|
      context order do
        it_behaves_like 'concat', '/etc/foo.bar', order: order
      end
    end

    context 'when invalid' do
      let(:title) { '/etc/foo.bar' }
      let(:params) { { order: 'invalid' } }

      it 'fails' do
        expect { catalogue }.to raise_error(Puppet::Error, %r{expects a match for Enum\['alpha', 'numeric'\]})
      end
    end
  end
  # order =>

  context 'when ensure_newline =>' do
    [true, false].each do |ensure_newline|
      context 'when true' do
        it_behaves_like 'concat', '/etc/foo.bar', ensure_newline: ensure_newline
      end
    end

    context 'when 123' do
      let(:title) { '/etc/foo.bar' }
      let(:params) { { ensure_newline: 123 } }

      it 'fails' do
        expect { catalogue }.to raise_error(Puppet::Error, %r{parameter 'ensure_newline' expects a Boolean value})
      end
    end
  end
  # ensure_newline =>

  context 'when validate_cmd =>' do
    context 'when /usr/bin/test -e %' do
      it_behaves_like 'concat', '/etc/foo.bar', validate_cmd: '/usr/bin/test -e %'
    end

    [1234, true].each do |cmd|
      context cmd do
        let(:title) { '/etc/foo.bar' }
        let(:params) { { validate_cmd: cmd } }

        it 'fails' do
          expect { catalogue }.to raise_error(Puppet::Error, %r{parameter 'validate_cmd' expects.*String.*})
        end
      end
    end
  end
  # validate_cmd =>

  context 'when selinux_ignore_defaults =>' do
    let(:title) { '/etc/foo.bar' }

    [true, false].each do |v|
      context v do
        it_behaves_like 'concat', '/etc/foo.bar', selinux_ignore_defaults: v
      end
    end

    context 'when 123' do
      let(:title) { '/etc/foo.bar' }
      let(:params) { { selinux_ignore_defaults: 123 } }

      it 'fails' do
        expect { catalogue }.to raise_error(Puppet::Error, %r{Evaluation Error.*expects.*Boolean.*})
      end
    end
  end
  # selinux_ignore_defaults =>

  [
    :selrange,
    :selrole,
    :seltype,
    :seluser,
  ].each do |p|
    context " #{p} =>" do
      let(:title) { '/etc/foo.bar' }

      context 'when foo' do
        it_behaves_like 'concat', '/etc/foo.bar', p => 'foo'
      end

      context 'when false' do
        let(:title) { '/etc/foo.bar' }
        let(:params) { { p => false } }

        it 'fails' do
          expect { catalogue }.to raise_error(Puppet::Error, %r{parameter '#{p}' expects.*String.*})
        end
      end
    end
    # #{p} =>
  end
end