]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/inifile/spec/acceptance/ini_setting_spec.rb
add puppetlabs/inifile to 3rdparty
[dsa-puppet.git] / 3rdparty / modules / inifile / spec / acceptance / ini_setting_spec.rb
1 require 'spec_helper_acceptance'
2
3 tmpdir = default.tmpdir('tmp')
4
5 describe 'ini_setting resource' do
6   after :all do
7     shell("rm #{tmpdir}/*.ini", :acceptable_exit_codes => [0, 1, 2])
8   end
9
10   shared_examples 'has_content' do |path, pp, content|
11     before :all do
12       shell("rm #{path}", :acceptable_exit_codes => [0, 1, 2])
13     end
14     after :all do
15       shell("cat #{path}", :acceptable_exit_codes => [0, 1, 2])
16       shell("rm #{path}", :acceptable_exit_codes => [0, 1, 2])
17     end
18
19     it 'applies the manifest twice' do
20       apply_manifest(pp, :catch_failures => true)
21       apply_manifest(pp, :catch_changes => true)
22     end
23
24     describe file(path) do
25       it { should be_file }
26       its(:content) { should match content }
27     end
28   end
29
30   shared_examples 'has_error' do |path, pp, error|
31     before :all do
32       shell("rm #{path}", :acceptable_exit_codes => [0, 1, 2])
33     end
34     after :all do
35       shell("cat #{path}", :acceptable_exit_codes => [0, 1, 2])
36       shell("rm #{path}", :acceptable_exit_codes => [0, 1, 2])
37     end
38
39     it 'applies the manifest and gets a failure message' do
40       expect(apply_manifest(pp, :expect_failures => true).stderr).to match(error)
41     end
42
43     describe file(path) do
44       it { should_not be_file }
45     end
46   end
47
48   describe 'ensure parameter' do
49     context '=> present for global and section' do
50       pp = <<-EOS
51       ini_setting { 'ensure => present for section':
52         ensure  => present,
53         path    => "#{tmpdir}/ini_setting.ini",
54         section => 'one',
55         setting => 'two',
56         value   => 'three',
57       }
58       ini_setting { 'ensure => present for global':
59         ensure  => present,
60         path    => "#{tmpdir}/ini_setting.ini",
61         section => '',
62         setting => 'four',
63         value   => 'five',
64       }
65       EOS
66
67       it 'applies the manifest twice' do
68         apply_manifest(pp, :catch_failures => true)
69         apply_manifest(pp, :catch_changes => true)
70       end
71
72       it_behaves_like 'has_content', "#{tmpdir}/ini_setting.ini", pp, /four = five\n\n\[one\]\ntwo = three/
73     end
74
75     context '=> absent for key/value' do
76       before :all do
77         if fact('osfamily') == 'Darwin'
78           shell("echo \"four = five[one]\ntwo = three\" > #{tmpdir}/ini_setting.ini")
79         else
80           shell("echo -e \"four = five\n[one]\ntwo = three\" > #{tmpdir}/ini_setting.ini")
81         end
82       end
83
84       pp = <<-EOS
85       ini_setting { 'ensure => absent for key/value':
86         ensure  => absent,
87         path    => "#{tmpdir}/ini_setting.ini",
88         section => 'one',
89         setting => 'two',
90         value   => 'three',
91       }
92       EOS
93
94       it 'applies the manifest twice' do
95         apply_manifest(pp, :catch_failures => true)
96         apply_manifest(pp, :catch_changes => true)
97       end
98
99       describe file("#{tmpdir}/ini_setting.ini") do
100         it { should be_file }
101         its(:content) {
102           should match /four = five/
103           should match /\[one\]/
104           should_not match /two = three/
105         }
106       end
107     end
108
109     context '=> absent for global' do
110       before :all do
111         if fact('osfamily') == 'Darwin'
112           shell("echo \"four = five\n[one]\ntwo = three\" > #{tmpdir}/ini_setting.ini")
113         else
114           shell("echo -e \"four = five\n[one]\ntwo = three\" > #{tmpdir}/ini_setting.ini")
115         end
116       end
117       after :all do
118         shell("cat #{tmpdir}/ini_setting.ini", :acceptable_exit_codes => [0, 1, 2])
119         shell("rm #{tmpdir}/ini_setting.ini", :acceptable_exit_codes => [0, 1, 2])
120       end
121
122       pp = <<-EOS
123       ini_setting { 'ensure => absent for global':
124         ensure  => absent,
125         path    => "#{tmpdir}/ini_setting.ini",
126         section => '',
127         setting => 'four',
128         value   => 'five',
129       }
130       EOS
131
132       it 'applies the manifest twice' do
133         apply_manifest(pp, :catch_failures => true)
134         apply_manifest(pp, :catch_changes => true)
135       end
136
137       describe file("#{tmpdir}/ini_setting.ini") do
138         it { should be_file }
139         its(:content) {
140           should_not match /four = five/
141           should match /\[one\]/
142           should match /two = three/
143         }
144       end
145     end
146   end
147
148   describe 'section, setting, value parameters' do
149     {
150         "section => 'test', setting => 'foo', value => 'bar',"         => /\[test\]\nfoo = bar/,
151         "section => 'more', setting => 'baz', value => 'quux',"        => /\[more\]\nbaz = quux/,
152         "section => '',     setting => 'top', value => 'level',"       => /top = level/,
153         "section => 'z',    setting => 'sp aces', value => 'foo bar'," => /\[z\]\nsp aces = foo bar/,
154     }.each do |parameter_list, content|
155       context parameter_list do
156         pp = <<-EOS
157         ini_setting { "#{parameter_list}":
158           ensure  => present,
159           path    => "#{tmpdir}/ini_setting.ini",
160           #{parameter_list}
161         }
162         EOS
163
164         it_behaves_like 'has_content', "#{tmpdir}/ini_setting.ini", pp, content
165       end
166     end
167
168     {
169         "section => 'test',"                   => /setting is a required.+value is a required/,
170         "setting => 'foo',  value   => 'bar'," => /section is a required/,
171         "section => 'test', setting => 'foo'," => /value is a required/,
172         "section => 'test', value   => 'bar'," => /setting is a required/,
173         "value   => 'bar',"                    => /section is a required.+setting is a required/,
174         "setting => 'foo',"                    => /section is a required.+value is a required/,
175     }.each do |parameter_list, error|
176       context parameter_list, :pending => 'no error checking yet' do
177         pp = <<-EOS
178         ini_setting { "#{parameter_list}":
179           ensure  => present,
180           path    => "#{tmpdir}/ini_setting.ini",
181           #{parameter_list}
182         }
183         EOS
184
185         it_behaves_like 'has_error', "#{tmpdir}/ini_setting.ini", pp, error
186       end
187     end
188   end
189
190   describe 'path parameter' do
191     [
192         "#{tmpdir}/one.ini",
193         "#{tmpdir}/two.ini",
194         "#{tmpdir}/three.ini",
195     ].each do |path|
196       context "path => #{path}" do
197         pp = <<-EOS
198         ini_setting { 'path => #{path}':
199           ensure  => present,
200           section => 'one',
201           setting => 'two',
202           value   => 'three',
203           path    => '#{path}',
204         }
205         EOS
206
207         it_behaves_like 'has_content', path, pp, /\[one\]\ntwo = three/
208       end
209     end
210
211     context "path => foo" do
212       pp = <<-EOS
213         ini_setting { 'path => foo':
214           ensure     => present,
215           section    => 'one',
216           setting    => 'two',
217           value      => 'three',
218           path       => 'foo',
219         }
220       EOS
221
222       it_behaves_like 'has_error', 'foo', pp, /must be fully qualified/
223     end
224   end
225
226   describe 'key_val_separator parameter' do
227     {
228         ""                             => /two = three/,
229         "key_val_separator => '=',"    => /two=three/,
230         "key_val_separator => ' =  '," => /two =  three/,
231     }.each do |parameter, content|
232       context "with \"#{parameter}\" makes \"#{content}\"" do
233         pp = <<-EOS
234         ini_setting { "with #{parameter} makes #{content}":
235           ensure  => present,
236           section => 'one',
237           setting => 'two',
238           value   => 'three',
239           path    => "#{tmpdir}/key_val_separator.ini",
240           #{parameter}
241         }
242         EOS
243
244         it_behaves_like 'has_content', "#{tmpdir}/key_val_separator.ini", pp, content
245       end
246     end
247   end
248 end