Update stdlib and concat to 6.1.0 both
[mirror/dsa-puppet.git] / 3rdparty / modules / concat / spec / defines / concat_spec.rb
1 require 'spec_helper'
2
3 describe 'concat' do
4   shared_examples 'concat' do |title, params, id|
5     params = {} if params.nil?
6     id = 'root' if id.nil?
7
8     # default param values
9     p = {
10       ensure: 'present',
11       path: title,
12       owner: nil,
13       group: nil,
14       mode: '0644',
15       warn: false,
16       backup: 'puppet',
17       replace: true,
18       force: false,
19     }.merge(params)
20
21     file_defaults = {
22       backup: p[:backup],
23     }
24
25     present_expect = {
26       ensure: 'present',
27       owner: p[:owner],
28       group: p[:group],
29       mode: p[:mode],
30       path: p[:path],
31       backup: p[:backup],
32       replace: p[:replace],
33       selinux_ignore_defaults: p[:selinux_ignore_defaults],
34       selrange: p[:selrange],
35       selrole: p[:selrole],
36       seltype: p[:seltype],
37       seluser: p[:seluser],
38       force: p[:force],
39     }
40
41     let(:title) { title }
42     let(:params) { params }
43     let(:facts) do
44       {
45         id: id,
46         osfamily: 'Debian',
47         path: '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
48         kernel: 'Linux',
49         is_pe: false,
50       }
51     end
52
53     if p[:ensure] == 'present'
54       it do
55         is_expected.to contain_concat(title).with(file_defaults.merge(present_expect))
56       end
57     else
58       it do
59         is_expected.to contain_concat(title).with(file_defaults.merge(ensure: 'absent',
60                                                                       backup: p[:backup]))
61       end
62     end
63   end
64
65   context 'when title without path param' do
66     # title/name is the default value for the path param. therefore, the
67     # title must be an absolute path unless path is specified
68     ['/foo', '/foo/bar', '/foo/bar/baz'].each do |title|
69       context title do
70         it_behaves_like 'concat', '/etc/foo.bar'
71       end
72     end
73
74     ['./foo', 'foo', 'foo/bar'].each do |title|
75       context title do
76         let(:title) { title }
77
78         it 'fails' do
79           expect { catalogue }.to raise_error(Puppet::Error, %r{Stdlib::Unixpath})
80         end
81       end
82     end
83   end
84
85   context 'when title with path param' do
86     ['/foo', 'foo', 'foo/bar'].each do |title|
87       context title do
88         it_behaves_like 'concat', title, path: '/etc/foo.bar'
89       end
90     end
91   end
92
93   context 'when title with special characters in title' do
94     ['foo:bar', 'foo*bar', 'foo(bar)', 'foo@bar'].each do |title|
95       context title do
96         it_behaves_like 'concat', title, path: '/etc/foo.bar'
97       end
98     end
99   end
100
101   context 'when as non-root user' do
102     it_behaves_like 'concat', '/etc/foo.bar', {}, 'bob'
103   end
104
105   context 'when ensure =>' do
106     ['present', 'absent'].each do |ens|
107       context ens do
108         it_behaves_like 'concat', '/etc/foo.bar', ensure: ens
109       end
110     end
111
112     context 'when invalid' do
113       let(:title) { '/etc/foo.bar' }
114       let(:params) { { ensure: 'invalid' } }
115
116       it 'fails' do
117         expect { catalogue }.to raise_error(Puppet::Error, %r{expects a match for Enum\['absent', 'present'\]})
118       end
119     end
120   end
121   # ensure =>
122
123   context 'when path =>' do
124     context 'when /foo' do
125       it_behaves_like 'concat', '/etc/foo.bar', path: '/foo'
126     end
127
128     context 'when false' do
129       let(:title) { '/etc/foo.bar' }
130       let(:params) { { path: false } }
131
132       it 'fails' do
133         expect { catalogue }.to raise_error(Puppet::Error, %r{Stdlib::Unixpath})
134       end
135     end
136
137     ['./foo', 'foo', 'foo/bar'].each do |path|
138       context path do
139         let(:title) { '/etc/foo.bar' }
140         let(:params) { { path: path } }
141
142         it 'fails' do
143           expect { catalogue }.to raise_error(Puppet::Error, %r{Stdlib::Unixpath})
144         end
145       end
146     end
147   end
148   # path =>
149
150   context 'when owner =>' do
151     ['apenney', 1000, '1001'].each do |owner|
152       context owner do
153         it_behaves_like 'concat', '/etc/foo.bar', owner: owner
154       end
155     end
156
157     context 'when false' do
158       let(:title) { '/etc/foo.bar' }
159       let(:params) { { owner: false } }
160
161       it 'fails' do
162         expect { catalogue }.to raise_error(Puppet::Error, %r{Evaluation Error.*expects.*String.*Boolean.*})
163       end
164     end
165   end
166   # owner =>
167
168   context 'when group =>' do
169     ['apenney', 1000, '1001'].each do |group|
170       context group do
171         it_behaves_like 'concat', '/etc/foo.bar', group: group
172       end
173     end
174
175     context 'when false' do
176       let(:title) { '/etc/foo.bar' }
177       let(:params) { { group: false } }
178
179       it 'fails' do
180         expect { catalogue }.to raise_error(Puppet::Error, %r{Evaluation Error.*expects.*String.*Boolean.*})
181       end
182     end
183   end
184   # group =>
185
186   context 'when mode =>' do
187     context 'when 1755' do
188       it_behaves_like 'concat', '/etc/foo.bar', mode: '1755'
189     end
190
191     context 'when false' do
192       let(:title) { '/etc/foo.bar' }
193       let(:params) { { mode: false } }
194
195       it 'fails' do
196         expect { catalogue }.to raise_error(Puppet::Error, %r{parameter 'mode' expects .*String.*})
197       end
198     end
199   end
200   # mode =>
201
202   context 'when warn =>' do
203     [true, false, '# foo'].each do |warn|
204       context warn do
205         it_behaves_like 'concat', '/etc/foo.bar', warn: warn
206       end
207     end
208
209     context 'when (stringified boolean)' do
210       ['true', 'yes', 'on', 'false', 'no', 'off'].each do |warn|
211         define warn do
212           it_behaves_like 'concat', '/etc/foo.bar', warn: warn
213
214           it 'creates a warning' do
215             skip('rspec-puppet support for testing warning()')
216           end
217         end
218       end
219     end
220
221     context 'when 123' do
222       let(:title) { '/etc/foo.bar' }
223       let(:params) { { warn: 123 } }
224
225       it 'fails' do
226         expect { catalogue }.to raise_error(Puppet::Error, %r{parameter 'warn' expects .*Boolean.*String.*})
227       end
228     end
229   end
230   # warn =>
231
232   context 'when show_diff =>' do
233     [true, false].each do |show_diff|
234       context show_diff do
235         it_behaves_like 'concat', '/etc/foo.bar', show_diff: show_diff
236       end
237     end
238
239     context 'when 123' do
240       let(:title) { '/etc/foo.bar' }
241       let(:params) { { show_diff: 123 } }
242
243       it 'fails' do
244         expect { catalogue }.to raise_error(Puppet::Error, %r{parameter 'show_diff' expects .*Boolean.*})
245       end
246     end
247   end
248   # show_diff =>
249
250   context 'when backup =>' do
251     ['reverse', false, true].each do |backup|
252       context backup.to_s do
253         it_behaves_like 'concat', '/etc/foo.bar', backup: backup
254       end
255     end
256
257     context 'when true' do
258       let(:title) { '/etc/foo.bar' }
259       let(:params) { { backup: [] } }
260
261       it 'fails' do
262         expect { catalogue }.to raise_error(Puppet::Error, %r{parameter 'backup' expects .*Boolean.*String.*})
263       end
264     end
265   end
266   # backup =>
267
268   context 'when replace =>' do
269     [true, false].each do |replace|
270       context replace do
271         it_behaves_like 'concat', '/etc/foo.bar', replace: replace
272       end
273     end
274
275     context 'when 123' do
276       let(:title) { '/etc/foo.bar' }
277       let(:params) { { replace: 123 } }
278
279       it 'fails' do
280         expect { catalogue }.to raise_error(Puppet::Error, %r{parameter 'replace' expects .*Boolean.*})
281       end
282     end
283   end
284   # replace =>
285
286   context 'when force =>' do
287     [true, false].each do |force|
288       context force do
289         it_behaves_like 'concat', '/etc/foo.bar', force: force
290       end
291     end
292
293     context 'when 123' do
294       let(:title) { '/etc/foo.bar' }
295       let(:params) { { force: 123 } }
296
297       it 'fails' do
298         expect { catalogue }.to raise_error(Puppet::Error, %r{parameter 'force' expects .*Boolean.*})
299       end
300     end
301   end
302   # force =>
303
304   context 'when order =>' do
305     ['alpha', 'numeric'].each do |order|
306       context order do
307         it_behaves_like 'concat', '/etc/foo.bar', order: order
308       end
309     end
310
311     context 'when invalid' do
312       let(:title) { '/etc/foo.bar' }
313       let(:params) { { order: 'invalid' } }
314
315       it 'fails' do
316         expect { catalogue }.to raise_error(Puppet::Error, %r{expects a match for Enum\['alpha', 'numeric'\]})
317       end
318     end
319   end
320   # order =>
321
322   context 'when ensure_newline =>' do
323     [true, false].each do |ensure_newline|
324       context 'when true' do
325         it_behaves_like 'concat', '/etc/foo.bar', ensure_newline: ensure_newline
326       end
327     end
328
329     context 'when 123' do
330       let(:title) { '/etc/foo.bar' }
331       let(:params) { { ensure_newline: 123 } }
332
333       it 'fails' do
334         expect { catalogue }.to raise_error(Puppet::Error, %r{parameter 'ensure_newline' expects a Boolean value})
335       end
336     end
337   end
338   # ensure_newline =>
339
340   context 'when validate_cmd =>' do
341     context 'when /usr/bin/test -e %' do
342       it_behaves_like 'concat', '/etc/foo.bar', validate_cmd: '/usr/bin/test -e %'
343     end
344
345     [1234, true].each do |cmd|
346       context cmd do
347         let(:title) { '/etc/foo.bar' }
348         let(:params) { { validate_cmd: cmd } }
349
350         it 'fails' do
351           expect { catalogue }.to raise_error(Puppet::Error, %r{parameter 'validate_cmd' expects.*String.*})
352         end
353       end
354     end
355   end
356   # validate_cmd =>
357
358   context 'when selinux_ignore_defaults =>' do
359     let(:title) { '/etc/foo.bar' }
360
361     [true, false].each do |v|
362       context v do
363         it_behaves_like 'concat', '/etc/foo.bar', selinux_ignore_defaults: v
364       end
365     end
366
367     context 'when 123' do
368       let(:title) { '/etc/foo.bar' }
369       let(:params) { { selinux_ignore_defaults: 123 } }
370
371       it 'fails' do
372         expect { catalogue }.to raise_error(Puppet::Error, %r{Evaluation Error.*expects.*Boolean.*})
373       end
374     end
375   end
376   # selinux_ignore_defaults =>
377
378   [
379     :selrange,
380     :selrole,
381     :seltype,
382     :seluser,
383   ].each do |p|
384     context " #{p} =>" do
385       let(:title) { '/etc/foo.bar' }
386
387       context 'when foo' do
388         it_behaves_like 'concat', '/etc/foo.bar', p => 'foo'
389       end
390
391       context 'when false' do
392         let(:title) { '/etc/foo.bar' }
393         let(:params) { { p => false } }
394
395         it 'fails' do
396           expect { catalogue }.to raise_error(Puppet::Error, %r{parameter '#{p}' expects.*String.*})
397         end
398       end
399     end
400     # #{p} =>
401   end
402 end