diff options
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | lib/chef/guard_interpreter/resource_guard_interpreter.rb | 5 | ||||
-rw-r--r-- | spec/unit/guard_interpreter/resource_guard_interpreter_spec.rb | 8 |
3 files changed, 13 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c0bbb0677..7ddb91ab3d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ ## Unreleased +* [pr#3497](https://github.com/chef/chef/pull/3497): Issue 3485: Corruption of node's run\_context when non-default guard\_interpreter is evaluated * [**Yukihiko SAWANOBORI**](https://github.com/sawanoboly): Pass name by knife cil attribute [pr#3195](https://github.com/chef/chef/pull/3195) * [**Torben Knerr**](https://github.com/tknerr): diff --git a/lib/chef/guard_interpreter/resource_guard_interpreter.rb b/lib/chef/guard_interpreter/resource_guard_interpreter.rb index 1e2a534c18..d4b386a15a 100644 --- a/lib/chef/guard_interpreter/resource_guard_interpreter.rb +++ b/lib/chef/guard_interpreter/resource_guard_interpreter.rb @@ -92,8 +92,11 @@ class Chef raise ArgumentError, "Specified guard interpreter class #{resource_class} must be a kind of Chef::Resource::Execute resource" end + # Duplicate the node below because the new RunContext + # overwrites the state of Node instances passed to it. + # See https://github.com/chef/chef/issues/3485. empty_events = Chef::EventDispatch::Dispatcher.new - anonymous_run_context = Chef::RunContext.new(parent_resource.node, {}, empty_events) + anonymous_run_context = Chef::RunContext.new(parent_resource.node.dup, {}, empty_events) interpreter_resource = resource_class.new('Guard resource', anonymous_run_context) interpreter_resource.is_guard_interpreter = true diff --git a/spec/unit/guard_interpreter/resource_guard_interpreter_spec.rb b/spec/unit/guard_interpreter/resource_guard_interpreter_spec.rb index 02e4eb2fae..acf1b15fd8 100644 --- a/spec/unit/guard_interpreter/resource_guard_interpreter_spec.rb +++ b/spec/unit/guard_interpreter/resource_guard_interpreter_spec.rb @@ -84,6 +84,14 @@ describe Chef::GuardInterpreter::ResourceGuardInterpreter do expect(guard_interpreter.evaluate).to eq(true) end + it "does not corrupt the run_context of the node" do + node_run_context_before_guard_execution = parent_resource.run_context + expect(node_run_context_before_guard_execution.object_id).to eq(parent_resource.node.run_context.object_id) + guard_interpreter.evaluate + node_run_context_after_guard_execution = parent_resource.run_context + expect(node_run_context_after_guard_execution.object_id).to eq(parent_resource.node.run_context.object_id) + end + describe "script command opts switch" do let(:command_opts) { {} } let(:guard_interpreter) { Chef::GuardInterpreter::ResourceGuardInterpreter.new(parent_resource, "exit 0", command_opts) } |