]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/inifile/spec/acceptance/ini_subsetting_spec.rb
add puppetlabs/inifile to 3rdparty
[dsa-puppet.git] / 3rdparty / modules / inifile / spec / acceptance / ini_subsetting_spec.rb
1 require 'spec_helper_acceptance'
2
3 tmpdir = default.tmpdir('tmp')
4
5 describe 'ini_subsetting 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) {
27         should match content
28       }
29     end
30   end
31
32   shared_examples 'has_error' do |path, pp, error|
33     before :all do
34       shell("rm #{path}", :acceptable_exit_codes => [0, 1, 2])
35     end
36     after :all do
37       shell("cat #{path}", :acceptable_exit_codes => [0, 1, 2])
38       shell("rm #{path}", :acceptable_exit_codes => [0, 1, 2])
39     end
40
41     it 'applies the manifest and gets a failure message' do
42       expect(apply_manifest(pp, :expect_failures => true).stderr).to match(error)
43     end
44
45     describe file(path) do
46       it { should_not be_file }
47     end
48   end
49
50   describe 'ensure, section, setting, subsetting, & value parameters' do
51     context '=> present with subsections' do
52       pp = <<-EOS
53       ini_subsetting { 'ensure => present for alpha':
54         ensure     => present,
55         path       => "#{tmpdir}/ini_subsetting.ini",
56         section    => 'one',
57         setting    => 'key',
58         subsetting => 'alpha',
59         value      => 'bet',
60       }
61       ini_subsetting { 'ensure => present for beta':
62         ensure     => present,
63         path       => "#{tmpdir}/ini_subsetting.ini",
64         section    => 'one',
65         setting    => 'key',
66         subsetting => 'beta',
67         value      => 'trons',
68         require    => Ini_subsetting['ensure => present for alpha'],
69       }
70       EOS
71
72       it 'applies the manifest twice' do
73         apply_manifest(pp, :catch_failures => true)
74         apply_manifest(pp, :catch_changes => true)
75       end
76
77       describe file("#{tmpdir}/ini_subsetting.ini") do
78         it { should be_file }
79         its(:content) {
80           should match /\[one\]\nkey = alphabet betatrons/
81         }
82       end
83     end
84
85     context 'ensure => absent' do
86       before :all do
87         if fact('osfamily') == 'Darwin'
88           shell("echo \"[one]\nkey = alphabet betatrons\" > #{tmpdir}/ini_subsetting.ini")
89         else
90           shell("echo -e \"[one]\nkey = alphabet betatrons\" > #{tmpdir}/ini_subsetting.ini")
91         end
92       end
93
94       pp = <<-EOS
95       ini_subsetting { 'ensure => absent for subsetting':
96         ensure     => absent,
97         path       => "#{tmpdir}/ini_subsetting.ini",
98         section    => 'one',
99         setting    => 'key',
100         subsetting => 'alpha',
101       }
102       EOS
103
104       it 'applies the manifest twice' do
105         apply_manifest(pp, :catch_failures => true)
106         apply_manifest(pp, :catch_changes => true)
107       end
108
109       describe file("#{tmpdir}/ini_subsetting.ini") do
110         it { should be_file }
111         its(:content) {
112           should match /\[one\]/
113           should match /key = betatrons/
114           should_not match /alphabet/
115         }
116       end
117     end
118   end
119
120   describe 'subsetting_separator' do
121     {
122         ""                                => /two = twinethree foobar/,
123         "subsetting_separator => ',',"    => /two = twinethree,foobar/,
124         "subsetting_separator => '   ',"  => /two = twinethree   foobar/,
125         "subsetting_separator => ' == '," => /two = twinethree == foobar/,
126         "subsetting_separator => '=',"    => /two = twinethree=foobar/,
127     }.each do |parameter, content|
128       context "with \"#{parameter}\" makes \"#{content}\"" do
129         pp = <<-EOS
130         ini_subsetting { "with #{parameter} makes #{content}":
131           ensure     => present,
132           section    => 'one',
133           setting    => 'two',
134           subsetting => 'twine',
135           value      => 'three',
136           path       => "#{tmpdir}/subsetting_separator.ini",
137           before     => Ini_subsetting['foobar'],
138           #{parameter}
139         }
140         ini_subsetting { "foobar":
141           ensure     => present,
142           section    => 'one',
143           setting    => 'two',
144           subsetting => 'foo',
145           value      => 'bar',
146           path       => "#{tmpdir}/subsetting_separator.ini",
147           #{parameter}
148         }
149         EOS
150
151         it_behaves_like 'has_content', "#{tmpdir}/subsetting_separator.ini", pp, content
152       end
153     end
154   end
155
156   describe 'quote_char' do
157     {
158         ['-Xmx']         => /args=""/,
159         ['-Xmx', '256m'] => /args=-Xmx256m/,
160         ['-Xmx', '512m'] => /args="-Xmx512m"/,
161         ['-Xms', '256m'] => /args="-Xmx256m -Xms256m"/,
162     }.each do |parameter, content|
163       context %Q{with '#{parameter.first}' #{parameter.length > 1 ? '=> \'' << parameter[1] << '\'' : 'absent'} makes '#{content}'} do
164         path = File.join(tmpdir, 'ini_subsetting.ini')
165
166         before :all do
167           shell(%Q{echo '[java]\nargs=-Xmx256m' > #{path}})
168         end
169         after :all do
170           shell("cat #{path}", :acceptable_exit_codes => [0, 1, 2])
171           shell("rm #{path}", :acceptable_exit_codes => [0, 1, 2])
172         end
173
174         pp = <<-EOS
175         ini_subsetting { '#{parameter.first}':
176           ensure     => #{parameter.length > 1 ? 'present' : 'absent'},
177           path       => '#{path}',
178           section    => 'java',
179           setting    => 'args',
180           quote_char => '"',
181           subsetting => '#{parameter.first}',
182           value      => '#{parameter.length > 1 ? parameter[1] : ''}'
183         }
184         EOS
185
186         it 'applies the manifest twice' do
187           apply_manifest(pp, :catch_failures => true)
188           apply_manifest(pp, :catch_changes => true)
189         end
190
191         describe file("#{tmpdir}/ini_subsetting.ini") do
192           it { should be_file }
193           its(:content) {
194             should match content
195           }
196         end
197       end
198     end
199   end
200 end