summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoshua Miller <joshmiller@fb.com>2021-03-11 09:42:18 -0500
committerTim Smith <tsmith84@gmail.com>2021-04-02 09:31:55 -0700
commit819b374aaf48b81c2a7df71f8fe40e70324e3f20 (patch)
tree9d5fbe754d90608ee820c708115ed31ace5b01a2
parentd69c651e6a3c644611dd6ffc989903c8e3470b79 (diff)
downloadchef-819b374aaf48b81c2a7df71f8fe40e70324e3f20.tar.gz
only run file verifiers when the contents changed
Signed-off-by: Joshua Miller <joshmiller@fb.com>
-rw-r--r--lib/chef/provider/file.rb2
-rw-r--r--spec/support/shared/unit/provider/file.rb14
2 files changed, 15 insertions, 1 deletions
diff --git a/lib/chef/provider/file.rb b/lib/chef/provider/file.rb
index e2c07ad9f7..fa77015153 100644
--- a/lib/chef/provider/file.rb
+++ b/lib/chef/provider/file.rb
@@ -338,7 +338,7 @@ class Chef
raise Chef::Exceptions::ChecksumMismatch.new(short_cksum(new_resource.checksum), short_cksum(tempfile_checksum))
end
- if tempfile
+ if tempfile && contents_changed?
new_resource.verify.each do |v|
unless v.verify(tempfile.path)
backupfile = "#{Chef::Config[:file_cache_path]}/failed_validations/#{::File.basename(tempfile.path)}"
diff --git a/spec/support/shared/unit/provider/file.rb b/spec/support/shared/unit/provider/file.rb
index f624a6ae44..654765e82d 100644
--- a/spec/support/shared/unit/provider/file.rb
+++ b/spec/support/shared/unit/provider/file.rb
@@ -479,12 +479,14 @@ shared_examples_for Chef::Provider::File do
it "calls #verify on each verification with tempfile path" do
provider.new_resource.verify windows? ? "REM" : "true"
provider.new_resource.verify windows? ? "REM" : "true"
+ allow(provider).to receive(:contents_changed?).and_return(true)
provider.send(:do_validate_content)
end
it "raises an exception if any verification fails" do
allow(File).to receive(:directory?).with("C:\\Windows\\system32/cmd.exe").and_return(false)
allow(provider).to receive(:tempfile).and_return(tempfile)
+ allow(provider).to receive(:contents_changed?).and_return(true)
provider.new_resource.verify windows? ? "cmd.exe c exit 1" : "false"
provider.new_resource.verify.each do |v|
allow(v).to receive(:verify).and_return(false)
@@ -492,9 +494,21 @@ shared_examples_for Chef::Provider::File do
expect { provider.send(:do_validate_content) }.to raise_error(Chef::Exceptions::ValidationFailed)
end
+ it "does not run verifications when the contents did not change" do
+ allow(File).to receive(:directory?).with("C:\\Windows\\system32/cmd.exe").and_return(false)
+ allow(provider).to receive(:tempfile).and_return(tempfile)
+ allow(provider).to receive(:contents_changed?).and_return(false)
+ provider.new_resource.verify windows? ? "cmd.exe c exit 1" : "false"
+ provider.new_resource.verify.each do |v|
+ expect(v).not_to receive(:verify)
+ end
+ provider.send(:do_validate_content)
+ end
+
it "does not show verification for sensitive resources" do
allow(File).to receive(:directory?).with("C:\\Windows\\system32/cmd.exe").and_return(false)
allow(provider).to receive(:tempfile).and_return(tempfile)
+ allow(provider).to receive(:contents_changed?).and_return(true)
provider.new_resource.sensitive true
provider.new_resource.verify windows? ? "cmd.exe c exit 1" : "false"
provider.new_resource.verify.each do |v|