source: other-projects/hathitrust/vagrant-hadoop-cluster/trunk/modules/stdlib/spec/unit/puppet/provider/file_line/ruby_spec.rb@ 30903

Last change on this file since 30903 was 30903, checked in by davidb, 7 years ago

Vagrant provisioning files for a 4-node Hadoop cluster. See README.txt for more details

  • Property svn:executable set to *
File size: 13.8 KB
Line 
1#! /usr/bin/env ruby -S rspec
2require 'spec_helper'
3require 'tempfile'
4provider_class = Puppet::Type.type(:file_line).provider(:ruby)
5describe provider_class do
6 context "when adding" do
7 let :tmpfile do
8 tmp = Tempfile.new('tmp')
9 path = tmp.path
10 tmp.close!
11 path
12 end
13 let :resource do
14 Puppet::Type::File_line.new(
15 {:name => 'foo', :path => tmpfile, :line => 'foo'}
16 )
17 end
18 let :provider do
19 provider_class.new(resource)
20 end
21
22 it 'should detect if the line exists in the file' do
23 File.open(tmpfile, 'w') do |fh|
24 fh.write('foo')
25 end
26 expect(provider.exists?).to be_truthy
27 end
28 it 'should detect if the line does not exist in the file' do
29 File.open(tmpfile, 'w') do |fh|
30 fh.write('foo1')
31 end
32 expect(provider.exists?).to be_nil
33 end
34 it 'should append to an existing file when creating' do
35 provider.create
36 expect(File.read(tmpfile).chomp).to eq('foo')
37 end
38 end
39 context 'when using replace' do
40 before :each do
41 # TODO: these should be ported over to use the PuppetLabs spec_helper
42 # file fixtures once the following pull request has been merged:
43 # https://github.com/puppetlabs/puppetlabs-stdlib/pull/73/files
44 tmp = Tempfile.new('tmp')
45 @tmpfile = tmp.path
46 tmp.close!
47 @resource = Puppet::Type::File_line.new(
48 {
49 :name => 'foo',
50 :path => @tmpfile,
51 :line => 'foo = bar',
52 :match => '^foo\s*=.*$',
53 :replace => false,
54 }
55 )
56 @provider = provider_class.new(@resource)
57 end
58
59 it 'should not replace the matching line' do
60 File.open(@tmpfile, 'w') do |fh|
61 fh.write("foo1\nfoo=blah\nfoo2\nfoo3")
62 end
63 expect(@provider.exists?).to be_truthy
64 @provider.create
65 expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo=blah\nfoo2\nfoo3")
66 end
67
68 it 'should append the line if no matches are found' do
69 File.open(@tmpfile, 'w') do |fh|
70 fh.write("foo1\nfoo2")
71 end
72 expect(@provider.exists?).to be_nil
73 @provider.create
74 expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo2\nfoo = bar")
75 end
76
77 it 'should raise an error with invalid values' do
78 expect {
79 @resource = Puppet::Type::File_line.new(
80 {
81 :name => 'foo',
82 :path => @tmpfile,
83 :line => 'foo = bar',
84 :match => '^foo\s*=.*$',
85 :replace => 'asgadga',
86 }
87 )
88 }.to raise_error(Puppet::Error, /Invalid value "asgadga"\. Valid values are true, false\./)
89 end
90 end
91 context "when matching" do
92 before :each do
93 # TODO: these should be ported over to use the PuppetLabs spec_helper
94 # file fixtures once the following pull request has been merged:
95 # https://github.com/puppetlabs/puppetlabs-stdlib/pull/73/files
96 tmp = Tempfile.new('tmp')
97 @tmpfile = tmp.path
98 tmp.close!
99 @resource = Puppet::Type::File_line.new(
100 {
101 :name => 'foo',
102 :path => @tmpfile,
103 :line => 'foo = bar',
104 :match => '^foo\s*=.*$',
105 }
106 )
107 @provider = provider_class.new(@resource)
108 end
109
110 describe 'using match' do
111 it 'should raise an error if more than one line matches, and should not have modified the file' do
112 File.open(@tmpfile, 'w') do |fh|
113 fh.write("foo1\nfoo=blah\nfoo2\nfoo=baz")
114 end
115 expect(@provider.exists?).to be_nil
116 expect { @provider.create }.to raise_error(Puppet::Error, /More than one line.*matches/)
117 expect(File.read(@tmpfile)).to eql("foo1\nfoo=blah\nfoo2\nfoo=baz")
118 end
119
120 it 'should replace all lines that matches' do
121 @resource = Puppet::Type::File_line.new(
122 {
123 :name => 'foo',
124 :path => @tmpfile,
125 :line => 'foo = bar',
126 :match => '^foo\s*=.*$',
127 :multiple => true,
128 }
129 )
130 @provider = provider_class.new(@resource)
131 File.open(@tmpfile, 'w') do |fh|
132 fh.write("foo1\nfoo=blah\nfoo2\nfoo=baz")
133 end
134 expect(@provider.exists?).to be_nil
135 @provider.create
136 expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2\nfoo = bar")
137 end
138
139 it 'should raise an error with invalid values' do
140 expect {
141 @resource = Puppet::Type::File_line.new(
142 {
143 :name => 'foo',
144 :path => @tmpfile,
145 :line => 'foo = bar',
146 :match => '^foo\s*=.*$',
147 :multiple => 'asgadga',
148 }
149 )
150 }.to raise_error(Puppet::Error, /Invalid value "asgadga"\. Valid values are true, false\./)
151 end
152
153 it 'should replace a line that matches' do
154 File.open(@tmpfile, 'w') do |fh|
155 fh.write("foo1\nfoo=blah\nfoo2")
156 end
157 expect(@provider.exists?).to be_nil
158 @provider.create
159 expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2")
160 end
161 it 'should add a new line if no lines match' do
162 File.open(@tmpfile, 'w') do |fh|
163 fh.write("foo1\nfoo2")
164 end
165 expect(@provider.exists?).to be_nil
166 @provider.create
167 expect(File.read(@tmpfile)).to eql("foo1\nfoo2\nfoo = bar\n")
168 end
169 it 'should do nothing if the exact line already exists' do
170 File.open(@tmpfile, 'w') do |fh|
171 fh.write("foo1\nfoo = bar\nfoo2")
172 end
173 expect(@provider.exists?).to be_truthy
174 @provider.create
175 expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2")
176 end
177 end
178
179 describe 'using after' do
180 let :resource do
181 Puppet::Type::File_line.new(
182 {
183 :name => 'foo',
184 :path => @tmpfile,
185 :line => 'inserted = line',
186 :after => '^foo1',
187 }
188 )
189 end
190
191 let :provider do
192 provider_class.new(resource)
193 end
194 context 'match and after set' do
195 shared_context 'resource_create' do
196 let(:match) { '^foo2$' }
197 let(:after) { '^foo1$' }
198 let(:resource) {
199 Puppet::Type::File_line.new(
200 {
201 :name => 'foo',
202 :path => @tmpfile,
203 :line => 'inserted = line',
204 :after => after,
205 :match => match,
206 }
207 )
208 }
209 end
210 before :each do
211 File.open(@tmpfile, 'w') do |fh|
212 fh.write("foo1\nfoo2\nfoo = baz")
213 end
214 end
215 describe 'inserts at match' do
216 include_context 'resource_create'
217 it {
218 provider.create
219 expect(File.read(@tmpfile).chomp).to eq("foo1\ninserted = line\nfoo = baz")
220 }
221 end
222 describe 'inserts a new line after when no match' do
223 include_context 'resource_create' do
224 let(:match) { '^nevergoingtomatch$' }
225 end
226 it {
227 provider.create
228 expect(File.read(@tmpfile).chomp).to eq("foo1\ninserted = line\nfoo2\nfoo = baz")
229 }
230 end
231 describe 'append to end of file if no match for both after and match' do
232 include_context 'resource_create' do
233 let(:match) { '^nevergoingtomatch$' }
234 let(:after) { '^stillneverafter' }
235 end
236 it {
237 provider.create
238 expect(File.read(@tmpfile).chomp).to eq("foo1\nfoo2\nfoo = baz\ninserted = line")
239 }
240 end
241 end
242 context 'with one line matching the after expression' do
243 before :each do
244 File.open(@tmpfile, 'w') do |fh|
245 fh.write("foo1\nfoo = blah\nfoo2\nfoo = baz")
246 end
247 end
248
249 it 'inserts the specified line after the line matching the "after" expression' do
250 provider.create
251 expect(File.read(@tmpfile).chomp).to eql("foo1\ninserted = line\nfoo = blah\nfoo2\nfoo = baz")
252 end
253 end
254
255 context 'with multiple lines matching the after expression' do
256 before :each do
257 File.open(@tmpfile, 'w') do |fh|
258 fh.write("foo1\nfoo = blah\nfoo2\nfoo1\nfoo = baz")
259 end
260 end
261
262 it 'errors out stating "One or no line must match the pattern"' do
263 expect { provider.create }.to raise_error(Puppet::Error, /One or no line must match the pattern/)
264 end
265
266 it 'adds the line after all lines matching the after expression' do
267 @resource = Puppet::Type::File_line.new(
268 {
269 :name => 'foo',
270 :path => @tmpfile,
271 :line => 'inserted = line',
272 :after => '^foo1$',
273 :multiple => true,
274 }
275 )
276 @provider = provider_class.new(@resource)
277 expect(@provider.exists?).to be_nil
278 @provider.create
279 expect(File.read(@tmpfile).chomp).to eql("foo1\ninserted = line\nfoo = blah\nfoo2\nfoo1\ninserted = line\nfoo = baz")
280 end
281 end
282
283 context 'with no lines matching the after expression' do
284 let :content do
285 "foo3\nfoo = blah\nfoo2\nfoo = baz\n"
286 end
287
288 before :each do
289 File.open(@tmpfile, 'w') do |fh|
290 fh.write(content)
291 end
292 end
293
294 it 'appends the specified line to the file' do
295 provider.create
296 expect(File.read(@tmpfile)).to eq(content << resource[:line] << "\n")
297 end
298 end
299 end
300 end
301
302 context "when removing" do
303 before :each do
304 # TODO: these should be ported over to use the PuppetLabs spec_helper
305 # file fixtures once the following pull request has been merged:
306 # https://github.com/puppetlabs/puppetlabs-stdlib/pull/73/files
307 tmp = Tempfile.new('tmp')
308 @tmpfile = tmp.path
309 tmp.close!
310 @resource = Puppet::Type::File_line.new(
311 {
312 :name => 'foo',
313 :path => @tmpfile,
314 :line => 'foo',
315 :ensure => 'absent',
316 }
317 )
318 @provider = provider_class.new(@resource)
319 end
320 it 'should remove the line if it exists' do
321 File.open(@tmpfile, 'w') do |fh|
322 fh.write("foo1\nfoo\nfoo2")
323 end
324 @provider.destroy
325 expect(File.read(@tmpfile)).to eql("foo1\nfoo2")
326 end
327
328 it 'should remove the line without touching the last new line' do
329 File.open(@tmpfile, 'w') do |fh|
330 fh.write("foo1\nfoo\nfoo2\n")
331 end
332 @provider.destroy
333 expect(File.read(@tmpfile)).to eql("foo1\nfoo2\n")
334 end
335
336 it 'should remove any occurence of the line' do
337 File.open(@tmpfile, 'w') do |fh|
338 fh.write("foo1\nfoo\nfoo2\nfoo\nfoo")
339 end
340 @provider.destroy
341 expect(File.read(@tmpfile)).to eql("foo1\nfoo2\n")
342 end
343 end
344
345 context "when removing with a match" do
346 before :each do
347 # TODO: these should be ported over to use the PuppetLabs spec_helper
348 # file fixtures once the following pull request has been merged:
349 # https://github.com/puppetlabs/puppetlabs-stdlib/pull/73/files
350 tmp = Tempfile.new('tmp')
351 @tmpfile = tmp.path
352 tmp.close!
353 @resource = Puppet::Type::File_line.new(
354 {
355 :name => 'foo',
356 :path => @tmpfile,
357 :line => 'foo2',
358 :ensure => 'absent',
359 :match => 'o$',
360 :match_for_absence => true,
361 }
362 )
363 @provider = provider_class.new(@resource)
364 end
365
366 it 'should find a line to match' do
367 File.open(@tmpfile, 'w') do |fh|
368 fh.write("foo1\nfoo\nfoo2")
369 end
370 expect(@provider.exists?).to be_truthy
371 end
372
373 it 'should remove one line if it matches' do
374 File.open(@tmpfile, 'w') do |fh|
375 fh.write("foo1\nfoo\nfoo2")
376 end
377 @provider.destroy
378 expect(File.read(@tmpfile)).to eql("foo1\nfoo2")
379 end
380
381 it 'should raise an error if more than one line matches' do
382 File.open(@tmpfile, 'w') do |fh|
383 fh.write("foo1\nfoo\nfoo2\nfoo\nfoo")
384 end
385 expect { @provider.destroy }.to raise_error(Puppet::Error, /More than one line/)
386 end
387
388 it 'should remove multiple lines if :multiple is true' do
389 @resource = Puppet::Type::File_line.new(
390 {
391 :name => 'foo',
392 :path => @tmpfile,
393 :line => 'foo2',
394 :ensure => 'absent',
395 :match => 'o$',
396 :multiple => true,
397 :match_for_absence => true,
398 }
399 )
400 @provider = provider_class.new(@resource)
401 File.open(@tmpfile, 'w') do |fh|
402 fh.write("foo1\nfoo\nfoo2\nfoo\nfoo")
403 end
404 @provider.destroy
405 expect(File.read(@tmpfile)).to eql("foo1\nfoo2\n")
406 end
407
408 it 'should ignore the match if match_for_absence is not specified' do
409 @resource = Puppet::Type::File_line.new(
410 {
411 :name => 'foo',
412 :path => @tmpfile,
413 :line => 'foo2',
414 :ensure => 'absent',
415 :match => 'o$',
416 }
417 )
418 @provider = provider_class.new(@resource)
419 File.open(@tmpfile, 'w') do |fh|
420 fh.write("foo1\nfoo\nfoo2")
421 end
422 @provider.destroy
423 expect(File.read(@tmpfile)).to eql("foo1\nfoo\n")
424 end
425
426 it 'should ignore the match if match_for_absence is false' do
427 @resource = Puppet::Type::File_line.new(
428 {
429 :name => 'foo',
430 :path => @tmpfile,
431 :line => 'foo2',
432 :ensure => 'absent',
433 :match => 'o$',
434 :match_for_absence => false,
435 }
436 )
437 @provider = provider_class.new(@resource)
438 File.open(@tmpfile, 'w') do |fh|
439 fh.write("foo1\nfoo\nfoo2")
440 end
441 @provider.destroy
442 expect(File.read(@tmpfile)).to eql("foo1\nfoo\n")
443 end
444
445 end
446
447end
Note: See TracBrowser for help on using the repository browser.