~ubuntu-branches/ubuntu/trusty/puppet/trusty

« back to all changes in this revision

Viewing changes to spec/unit/provider/augeas/augeas_spec.rb

  • Committer: Package Import Robot
  • Author(s): Stig Sandbeck Mathisen
  • Date: 2011-10-22 14:08:22 UTC
  • mfrom: (1.1.25) (3.1.32 sid)
  • Revision ID: package-import@ubuntu.com-20111022140822-odxde5lohc45yhuz
Tags: 2.7.6-1
* New upstream release (CVE-2011-3872)
* Remove cherry-picked "groupadd_aix_warning" patch
* Install all new manpages

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
provider_class = Puppet::Type.type(:augeas).provider(:augeas)
5
5
 
6
6
describe provider_class do
7
 
 
8
7
  describe "command parsing" do
9
8
    before do
10
9
      @resource = stub("resource")
252
251
    it "should handle no filters" do
253
252
      resource = stub("resource")
254
253
      resource.stubs(:[]).returns(false).then.returns("").then.returns("")
 
254
      resource.stubs(:noop?).returns(false)
255
255
      augeas_stub = stub("augeas", :match => ["set", "of", "values"])
256
256
      augeas_stub.stubs("close")
257
257
      provider = provider_class.new(resource)
263
263
    it "should return true when a get filter matches" do
264
264
      resource = stub("resource")
265
265
      resource.stubs(:[]).returns(false).then.returns("get path == value").then.returns("")
 
266
      resource.stubs(:noop?).returns(false)
266
267
      provider = provider_class.new(resource)
267
268
      augeas_stub = stub("augeas", :get => "value")
268
269
      augeas_stub.stubs("close")
285
286
    it "should return true when a match filter matches" do
286
287
      resource = stub("resource")
287
288
      resource.stubs(:[]).returns(false).then.returns("match path size == 3").then.returns("")
 
289
      resource.stubs(:noop?).returns(false)
288
290
      provider = provider_class.new(resource)
289
291
      augeas_stub = stub("augeas", :match => ["set", "of", "values"])
290
292
      augeas_stub.stubs("close")
320
322
    it "should return true when a size != the provided value" do
321
323
      resource = stub("resource")
322
324
      resource.stubs(:[]).returns(false).then.returns("match path size != 17").then.returns("")
 
325
      resource.stubs(:noop?).returns(false)
323
326
      provider = provider_class.new(resource)
324
327
      augeas_stub = stub("augeas", :match => ["set", "of", "values"])
325
328
      augeas_stub.stubs("close")
339
342
      provider.stubs(:get_augeas_version).returns("0.3.5")
340
343
      provider.need_to_run?.should == false
341
344
    end
 
345
 
 
346
    # Ticket 2728 (diff files)
 
347
    describe "and Puppet[:show_diff] is set", :if => Puppet.features.augeas? do
 
348
      before do
 
349
        Puppet[:show_diff] = true
 
350
 
 
351
        @resource = Puppet::Type.type(:augeas).new(:name => "test")
 
352
        @provider = provider_class.new(@resource)
 
353
        @augeas_stub = stub("augeas")
 
354
        @provider.aug = @augeas_stub
 
355
 
 
356
        @augeas_stub.stubs("get").with("/augeas/version").returns("0.7.2")
 
357
        @augeas_stub.stubs(:set).returns(true)
 
358
        @augeas_stub.stubs(:save).returns(true)
 
359
      end
 
360
 
 
361
      it "should call diff when a file is shown to have been changed" do
 
362
        file = "/etc/hosts"
 
363
 
 
364
        @resource[:context] = "/files"
 
365
        @resource[:changes] = ["set #{file}/foo bar"]
 
366
 
 
367
        @augeas_stub.stubs(:match).with("/augeas/events/saved").returns(["/augeas/events/saved"])
 
368
        @augeas_stub.stubs(:get).with("/augeas/events/saved").returns(["/files#{file}"])
 
369
        @augeas_stub.expects(:set).with("/augeas/save", "newfile")
 
370
        @augeas_stub.expects(:close).never()
 
371
 
 
372
        @provider.expects("diff").with("#{file}", "#{file}.augnew").returns("")
 
373
        @provider.should be_need_to_run
 
374
      end
 
375
 
 
376
      it "should call diff for each file thats changed" do
 
377
        file1 = "/etc/hosts"
 
378
        file2 = "/etc/resolv.conf"
 
379
 
 
380
        @resource[:context] = "/files"
 
