forked from puppetlabs/puppetlabs-inifile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathini_setting_spec.rb
325 lines (282 loc) · 9.95 KB
/
ini_setting_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
require 'spec_helper_acceptance'
tmpdir = default.tmpdir('tmp')
describe 'ini_setting resource' do
after :all do
shell("rm #{tmpdir}/*.ini", :acceptable_exit_codes => [0, 1, 2])
end
shared_examples 'has_content' do |path, pp, content|
before :all do
shell("rm #{path}", :acceptable_exit_codes => [0, 1, 2])
end
after :all do
shell("cat #{path}", :acceptable_exit_codes => [0, 1, 2])
shell("rm #{path}", :acceptable_exit_codes => [0, 1, 2])
end
it 'applies the manifest twice' do
apply_manifest(pp, :catch_failures => true)
apply_manifest(pp, :catch_changes => true)
end
describe file(path) do
it { is_expected.to be_file }
describe '#content' do
subject { super().content }
it { is_expected.to match content }
end
end
end
shared_examples 'has_error' do |path, pp, error|
before :all do
shell("rm #{path}", :acceptable_exit_codes => [0, 1, 2])
end
after :all do
shell("cat #{path}", :acceptable_exit_codes => [0, 1, 2])
shell("rm #{path}", :acceptable_exit_codes => [0, 1, 2])
end
it 'applies the manifest and gets a failure message' do
expect(apply_manifest(pp, :expect_failures => true).stderr).to match(error)
end
describe file(path) do
it { is_expected.not_to be_file }
end
end
context 'ensure parameter => present for global and section' do
pp = <<-EOS
ini_setting { 'ensure => present for section':
ensure => present,
path => "#{tmpdir}/ini_setting.ini",
section => 'one',
setting => 'two',
value => 'three',
}
ini_setting { 'ensure => present for global':
ensure => present,
path => "#{tmpdir}/ini_setting.ini",
section => '',
setting => 'four',
value => 'five',
}
EOS
it 'applies the manifest twice' do
apply_manifest(pp, :catch_failures => true)
apply_manifest(pp, :catch_changes => true)
end
it_behaves_like 'has_content', "#{tmpdir}/ini_setting.ini", pp, %r{four = five\n\n\[one\]\ntwo = three}
end
context 'ensure parameter => present for global and section (from previous blank value)' do
before :all do
if fact('osfamily') == 'Darwin'
shell("echo \"four =[one]\ntwo =\" > #{tmpdir}/ini_setting.ini")
else
shell("echo -e \"four =\n[one]\ntwo =\" > #{tmpdir}/ini_setting.ini")
end
end
pp = <<-EOS
ini_setting { 'ensure => present for section':
ensure => present,
path => "#{tmpdir}/ini_setting.ini",
section => 'one',
setting => 'two',
value => 'three',
}
ini_setting { 'ensure => present for global':
ensure => present,
path => "#{tmpdir}/ini_setting.ini",
section => '',
setting => 'four',
value => 'five',
}
EOS
it 'applies the manifest twice' do
apply_manifest(pp, :catch_failures => true)
apply_manifest(pp, :catch_changes => true)
end
it_behaves_like 'has_content', "#{tmpdir}/ini_setting.ini", pp, %r{four = five\n\n\[one\]\ntwo = three}
end
context 'ensure parameter => absent for key/value' do
before :all do
if fact('osfamily') == 'Darwin'
shell("echo \"four = five[one]\ntwo = three\" > #{tmpdir}/ini_setting.ini")
else
shell("echo -e \"four = five\n[one]\ntwo = three\" > #{tmpdir}/ini_setting.ini")
end
end
pp = <<-EOS
ini_setting { 'ensure => absent for key/value':
ensure => absent,
path => "#{tmpdir}/ini_setting.ini",
section => 'one',
setting => 'two',
value => 'three',
}
EOS
it 'applies the manifest twice' do
apply_manifest(pp, :catch_failures => true)
apply_manifest(pp, :catch_changes => true)
end
describe file("#{tmpdir}/ini_setting.ini") do
it { is_expected.to be_file }
describe '#content' do
subject { super().content }
it { is_expected.to match %r{four = five} }
it { is_expected.not_to match %r{\[one\]} }
it { is_expected.not_to match %r{two = three} }
end
end
end
context 'ensure parameter => absent for global' do
before :all do
if fact('osfamily') == 'Darwin'
shell("echo \"four = five\n[one]\ntwo = three\" > #{tmpdir}/ini_setting.ini")
else
shell("echo -e \"four = five\n[one]\ntwo = three\" > #{tmpdir}/ini_setting.ini")
end
end
after :all do
shell("cat #{tmpdir}/ini_setting.ini", :acceptable_exit_codes => [0, 1, 2])
shell("rm #{tmpdir}/ini_setting.ini", :acceptable_exit_codes => [0, 1, 2])
end
pp = <<-EOS
ini_setting { 'ensure => absent for global':
ensure => absent,
path => "#{tmpdir}/ini_setting.ini",
section => '',
setting => 'four',
value => 'five',
}
EOS
it 'applies the manifest twice' do
apply_manifest(pp, :catch_failures => true)
apply_manifest(pp, :catch_changes => true)
end
describe file("#{tmpdir}/ini_setting.ini") do
it { is_expected.to be_file }
describe '#content' do
subject { super().content }
it { is_expected.not_to match %r{four = five} }
it { is_expected.to match %r{\[one\]} }
it { is_expected.to match %r{two = three} }
end
end
end
describe 'section, setting, value parameters' do
{
"section => 'test', setting => 'foo', value => 'bar'," => %r{\[test\]\nfoo = bar},
"section => 'more', setting => 'baz', value => 'quux'," => %r{\[more\]\nbaz = quux},
"section => '', setting => 'top', value => 'level'," => %r{top = level},
"section => 'z', setting => 'sp aces', value => 'foo bar'," => %r{\[z\]\nsp aces = foo bar},
}.each do |parameter_list, content|
context parameter_list do
pp = <<-EOS
ini_setting { "#{parameter_list}":
ensure => present,
path => "#{tmpdir}/ini_setting.ini",
#{parameter_list}
}
EOS
it_behaves_like 'has_content', "#{tmpdir}/ini_setting.ini", pp, content
end
end
{
"section => 'test'," => %r{setting is a required.+value is a required},
"setting => 'foo', value => 'bar'," => %r{section is a required},
"section => 'test', setting => 'foo'," => %r{value is a required},
"section => 'test', value => 'bar'," => %r{setting is a required},
"value => 'bar'," => %r{section is a required.+setting is a required},
"setting => 'foo'," => %r{section is a required.+value is a required},
}.each do |parameter_list, error|
context parameter_list, pending: 'no error checking yet' do
pp = <<-EOS
ini_setting { "#{parameter_list}":
ensure => present,
path => "#{tmpdir}/ini_setting.ini",
#{parameter_list}
}
EOS
it_behaves_like 'has_error', "#{tmpdir}/ini_setting.ini", pp, error
end
end
end
describe 'path parameter' do
[
"#{tmpdir}/one.ini",
"#{tmpdir}/two.ini",
"#{tmpdir}/three.ini",
].each do |path|
context "path => #{path}" do
pp = <<-EOS
ini_setting { 'path => #{path}':
ensure => present,
section => 'one',
setting => 'two',
value => 'three',
path => '#{path}',
}
EOS
it_behaves_like 'has_content', path, pp, %r{\[one\]\ntwo = three}
end
end
context 'path => foo' do
pp = <<-EOS
ini_setting { 'path => foo':
ensure => present,
section => 'one',
setting => 'two',
value => 'three',
path => 'foo',
}
EOS
it_behaves_like 'has_error', 'foo', pp, %r{must be fully qualified}
end
end
describe 'key_val_separator parameter' do
{
'' => %r{two = three},
"key_val_separator => '='," => %r{two=three},
"key_val_separator => ' = '," => %r{two = three},
"key_val_separator => ' '," => %r{two three},
"key_val_separator => ' '," => %r{two three},
}.each do |parameter, content|
context "with \"#{parameter}\" makes \"#{content}\"" do
pp = <<-EOS
ini_setting { "with #{parameter} makes #{content}":
ensure => present,
section => 'one',
setting => 'two',
value => 'three',
path => "#{tmpdir}/key_val_separator.ini",
#{parameter}
}
EOS
it_behaves_like 'has_content', "#{tmpdir}/key_val_separator.ini", pp, content
end
end
end
describe 'show_diff parameter and logging:' do
[{ :value => 'initial_value', :matcher => 'created', :show_diff => true },
{ :value => 'public_value', :matcher => %r{initial_value.*public_value}, :show_diff => true },
{ :value => 'secret_value', :matcher => %r{redacted sensitive information.*redacted sensitive information}, :show_diff => false },
{ :value => 'md5_value', :matcher => %r{\{md5\}881671aa2bbc680bc530c4353125052b.*\{md5\}ed0903a7fa5de7886ca1a7a9ad06cf51}, :show_diff => :md5 }].each do |i|
pp = <<-EOS
ini_setting { 'test_show_diff':
ensure => present,
section => 'test',
setting => 'something',
value => '#{i[:value]}',
path => "#{tmpdir}/test_show_diff.ini",
show_diff => #{i[:show_diff]}
}
EOS
context "show_diff => #{i[:show_diff]}" do
config = { 'main' => { 'show_diff' => true } }
configure_puppet_on(default, config)
res = apply_manifest(pp, :expect_changes => true)
it 'applies manifest and expects changed value to be logged in proper form' do
expect(res.stdout).to match(i[:matcher])
end
it 'applies manifest and expects changed value to be logged in proper form #optional test' do
expect(res.stdout).not_to match(i[:value]) unless i[:show_diff] == true
end
end
end
end
end