diff options
author | Akira Kitada <akitada@gmail.com> | 2017-05-08 00:13:44 +0900 |
---|---|---|
committer | Akira Kitada <akitada@gmail.com> | 2017-05-10 23:39:22 +0900 |
commit | c7dd04cf25bfa9c4740fb36222e9e82c41038fee (patch) | |
tree | e6d7c51d6126c5d131a72c274877de4e19a00ddd | |
parent | a769920c0546caf6e90b38c71da91e327d87a080 (diff) | |
download | chef-c7dd04cf25bfa9c4740fb36222e9e82c41038fee.tar.gz |
Discard caches for files not in the manifest
cookbook_segment returns nil if the cache is not found in the manifest.
This can happen when chef client 12 runs on a node where chef client 13
generated cookbook cache files.
Currently, chef client fails in this case. This patch fixes it.
It should be safe to remove those cache files because they are not
cached in Chef 12, after all.
Signed-off-by: Akira Kitada <akitada@gmail.com>
-rw-r--r-- | lib/chef/cookbook/synchronizer.rb | 2 | ||||
-rw-r--r-- | spec/unit/cookbook/synchronizer_spec.rb | 12 |
2 files changed, 13 insertions, 1 deletions
diff --git a/lib/chef/cookbook/synchronizer.rb b/lib/chef/cookbook/synchronizer.rb index bb44bc3d5c..e243d19151 100644 --- a/lib/chef/cookbook/synchronizer.rb +++ b/lib/chef/cookbook/synchronizer.rb @@ -202,7 +202,7 @@ class Chef ( cookbook_name, segment, file ) = md[1..3] if have_cookbook?(cookbook_name) manifest_segment = cookbook_segment(cookbook_name, segment) - if manifest_segment.select { |manifest_record| manifest_record["path"] == "#{segment}/#{file}" }.empty? + if manifest_segment.nil? || manifest_segment.select { |manifest_record| manifest_record["path"] == "#{segment}/#{file}" }.empty? Chef::Log.info("Removing #{cache_file} from the cache; its is no longer in the cookbook manifest.") cache.delete(cache_file) @events.removed_cookbook_file(cache_file) diff --git a/spec/unit/cookbook/synchronizer_spec.rb b/spec/unit/cookbook/synchronizer_spec.rb index 82876273e7..d652ce4063 100644 --- a/spec/unit/cookbook/synchronizer_spec.rb +++ b/spec/unit/cookbook/synchronizer_spec.rb @@ -191,6 +191,18 @@ describe Chef::CookbookSynchronizer do allow(synchronizer).to receive(:cache).and_return(file_cache) synchronizer.remove_deleted_files end + + it "removes files not in the manifest" do + not_in_manifest_cb_file = %w{cookbooks/valid1/test/deleted.rb cookbooks/valid1/spec/deleted.rb} + expect(file_cache).to receive(:find).with(File.join(%w{cookbooks ** {*,.*}})).and_return(not_in_manifest_cb_file) + expect(synchronizer).to receive(:have_cookbook?).with("valid1").at_least(:once).and_return(true) + expect(file_cache).to receive(:delete).with("cookbooks/valid1/test/deleted.rb") + expect(file_cache).to receive(:delete).with("cookbooks/valid1/spec/deleted.rb") + expect(synchronizer).to receive(:cookbook_segment).with("valid1", "test").at_least(:once).and_return(nil) + expect(synchronizer).to receive(:cookbook_segment).with("valid1", "spec").at_least(:once).and_return(nil) + allow(synchronizer).to receive(:cache).and_return(file_cache) + synchronizer.remove_deleted_files + end end let(:cookbook_a_default_recipe_tempfile) do |