381
        @resource[:changes] = ["set #{file1}/foo bar", "set #{file2}/baz biz"]
 
382
 
 
383
        @augeas_stub.stubs(:match).with("/augeas/events/saved").returns(["/augeas/events/saved[1]", "/augeas/events/saved[2]"])
 
384
        @augeas_stub.stubs(:get).with("/augeas/events/saved[1]").returns(["/files#{file1}"])
 
385
        @augeas_stub.stubs(:get).with("/augeas/events/saved[2]").returns(["/files#{file2}"])
 
386
        @augeas_stub.expects(:set).with("/augeas/save", "newfile")
 
387
        @augeas_stub.expects(:close).never()
 
388
 
 
389
        @provider.expects(:diff).with("#{file1}", "#{file1}.augnew").returns("")
 
390
        @provider.expects(:diff).with("#{file2}", "#{file2}.augnew").returns("")
 
391
        @provider.should be_need_to_run
 
392
      end
 
393
 
 
394
      describe "and resource[:root] is set" do
 
395
        it "should call diff when a file is shown to have been changed" do
 
396
          root = "/tmp/foo"
 
397
          file = "/etc/hosts"
 
398
 
 
399
          @resource[:context] = "/files"
 
400
          @resource[:changes] = ["set #{file}/foo bar"]
 
401
          @resource[:root] = root
 
402
 
 
403
          @augeas_stub.stubs(:match).with("/augeas/events/saved").returns(["/augeas/events/saved"])
 
404
          @augeas_stub.stubs(:get).with("/augeas/events/saved").returns(["/files#{file}"])
 
405
          @augeas_stub.expects(:set).with("/augeas/save", "newfile")
 
406
          @augeas_stub.expects(:close).never()
 
407
 
 
408
          @provider.expects(:diff).with("#{root}#{file}", "#{root}#{file}.augnew").returns("")
 
409
          @provider.should be_need_to_run
 
410
        end
 
411
      end
 
412
 
 
413
      it "should not call diff if no files change" do
 
414
        file = "/etc/hosts"
 
415
 
 
416
        @resource[:context] = "/files"
 
417
        @resource[:changes] = ["set #{file}/foo bar"]
 
418
 
 
419
        @augeas_stub.stubs(:match).with("/augeas/events/saved").returns([])
 
420
        @augeas_stub.expects(:set).with("/augeas/save", "newfile")
 
421
        @augeas_stub.expects(:get).with("/augeas/events/saved").never()
 
422
        @augeas_stub.expects(:close)
 
423
 
 
424
        @provider.expects(:diff).never()
 
425
        @provider.should_not be_need_to_run
 
426
      end
 
427
 
 
428
      it "should cleanup when in noop mode" do
 
429
        file = "/etc/hosts"
 
430
 
 
431
        @resource[:noop] = true
 
432
        @resource[:context] = "/files"
 
433
        @resource[:changes] = ["set #{file}/foo bar"]
 
434
 
 
435
        @augeas_stub.stubs(:match).with("/augeas/events/saved").returns(["/augeas/events/saved"])
 
436
        @augeas_stub.stubs(:get).with("/augeas/events/saved").returns(["/files#{file}"])
 
437
        @augeas_stub.expects(:set).with("/augeas/save", "newfile")
 
438
        @augeas_stub.expects(:close)
 
439
 
 
440
        File.expects(:delete).with(file + ".augnew")
 
441
 
 
442
        @provider.expects(:diff).with("#{file}", "#{file}.augnew").returns("")
 
443
        @provider.should be_need_to_run
 
444
      end
 
445
 
 
446
      it "should fail with an error if saving fails" do
 
447
        file = "/etc/hosts"
 
448
 
 
449
        @resource[:context] = "/files"
 
450
        @resource[:changes] = ["set #{file}/foo bar"]
 
451
 
 
452
        @augeas_stub.stubs(:save).returns(false)
 
453
        @augeas_stub.stubs(:match).with("/augeas/events/saved").returns([])
 
454
        @augeas_stub.expects(:close)
 
455
 
 
456
        @provider.expects(:diff).never()
 
457
        lambda { @provider.need_to_run? }.should raise_error
 
458
      end
 
459
    end
342
460
  end
343
461
 
344
462
  describe "augeas execution integration" do
349
467
      @augeas = stub("augeas")
350
468
      @provider.aug= @augeas
351
469
      @provider.stubs(:get_augeas_version).returns("0.3.5")
 
470
      @augeas.stubs(:match).with("/augeas/events/saved")
352
471
    end
353
472
 
354
473
    it "should handle set commands" do