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