diff options
author | Scott Christopherson <scott@scott-christopherson.com> | 2017-01-20 10:56:48 -0500 |
---|---|---|
committer | Scott Christopherson <scott@scott-christopherson.com> | 2017-01-20 11:01:24 -0500 |
commit | 1f514343a89f7ebc786422b66672c418f71e5093 (patch) | |
tree | d9e233794ddf651579ba0dfb7cf4c9e9e41f511e | |
parent | a2e1d1d4edb74a968b416517fdbdf35740686467 (diff) | |
download | chef-1f514343a89f7ebc786422b66672c418f71e5093.tar.gz |
Do not modify File's new_resource during why-run
The `File` provider's `action_create` method would modify the `new_resource` object during a
`why-run`. This was problematic when a `File` resource was configured with a `notifies :before`
because resources with `before` timers have their actions executed twice. [Once](https://github.com/chef/chef/blob/a2e1d1d4edb74a968b416517fdbdf35740686467/lib/chef/runner.rb#L54-L57) in `why-run` mode to
see if the other resources need to be notified, and then executed [once again](https://github.com/chef/chef/blob/a2e1d1d4edb74a968b416517fdbdf35740686467/lib/chef/runner.rb#L68-L69) after the `before`
notifications are resolved. This behavior would result in existing File resources not getting their
ownership and permissions updated properly during a converge.
Signed-off-by: Scott Christopherson <scott@chef.io>
-rw-r--r-- | lib/chef/provider/directory.rb | 2 | ||||
-rw-r--r-- | lib/chef/provider/file.rb | 2 | ||||
-rw-r--r-- | spec/support/shared/unit/provider/file.rb | 10 | ||||
-rw-r--r-- | spec/unit/provider/directory_spec.rb | 10 |
4 files changed, 22 insertions, 2 deletions
diff --git a/lib/chef/provider/directory.rb b/lib/chef/provider/directory.rb index 619ab5d8b6..1cacc3fcb9 100644 --- a/lib/chef/provider/directory.rb +++ b/lib/chef/provider/directory.rb @@ -138,7 +138,7 @@ class Chef end do_acl_changes do_selinux(true) - load_resource_attributes_from_file(@new_resource) + load_resource_attributes_from_file(@new_resource) unless Chef::Config[:why_run] end def action_delete diff --git a/lib/chef/provider/file.rb b/lib/chef/provider/file.rb index 84bb4d1c94..f77986fa03 100644 --- a/lib/chef/provider/file.rb +++ b/lib/chef/provider/file.rb @@ -154,7 +154,7 @@ class Chef do_contents_changes do_acl_changes do_selinux - load_resource_attributes_from_file(@new_resource) + load_resource_attributes_from_file(@new_resource) unless Chef::Config[:why_run] end def action_create_if_missing diff --git a/spec/support/shared/unit/provider/file.rb b/spec/support/shared/unit/provider/file.rb index 394fdaa02f..b58159fcc9 100644 --- a/spec/support/shared/unit/provider/file.rb +++ b/spec/support/shared/unit/provider/file.rb @@ -683,6 +683,16 @@ shared_examples_for Chef::Provider::File do end end + context "in why run mode" do + before { Chef::Config[:why_run] = true } + after { Chef::Config[:why_run] = false } + + it "does not modify new_resource" do + setup_missing_file + expect(provider).not_to receive(:load_resource_attributes_from_file).with(provider.new_resource) + provider.run_action(:create) + end + end end context "action delete" do diff --git a/spec/unit/provider/directory_spec.rb b/spec/unit/provider/directory_spec.rb index aebbaa6e81..4672db7d8d 100644 --- a/spec/unit/provider/directory_spec.rb +++ b/spec/unit/provider/directory_spec.rb @@ -148,6 +148,16 @@ describe Chef::Provider::Directory do directory.run_action(:create) expect(new_resource).not_to be_updated end + + context "in why run mode" do + before { Chef::Config[:why_run] = true } + after { Chef::Config[:why_run] = false } + + it "does not modify new_resource" do + expect(directory).not_to receive(:load_resource_attributes_from_file).with(new_resource) + directory.run_action(:create) + end + end end describe "when the directory does not exist" do |