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
|
#! /usr/bin/env ruby
require 'spec_helper'
require 'puppet/util/backups'
describe Puppet::Util::Backups do
include PuppetSpec::Files
let(:bucket) { stub('bucket', :name => "foo") }
let!(:file) do
f = Puppet::Type.type(:file).new(:name => path, :backup => 'foo')
f.stubs(:bucket).returns(bucket)
f
end
describe "when backing up a file" do
let(:path) { make_absolute('/no/such/file') }
it "should noop if the file does not exist" do
file = Puppet::Type.type(:file).new(:name => path)
file.expects(:bucket).never
Puppet::FileSystem.expects(:exist?).with(path).returns false
file.perform_backup
end
it "should succeed silently if self[:backup] is false" do
file = Puppet::Type.type(:file).new(:name => path, :backup => false)
file.expects(:bucket).never
Puppet::FileSystem.expects(:exist?).never
file.perform_backup
end
it "a bucket should be used when provided" do
lstat_path_as(path, 'file')
bucket.expects(:backup).with(path).returns("mysum")
Puppet::FileSystem.expects(:exist?).with(path).returns(true)
file.perform_backup
end
it "should propagate any exceptions encountered when backing up to a filebucket" do
lstat_path_as(path, 'file')
bucket.expects(:backup).raises ArgumentError
Puppet::FileSystem.expects(:exist?).with(path).returns(true)
expect { file.perform_backup }.to raise_error(ArgumentError)
end
describe "and local backup is configured" do
let(:ext) { 'foobkp' }
let(:backup) { path + '.' + ext }
let(:file) { Puppet::Type.type(:file).new(:name => path, :backup => '.'+ext) }
it "should remove any local backup if one exists" do
lstat_path_as(backup, 'file')
Puppet::FileSystem.expects(:unlink).with(backup)
FileUtils.stubs(:cp_r)
Puppet::FileSystem.expects(:exist?).with(path).returns(true)
file.perform_backup
end
it "should fail when the old backup can't be removed" do
lstat_path_as(backup, 'file')
Puppet::FileSystem.expects(:unlink).with(backup).raises ArgumentError
FileUtils.expects(:cp_r).never
Puppet::FileSystem.expects(:exist?).with(path).returns(true)
expect { file.perform_backup }.to raise_error(Puppet::Error)
end
it "should not try to remove backups that don't exist" do
Puppet::FileSystem.expects(:lstat).with(backup).raises(Errno::ENOENT)
Puppet::FileSystem.expects(:unlink).with(backup).never
FileUtils.stubs(:cp_r)
Puppet::FileSystem.expects(:exist?).with(path).returns(true)
file.perform_backup
end
it "a copy should be created in the local directory" do
FileUtils.expects(:cp_r).with(path, backup, :preserve => true)
Puppet::FileSystem.stubs(:exist?).with(path).returns(true)
expect(file.perform_backup).to be_truthy
end
it "should propagate exceptions if no backup can be created" do
FileUtils.expects(:cp_r).raises ArgumentError
Puppet::FileSystem.stubs(:exist?).with(path).returns(true)
expect { file.perform_backup }.to raise_error(Puppet::Error)
end
end
end
describe "when backing up a directory" do
let(:path) { make_absolute('/my/dir') }
let(:filename) { File.join(path, 'file') }
it "a bucket should work when provided" do
File.stubs(:file?).with(filename).returns true
Find.expects(:find).with(path).yields(filename)
bucket.expects(:backup).with(filename).returns true
lstat_path_as(path, 'directory')
Puppet::FileSystem.stubs(:exist?).with(path).returns(true)
Puppet::FileSystem.stubs(:exist?).with(filename).returns(true)
file.perform_backup
end
it "should do nothing when recursing" do
file = Puppet::Type.type(:file).new(:name => path, :backup => 'foo', :recurse => true)
bucket.expects(:backup).never
stub_file = stub('file', :stat => stub('stat', :ftype => 'directory'))
Puppet::FileSystem.stubs(:new).with(path).returns stub_file
Find.expects(:find).never
file.perform_backup
end
end
def lstat_path_as(path, ftype)
Puppet::FileSystem.expects(:lstat).with(path).returns(stub('File::Stat', :ftype => ftype))
end
end
|