diff options
| author | Luke Kanies <luke@madstop.com> | 2009-08-07 17:39:44 -0700 |
|---|---|---|
| committer | James Turnbull <james@lovedthanlost.net> | 2009-08-10 17:36:17 +1000 |
| commit | 0cb9072c0e3b37332f4eeaeff061950d6f73d021 (patch) | |
| tree | bb813907b006c28601117fc939887cd437a21485 | |
| parent | 23948d0b7efb482f891a333d4af56dc5ac59c00f (diff) | |
| download | puppet-0cb9072c0e3b37332f4eeaeff061950d6f73d021.tar.gz puppet-0cb9072c0e3b37332f4eeaeff061950d6f73d021.tar.xz puppet-0cb9072c0e3b37332f4eeaeff061950d6f73d021.zip | |
Fixing #2541 - file cache is more resilient to failure
Signed-off-by: Luke Kanies <luke@madstop.com>
| -rw-r--r-- | lib/puppet/util/autoload/file_cache.rb | 20 | ||||
| -rwxr-xr-x | spec/unit/util/autoload/file_cache.rb | 30 |
2 files changed, 42 insertions, 8 deletions
diff --git a/lib/puppet/util/autoload/file_cache.rb b/lib/puppet/util/autoload/file_cache.rb index 12400f620..4ad0c7f39 100644 --- a/lib/puppet/util/autoload/file_cache.rb +++ b/lib/puppet/util/autoload/file_cache.rb @@ -23,7 +23,7 @@ module Puppet::Util::Autoload::FileCache cache = cached_data?(path, :directory?) return cache unless cache.nil? - begin + protect(path) do stat = File.lstat(path) if stat.directory? found_file(path, stat) @@ -32,9 +32,6 @@ module Puppet::Util::Autoload::FileCache missing_file(path) return false end - rescue Errno::ENOENT - missing_file(path) - return false end end @@ -42,13 +39,10 @@ module Puppet::Util::Autoload::FileCache cache = cached_data?(path) return cache unless cache.nil? - begin + protect(path) do stat = File.lstat(path) found_file(path, stat) return true - rescue Errno::ENOENT - missing_file(path) - return false end end @@ -108,4 +102,14 @@ module Puppet::Util::Autoload::FileCache def data_expired?(time) Time.now - time > 15 end + + def protect(path) + begin + yield + rescue => detail + raise unless detail.class.to_s.include?("Errno") + missing_file(path) + return false + end + end end diff --git a/spec/unit/util/autoload/file_cache.rb b/spec/unit/util/autoload/file_cache.rb index 333ddb545..59620ed2d 100755 --- a/spec/unit/util/autoload/file_cache.rb +++ b/spec/unit/util/autoload/file_cache.rb @@ -31,6 +31,21 @@ describe Puppet::Util::Autoload::FileCache do @cacher.should_not be_file_exist("/my/file") end + it "should consider a file as absent if the directory is absent" do + File.expects(:lstat).with("/my/file").raises Errno::ENOTDIR + @cacher.should_not be_file_exist("/my/file") + end + + it "should consider a file as absent permissions are missing" do + File.expects(:lstat).with("/my/file").raises Errno::EACCES + @cacher.should_not be_file_exist("/my/file") + end + + it "should raise non-fs exceptions" do + File.expects(:lstat).with("/my/file").raises ArgumentError + lambda { @cacher.file_exist?("/my/file") }.should raise_error(ArgumentError) + end + it "should consider a file as present if its lstat succeeds" do File.expects(:lstat).with("/my/file").returns mock("stat") @cacher.should be_file_exist("/my/file") @@ -87,6 +102,21 @@ describe Puppet::Util::Autoload::FileCache do @cacher.should_not be_directory_exist("/my/file") end + it "should consider a file as absent if the directory is absent" do + File.expects(:lstat).with("/my/file").raises Errno::ENOTDIR + @cacher.should_not be_directory_exist("/my/file") + end + + it "should consider a file as absent permissions are missing" do + File.expects(:lstat).with("/my/file").raises Errno::EACCES + @cacher.should_not be_directory_exist("/my/file") + end + + it "should raise non-fs exceptions" do + File.expects(:lstat).with("/my/file").raises ArgumentError + lambda { @cacher.directory_exist?("/my/file") }.should raise_error(ArgumentError) + end + it "should consider a directory as present if its lstat succeeds and the stat is of a directory" do @stat.expects(:directory?).returns true File.expects(:lstat).with("/my/file").returns @stat |
