]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/concat/spec/unit/defines/concat_spec.rb
b2254f4ec138b73f81d4f4f56c4aa4c3fed56904
[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     safe_name            = title.gsub('/', '_')
22     concat_name          = 'fragments.concat.out'
23     default_warn_message = "# This file is managed by Puppet. DO NOT EDIT.\n"
24
25     file_defaults = {
26       :backup  => p[:backup],
27     }
28
29     let(:title) { title }
30     let(:params) { params }
31     let(:facts) do
32       {
33         :id             => id,
34         :osfamily       => 'Debian',
35         :path           => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
36         :kernel         => 'Linux',
37         :is_pe          => false,
38       }
39     end
40
41     if p[:ensure] == 'present'
42       it do
43         should contain_concat(title).with(file_defaults.merge({
44           :ensure       => 'present',
45           :owner        => p[:owner],
46           :group        => p[:group],
47           :mode         => p[:mode],
48           :path         => p[:path],
49           :backup       => p[:backup],
50           :replace      => p[:replace],
51         }))
52       end
53     else
54       it do
55         should contain_concat(title).with(file_defaults.merge({
56           :ensure => 'absent',
57           :backup => p[:backup],
58         }))
59       end
60     end
61   end
62
63   context 'title' do
64     context 'without path param' do
65       # title/name is the default value for the path param. therefore, the
66       # title must be an absolute path unless path is specified
67       ['/foo', '/foo/bar', '/foo/bar/baz'].each do |title|
68         context title do
69           it_behaves_like 'concat', '/etc/foo.bar'
70         end
71       end
72
73       ['./foo', 'foo', 'foo/bar'].each do |title|
74         context title do
75           let(:title) { title }
76           it 'should fail' do
77             expect { catalogue }.to raise_error(Puppet::Error, /is not an absolute path/)
78           end
79         end
80       end
81     end
82
83     context 'with path param' do
84       ['/foo', 'foo', 'foo/bar'].each do |title|
85         context title do
86           it_behaves_like 'concat', title, { :path => '/etc/foo.bar' }
87         end
88       end
89     end
90   end # title =>
91
92   context 'as non-root user' do
93     it_behaves_like 'concat', '/etc/foo.bar', {}, 'bob'
94   end
95
96   context 'ensure =>' do
97     ['present', 'absent'].each do |ens|
98       context ens do
99         it_behaves_like 'concat', '/etc/foo.bar', { :ensure => ens }
100       end
101     end
102
103     context 'invalid' do
104       let(:title) { '/etc/foo.bar' }
105       let(:params) {{ :ensure => 'invalid' }}
106       it 'should fail' do
107         expect { catalogue }.to raise_error(Puppet::Error, /#{Regexp.escape('does not match "^present$|^absent$"')}/)
108       end
109     end
110   end # ensure =>
111
112   context 'path =>' do
113     context '/foo' do
114       it_behaves_like 'concat', '/etc/foo.bar', { :path => '/foo' }
115     end
116
117     ['./foo', 'foo', 'foo/bar', false].each do |path|
118       context path do
119         let(:title) { '/etc/foo.bar' }
120         let(:params) {{ :path => path }}
121         it 'should fail' do
122           expect { catalogue }.to raise_error(Puppet::Error, /is not an absolute path/)
123         end
124       end
125     end
126   end # path =>
127
128   context 'owner =>' do
129     context 'apenney' do
130       it_behaves_like 'concat', '/etc/foo.bar', { :owner => 'apenny' }
131     end
132
133     context 'false' do
134       let(:title) { '/etc/foo.bar' }
135       let(:params) {{ :owner => false }}
136       it 'should fail' do
137         expect { catalogue }.to raise_error(Puppet::Error, /is not a string/)
138       end
139     end
140   end # owner =>
141
142   context 'group =>' do
143     context 'apenney' do
144       it_behaves_like 'concat', '/etc/foo.bar', { :group => 'apenny' }
145     end
146
147     context 'false' do
148       let(:title) { '/etc/foo.bar' }
149       let(:params) {{ :group => false }}
150       it 'should fail' do
151         expect { catalogue }.to raise_error(Puppet::Error, /is not a string/)
152       end
153     end
154   end # group =>
155
156   context 'mode =>' do
157     context '1755' do
158       it_behaves_like 'concat', '/etc/foo.bar', { :mode => '1755' }
159     end
160
161     context 'false' do
162       let(:title) { '/etc/foo.bar' }
163       let(:params) {{ :mode => false }}
164       it 'should fail' do
165         expect { catalogue }.to raise_error(Puppet::Error, /is not a string/)
166       end
167     end
168   end # mode =>
169
170   context 'warn =>' do
171     [true, false, '# foo'].each do |warn|
172       context warn do
173         it_behaves_like 'concat', '/etc/foo.bar', { :warn => warn }
174       end
175     end
176
177     context '(stringified boolean)' do
178       ['true', 'yes', 'on', 'false', 'no', 'off'].each do |warn|
179         context warn do
180           it_behaves_like 'concat', '/etc/foo.bar', { :warn => warn }
181
182           it 'should create a warning' do
183             skip('rspec-puppet support for testing warning()')
184           end
185         end
186       end
187     end
188
189     context '123' do
190       let(:title) { '/etc/foo.bar' }
191       let(:params) {{ :warn => 123 }}
192       it 'should fail' do
193         expect { catalogue }.to raise_error(Puppet::Error, /is not a string or boolean/)
194       end
195     end
196   end # warn =>
197
198   context 'backup =>' do
199     context 'reverse' do
200       it_behaves_like 'concat', '/etc/foo.bar', { :backup => 'reverse' }
201     end
202
203     context 'false' do
204       it_behaves_like 'concat', '/etc/foo.bar', { :backup => false }
205     end
206
207     context 'true' do
208       it_behaves_like 'concat', '/etc/foo.bar', { :backup => true }
209     end
210
211     context 'true' do
212       let(:title) { '/etc/foo.bar' }
213       let(:params) {{ :backup => [] }}
214       it 'should fail' do
215         expect { catalogue }.to raise_error(Puppet::Error, /backup must be string or bool/)
216       end
217     end
218   end # backup =>
219
220   context 'replace =>' do
221     [true, false].each do |replace|
222       context replace do
223         it_behaves_like 'concat', '/etc/foo.bar', { :replace => replace }
224       end
225     end
226
227     context '123' do
228       let(:title) { '/etc/foo.bar' }
229       let(:params) {{ :replace => 123 }}
230       it 'should fail' do
231         expect { catalogue }.to raise_error(Puppet::Error, /is not a boolean/)
232       end
233     end
234   end # replace =>
235
236   context 'order =>' do
237     ['alpha', 'numeric'].each do |order|
238       context order do
239         it_behaves_like 'concat', '/etc/foo.bar', { :order => order }
240       end
241     end
242
243     context 'invalid' do
244       let(:title) { '/etc/foo.bar' }
245       let(:params) {{ :order => 'invalid' }}
246       it 'should fail' do
247         expect { catalogue }.to raise_error(Puppet::Error, /#{Regexp.escape('does not match "^alpha$|^numeric$"')}/)
248       end
249     end
250   end # order =>
251
252   context 'ensure_newline =>' do
253     [true, false].each do |ensure_newline|
254       context 'true' do
255         it_behaves_like 'concat', '/etc/foo.bar', { :ensure_newline => ensure_newline}
256       end
257     end
258
259     context '123' do
260       let(:title) { '/etc/foo.bar' }
261       let(:params) {{ :ensure_newline => 123 }}
262       it 'should fail' do
263         expect { catalogue }.to raise_error(Puppet::Error, /is not a boolean/)
264       end
265     end
266   end # ensure_newline =>
267
268   context 'validate_cmd =>' do
269     context '/usr/bin/test -e %' do
270       it_behaves_like 'concat', '/etc/foo.bar', { :validate_cmd => '/usr/bin/test -e %' }
271     end
272
273     [ 1234, true ].each do |cmd|
274       context cmd do
275         let(:title) { '/etc/foo.bar' }
276         let(:params) {{ :validate_cmd => cmd }}
277         it 'should fail' do
278           expect { catalogue }.to raise_error(Puppet::Error, /\$validate_cmd must be a string/)
279         end
280       end
281     end
282   end # validate_cmd =>
283 end