Blob Blame History Raw
From 66f688f2b3ddc795a087c81746da57190675e624 Mon Sep 17 00:00:00 2001
From: Takashi Kajinami <tkajinam@redhat.com>
Date: Wed, 27 Apr 2022 15:33:16 +0900
Subject: [PATCH] (FACT-3116) Ignore EROFS when deleting fact cache

When fact cache is placed in read only file system, deleting a file
fails with EROFS instead of EACCES .
---
 lib/facter/framework/core/cache_manager.rb |  2 +-
 spec/facter/cache_manager_spec.rb          | 20 +++++++++++++++++++-
 2 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/lib/facter/framework/core/cache_manager.rb b/lib/facter/framework/core/cache_manager.rb
index 100a7656a..d93020264 100644
--- a/lib/facter/framework/core/cache_manager.rb
+++ b/lib/facter/framework/core/cache_manager.rb
@@ -227,7 +227,7 @@ cache_format_version is incorrect!")
 
       begin
         File.delete(cache_file_name) if File.readable?(cache_file_name)
-      rescue Errno::EACCES => e
+      rescue Errno::EACCES, Errno::EROFS => e
         @log.warn("Could not delete cache: #{e.message}")
       end
     end
diff --git a/spec/facter/cache_manager_spec.rb b/spec/facter/cache_manager_spec.rb
index 8b3194340..324964bd5 100644
--- a/spec/facter/cache_manager_spec.rb
+++ b/spec/facter/cache_manager_spec.rb
@@ -132,7 +132,7 @@ describe Facter::CacheManager do
         )
       end
 
-      context 'when file cannot be deleted' do
+      context 'when file cannot be deleted because of access denied' do
         let(:cache_file) { File.join(cache_dir, 'ext_file.txt') }
 
         it 'logs warn if it cannot delete' do
@@ -149,6 +149,24 @@ describe Facter::CacheManager do
           expect(logger).to have_received(:warn)
         end
       end
+
+      context 'when file cannot be deleted because of read-only filesystem' do
+        let(:cache_file) { File.join(cache_dir, 'ext_file.txt') }
+
+        it 'logs warn if it cannot delete' do
+          allow(fact_groups).to receive(:get_fact).with('ext_file.txt').and_return(external_fact)
+          allow(File).to receive(:readable?).with(cache_file_name).and_return(false)
+          allow(Facter::Util::FileHelper).to receive(:safe_read).with(cache_file)
+                                                                .and_return(cached_external_fact)
+          allow(JSON).to receive(:parse).with(cached_external_fact).and_raise(JSON::ParserError)
+          allow(File).to receive(:readable?).with(cache_file).and_return(true)
+          allow(File).to receive(:mtime).with(File.join(cache_dir, 'ext_file.txt')).and_return(Time.now)
+          allow(File).to receive(:delete).with(cache_file).and_raise(Errno::EROFS)
+
+          cache_manager.resolve_facts([searched_external_fact])
+          expect(logger).to have_received(:warn)
+        end
+      end
     end
 
     context 'with timer' do
-- 
2.35.1