]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/concat/spec/unit/defines/concat_spec.rb
try if downgrading to 1.2.2 solves my problem
[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       :force          => false,
18       :backup         => 'puppet',
19       :replace        => true,
20       :order          => 'alpha',
21       :ensure_newline => false,
22       :validate_cmd   => nil,
23     }.merge(params)
24
25     safe_name            = title.gsub('/', '_')
26     concatdir            = '/var/lib/puppet/concat'
27     fragdir              = "#{concatdir}/#{safe_name}"
28     concat_name          = 'fragments.concat.out'
29     default_warn_message = '# This file is managed by Puppet. DO NOT EDIT.'
30
31     file_defaults = {
32       :backup  => p[:backup],
33     }
34
35     let(:title) { title }
36     let(:params) { params }
37     let(:facts) do
38       {
39         :concat_basedir => concatdir,
40         :id             => id,
41         :osfamily       => 'Debian',
42         :path           => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
43         :kernel         => 'Linux',
44         :is_pe          => false,
45       }
46     end
47
48     if p[:ensure] == 'present'
49       it do
50         should contain_file(fragdir).with(file_defaults.merge({
51           :ensure => 'directory',
52           :mode   => '0750',
53         }))
54       end
55
56       it do
57         should contain_file("#{fragdir}/fragments").with(file_defaults.merge({
58           :ensure  => 'directory',
59           :mode    => '0750',
60           :force   => true,
61           :ignore  => ['.svn', '.git', '.gitignore'],
62           :purge   => true,
63           :recurse => true,
64         }))
65       end
66
67       [
68         "#{fragdir}/fragments.concat",
69         "#{fragdir}/#{concat_name}",
70       ].each do |file|
71         it do
72           should contain_file(file).with(file_defaults.merge({
73             :ensure => 'present',
74             :mode   => '0640',
75           }))
76         end
77       end
78
79       it do
80         should contain_file(title).with(file_defaults.merge({
81           :ensure       => 'present',
82           :owner        => p[:owner],
83           :group        => p[:group],
84           :mode         => p[:mode],
85           :replace      => p[:replace],
86           :path         => p[:path],
87           :alias        => "concat_#{title}",
88           :source       => "#{fragdir}/#{concat_name}",
89           :validate_cmd => p[:validate_cmd],
90           :backup       => p[:backup],
91         }))
92       end
93
94       cmd = "#{concatdir}/bin/concatfragments.rb " +
95             "-o \"#{concatdir}/#{safe_name}/fragments.concat.out\" " +
96             "-d \"#{concatdir}/#{safe_name}\""
97
98       # flag order: fragdir, warnflag, forceflag, orderflag, newlineflag 
99       if p.has_key?(:warn)
100         case p[:warn]
101         when TrueClass
102           message = default_warn_message
103         when 'true', 'yes', 'on'
104           # should generate a stringified boolean warning
105           message = default_warn_message
106         when FalseClass
107           message = nil
108         when 'false', 'no', 'off'
109           # should generate a stringified boolean warning
110           message = nil
111         else
112           message = p[:warn]
113         end
114
115         unless message.nil?
116           cmd += " -w \'#{message}\'"
117         end
118       end
119
120       cmd += " -f" if p[:force]
121       cmd += " -n" if p[:order] == 'numeric'
122       cmd += " -l" if p[:ensure_newline] == true
123
124       it do
125         should contain_exec("concat_#{title}").with({
126           :alias   => "concat_#{fragdir}",
127           :command => cmd,
128           :unless  => "#{cmd} -t",
129         })
130       end
131     else
132       [
133         fragdir,
134         "#{fragdir}/fragments",
135         "#{fragdir}/fragments.concat",
136         "#{fragdir}/#{concat_name}",
137       ].each do |file|
138         it do
139           should contain_file(file).with(file_defaults.merge({
140             :ensure => 'absent',
141             :force  => true,
142           }))
143         end
144       end
145
146       it do
147         should contain_file(title).with(file_defaults.merge({
148           :ensure => 'absent',
149           :backup => p[:backup],
150         }))
151       end
152
153       it do
154         should contain_exec("concat_#{title}").with({
155           :alias   => "concat_#{fragdir}",
156           :command => 'true',
157           :unless  => 'true',
158           :path    => '/bin:/usr/bin',
159         })
160       end
161     end
162   end
163
164   context 'title' do
165     context 'without path param' do
166       # title/name is the default value for the path param. therefore, the
167       # title must be an absolute path unless path is specified
168       ['/foo', '/foo/bar', '/foo/bar/baz'].each do |title|
169         context title do
170           it_behaves_like 'concat', '/etc/foo.bar'
171         end
172       end
173
174       ['./foo', 'foo', 'foo/bar'].each do |title|
175         context title do
176           let(:title) { title }
177           it 'should fail' do
178             expect { should }.to raise_error(Puppet::Error, /is not an absolute path/)
179           end
180         end
181       end
182     end
183
184     context 'with path param' do
185       ['./foo', 'foo', 'foo/bar'].each do |title|
186         context title do
187           it_behaves_like 'concat', title, { :path => '/etc/foo.bar' }
188         end
189       end
190     end
191   end # title =>
192
193   context 'as non-root user' do
194     it_behaves_like 'concat', '/etc/foo.bar', {}, 'bob'
195   end
196
197   context 'ensure =>' do
198     ['present', 'absent'].each do |ens|
199       context ens do
200         it_behaves_like 'concat', '/etc/foo.bar', { :ensure => ens }
201       end
202     end
203
204     context 'invalid' do
205       let(:title) { '/etc/foo.bar' }
206       let(:params) {{ :ensure => 'invalid' }}
207       it 'should fail' do
208         expect { should }.to raise_error(Puppet::Error, /#{Regexp.escape('does not match "^present$|^absent$"')}/)
209       end
210     end
211   end # ensure =>
212
213   context 'path =>' do
214     context '/foo' do
215       it_behaves_like 'concat', '/etc/foo.bar', { :path => '/foo' }
216     end
217
218     ['./foo', 'foo', 'foo/bar', false].each do |path|
219       context path do
220         let(:title) { '/etc/foo.bar' }
221         let(:params) {{ :path => path }}
222         it 'should fail' do
223           expect { should }.to raise_error(Puppet::Error, /is not an absolute path/)
224         end
225       end
226     end
227   end # path =>
228
229   context 'owner =>' do
230     context 'apenney' do
231       it_behaves_like 'concat', '/etc/foo.bar', { :owner => 'apenny' }
232     end
233
234     context 'false' do
235       let(:title) { '/etc/foo.bar' }
236       let(:params) {{ :owner => false }}
237       it 'should fail' do
238         expect { should }.to raise_error(Puppet::Error, /is not a string/)
239       end
240     end
241   end # owner =>
242
243   context 'group =>' do
244     context 'apenney' do
245       it_behaves_like 'concat', '/etc/foo.bar', { :group => 'apenny' }
246     end
247
248     context 'false' do
249       let(:title) { '/etc/foo.bar' }
250       let(:params) {{ :group => false }}
251       it 'should fail' do
252         expect { should }.to raise_error(Puppet::Error, /is not a string/)
253       end
254     end
255   end # group =>
256
257   context 'mode =>' do
258     context '1755' do
259       it_behaves_like 'concat', '/etc/foo.bar', { :mode => '1755' }
260     end
261
262     context 'false' do
263       let(:title) { '/etc/foo.bar' }
264       let(:params) {{ :mode => false }}
265       it 'should fail' do
266         expect { should }.to raise_error(Puppet::Error, /is not a string/)
267       end
268     end
269   end # mode =>
270
271   context 'warn =>' do
272     [true, false, '# foo'].each do |warn|
273       context warn do
274         it_behaves_like 'concat', '/etc/foo.bar', { :warn => warn }
275       end
276     end
277
278     context '(stringified boolean)' do
279       ['true', 'yes', 'on', 'false', 'no', 'off'].each do |warn|
280         context warn do
281           it_behaves_like 'concat', '/etc/foo.bar', { :warn => warn }
282
283           it 'should create a warning' do
284             skip('rspec-puppet support for testing warning()')
285           end
286         end
287       end
288     end
289
290     context '123' do
291       let(:title) { '/etc/foo.bar' }
292       let(:params) {{ :warn => 123 }}
293       it 'should fail' do
294         expect { should }.to raise_error(Puppet::Error, /is not a string or boolean/)
295       end
296     end
297   end # warn =>
298
299   context 'force =>' do
300     [true, false].each do |force|
301       context force do
302         it_behaves_like 'concat', '/etc/foo.bar', { :force => force }
303       end
304     end
305
306     context '123' do
307       let(:title) { '/etc/foo.bar' }
308       let(:params) {{ :force => 123 }}
309       it 'should fail' do
310         expect { should }.to raise_error(Puppet::Error, /is not a boolean/)
311       end
312     end
313   end # force =>
314
315   context 'backup =>' do
316     context 'reverse' do
317       it_behaves_like 'concat', '/etc/foo.bar', { :backup => 'reverse' }
318     end
319
320     context 'false' do
321       it_behaves_like 'concat', '/etc/foo.bar', { :backup => false }
322     end
323
324     context 'true' do
325       it_behaves_like 'concat', '/etc/foo.bar', { :backup => true }
326     end
327
328     context 'true' do
329       let(:title) { '/etc/foo.bar' }
330       let(:params) {{ :backup => [] }}
331       it 'should fail' do
332         expect { should }.to raise_error(Puppet::Error, /backup must be string or bool/)
333       end
334     end
335   end # backup =>
336
337   context 'replace =>' do
338     [true, false].each do |replace|
339       context replace do
340         it_behaves_like 'concat', '/etc/foo.bar', { :replace => replace }
341       end
342     end
343
344     context '123' do
345       let(:title) { '/etc/foo.bar' }
346       let(:params) {{ :replace => 123 }}
347       it 'should fail' do
348         expect { should }.to raise_error(Puppet::Error, /is not a boolean/)
349       end
350     end
351   end # replace =>
352
353   context 'order =>' do
354     ['alpha', 'numeric'].each do |order|
355       context order do
356         it_behaves_like 'concat', '/etc/foo.bar', { :order => order }
357       end
358     end
359
360     context 'invalid' do
361       let(:title) { '/etc/foo.bar' }
362       let(:params) {{ :order => 'invalid' }}
363       it 'should fail' do
364         expect { should }.to raise_error(Puppet::Error, /#{Regexp.escape('does not match "^alpha$|^numeric$"')}/)
365       end
366     end
367   end # order =>
368
369   context 'ensure_newline =>' do
370     [true, false].each do |ensure_newline|
371       context 'true' do
372         it_behaves_like 'concat', '/etc/foo.bar', { :ensure_newline => ensure_newline}
373       end
374     end
375
376     context '123' do
377       let(:title) { '/etc/foo.bar' }
378       let(:params) {{ :ensure_newline => 123 }}
379       it 'should fail' do
380         expect { should }.to raise_error(Puppet::Error, /is not a boolean/)
381       end
382     end
383   end # ensure_newline =>
384
385   context 'validate_cmd =>' do
386     context '/usr/bin/test -e %' do
387       it_behaves_like 'concat', '/etc/foo.bar', { :validate_cmd => '/usr/bin/test -e %' }
388     end
389
390     [ 1234, true ].each do |cmd|
391       context cmd do
392         let(:title) { '/etc/foo.bar' }
393         let(:params) {{ :validate_cmd => cmd }}
394         it 'should fail' do
395           expect { should }.to raise_error(Puppet::Error, /\$validate_cmd must be a string/)
396         end
397       end
398     end
399   end # validate_cmd =>
400
401   describe 'deprecated parameter' do
402     context 'gnu =>' do
403       context 'foo' do
404         it_behaves_like 'concat', '/etc/foo.bar', { :gnu => 'foo'}
405
406         it 'should create a warning' do
407           skip('rspec-puppet support for testing warning()')
408         end
409       end
410     end
411   end
412
413 end
414
415 # vim:sw=2:ts=2:expandtab:textwidth=79